29T sign(
const Eigen::Vector3<T>& p1,
const Eigen::Vector3<T>& p2,
const Eigen::Vector3<T>& p3) {
30 return (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1]);
40 const int32_t face1) {
46 for (
size_t iVertex1 : mesh.
faces[face0]) {
47 for (
size_t iVertex2 : mesh.
faces[face1]) {
48 if (iVertex1 == iVertex2) {
60 size_t iFaceA = 0, iFaceB = 0;
61 for (
size_t i = 0; i < 2; i++) {
72 const auto& faceVertsB = mesh.
faces[iFaceB];
73 T planeD = -(mesh.
vertices[faceVertsB[0]].dot(faceNormals[iFaceB]));
75 const auto& faceVertsA = mesh.
faces[iFaceA];
77 for (
size_t iTriVertex = 0; iTriVertex < 3; iTriVertex++) {
80 const Eigen::Vector3<T>& e0 = mesh.
vertices[faceVertsA[iTriVertex]];
81 const Eigen::Vector3<T> eRay = mesh.
vertices[faceVertsA[(iTriVertex + 1) % 3]] - e0;
88 if (abs(eRay.dot(faceNormals[iFaceB])) -
momentum::Eps<T>(1e-7, 1e-16) < 0) {
91 const T rayUnitLength =
92 -(e0.dot(faceNormals[iFaceB]) + planeD) / (eRay.dot(faceNormals[iFaceB]));
96 if (rayUnitLength <= 0 || rayUnitLength >= 1) {
100 const Eigen::Vector3<T> pIntersect = e0 + rayUnitLength * eRay;
107 const T s1 = sign(pIntersect, mesh.
vertices[faceVertsB[0]], mesh.
vertices[faceVertsB[1]]);
108 const T s2 = sign(pIntersect, mesh.
vertices[faceVertsB[1]], mesh.
vertices[faceVertsB[2]]);
109 const T s3 = sign(pIntersect, mesh.
vertices[faceVertsB[2]], mesh.
vertices[faceVertsB[0]]);
111 const bool has_neg = (s1 < 0) || (s2 < 0) || (s3 < 0);
112 const bool has_pos = (s1 > 0) || (s2 > 0) || (s3 > 0);
114 if (!(has_neg && has_pos)) {
124 std::vector<std::pair<int32_t, int32_t>> intersectingFaces;
130 std::vector<Vector3<T>> faceNormals(mesh.
faces.size());
131 for (
size_t iFace = 0; iFace < mesh.
faces.size(); iFace++) {
132 const auto& faceVerts = mesh.
faces[iFace];
133 const Eigen::Vector3<T> v0 = mesh.
vertices[faceVerts[0]];
134 const Eigen::Vector3<T> v1 = mesh.
vertices[faceVerts[1]];
135 const Eigen::Vector3<T> v2 = mesh.
vertices[faceVerts[2]];
136 faceNormals[iFace] = (v1 - v0).
cross(v2 - v0).normalized();
140 for (
size_t iFace0 = 0; iFace0 < mesh.
faces.size(); iFace0++) {
141 for (
size_t iFace1 = iFace0 + 1; iFace1 < mesh.
faces.size(); iFace1++) {
143 mesh, faceNormals,
static_cast<int32_t
>(iFace0),
static_cast<int32_t
>(iFace1))) {
144 intersectingFaces.emplace_back(
static_cast<int32_t
>(iFace0),
static_cast<int32_t
>(iFace1));
149 return intersectingFaces;
154 std::vector<std::pair<int32_t, int32_t>> intersectingFaces;
156 std::vector<axel::BoundingBox<T>> aabbs(mesh.
faces.size());
162 std::vector<Vector3<T>> faceNormals(mesh.
faces.size());
163 for (
size_t iFace = 0; iFace < mesh.
faces.size(); iFace++) {
164 const auto& faceVerts = mesh.
faces[iFace];
165 const Eigen::Vector3<T> v0 = mesh.
vertices[faceVerts[0]];
166 const Eigen::Vector3<T> v1 = mesh.
vertices[faceVerts[1]];
167 const Eigen::Vector3<T> v2 = mesh.
vertices[faceVerts[2]];
168 faceNormals[iFace] = (v1 - v0).
cross(v2 - v0).normalized();
170 auto& aabb = aabbs[iFace];
171 aabb.aabb.min() = v0;
172 aabb.aabb.max() = v0;
173 aabb.id =
static_cast<axel::Index
>(iFace);
177 bvh.setBoundingBoxes(aabbs);
182 bvh.traverseOverlappingPairs([&](
size_t iFace0,
size_t iFace1) {
184 mesh, faceNormals,
static_cast<int32_t
>(iFace0),
static_cast<int32_t
>(iFace1))) {
185 intersectingFaces.emplace_back(
static_cast<int32_t
>(iFace0),
static_cast<int32_t
>(iFace1));
190 return intersectingFaces;
198 const int32_t face1);
203 const int32_t face1);
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
template std::vector< std::pair< int32_t, int32_t > > intersectMeshBruteForce< float >(const MeshT< float > &mesh)
bool intersectFace(const MeshT< T > &mesh, const std::vector< Vector3< T > > &faceNormals, const int32_t face0, const int32_t face1)
Tests whether two given triangle faces of a mesh intersect with each other.
template bool intersectFace< double >(const MeshT< double > &mesh, const std::vector< Vector3< double > > &faceNormals, const int32_t face0, const int32_t face1)
template std::vector< std::pair< int32_t, int32_t > > intersectMesh< float >(const MeshT< float > &mesh)
std::vector< std::pair< int32_t, int32_t > > intersectMeshBruteForce(const MeshT< T > &mesh)
Tests if the mesh self-intersects anywhere and returns all intersecting face pairs using a brute-forc...
std::vector< std::pair< int32_t, int32_t > > intersectMesh(const MeshT< T > &mesh)
Tests if the mesh self-intersects anywhere and returns all intersecting face pairs using a BVH (bound...
Vector3P< S > cross(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes cross product of Eigen::Vector3f and Vector3P.
template std::vector< std::pair< int32_t, int32_t > > intersectMeshBruteForce< double >(const MeshT< double > &mesh)
template bool intersectFace< float >(const MeshT< float > &mesh, const std::vector< Vector3< float > > &faceNormals, const int32_t face0, const int32_t face1)
template std::vector< std::pair< int32_t, int32_t > > intersectMesh< double >(const MeshT< double > &mesh)
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
std::vector< Eigen::Vector3i > faces
List of triangular faces defined by vertex indices.