|
23 | 23 | #include "mesh_fixes.h"
|
24 | 24 | #include "par.h"
|
25 | 25 | #include "svd.h"
|
| 26 | +#include "tri_dist.h" |
26 | 27 |
|
27 | 28 | namespace {
|
28 | 29 | using namespace manifold;
|
@@ -902,4 +903,48 @@ SparseIndices Manifold::Impl::VertexCollisionsZ(
|
902 | 903 | else
|
903 | 904 | return collider_.Collisions<false, false>(vertsIn);
|
904 | 905 | }
|
| 906 | + |
| 907 | +/* |
| 908 | + * Returns the minimum gap between two manifolds. Returns a float between |
| 909 | + * 0 and searchLength. |
| 910 | + */ |
| 911 | +float Manifold::Impl::MinGap(const Manifold::Impl& other, |
| 912 | + float searchLength) const { |
| 913 | + ZoneScoped; |
| 914 | + Vec<Box> faceBoxOther; |
| 915 | + Vec<uint32_t> faceMortonOther; |
| 916 | + |
| 917 | + other.GetFaceBoxMorton(faceBoxOther, faceMortonOther); |
| 918 | + |
| 919 | + transform(autoPolicy(faceBoxOther.size()), faceBoxOther.begin(), |
| 920 | + faceBoxOther.end(), faceBoxOther.begin(), |
| 921 | + [searchLength](const Box& box) { |
| 922 | + return Box(box.min - glm::vec3(searchLength), |
| 923 | + box.max + glm::vec3(searchLength)); |
| 924 | + }); |
| 925 | + |
| 926 | + SparseIndices collisions = collider_.Collisions(faceBoxOther.cview()); |
| 927 | + |
| 928 | + float minDistanceSquared = transform_reduce<float>( |
| 929 | + autoPolicy(collisions.size()), thrust::counting_iterator<int>(0), |
| 930 | + thrust::counting_iterator<int>(collisions.size()), |
| 931 | + [&collisions, this, &other](int i) { |
| 932 | + const int tri = collisions.Get(i, 1); |
| 933 | + const int triOther = collisions.Get(i, 0); |
| 934 | + |
| 935 | + std::array<glm::vec3, 3> p; |
| 936 | + std::array<glm::vec3, 3> q; |
| 937 | + |
| 938 | + for (const int j : {0, 1, 2}) { |
| 939 | + p[j] = vertPos_[halfedge_[3 * tri + j].startVert]; |
| 940 | + q[j] = other.vertPos_[other.halfedge_[3 * triOther + j].startVert]; |
| 941 | + } |
| 942 | + |
| 943 | + return DistanceTriangleTriangleSquared(p, q); |
| 944 | + }, |
| 945 | + searchLength * searchLength, thrust::minimum<float>()); |
| 946 | + |
| 947 | + return sqrt(minDistanceSquared); |
| 948 | +}; |
| 949 | + |
905 | 950 | } // namespace manifold
|
0 commit comments