MCPcopy Create free account
hub / github.com/MeshInspector/MeshLib / findTriTriDistanceT

Function findTriTriDistanceT

source/MRMesh/MRTriDist.cpp:93–203  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91
92template<class T>
93TriTriDistanceResult<T> findTriTriDistanceT( const Triangle3<T>& a, const Triangle3<T>& b, const TriTriDistanceParams<T>& params )
94{
95 // For each edge pair, the vector connecting the closest points
96 // of the edges defines a slab (parallel planes at head and tail
97 // enclose the slab). If we can show that the off-edge vertex of
98 // each triangle is outside of the slab, then the closest points
99 // of the edges are the closest points for the triangles.
100 // Even if these tests fail, it may be helpful to know the closest
101 // points found, and whether the triangles were shown disjoint
102
103 // the distance between the triangles is not more than the distance between two of their points
104 TriTriDistanceResult<T> res
105 {
106 .a = a[0],
107 .b = b[0],
108 .distSq = distanceSq( a[0], b[0] )
109 };
110
111 for ( int i = 0; i < 3; i++ )
112 {
113 for ( int j = 0; j < 3; j++ )
114 {
115 // Find closest points on edges { a[i], a[next[i]] } & { b[j], b[next[j]] }, plus the
116 // vector (and distance squared) between these points
117
118 static constexpr int prev[3] = { 2, 0, 1 };
119 static constexpr int next[3] = { 1, 2, 0 };
120 const auto sd = findTwoLineSegmClosestPoints( { a[i], a[next[i]] }, { b[j], b[next[j]] } );
121 const T dd = distanceSq( sd.a, sd.b );
122
123 // Verify this closest point pair only if the distance
124 // squared is less than the minimum found thus far.
125
126 if ( dd <= res.distSq ) // no strictly less, to set res.overlap
127 {
128 res.a = sd.a;
129 res.b = sd.b;
130 res.distSq = dd;
131
132 // a[prev[i]] and b[prev[j]] are remaining vertices of the triangles
133 // on top of the vertices from the considered edges
134 T s = dot( a[prev[i]] - res.a, sd.dir );
135 T t = dot( b[prev[j]] - res.b, sd.dir );
136
137 // if the remaining points are further along sd.dir than the considered edges
138 if ( ( s <= 0 ) && ( t >= 0 ) )
139 {
140 res.overlap = false;
141 return res;
142 }
143
144 // the distance along sd.dir between the considered edges
145 const T p = dot( res.b - res.a, sd.dir );
146
147 if ( s < 0 ) s = 0;
148 if ( t > 0 ) t = 0;
149
150 // sd.dir is a separating direction

Callers 1

findTriTriDistanceFunction · 0.85

Calls 7

projectBonAFunction · 0.85
canExitEarlierMethod · 0.80
distanceSqFunction · 0.70
dotFunction · 0.70
sqrFunction · 0.70
swapFunction · 0.70

Tested by

no test coverage detected