1 from test_msg
import debug, info, error, warning
6 from test_import
import test_import
8 from ROOT
import geoalgo
21 debug(BLUE +
"Precision Being Required to Consider Two numbers Equal: {0:.2e}".
format(_epsilon) + ENDC)
33 info(
'Testing Point & Infinite Line Distance')
37 for y
in range(tests):
45 p0 = p1 - pDir.Dir() * 3;
52 dirct = l.Pt2()-l.Pt1()
53 transZ = (-transX*dirct[0]-transY*dirct[1])/dirct[2]
58 answer = vectTranslate.SqLength()
61 a1 = dAlgo.SqDist(pt,l)
62 sqdistT += (
time()-tim)
63 a2 = dAlgo.SqDist(l,pt)
65 pAnswer1 = dAlgo.ClosestPt(pt,l)
66 closestT += (
time()-tim)
67 pAnswer2 = dAlgo.ClosestPt(l,pt)
68 if not ( np.abs(answer-a1) < _epsilon ) : success = 0
69 if not ( np.abs(answer-a2) < _epsilon ) : success = 0
71 if not ( np.abs(p1[x]-pAnswer1[x]) < _epsilon) : success = 0
72 if not ( np.abs(p1[x]-pAnswer2[x]) < _epsilon) : success = 0
74 if ( float(totSuccess)/tests < 0.999):
75 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
77 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
78 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
79 info(
"Time for ClosestPt : {0:.3f} us".
format(1E6*closestT/tests))
82 info(
'Testing Point & LineSegment Distance')
88 for y
in range(tests):
104 transZ = (-transX*d[0]-transY*d[1])/d[2]
106 p1 = l.Start()+vectTranslate
107 p2 = l.End()+vectTranslate
116 dirOut = lPar.Dir()*(-1*dist/lPar.Dir().
Length())
118 answer = dirOut.SqLength()+vectTranslate.SqLength()
120 a1 = dAlgo.SqDist(pTest,l)
121 sqdistT_out += (
time() - tim)
122 a2 = dAlgo.SqDist(l,pTest)
123 if not ( np.abs(answer-a1) < _epsilon): success = 0
124 if not ( np.abs(answer-a2) < _epsilon): success = 0
126 point1 = dAlgo.ClosestPt(pTest,l)
127 closestT_out += (
time() - tim)
128 point2 = dAlgo.ClosestPt(l,pTest)
130 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
131 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
137 dirIn = lPar.Dir()*dist
139 answer = vectTranslate.SqLength()
141 a1 = dAlgo.SqDist(pTest,l)
142 sqdistT_in += (
time() - tim)
143 a2 = dAlgo.SqDist(l,pTest)
144 if not (np.abs(answer-a1) < _epsilon): success = 0
145 if not (np.abs(answer-a2) < _epsilon): success = 0
146 pAns = l.Start()+dirIn
148 point1 = dAlgo.ClosestPt(pTest,l)
149 closestT_in += (
time() - tim)
150 point2 = dAlgo.ClosestPt(l,pTest)
152 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
153 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
157 dirOut = lPar.Dir()*(dist/lPar.Dir().
Length())
159 answer = dirOut.SqLength()+vectTranslate.SqLength()
160 if not ( np.abs(answer-dAlgo.SqDist(pTest,l)) < _epsilon): success = 0
161 if not ( np.abs(answer-dAlgo.SqDist(l,pTest)) < _epsilon): success = 0
162 point1 = dAlgo.ClosestPt(pTest,l)
163 point2 = dAlgo.ClosestPt(pTest,l)
165 if not ( (l.End()[x]-point1[x]) < _epsilon ) : success = 0
166 if not ( (l.End()[x]-point2[x]) < _epsilon ) : success = 0
168 if (success == 1) : totSuccess += 1
169 if ( float(totSuccess)/tests < 0.999):
170 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
172 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
173 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
174 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
175 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
176 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
179 debug(
'Testing Point & HalfLine Distance')
186 for x
in range(tests):
197 transZ = (-transX*l.Dir()[0]-transY*l.Dir()[1])/l.Dir()[2]
206 dirOut = lPar.Dir()*(-1*dist)
207 pTest = lPar.Start()+dirOut
208 answer = dirOut.SqLength()+vectTranslate.SqLength()
210 a1 = dAlgo.SqDist(pTest,l)
213 a2 = dAlgo.SqDist(l,pTest)
214 if not ( np.abs(answer-a1) < _epsilon): success = 0
215 if not ( np.abs(answer-a2) < _epsilon): success = 0
217 point1 = dAlgo.ClosestPt(pTest,l)
220 point2 = dAlgo.ClosestPt(l,pTest)
222 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
223 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
229 dirIn = lPar.Dir()*dist
230 pTest = lPar.Start()+dirIn
231 answer = vectTranslate.SqLength()
232 pAns = l.Start()+dirIn
234 a1 = dAlgo.SqDist(pTest,l)
237 a2 = dAlgo.SqDist(l,pTest)
238 if not ( np.abs(answer-a1) < _epsilon): success = 0
239 if not ( np.abs(answer-a2) < _epsilon): success = 0
241 point1 = dAlgo.ClosestPt(pTest,l)
244 point2 = dAlgo.ClosestPt(l,pTest)
246 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
247 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
249 if (success == 1) : totSuccess += 1
250 if ( float(totSuccess)/tests < 0.999):
251 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
253 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
254 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
255 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
256 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
257 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
260 debug(
'Testing Inf Line & Inf Line Distance')
264 for y
in range(tests):
274 p1 = (l1.Pt2()+l1.Pt1())/2
276 d1 = (l1.Pt2()-l1.Pt1())
280 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
287 vectRotate = vectTranslate.Cross(d1)
288 l2 =
geoalgo.Line(p1+vectTranslate,p1+vectTranslate+vectRotate)
290 answer = vectTranslate.SqLength()
292 a1 = dAlgo.SqDist(l1,l2)
298 a2 = dAlgo.SqDist(l1,l2,ptL1,ptL2)
299 if not (np.abs(answer-a1) < _epsilon): success = 0
300 if not (np.abs(answer-a2) < _epsilon) : success = 0
302 if not ( np.abs(ptL1[x]-p1[x]) < _epsilon ) : success = 0
303 if not ( np.abs(ptL2[x]-l2.Pt1()[x]) < _epsilon ) : success = 0
305 if (success == 1) : totSuccess += 1
306 if ( float(totSuccess)/tests < 0.999):
307 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
309 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
310 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
313 debug(
'Testing Half-Inf Line & Half-Inf Line Distance')
319 for y
in range(tests):
333 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
337 if (
random() < 0.5) : aligned = 1
346 a1 = dAlgo.SqDist(l1,l2)
350 a2 = dAlgo.SqDist(l1,l2,L1,L2)
352 if not (np.abs(answer-a1) < _epsilon): success = 0
353 if not (np.abs(answer-a2) < _epsilon): success = 0
355 if not (L1[x]-L2[x] < _epsilon) : success = 0
358 answer = vectTranslate.SqLength()
360 a1 = dAlgo.SqDist(l1,l2)
364 a2 = dAlgo.SqDist(l1,l2,L1,L2)
366 if not (np.abs(answer-a1) < _epsilon): success = 0
367 if not (np.abs(answer-a2) < _epsilon): success = 0
369 if not (L1[x]-l1.Start()[x] < _epsilon) : success = 0
370 if not (L2[x]-l2.Start()[x] < _epsilon) : success = 0
372 if (success == 1) : totSuccess += 1
373 if ( float(totSuccess)/tests < 0.999):
374 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
376 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
377 info(
"Time for SqDist (OUT) : {0:.3f} us".
format(1E6*sqdistT_out/timesOUT))
378 info(
"Time for SqDist (IN) : {0:.3f} us".
format(1E6*sqdistT_in/timesIN))
382 debug(
'Testing Half Line & Line Segment Distance')
391 for y
in range(tests):
413 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
421 answer = vectTranslate*vectTranslate+dist*dist
423 a1 = dAlgo.SqDist(l1,seg)
428 a2 = dAlgo.SqDist(l1,seg,L1,L2)
429 if not (np.abs(answer-a1) < _epsilon): success1 = 0
430 if not (np.abs(answer-a2) < _epsilon): success1 = 0
432 if not (L1[x]-l1.Start()[x] < _epsilon) : success1 = 0
433 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
434 if (success1 == 1) : totSuccess1 += 1
437 vectRotate = vectTranslate.Cross(d1)
439 pivot = p1+vectTranslate-d1*dist
443 answer = vectTranslate*vectTranslate+dist*dist
445 a1 = dAlgo.SqDist(l1,seg,L1,L2)
446 sqdistT2 += (
time()-tim)
447 a2 = dAlgo.SqDist(seg,l1)
448 if not (np.abs(answer-a1) < _epsilon): success2 = 0
449 if not (np.abs(answer-a2) < _epsilon): success2 = 0
451 if not (L1[x]-l1.Start()[x] < _epsilon) : success2 = 0
452 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
453 if (success2 == 1) : totSuccess2 += 1
458 answer = vectTranslate*vectTranslate
460 a1 = dAlgo.SqDist(l1,seg,L1,L2)
461 sqdistT3 += (
time()-tim)
462 a2 = dAlgo.SqDist(seg,l1)
463 if not (np.abs(answer-a1) < _epsilon): success3 = 0
464 if not (np.abs(answer-a2) < _epsilon): success3 = 0
465 ptLine = l1.Start()+d1*dist
467 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
468 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
469 if (success3 == 1) : totSuccess3 += 1
471 pivot = p1+vectTranslate+d1*dist
475 answer = vectTranslate*vectTranslate
477 a1 = dAlgo.SqDist(l1,seg,L1,L2)
478 sqdistT4 += (
time()-tim)
479 a2 = dAlgo.SqDist(seg,l1)
480 if not (np.abs(answer-a1) < _epsilon): success4 = 0
481 if not (np.abs(answer-a2) < _epsilon): success4 = 0
482 ptLine = l1.Start()+d1*dist
484 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
485 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
486 if (success4 == 1) : totSuccess4 += 1
488 if ( float(totSuccess1)/tests < 0.999):
489 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
491 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
492 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
493 if ( float(totSuccess2)/tests < 0.999):
494 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
496 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
497 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
498 if ( float(totSuccess3)/tests < 0.999):
499 info(NO +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
501 info(OK +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
502 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
503 if ( float(totSuccess4)/tests < 0.999):
504 info(NO +
"Success: {0}%".
format(100*float(totSuccess4)/tests) + ENDC)
506 info(OK +
"Success: {0}%".
format(100*float(totSuccess4)/tests) + ENDC)
507 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
510 debug(
'Testing Line Segment & Line Segment Distance')
519 for y
in range(tests):
543 dirz = (-dirx*d[0]-diry*d[1])/d[2]
552 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
554 a1 = dAlgo.SqDist(seg1,seg)
559 a2 = dAlgo.SqDist(seg1,seg,L1,L2)
560 if not (np.abs(answer-a1) < _epsilon): success1 = 0
561 if not (np.abs(answer-a2) < _epsilon): success1 = 0
563 if not (L1[x]-seg1.Start()[x] < _epsilon) : success1 = 0
564 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
565 if (success1 == 1) : totSuccess1 += 1
568 vectRotate = vectTranslate.Cross(d)
570 pivot = p1+vectTranslate-d*dist
574 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
576 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
577 sqdistT2 += (
time()-tim)
578 a2 = dAlgo.SqDist(seg,seg1)
579 if not (np.abs(answer-a1) < _epsilon): success2 = 0
580 if not (np.abs(answer-a2) < _epsilon): success2 = 0
582 if not (L1[x]-seg1.Start()[x] < _epsilon) : success2 = 0
583 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
584 if (success2 == 1) : totSuccess2 += 1
590 answer = vectTranslate*vectTranslate
592 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
593 sqdistT3 += (
time()-tim)
594 a2 = dAlgo.SqDist(seg,seg1)
595 if not (np.abs(answer-a1) < _epsilon): success3 = 0
596 if not (np.abs(answer-a2) < _epsilon): success3 = 0
597 ptLine = seg1.Start()+d*distin
599 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
600 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
601 if (success3 == 1) : totSuccess3 += 1
603 pivot = p1+vectTranslate+d*distin
607 answer = vectTranslate*vectTranslate
609 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
610 sqdistT4 += (
time()-tim)
611 a2 = dAlgo.SqDist(seg,seg1)
612 if not (np.abs(answer-a1) < _epsilon): success4 = 0
613 if not (np.abs(answer-a2) < _epsilon): success4 = 0
614 ptLine = seg1.Start()+d*distin
616 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
617 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
618 if (success4 == 1) : totSuccess4 += 1
620 if ( float(totSuccess1)/tests < 0.999):
621 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
623 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
624 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
625 if ( float(totSuccess2)/tests < 0.999):
626 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
628 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
629 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
630 if ( float(totSuccess3)/tests < 0.999):
631 info(NO +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
633 info(OK +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
634 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
635 if ( float(totSuccess4)/tests < 0.999):
636 info(NO +
"Success: {0}%".
format(100*float(totSuccess4)/float(tests)) + ENDC)
638 info(OK +
"Success: {0}%".
format(100*float(totSuccess4)/float(tests)) + ENDC)
639 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
643 debug(
'Testing Point and AABox Distance/Closest Point')
660 for y
in range(tests):
673 if not ( b.Contain(p) ) : success1 = 0
682 if ( dBot < dMin ) : dMin = dBot
683 if ( dLeft < dMin ) : dMin = dLeft
684 if ( dRight < dMin ) : dMin = dRight
685 if ( dFront < dMin ) : dMin = dFront
686 if ( dBack < dMin ) : dMin = dBack
689 a1 = dAlgo.SqDist(p,b)
690 sqdistT1 += (
time()-tim)
691 a2 = dAlgo.SqDist(b,p)
694 pt1 = dAlgo.ClosestPt(b,p)
695 closestT1 += (
time()-tim)
696 pt2 = dAlgo.ClosestPt(p,b)
697 if not (np.abs(answer-a1) < _epsilon): success1 = 0
698 if not (np.abs(answer-a2) < _epsilon): success1 = 0
700 if not (pt1[x]-p[x] < _epsilon) : success1 = 0
701 if not (pt2[x]-p[x] < _epsilon) : success1 = 0
702 if (success1 == 1) : totSuccess1 += 1
707 if ( (pick > 0.33)
and (pick < 0.67) ) : side = 1
708 if ( pick > 0.67 ) : side = 2
711 if (
random() < 0.5 ) : direction = -1
719 if ( b.Contain(p) ) : success1 = 0
721 if ( (side == 0)
and (direction == 1) ):
725 if ( (side == 0)
and (direction == -1) ):
729 if ( (side == 1)
and (direction == 1) ):
733 if ( (side == 1)
and (direction == -1) ):
737 if ( (side == 2)
and (direction == 1) ):
741 if ( (side == 2)
and (direction == -1) ):
747 a1 = dAlgo.SqDist(p,b)
748 sqdistT2 += (
time()-tim)
749 a2 = dAlgo.SqDist(b,p)
752 pt1 = dAlgo.ClosestPt(b,p)
753 closestT2 += (
time()-tim)
754 pt2 = dAlgo.ClosestPt(p,b)
758 if not (np.abs(answer-a1) < _epsilon): success2 = 0
759 if not (np.abs(answer-a2) < _epsilon): success2 = 0
763 if not (pt1[x]-pMin[x] < _epsilon) : success2 = 0
764 if not (pt2[x]-pMin[x] < _epsilon) : success2 = 0
766 if (success2 == 1) : totSuccess2 += 1
768 if ( float(totSuccess1)/tests < 0.999):
769 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
771 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
772 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
773 info(
"Time for ClosestPt (Case 1) : {0:.3f} us".
format(1E6*closestT1/tests))
774 if ( float(totSuccess2)/tests < 0.999):
775 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
777 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
778 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
779 info(
"Time for ClosestPt (Case 2) : {0:.3f} us".
format(1E6*closestT2/tests))
782 error(
'geoalgo::DistanceAlgo unit test failed.')
783 print(traceback.format_exception(*sys.exc_info())[2])
786 info(
'geoalgo::DistanceAlgo unit test complete.')
789 if __name__ ==
'__main__':
float Length(const PFPStruct &pfp)
Algorithm to compute various geometrical relation among geometrical objects. In particular functions ...
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
Representation of a simple 3D line segment Defines a finite 3D straight line by having the start and ...
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): .
do one_file $F done echo for F in find $TOP name CMakeLists txt print
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...