bool PolyhedronIntersectsAABB_OBB(const Polyhedron &p, const T &obj) { if (p.Contains(obj.CenterPoint())) return true; if (obj.Contains(p.Centroid())) return true; // Test for each edge of the AABB/OBB whether this polyhedron intersects it. for(int i = 0; i < obj.NumEdges(); ++i) if (p.Intersects(obj.Edge(i))) return true; // Test for each edge of this polyhedron whether the AABB/OBB intersects it. for(size_t i = 0; i < p.f.size(); ++i) { assert(!p.f[i].v.empty()); // Cannot have degenerate faces here, and for performance reasons, don't start checking for this condition in release mode! int v0 = p.f[i].v.back(); float3 l0 = p.v[v0]; for(size_t j = 0; j < p.f[i].v.size(); ++j) { int v1 = p.f[i].v[j]; float3 l1 = p.v[v1]; if (v0 < v1 && obj.Intersects(LineSegment(l0, l1))) // If v0 < v1, then this line segment is the canonical one. return true; l0 = l1; v0 = v1; } } return false; }
/** The algorithm for Polyhedron-Polyhedron intersection is from Christer Ericson's Real-Time Collision Detection, p. 384. As noted by the author, the algorithm is very naive (and here unoptimized), and better methods exist. [groupSyntax] */ bool Polyhedron::Intersects(const Polyhedron &polyhedron) const { if (polyhedron.Contains(this->Centroid())) return true; if (this->Contains(polyhedron.Centroid())) return true; // This test assumes that both this and the other polyhedron are closed. // This means that for each edge running through vertices i and j, there's a face // that contains the line segment (i,j) and another neighboring face that contains // the line segment (j,i). These represent the same line segment (but in opposite direction) // so we only have to test one of them for intersection. Take i < j as the canonical choice // and skip the other winding order. // Test for each edge of this polyhedron whether the other polyhedron intersects it. for(size_t i = 0; i < f.size(); ++i) { assert(!f[i].v.empty()); // Cannot have degenerate faces here, and for performance reasons, don't start checking for this condition in release mode! int v0 = f[i].v.back(); float3 l0 = v[v0]; for(size_t j = 0; j < f[i].v.size(); ++j) { int v1 = f[i].v[j]; float3 l1 = v[v1]; if (v0 < v1 && polyhedron.Intersects(LineSegment(l0, l1))) // If v0 < v1, then this line segment is the canonical one. return true; l0 = l1; v0 = v1; } } // Test for each edge of the other polyhedron whether this polyhedron intersects it. for(size_t i = 0; i < polyhedron.f.size(); ++i) { assert(!polyhedron.f[i].v.empty()); // Cannot have degenerate faces here, and for performance reasons, don't start checking for this condition in release mode! int v0 = polyhedron.f[i].v.back(); float3 l0 = polyhedron.v[v0]; for(size_t j = 0; j < polyhedron.f[i].v.size(); ++j) { int v1 = polyhedron.f[i].v[j]; float3 l1 = polyhedron.v[v1]; if (v0 < v1 && Intersects(LineSegment(l0, l1))) // If v0 < v1, then this line segment is the canonical one. return true; l0 = l1; v0 = v1; } } return false; }
Polyhedron AABB::ToPolyhedron() const { // Note to maintainer: This function is an exact copy of OBB:ToPolyhedron() and Frustum::ToPolyhedron(). Polyhedron p; // Populate the corners of this AABB. // The will be in the order 0: ---, 1: --+, 2: -+-, 3: -++, 4: +--, 5: +-+, 6: ++-, 7: +++. for(int i = 0; i < 8; ++i) p.v.push_back(CornerPoint(i)); // Generate the 6 faces of this AABB. const int faces[6][4] = { { 0, 1, 3, 2 }, // X- { 4, 6, 7, 5 }, // X+ { 0, 4, 5, 1 }, // Y- { 7, 6, 2, 3 }, // Y+ { 0, 2, 6, 4 }, // Z- { 1, 5, 7, 3 }, // Z+ }; for(int f = 0; f < 6; ++f) { Polyhedron::Face face; for(int v = 0; v < 4; ++v) face.v.push_back(faces[f][v]); p.f.push_back(face); } assume(p.IsClosed()); assume(p.IsConvex()); assume(p.EulerFormulaHolds()); assume(p.FaceIndicesValid()); assume(p.FacesAreNondegeneratePlanar()); assume(p.Contains(this->CenterPoint())); return p; }