All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
larcorealg/test/GeoAlgo/test_intersection.py
Go to the documentation of this file.
1 from test_msg import debug, info, error, warning
2 import traceback,sys
3 from random import *
4 import numpy as np
5 from time import *
6 from test_import import test_import
8 from ROOT import geoalgo
9 
10 _epsilon = 1E-6
11 
12 # colors
13 OK = '\033[92m'
14 NO = '\033[91m'
15 BLUE = '\033[94m'
16 ENDC = '\033[0m'
17 
18 def test_dAlgo():
19 
20  debug()
21  debug(BLUE + "Precision Being Required to Consider Two numbers Equal: {0:.2e}".format(_epsilon) + ENDC)
22  debug()
23 
24  # number of times to test each function
25  tests = 10000
26 
27  # import Distance Algo
28  iAlgo = geoalgo.GeoAlgo()
29 
30  try:
31 
32  # test distance to and back from wall
33  info('Testing Intersection Between Half-Line & AABox')
34  totSuccess_f = 0
35  intersectT_f = 0
36  totSuccess_b = 0
37  intersectT_b = 0
38  for y in range(tests):
39  success_f = 1
40  success_b = 1
41  # generate a unit cubic box from (0,0,0) to (1,1,1)
42  box = geoalgo.AABox(0,0,0,1,1,1)
43  # generate random point inside box
44  # half-line will start from this point
46  # now find random intersection point
47  # pick a side of the box that the point is on
48  pick = random()
49  side = 0
50  if ( (pick > 0.33) and (pick < 0.67) ) : side = 1
51  if ( pick > 0.67 ) : side = 2
52  # pick a direction (Left/Right), (Top/Bottom), (Front/Back) that the point should be on
53  # given the direction & side, find the intersection point on the relevant face of the cube. That will be our intersection point. Make the half-line pass through there
54  direction = 1
55  if ( random() < 0.5 ) : direction = -1
56  # pl is a parameter to know numerical value of coordinate for the face we are on
57  # if direction = 1 -> pl = +1 (box.Max())
58  # if direction = -1 -> pl = -1 (box.Min())
59  pl = 1
60  if ( direction == -1 ) :
61  pl = 0
62  if ( side == 0 ) :
63  i = geoalgo.Vector(pl,random(),random())
64  elif ( side == 1 ) :
65  i = geoalgo.Vector(random(),pl,random())
66  else :
67  i = geoalgo.Vector(random(),random(),pl)
68  # now generate half-line passing thorugh p and i & starting from p
69  d = i-p
70  lin = geoalgo.HalfLine(p,d)
71  # answer should be distance between p & i
72  # point should be i
73  answer = i.SqDist(p)
74  pt1 = geoalgo.Vector(3)
75  pt2 = geoalgo.Vector(3)
76  tim = time()
77  pt1_v = iAlgo.Intersection(box,lin)
78  intersectT_f += (time()-tim)
79  pt2_v = iAlgo.Intersection(lin,box)
80  if pt1_v.size(): pt1 = pt1_v[0]
81  if pt2_v.size(): pt2 = pt2_v[0]
82  a1 = pt1.SqDist(p)
83  a2 = pt2.SqDist(p)
84  if not ( np.abs(answer-a1) < _epsilon ) : success_f = 0
85  if not ( np.abs(answer-a2) < _epsilon ) : success_f = 0
86  for x in xrange(3):
87  if not ( np.abs(pt1[x]-i[x]) < _epsilon) : success_f = 0
88  if not ( np.abs(pt1[x]-i[x]) < _epsilon) : success_f = 0
89  totSuccess_f += success_f
90  # now backwards:
91  # make line go in opposite direction -> intersection point should be the same
92  d = p-i
93  lin = geoalgo.HalfLine(p,d)
94  pt1 = geoalgo.Vector(3)
95  pt2 = geoalgo.Vector(3)
96  tim = time()
97  pt1_v = iAlgo.Intersection(box,lin,1)
98  intersectT_b += (time()-tim)
99  pt2_v = iAlgo.Intersection(lin,box,1)
100  if pt1_v.size(): pt1 = pt1_v[0]
101  if pt2_v.size(): pt2 = pt2_v[0]
102  a1 = pt1.SqDist(p)
103  a2 = pt2.SqDist(p)
104  if not ( np.abs(answer-a1) < _epsilon ) : success_b = 0
105  if not ( np.abs(answer-a2) < _epsilon ) : success_b = 0
106  for x in xrange(3):
107  if not ( np.abs(pt1[x]-i[x]) < _epsilon) : success_b = 0
108  if not ( np.abs(pt1[x]-i[x]) < _epsilon) : success_b = 0
109  if (success_b == 1) : totSuccess_b += 1
110 
111  if ( float(totSuccess_f)/tests < 1):
112  info(NO + "Success: {0}%".format(100*float(totSuccess_f)/tests) + ENDC)
113  else:
114  info(OK + "Success: {0}%".format(100*float(totSuccess_f)/tests) + ENDC)
115  info("Time for Intersection : {0:.3f} us".format(1E6*intersectT_f/tests))
116  if ( float(totSuccess_b)/tests < 1):
117  info(NO + "Success: {0}%".format(100*float(totSuccess_b)/tests) + ENDC)
118  else:
119  info(OK + "Success: {0}%".format(100*float(totSuccess_b)/tests) + ENDC)
120  info("Time for Intersection : {0:.3f} us".format(1E6*intersectT_b/tests))
121 
122 
123  except Exception:
124  error('geoalgo::IntersectAlgo unit test failed.')
125  print traceback.format_exception(*sys.exc_info())[2]
126  return 1
127 
128  info('geoalgo::IntersectAlgo unit test complete.')
129  return 0
130 
131 if __name__ == '__main__':
132  import test_msg
133  test_msg.test_msg.level = 0
134  test_dAlgo()
135 
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)
Definition: fclmodule.cxx:374
Representation of a 3D rectangular box which sides are aligned w/ coordinate axis. A representation of an Axis-Aligned-Boundary-Box, a simple &amp; 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 semi-infinite line. Defines a semi-infinite 3D line by having a start point (P...