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):
49 dirct = l.Pt2()-l.Pt1()
50 transZ = (-transX*dirct[0]-transY*dirct[1])/dirct[2]
55 answer = vectTranslate.SqLength()
58 a1 = dAlgo.SqDist(pt,l)
59 sqdistT += (
time()-tim)
60 a2 = dAlgo.SqDist(l,pt)
62 pAnswer1 = dAlgo.ClosestPt(pt,l)
63 closestT += (
time()-tim)
64 pAnswer2 = dAlgo.ClosestPt(l,pt)
65 if not (
abs(answer-a1) < _epsilon ) : success = 0
66 if not (
abs(answer-a2) < _epsilon ) : success = 0
68 if not (
abs(p1[x]-pAnswer1[x]) < _epsilon) : success = 0
69 if not (
abs(p1[x]-pAnswer2[x]) < _epsilon) : success = 0
71 if ( float(totSuccess)/tests < 1):
72 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
74 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
75 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
76 info(
"Time for ClosestPt : {0:.3f} us".
format(1E6*closestT/tests))
79 info(
'Testing Point & LineSegment Distance')
85 for y
in range(tests):
101 transZ = (-transX*d[0]-transY*d[1])/d[2]
103 p1 = l.Start()+vectTranslate
104 p2 = l.End()+vectTranslate
113 dirOut = lPar.Dir()*(-1*dist/lPar.Dir().
Length())
115 answer = dirOut.SqLength()+vectTranslate.SqLength()
117 a1 = dAlgo.SqDist(pTest,l)
118 sqdistT_out += (
time() - tim)
119 a2 = dAlgo.SqDist(l,pTest)
120 if not (
abs(answer-a1) < _epsilon): success = 0
121 if not (
abs(answer-a2) < _epsilon): success = 0
123 point1 = dAlgo.ClosestPt(pTest,l)
124 closestT_out += (
time() - tim)
125 point2 = dAlgo.ClosestPt(l,pTest)
127 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
128 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
134 dirIn = lPar.Dir()*dist
136 answer = vectTranslate.SqLength()
138 a1 = dAlgo.SqDist(pTest,l)
139 sqdistT_in += (
time() - tim)
140 a2 = dAlgo.SqDist(l,pTest)
141 if not (
abs(answer-a1) < _epsilon): success = 0
142 if not (
abs(answer-a2) < _epsilon): success = 0
143 pAns = l.Start()+dirIn
145 point1 = dAlgo.ClosestPt(pTest,l)
146 closestT_in += (
time() - tim)
147 point2 = dAlgo.ClosestPt(l,pTest)
149 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
150 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
154 dirOut = lPar.Dir()*(dist/lPar.Dir().
Length())
156 answer = dirOut.SqLength()+vectTranslate.SqLength()
157 if not (
abs(answer-dAlgo.SqDist(pTest,l)) < _epsilon): success = 0
158 if not (
abs(answer-dAlgo.SqDist(l,pTest)) < _epsilon): success = 0
159 point1 = dAlgo.ClosestPt(pTest,l)
160 point2 = dAlgo.ClosestPt(pTest,l)
162 if not ( (l.End()[x]-point1[x]) < _epsilon ) : success = 0
163 if not ( (l.End()[x]-point2[x]) < _epsilon ) : success = 0
165 if (success == 1) : totSuccess += 1
166 if ( float(totSuccess)/tests < 1):
167 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
169 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
170 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
171 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
172 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
173 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
176 debug(
'Testing Point & HalfLine Distance')
183 for x
in range(tests):
194 transZ = (-transX*l.Dir()[0]-transY*l.Dir()[1])/l.Dir()[2]
203 dirOut = lPar.Dir()*(-1*dist)
204 pTest = lPar.Start()+dirOut
205 answer = dirOut.SqLength()+vectTranslate.SqLength()
207 a1 = dAlgo.SqDist(pTest,l)
210 a2 = dAlgo.SqDist(l,pTest)
211 if not (
abs(answer-a1) < _epsilon): success = 0
212 if not (
abs(answer-a2) < _epsilon): success = 0
214 point1 = dAlgo.ClosestPt(pTest,l)
217 point2 = dAlgo.ClosestPt(l,pTest)
219 if not ( (l.Start()[x]-point1[x]) < _epsilon ) : success = 0
220 if not ( (l.Start()[x]-point2[x]) < _epsilon ) : success = 0
226 dirIn = lPar.Dir()*dist
227 pTest = lPar.Start()+dirIn
228 answer = vectTranslate.SqLength()
229 pAns = l.Start()+dirIn
231 a1 = dAlgo.SqDist(pTest,l)
234 a2 = dAlgo.SqDist(l,pTest)
235 if not (
abs(answer-a1) < _epsilon): success = 0
236 if not (
abs(answer-a2) < _epsilon): success = 0
238 point1 = dAlgo.ClosestPt(pTest,l)
241 point2 = dAlgo.ClosestPt(l,pTest)
243 if not ( (pAns[x]-point1[x]) < _epsilon ) : success = 0
244 if not ( (pAns[x]-point2[x]) < _epsilon ) : success = 0
246 if (success == 1) : totSuccess += 1
247 if ( float(totSuccess)/tests < 1):
248 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
250 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
251 info(
"Time for SqDist (Pt Out of Segment) : {0:.3f} us".
format(1E6*sqdistT_out/tests))
252 info(
"Time for ClosestPt (Pt Out of Segment): {0:.3f} us".
format(1E6*closestT_out/tests))
253 info(
"Time for SqDist (Pt In Segment) : {0:.3f} us".
format(1E6*sqdistT_in/tests))
254 info(
"Time for ClosestPt (Pt In Segment) : {0:.3f} us".
format(1E6*closestT_in/tests))
257 debug(
'Testing Inf Line & Inf Line Distance')
261 for y
in range(tests):
271 p1 = (l1.Pt2()+l1.Pt1())/2
273 d1 = (l1.Pt2()-l1.Pt1())
277 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
284 vectRotate = vectTranslate.Cross(d1)
285 l2 =
geoalgo.Line(p1+vectTranslate,p1+vectTranslate+vectRotate)
287 answer = vectTranslate.SqLength()
289 a1 = dAlgo.SqDist(l1,l2)
295 a2 = dAlgo.SqDist(l1,l2,ptL1,ptL2)
296 if not (
abs(answer-a1) < _epsilon): success = 0
297 if not (
abs(answer-a2) < _epsilon) : success = 0
299 if not (
abs(ptL1[x]-p1[x]) < _epsilon ) : success = 0
300 if not (
abs(ptL2[x]-l2.Pt1()[x]) < _epsilon ) : success = 0
302 if (success == 1) : totSuccess += 1
303 if ( float(totSuccess)/tests < 1):
304 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
306 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
307 info(
"Time for SqDist : {0:.3f} us".
format(1E6*sqdistT/tests))
310 debug(
'Testing Half-Inf Line & Half-Inf Line Distance')
316 for y
in range(tests):
330 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
334 if (
random() < 0.5) : aligned = 1
343 a1 = dAlgo.SqDist(l1,l2)
347 a2 = dAlgo.SqDist(l1,l2,L1,L2)
349 if not (
abs(answer-a1) < _epsilon): success = 0
350 if not (
abs(answer-a2) < _epsilon): success = 0
352 if not (L1[x]-L2[x] < _epsilon) : success = 0
355 answer = vectTranslate.SqLength()
357 a1 = dAlgo.SqDist(l1,l2)
361 a2 = dAlgo.SqDist(l1,l2,L1,L2)
363 if not (
abs(answer-a1) < _epsilon): success = 0
364 if not (
abs(answer-a2) < _epsilon): success = 0
366 if not (L1[x]-l1.Start()[x] < _epsilon) : success = 0
367 if not (L2[x]-l2.Start()[x] < _epsilon) : success = 0
369 if (success == 1) : totSuccess += 1
370 if ( float(totSuccess)/tests < 1):
371 info(NO +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
373 info(OK +
"Success: {0}%".
format(100*float(totSuccess)/tests) + ENDC)
374 info(
"Time for SqDist (OUT) : {0:.3f} us".
format(1E6*sqdistT_out/timesOUT))
375 info(
"Time for SqDist (IN) : {0:.3f} us".
format(1E6*sqdistT_in/timesIN))
379 debug(
'Testing Half Line & Line Segment Distance')
388 for y
in range(tests):
410 dirz = (-dirx*d1[0]-diry*d1[1])/d1[2]
418 answer = vectTranslate*vectTranslate+dist*dist
420 a1 = dAlgo.SqDist(l1,seg)
425 a2 = dAlgo.SqDist(l1,seg,L1,L2)
426 if not (
abs(answer-a1) < _epsilon): success1 = 0
427 if not (
abs(answer-a2) < _epsilon): success1 = 0
429 if not (L1[x]-l1.Start()[x] < _epsilon) : success1 = 0
430 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
431 if (success1 == 1) : totSuccess1 += 1
434 vectRotate = vectTranslate.Cross(d1)
436 pivot = p1+vectTranslate-d1*dist
440 answer = vectTranslate*vectTranslate+dist*dist
442 a1 = dAlgo.SqDist(l1,seg,L1,L2)
443 sqdistT2 += (
time()-tim)
444 a2 = dAlgo.SqDist(seg,l1)
445 if not (
abs(answer-a1) < _epsilon): success2 = 0
446 if not (
abs(answer-a2) < _epsilon): success2 = 0
448 if not (L1[x]-l1.Start()[x] < _epsilon) : success2 = 0
449 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
450 if (success2 == 1) : totSuccess2 += 1
455 answer = vectTranslate*vectTranslate
457 a1 = dAlgo.SqDist(l1,seg,L1,L2)
458 sqdistT3 += (
time()-tim)
459 a2 = dAlgo.SqDist(seg,l1)
460 if not (
abs(answer-a1) < _epsilon): success3 = 0
461 if not (
abs(answer-a2) < _epsilon): success3 = 0
462 ptLine = l1.Start()+d1*dist
464 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
465 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
466 if (success3 == 1) : totSuccess3 += 1
468 pivot = p1+vectTranslate+d1*dist
472 answer = vectTranslate*vectTranslate
474 a1 = dAlgo.SqDist(l1,seg,L1,L2)
475 sqdistT4 += (
time()-tim)
476 a2 = dAlgo.SqDist(seg,l1)
477 if not (
abs(answer-a1) < _epsilon): success4 = 0
478 if not (
abs(answer-a2) < _epsilon): success4 = 0
479 ptLine = l1.Start()+d1*dist
481 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
482 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
483 if (success4 == 1) : totSuccess4 += 1
485 if ( float(totSuccess1)/tests < 1):
486 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
488 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
489 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
490 if ( float(totSuccess2)/tests < 1):
491 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
493 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
494 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
495 if ( float(totSuccess3)/tests < 1):
496 info(NO +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
498 info(OK +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
499 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
500 if ( float(totSuccess4)/tests < 1):
501 info(NO +
"Success: {0}%".
format(100*float(totSuccess4)/tests) + ENDC)
503 info(OK +
"Success: {0}%".
format(100*float(totSuccess4)/tests) + ENDC)
504 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
507 debug(
'Testing Line Segment & Line Segment Distance')
516 for y
in range(tests):
540 dirz = (-dirx*d[0]-diry*d[1])/d[2]
549 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
551 a1 = dAlgo.SqDist(seg1,seg)
556 a2 = dAlgo.SqDist(seg1,seg,L1,L2)
557 if not (
abs(answer-a1) < _epsilon): success1 = 0
558 if not (
abs(answer-a2) < _epsilon): success1 = 0
560 if not (L1[x]-seg1.Start()[x] < _epsilon) : success1 = 0
561 if not (L2[x]-seg.Start()[x] < _epsilon) : success1 = 0
562 if (success1 == 1) : totSuccess1 += 1
565 vectRotate = vectTranslate.Cross(d)
567 pivot = p1+vectTranslate-d*dist
571 answer = vectTranslate*vectTranslate+dist*dist*d.SqLength()
573 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
574 sqdistT2 += (
time()-tim)
575 a2 = dAlgo.SqDist(seg,seg1)
576 if not (
abs(answer-a1) < _epsilon): success2 = 0
577 if not (
abs(answer-a2) < _epsilon): success2 = 0
579 if not (L1[x]-seg1.Start()[x] < _epsilon) : success2 = 0
580 if not (L2[x]-pivot[x] < _epsilon) : success2 = 0
581 if (success2 == 1) : totSuccess2 += 1
587 answer = vectTranslate*vectTranslate
589 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
590 sqdistT3 += (
time()-tim)
591 a2 = dAlgo.SqDist(seg,seg1)
592 if not (
abs(answer-a1) < _epsilon): success3 = 0
593 if not (
abs(answer-a2) < _epsilon): success3 = 0
594 ptLine = seg1.Start()+d*distin
596 if not (L1[x]-ptLine[x] < _epsilon) : success3 = 0
597 if not (L2[x]-seg.Start()[x] < _epsilon) : success3 = 0
598 if (success3 == 1) : totSuccess3 += 1
600 pivot = p1+vectTranslate+d*distin
604 answer = vectTranslate*vectTranslate
606 a1 = dAlgo.SqDist(seg1,seg,L1,L2)
607 sqdistT4 += (
time()-tim)
608 a2 = dAlgo.SqDist(seg,seg1)
609 if not (
abs(answer-a1) < _epsilon): success4 = 0
610 if not (
abs(answer-a2) < _epsilon): success4 = 0
611 ptLine = seg1.Start()+d*distin
613 if not (L1[x]-ptLine[x] < _epsilon) : success4 = 0
614 if not (L2[x]-pivot[x] < _epsilon) : success4 = 0
615 if (success4 == 1) : totSuccess4 += 1
617 if ( float(totSuccess1)/tests < 1):
618 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
620 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
621 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
622 if ( float(totSuccess2)/tests < 1):
623 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
625 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
626 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
627 if ( float(totSuccess3)/tests < 1):
628 info(NO +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
630 info(OK +
"Success: {0}%".
format(100*float(totSuccess3)/tests) + ENDC)
631 info(
"Time for SqDist (Case 3) : {0:.3f} us".
format(1E6*sqdistT3/tests))
632 if ( float(totSuccess4)/tests < 1):
633 info(NO +
"Success: {0}%".
format(100*float(totSuccess4)/float(tests)) + ENDC)
635 info(OK +
"Success: {0}%".
format(100*float(totSuccess4)/float(tests)) + ENDC)
636 info(
"Time for SqDist (Case 4) : {0:.3f} us".
format(1E6*sqdistT4/tests))
640 debug(
'Testing Point and AABox Distance/Closest Point')
657 for y
in xrange(tests):
670 if not ( b.Contain(p) ) : success1 = 0
679 if ( dBot < dMin ) : dMin = dBot
680 if ( dLeft < dMin ) : dMin = dLeft
681 if ( dRight < dMin ) : dMin = dRight
682 if ( dFront < dMin ) : dMin = dFront
683 if ( dBack < dMin ) : dMin = dBack
686 a1 = dAlgo.SqDist(p,b)
687 sqdistT1 += (
time()-tim)
688 a2 = dAlgo.SqDist(b,p)
691 pt1 = dAlgo.ClosestPt(b,p)
692 closestT1 += (
time()-tim)
693 pt2 = dAlgo.ClosestPt(p,b)
694 if not (
abs(answer-a1) < _epsilon): success1 = 0
695 if not (
abs(answer-a2) < _epsilon): success1 = 0
697 if not (pt1[x]-p[x] < _epsilon) : success1 = 0
698 if not (pt2[x]-p[x] < _epsilon) : success1 = 0
699 if (success1 == 1) : totSuccess1 += 1
704 if ( (pick > 0.33)
and (pick < 0.67) ) : side = 1
705 if ( pick > 0.67 ) : side = 2
708 if (
random() < 0.5 ) : direction = -1
716 if ( b.Contain(p) ) : success1 = 0
718 if ( (side == 0)
and (direction == 1) ):
722 if ( (side == 0)
and (direction == -1) ):
726 if ( (side == 1)
and (direction == 1) ):
730 if ( (side == 1)
and (direction == -1) ):
734 if ( (side == 2)
and (direction == 1) ):
738 if ( (side == 2)
and (direction == -1) ):
744 a1 = dAlgo.SqDist(p,b)
745 sqdistT2 += (
time()-tim)
746 a2 = dAlgo.SqDist(b,p)
749 pt1 = dAlgo.ClosestPt(b,p)
750 closestT2 += (
time()-tim)
751 pt2 = dAlgo.ClosestPt(p,b)
755 if not (
abs(answer-a1) < _epsilon): success2 = 0
756 if not (
abs(answer-a2) < _epsilon): success2 = 0
760 if not (pt1[x]-pMin[x] < _epsilon) : success2 = 0
761 if not (pt2[x]-pMin[x] < _epsilon) : success2 = 0
763 if (success2 == 1) : totSuccess2 += 1
765 if ( float(totSuccess1)/tests < 1):
766 info(NO +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
768 info(OK +
"Success: {0}%".
format(100*float(totSuccess1)/tests) + ENDC)
769 info(
"Time for SqDist (Case 1) : {0:.3f} us".
format(1E6*sqdistT1/tests))
770 info(
"Time for ClosestPt (Case 1) : {0:.3f} us".
format(1E6*closestT1/tests))
771 if ( float(totSuccess2)/tests < 1):
772 info(NO +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
774 info(OK +
"Success: {0}%".
format(100*float(totSuccess2)/tests) + ENDC)
775 info(
"Time for SqDist (Case 2) : {0:.3f} us".
format(1E6*sqdistT2/tests))
776 info(
"Time for ClosestPt (Case 2) : {0:.3f} us".
format(1E6*closestT2/tests))
779 error(
'geoalgo::DistanceAlgo unit test failed.')
780 print traceback.format_exception(*sys.exc_info())[2]
783 info(
'geoalgo::DistanceAlgo unit test complete.')