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; }
bool Polygon::Intersects(const Polyhedron &polyhedron) const { return polyhedron.Intersects(*this); }
bool Sphere::Intersects(const Polyhedron &polyhedron) const { return polyhedron.Intersects(*this); }
bool Capsule::Intersects(const Polyhedron &polyhedron) const { return polyhedron.Intersects(*this); }
bool Triangle::Intersects(const Polyhedron &polyhedron) const { return polyhedron.Intersects(*this); }