20 debug(BLUE +
"Precision Being Required to Consider Two numbers Equal: {0:.2e}".
format(_epsilon) + ENDC)
32 info(
'Testing Point & Infinite Line Distance')
36 for y
in range(tests):
48 dirct = l.Pt2()-l.Pt1()
49 transZ = (-transX*dirct[0]-transY*dirct[1])/dirct[2]
54 answer = vectTranslate.SqLength()
57 a1 = dAlgo.SqDist(pt,l)
58 sqdistT += (
time()-tim)
59 a2 = dAlgo.SqDist(l,pt)
61 pAnswer1 = dAlgo.ClosestPt(pt,l)
62 closestT += (
time()-tim)
63 pAnswer2 = dAlgo.ClosestPt(l,pt)
64 if not (
abs(answer-a1) < _epsilon ) : success = 0
65 if not (
abs(answer-a2) < _epsilon ) : success = 0
67 if not (
abs(p1[x]-pAnswer1[x]) < _epsilon) : success = 0
68 if not (
abs(p1[x]-pAnswer2[x]) < _epsilon) : success = 0
70 if (
float(totSuccess)/tests < 1):
74 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
75 info(
"Time for ClosestPt : {0:.3f} us".
format(1E6*closestT/tests))
78 info(
'Testing Point & LineSegment Distance')
84 for y
in range(tests):
100 transZ = (-transX*d[0]-transY*d[1])/d[2]
102 p1 = l.Start()+vectTranslate
103 p2 = l.End()+vectTranslate
112 dirOut = lPar.Dir()*(-1*dist/lPar.Dir().
Length())
114 answer = dirOut.SqLength()+vectTranslate.SqLength()
116 a1 = dAlgo.SqDist(pTest,l)
117 sqdistT_out += (
time() - tim)
118 a2 = dAlgo.SqDist(l,pTest)
119 if not (
abs(answer-a1) < _epsilon): success = 0
120 if not (
abs(answer-a2) < _epsilon): success = 0
122 point1 = dAlgo.ClosestPt(pTest,l)
123 closestT_out += (
time() - tim)
124 point2 = dAlgo.ClosestPt(l,pTest)
126 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
127 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
133 dirIn = lPar.Dir()*dist
135 answer = vectTranslate.SqLength()
137 a1 = dAlgo.SqDist(pTest,l)
138 sqdistT_in += (
time() - tim)
139 a2 = dAlgo.SqDist(l,pTest)
140 if not (
abs(answer-a1) < _epsilon): success = 0
141 if not (
abs(answer-a2) < _epsilon): success = 0
142 pAns = l.Start()+dirIn
144 point1 = dAlgo.ClosestPt(pTest,l)
145 closestT_in += (
time() - tim)
146 point2 = dAlgo.ClosestPt(l,pTest)
148 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
149 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
153 dirOut = lPar.Dir()*(dist/lPar.Dir().
Length())
155 answer = dirOut.SqLength()+vectTranslate.SqLength()
156 if not (
abs(answer-dAlgo.SqDist(pTest,l)) < _epsilon): success = 0
157 if not (
abs(answer-dAlgo.SqDist(l,pTest)) < _epsilon): success = 0
158 point1 = dAlgo.ClosestPt(pTest,l)
159 point2 = dAlgo.ClosestPt(pTest,l)
161 if not ( (l.End()[x]-point1[x]) < _epsilon ) : success = 0
162 if not ( (l.End()[x]-point2[x]) < _epsilon ) : success = 0
164 if (success == 1) : totSuccess += 1
165 if (
float(totSuccess)/tests < 1):
169 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
170 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
171 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
172 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
175 debug(
'Testing Point & HalfLine Distance')
182 for x
in range(tests):
193 transZ = (-transX*l.Dir()[0]-transY*l.Dir()[1])/l.Dir()[2]
202 dirOut = lPar.Dir()*(-1*dist)
203 pTest = lPar.Start()+dirOut
204 answer = dirOut.SqLength()+vectTranslate.SqLength()
206 a1 = dAlgo.SqDist(pTest,l)
209 a2 = dAlgo.SqDist(l,pTest)
210 if not (
abs(answer-a1) < _epsilon): success = 0
211 if not (
abs(answer-a2) < _epsilon): success = 0
213 point1 = dAlgo.ClosestPt(pTest,l)
216 point2 = dAlgo.ClosestPt(l,pTest)
218 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
219 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
225 dirIn = lPar.Dir()*dist
226 pTest = lPar.Start()+dirIn
227 answer = vectTranslate.SqLength()
228 pAns = l.Start()+dirIn
230 a1 = dAlgo.SqDist(pTest,l)
233 a2 = dAlgo.SqDist(l,pTest)
234 if not (
abs(answer-a1) < _epsilon): success = 0
235 if not (
abs(answer-a2) < _epsilon): success = 0
237 point1 = dAlgo.ClosestPt(pTest,l)
240 point2 = dAlgo.ClosestPt(l,pTest)
242 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
243 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
245 if (success == 1) : totSuccess += 1
246 if (
float(totSuccess)/tests < 1):
250 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
251 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
252 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
253 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
256 debug(
'Testing Inf Line & Inf Line Distance')
260 for y
in range(tests):
270 p1 = (l1.Pt2()+l1.Pt1())/2
272 d1 = (l1.Pt2()-l1.Pt1())
276 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
283 vectRotate = vectTranslate.Cross(d1)
284 l2 =
geoalgo.Line(p1+vectTranslate,p1+vectTranslate+vectRotate)
286 answer = vectTranslate.SqLength()
288 a1 = dAlgo.SqDist(l1,l2)
294 a2 = dAlgo.SqDist(l1,l2,ptL1,ptL2)
295 if not (
abs(answer-a1) < _epsilon): success = 0
296 if not (
abs(answer-a2) < _epsilon) : success = 0
298 if not (
abs(ptL1[x]-p1[x]) < _epsilon ) : success = 0
299 if not (
abs(ptL2[x]-l2.Pt1()[x]) < _epsilon ) : success = 0
301 if (success == 1) : totSuccess += 1
302 if (
float(totSuccess)/tests < 1):
306 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
309 debug(
'Testing Half-Inf Line & Half-Inf Line Distance')
315 for y
in range(tests):
329 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
333 if (
random() < 0.5) : aligned = 1
342 a1 = dAlgo.SqDist(l1,l2)
346 a2 = dAlgo.SqDist(l1,l2,L1,L2)
348 if not (
abs(answer-a1) < _epsilon): success = 0
349 if not (
abs(answer-a2) < _epsilon): success = 0
351 if not (L1[x]-L2[x] < _epsilon) : success = 0
354 answer = vectTranslate.SqLength()
356 a1 = dAlgo.SqDist(l1,l2)
360 a2 = dAlgo.SqDist(l1,l2,L1,L2)
362 if not (
abs(answer-a1) < _epsilon): success = 0
363 if not (
abs(answer-a2) < _epsilon): success = 0
365 if not (L1[x]-l1.Start()[x] < _epsilon) : success = 0
366 if not (L2[x]-l2.Start()[x] < _epsilon) : success = 0
368 if (success == 1) : totSuccess += 1
369 if (
float(totSuccess)/tests < 1):
373 info(
"Time for SqDist (OUT) : {0:.3f} us".
format(1E6*sqdistT_out/timesOUT))
374 info(
"Time for SqDist (IN) : {0:.3f} us".
format(1E6*sqdistT_in/timesIN))
378 debug(
'Testing Half Line & Line Segment Distance')
387 for y
in range(tests):
409 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
417 answer = vectTranslate*vectTranslate+dist*dist
419 a1 = dAlgo.SqDist(l1,seg)
424 a2 = dAlgo.SqDist(l1,seg,L1,L2)
425 if not (
abs(answer-a1) < _epsilon): success1 = 0
426 if not (
abs(answer-a2) < _epsilon): success1 = 0
428 if not (L1[x]-l1.Start()[x] < _epsilon) : success1 = 0
429 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
430 if (success1 == 1) : totSuccess1 += 1
433 vectRotate = vectTranslate.Cross(d1)
435 pivot = p1+vectTranslate-d1*dist
439 answer = vectTranslate*vectTranslate+dist*dist
441 a1 = dAlgo.SqDist(l1,seg,L1,L2)
442 sqdistT2 += (
time()-tim)
443 a2 = dAlgo.SqDist(seg,l1)
444 if not (
abs(answer-a1) < _epsilon): success2 = 0
445 if not (
abs(answer-a2) < _epsilon): success2 = 0
447 if not (L1[x]-l1.Start()[x] < _epsilon) : success2 = 0
448 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
449 if (success2 == 1) : totSuccess2 += 1
454 answer = vectTranslate*vectTranslate
456 a1 = dAlgo.SqDist(l1,seg,L1,L2)
457 sqdistT3 += (
time()-tim)
458 a2 = dAlgo.SqDist(seg,l1)
459 if not (
abs(answer-a1) < _epsilon): success3 = 0
460 if not (
abs(answer-a2) < _epsilon): success3 = 0
461 ptLine = l1.Start()+d1*dist
463 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
464 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
465 if (success3 == 1) : totSuccess3 += 1
467 pivot = p1+vectTranslate+d1*dist
471 answer = vectTranslate*vectTranslate
473 a1 = dAlgo.SqDist(l1,seg,L1,L2)
474 sqdistT4 += (
time()-tim)
475 a2 = dAlgo.SqDist(seg,l1)
476 if not (
abs(answer-a1) < _epsilon): success4 = 0
477 if not (
abs(answer-a2) < _epsilon): success4 = 0
478 ptLine = l1.Start()+d1*dist
480 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
481 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
482 if (success4 == 1) : totSuccess4 += 1
484 if (
float(totSuccess1)/tests < 1):
488 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
489 if (
float(totSuccess2)/tests < 1):
493 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
494 if (
float(totSuccess3)/tests < 1):
498 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
499 if (
float(totSuccess4)/tests < 1):
503 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
506 debug(
'Testing Line Segment & Line Segment Distance')
515 for y
in range(tests):
539 dirz = (-dirx*d[0]-diry*d[1])/d[2]
548 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
550 a1 = dAlgo.SqDist(seg1,seg)
555 a2 = dAlgo.SqDist(seg1,seg,L1,L2)
556 if not (
abs(answer-a1) < _epsilon): success1 = 0
557 if not (
abs(answer-a2) < _epsilon): success1 = 0
559 if not (L1[x]-seg1.Start()[x] < _epsilon) : success1 = 0
560 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
561 if (success1 == 1) : totSuccess1 += 1
564 vectRotate = vectTranslate.Cross(d)
566 pivot = p1+vectTranslate-d*dist
570 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
572 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
573 sqdistT2 += (
time()-tim)
574 a2 = dAlgo.SqDist(seg,seg1)
575 if not (
abs(answer-a1) < _epsilon): success2 = 0
576 if not (
abs(answer-a2) < _epsilon): success2 = 0
578 if not (L1[x]-seg1.Start()[x] < _epsilon) : success2 = 0
579 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
580 if (success2 == 1) : totSuccess2 += 1
586 answer = vectTranslate*vectTranslate
588 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
589 sqdistT3 += (
time()-tim)
590 a2 = dAlgo.SqDist(seg,seg1)
591 if not (
abs(answer-a1) < _epsilon): success3 = 0
592 if not (
abs(answer-a2) < _epsilon): success3 = 0
593 ptLine = seg1.Start()+d*distin
595 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
596 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
597 if (success3 == 1) : totSuccess3 += 1
599 pivot = p1+vectTranslate+d*distin
603 answer = vectTranslate*vectTranslate
605 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
606 sqdistT4 += (
time()-tim)
607 a2 = dAlgo.SqDist(seg,seg1)
608 if not (
abs(answer-a1) < _epsilon): success4 = 0
609 if not (
abs(answer-a2) < _epsilon): success4 = 0
610 ptLine = seg1.Start()+d*distin
612 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
613 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
614 if (success4 == 1) : totSuccess4 += 1
616 if (
float(totSuccess1)/tests < 1):
620 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
621 if (
float(totSuccess2)/tests < 1):
625 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
626 if (
float(totSuccess3)/tests < 1):
630 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
631 if (
float(totSuccess4)/tests < 1):
635 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
639 debug(
'Testing Point and AABox Distance/Closest Point')
656 for y
in xrange(tests):
669 if not ( b.Contain(p) ) : success1 = 0
678 if ( dBot < dMin ) : dMin = dBot
679 if ( dLeft < dMin ) : dMin = dLeft
680 if ( dRight < dMin ) : dMin = dRight
681 if ( dFront < dMin ) : dMin = dFront
682 if ( dBack < dMin ) : dMin = dBack
685 a1 = dAlgo.SqDist(p,b)
686 sqdistT1 += (
time()-tim)
687 a2 = dAlgo.SqDist(b,p)
690 pt1 = dAlgo.ClosestPt(b,p)
691 closestT1 += (
time()-tim)
692 pt2 = dAlgo.ClosestPt(p,b)
693 if not (
abs(answer-a1) < _epsilon): success1 = 0
694 if not (
abs(answer-a2) < _epsilon): success1 = 0
696 if not (pt1[x]-p[x] < _epsilon) : success1 = 0
697 if not (pt2[x]-p[x] < _epsilon) : success1 = 0
698 if (success1 == 1) : totSuccess1 += 1
703 if ( (pick > 0.33)
and (pick < 0.67) ) : side = 1
704 if ( pick > 0.67 ) : side = 2
707 if (
random() < 0.5 ) : direction = -1
715 if ( b.Contain(p) ) : success1 = 0
717 if ( (side == 0)
and (direction == 1) ):
721 if ( (side == 0)
and (direction == -1) ):
725 if ( (side == 1)
and (direction == 1) ):
729 if ( (side == 1)
and (direction == -1) ):
733 if ( (side == 2)
and (direction == 1) ):
737 if ( (side == 2)
and (direction == -1) ):
743 a1 = dAlgo.SqDist(p,b)
744 sqdistT2 += (
time()-tim)
745 a2 = dAlgo.SqDist(b,p)
748 pt1 = dAlgo.ClosestPt(b,p)
749 closestT2 += (
time()-tim)
750 pt2 = dAlgo.ClosestPt(p,b)
754 if not (
abs(answer-a1) < _epsilon): success2 = 0
755 if not (
abs(answer-a2) < _epsilon): success2 = 0
759 if not (pt1[x]-pMin[x] < _epsilon) : success2 = 0
760 if not (pt2[x]-pMin[x] < _epsilon) : success2 = 0
762 if (success2 == 1) : totSuccess2 += 1
764 if (
float(totSuccess1)/tests < 1):
768 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
769 info(
"Time for ClosestPt (Case 1) : {0:.3f} us".
format(1E6*closestT1/tests))
770 if (
float(totSuccess2)/tests < 1):
774 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
775 info(
"Time for ClosestPt (Case 2) : {0:.3f} us".
format(1E6*closestT2/tests))
778 error(
'geoalgo::DistanceAlgo unit test failed.')
779 print traceback.format_exception(*sys.exc_info())[2]
782 info(
'geoalgo::DistanceAlgo unit test complete.')
float Length(const PFPStruct &pfp)
Algorithm to compute various geometrical relation among geometrical objects. In particular functions ...
Representation of a simple 3D line segment Defines a finite 3D straight line by having the start and ...
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Representation of a 3D rectangular box which sides are aligned w/ coordinate axis. A representation of an Axis-Aligned-Boundary-Box, a simple & popular representation of 3D boundary box for collision detection. The concept was taken from the reference, Real-Time-Collision-Detection (RTCD), and in particular Ch. 4.2 (page 77): .
Representation of a 3D infinite line. Defines an infinite 3D line by having 2 points which completely...
Representation of a 3D semi-infinite line. Defines a semi-infinite 3D line by having a start point (P...