3 std::ostream &
operator<<(std::ostream &ost,
const Point& p)
5 ost <<
"(" << p.x <<
", " << p.y <<
", " << p.z <<
")";
25 const Point& v0,
const Point& v,
26 Point& cp0, Point& cp1)
78 else if ((-d +
b) >
a)
86 sc = (fabsf(sN) <
EPS ? 0.0f : sN / sD);
87 tc = (fabsf(tN) <
EPS ? 0.0f : tN / tD);
95 return dP.Magnitude();
105 inline float PointPlaneDist(
const Point& P,
const Point& pointOnPlane,
const Point&
n,
108 Point v = P - pointOnPlane;
119 float l = dir.Magnitude();
124 return v.Magnitude();
127 return dv.Magnitude();
129 return sqrtf(v.SquareMagnitude() - x*x);
142 const Point* edges,
const Point&
n)
145 for (
unsigned int i=0;
i<3;
i++){
146 v = P - *vertices[
i];
148 if ((v|outer)>0)
return false;
154 float TriTriDist(
const Point& U0,
const Point& U1,
const Point& U2,
155 const Point& V0,
const Point& V1,
const Point& V2,
156 Point& cp0, Point& cp1)
158 const Point* uvertices[] = {&U0, &U1, &U2};
159 const Point* vvertices[] = {&V0, &V1, &V2};
161 Point p0, p1,
n, vec;
165 Point uedges[3], vedges[3];
166 for (
unsigned int i=0;
i<3;
i++){
167 uedges[
i] = *uvertices[(
i+1)%3] - *uvertices[
i];
168 vedges[
i] = *vvertices[(
i+1)%3] - *vvertices[
i];
174 min_d = (cp0-V0).Magnitude();
177 for (
unsigned int i=0;
i<3;
i++){
178 for (
unsigned int j=0; j<3; j++){
180 *vvertices[j], vedges[j],
188 vec = *uvertices[(
i+2)%3] - cp0;
189 float u = (vec|
n)/min_d;
190 vec = *vvertices[(j+2)%3] - cp1;
191 float v = (vec|
n)/min_d;
193 if (u>=0 && v<=0)
return min_d;
197 if ((
n.Magnitude() + u - v) > 0){
204 Point un = uedges[0]^uedges[1];
206 Point vn = vedges[0]^vedges[1];
215 for (
int i=0;
i<3;
i++){
216 vec = *uvertices[
i] - *vvertices[0];
219 if (ds[0] > 0 && ds[1] > 0 && ds[2] > 0){
221 rank = ds[0] > ds[1] ? 1 : 0;
222 rank = ds[rank] > ds[2] ? 2 : rank;
223 }
else if(ds[0] < 0 && ds[1] < 0 && ds[2] < 0){
225 rank = ds[0] > ds[1] ? 0 : 1;
226 rank = ds[rank] > ds[2] ? rank : 2;
231 min_d = fabsf(
PointPlaneDist(*uvertices[rank], *vvertices[0], vn, p1));
232 cp0 = *uvertices[rank];
240 for (
int i=0;
i<3;
i++){
241 vec = *vvertices[
i] - *uvertices[0];
244 if (ds[0] > 0 && ds[1] > 0 && ds[2] > 0){
246 rank = ds[0] > ds[1] ? 1 : 0;
247 rank = ds[rank] > ds[2] ? 2 : rank;
248 }
else if(ds[0] < 0 && ds[1] < 0 && ds[2] < 0){
250 rank = ds[0] > ds[1] ? 0 : 1;
251 rank = ds[rank] > ds[2] ? rank : 2;
256 min_d = fabsf(
PointPlaneDist(*vvertices[rank], *uvertices[0], un, p0));
258 cp1 = *vvertices[rank];
272 const Point& v0,
const Point& v1)
275 return SegSegDist(u0, u1-u0, v0, v1-v0, cp0, cp1);
282 Point p0(0,0,0), p1(2,0,0), p2(1, 1, -1), p3(1, 1, 1);
287 std::cout <<
"test1 : d = " << d <<
", cp1 = " << cp1
288 <<
", cp2 = " << cp2 << std::endl;
290 Point p4(0,1,0), p5(2,1,0);
292 std::cout <<
"test2 : d = " << d <<
", cp1 = " << cp1
293 <<
", cp2 = " << cp2 << std::endl;
296 std::cout <<
"test3 : d = " << d <<
", cp1 = " << cp1
297 <<
", cp2 = " << cp2 << std::endl;
299 Point p6(0, 2, 0), p7(-2, 1, 0);
300 d =
TriTriDist(p0, p1, p5, p4, p6, p7, cp1, cp2);
301 std::cout <<
"test4 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = " << cp2
304 Point p8(3, -1, 1), p9(3, 2, 1), p10(-3, -1, 1);
305 d =
TriTriDist(p0, p1, p5, p8, p9, p10, cp1, cp2);
306 std::cout <<
"test5 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = " << cp2
309 d =
TriTriDist(p0, p1, p2, p8, p9, p10, cp1, cp2);
310 std::cout <<
"test6 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = " << cp2
312 std::cout <<
"answer: d = 1, cp1 = (0, 0, 0), cp2 = (0, 0, 1)"
315 d =
TriTriDist(p2, p0, p1, p8, p9, p10, cp1, cp2);
316 std::cout <<
"test7 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = " << cp2
318 std::cout <<
"answer: d = 1, cp1 = (0, 0, 0), cp2 = (0, 0, 1)"
321 d =
TriTriDist(p1, p2, p0, p8, p9, p10, cp1, cp2);
322 std::cout <<
"test8 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = " << cp2
324 std::cout <<
"answer: d = 1, cp1 = (2, 0, 0), cp2 = (2, 0, 1)"
328 Point pp0(-2, 2, 0), pp1(-2, -2, 0), pp2(5, -2, 0),
329 pp3(-0.1, 0.4, -0.1), pp4(-0.1, -0.4, -0.1), pp5(-0.1, -0.4, 0.1);
330 d =
TriTriDist(pp0, pp1, pp2, pp3, pp4, pp5, cp1, cp2);
331 std::cout <<
"test9 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = "
333 std::cout <<
"answer: d = 0.0" << std::endl;
337 Point pp0(-2, 2, 0), pp1(-2, -2, 0), pp2(5, -2, 0),
338 pp3(-0.1, 0.4, -0.1), pp4(-0.1, -0.4, -0.1), pp5(-0.1, -0.4, 0.1);
339 d =
TriTriDist(pp0, pp1, pp2, pp3, pp4, pp5, cp1, cp2);
340 std::cout <<
"test9 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = "
342 std::cout <<
"answer: d = 0.0" << std::endl;
346 Point p0(0.1, 0.4, 0), p1(-0.1, 0.4, 0), p2(0.1, -0.4, 0);
347 Point p3(0.05, 0.45, -0.15), p4(0.05, 0.45, 0.05), p5(0.05, -0.35, -0.15);
348 d =
TriTriDist(p0, p1, p2, p3, p4, p5, cp1, cp2);
349 std::cout <<
"test10 : d = " << d <<
", cp1 = " << cp1 <<
", cp2 = "
351 std::cout <<
"answer: d = 0.0" << std::endl;