stuff
This commit is contained in:
@@ -5,6 +5,45 @@ glm::vec3 CalculateTriangleNormal(Triangle t) {
|
||||
return glm::normalize(glm::cross(t.b - t.a, t.c - t.a));
|
||||
}
|
||||
|
||||
static bool TriangleAxisSeparates(const Triangle& t1, const Triangle& t2, const glm::vec3& axis) {
|
||||
const float a1 = glm::dot(t1.a, axis), b1 = glm::dot(t1.b, axis), c1 = glm::dot(t1.c, axis);
|
||||
const float a2 = glm::dot(t2.a, axis), b2 = glm::dot(t2.b, axis), c2 = glm::dot(t2.c, axis);
|
||||
const float min1 = glm::min(a1, glm::min(b1, c1)), max1 = glm::max(a1, glm::max(b1, c1));
|
||||
const float min2 = glm::min(a2, glm::min(b2, c2)), max2 = glm::max(a2, glm::max(b2, c2));
|
||||
return max1 < min2 || max2 < min1;
|
||||
}
|
||||
|
||||
//SEPARATING AXIS TEST, BOTH NORMALS PLUS EVERY EDGE PAIR
|
||||
bool TriangleInTriangle(const Triangle& t1, const Triangle& t2) {
|
||||
const glm::vec3 edges1[3] = { t1.b - t1.a, t1.c - t1.b, t1.a - t1.c };
|
||||
const glm::vec3 edges2[3] = { t2.b - t2.a, t2.c - t2.b, t2.a - t2.c };
|
||||
const glm::vec3 normal1 = glm::cross(edges1[0], edges1[1]);
|
||||
const glm::vec3 normal2 = glm::cross(edges2[0], edges2[1]);
|
||||
|
||||
if (TriangleAxisSeparates(t1, t2, normal1) || TriangleAxisSeparates(t1, t2, normal2))
|
||||
return false;
|
||||
|
||||
bool coplanar = true;
|
||||
for (const glm::vec3& edge1 : edges1) {
|
||||
for (const glm::vec3& edge2 : edges2) {
|
||||
const glm::vec3 axis = glm::cross(edge1, edge2);
|
||||
if (glm::dot(axis, axis) < 1e-14f) continue;
|
||||
coplanar = false;
|
||||
if (TriangleAxisSeparates(t1, t2, axis)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
//EDGE PAIRS GIVE NOTHING FOR COPLANAR TRIANGLES, SO TEST WITHIN THE PLANE
|
||||
if (coplanar) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (TriangleAxisSeparates(t1, t2, glm::cross(normal1, edges1[i]))) return false;
|
||||
if (TriangleAxisSeparates(t1, t2, glm::cross(normal1, edges2[i]))) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
float Magnitude2(const glm::vec3& v) {
|
||||
return v.x*v.x + v.y*v.y + v.z*v.z;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user