DM_compare_polylines.py
1 ## @package python.demo.DM_compare_polylines
2 #
3 # Small example script showing how to compare polylines based on the pyDM.GeometricAlgorithms.analyseDistance functionality
4 # (script works in python 2 and 3)
5 #
6 
7 from __future__ import print_function # print function syntax as in python 3
8 import sys
9 
10 from opals import pyDM
11 
12 def output_polyline(line,prefix=""):
13  print(prefix,"Polyline has",line.sizePart(),"parts with",line.sizePoint(),"points")
14  for idx, part in enumerate(line.parts()):
15  print("\tPart",idx,"with",part.sizePoint(),"points")
16  for p in part.points():
17  if sys.float_info.max == p.z:
18  print("\t\t(%.3f," % p.x, " %.3f," % p.y, "---)")
19  else:
20  print("\t\t(%.3f," % p.x, " %.3f," % p.y, "%.3f)" % p.z)
21 
22  return
23 
24 
25 f = pyDM.PolylineFactory()
26 
27 #create simple polyline
28 f.addPoint(0, 0, 0)
29 f.addPoint(5, 0, 0)
30 f.addPoint(10, 0, 0)
31 
32 line1 = f.getPolyline()
33 
34 f.addPoint(0, 1, 1 )
35 f.addPoint(5, 0.5, 0.5)
36 f.addPoint(10, 0, -0.5)
37 
38 line2 = f.getPolyline()
39 
40 
41 output_polyline(line1,"Line1:")
42 output_polyline(line2,"Line2:")
43 
44 
45 ##
46 # callback object for collecting distance information
47 class AnalyseDistance(pyDM.AnalyseDistance):
48 
49  def __init__(self):
50  # initialize the base class (mandatory!)
51  if sys.version_info >= (3, 0):
52  super().__init__()
53  else:
54  super(AnalyseDistance, self).__init__()
55  self.sumDistance = 0
56  self.counter = 0
57  self.exceedCounter = 0
58 
59  def reset(self):
60  self.sumDistance = 0
61  self.counter = 0
62  self.exceedCounter = 0
63 
64  def closest(self, distance, idx, basePt, lineIdx1, lineIdx2, minDistPt):
65  self.counter += 1
66  self.sumDistance += distance
67 
68  def exceeds(self,idx):
69  self.exceedCounter += 1
70 
71  def meanDistance(self):
72  return self.sumDistance / self.counter
73 
74 
75 line1dense = pyDM.GeometricAlgorithms.densify(line1,1.0) #create a densified line with equal vertex spacing of 1 m (or lower)
76 
77 obj = AnalyseDistance()
78 pyDM.GeometricAlgorithms.analyseDistance(line1dense,line2,obj)
79 
80 print("\nResults of distance analysis (no distance threshold):")
81 print("\tMean distance of lines: %.2f" % obj.meanDistance())
82 print("\tVertices exceed distance threshold: %d" % obj.exceedCounter)
83 
84 obj.reset()
85 maxDist = 1;
86 pyDM.GeometricAlgorithms.analyseDistance(line1dense,line2,obj,maxDist)
87 
88 print("\nResults of distance analysis (max distance threshold = %.2f):" % maxDist)
89 print("\tMean distance of lines: %.2f" % obj.meanDistance())
90 print("\tVertices exceed distance threshold: %d" % obj.exceedCounter)