DM_spatial_query_point_index.py
1 ## @package python.demo.DM_spatial_query_point_index
2 #
3 # example script showing how to load an OPALS datamanager and
4 # perform a simple circle, cylinder and sphere queries on the point index
5 # (script works in python 2 and 3)
6 #
7 
8 import sys, math
9 from datetime import datetime
10 from opals import pyDM
11 
12 if len(sys.argv) == 1:
13  print("ODM parameter missing")
14  sys.exit(-1)
15 
16 # open the odm / pyDM.Datamanager.load parameters: filename(string), readOnly(bool) threadSafety(bool)
17 dm = pyDM.Datamanager.load(sys.argv[1], True, False)
18 
19 # if the dm wasn't opened successfully exit function
20 if not dm:
21  print("Unable to open ODM '" + sys.argv[1] + "'")
22  sys.exit(-1)
23 
24 # Create a PointIndex from where we can access spatial queries
25 PointIndex = dm.getPointIndex()
26 
27 # Get realistic coordinates for our shapes
28 lim = dm.getLimit()
29 x = (lim.xmax + lim.xmin) / 2
30 y = (lim.ymax + lim.ymin) / 2
31 a = (lim.xmax - lim.xmin) / 3 # thirds, since we don't want the whole thing
32 b = (lim.ymax - lim.ymin) / 3
33 r = (a + b) / 2
34 
35 z = (lim.zmin + lim.zmax) / 2
36 c = (lim.zmax - lim.zmin) / 3
37 
38 zmax = z + c
39 zmin = z - c
40 
41 # Create a few query shapes
42 cir = pyDM.Circle(x, y, r)
43 cyl = pyDM.Cylinder(x, y, r, zmin, zmax)
44 sph = pyDM.Sphere(x, y, z, r)
45 
46 # Create a 2D-Polygon (approximating the circle)
47 PolygonF = pyDM.PolygonFactory()
48 for alp in range(0, 360, 6):
49  alpr = alp * math.pi / 180
50  PolygonF.addPoint(x + r * math.sin(alpr), y + r * math.cos(alpr))
51 PolygonF.closePart()
52 pol = PolygonF.getPolygon(pyDM.Orientation.cw)
53 
54 # Count points in the shapes
55 cirstart = datetime.now()
56 PointsInCir = PointIndex.searchPoint(cir)
57 NumPointsInCir = len(PointsInCir)
58 cirend = datetime.now()
59 
60 cylstart = datetime.now()
61 PointsInCyl = PointIndex.searchPoint(cyl)
62 NumPointsInCyl = len(PointsInCyl)
63 cylend = datetime.now()
64 
65 sphstart = datetime.now()
66 PointsInSph = PointIndex.searchPoint(sph)
67 NumPointsInSph = len(PointsInSph)
68 sphend = datetime.now()
69 
70 polstart = datetime.now()
71 PointsInPol = PointIndex.searchPoint(pol)
72 NumPointsInPol = len(PointsInPol)
73 polend = datetime.now()
74 
75 print("Out of %6i points total..." % dm.sizePoint())
76 print("The circle contains %6i points. (runtime: %s)" % (NumPointsInCir, str(cirend - cirstart)))
77 print("The cylinder contains %6i points. (runtime: %s)" % (NumPointsInCyl, str(cylend - cylstart)))
78 print("The sphere contains %6i points. (runtime: %s)" % (NumPointsInSph, str(sphend - sphstart)))
79 print("The polygon contains %6i points. (runtime: %s)" % (NumPointsInPol, str(polend - polstart)))