DM_polygon_test.py
1 """
2  Small example script showing how to create and manage polygons
3  (script works in python 2 and 3)
4 """
5 
6 from __future__ import print_function # print function syntax as in python 3
7 import sys
8 
9 from opals import pyDM
10 
11 
12 # raw_input("Press Enter to continue...")
13 
14 def output_polygon_part(level, part):
15  print("level", level, "part: ", end="")
16  if part.positveFace() == True:
17  print(" positive face", end="")
18  else:
19  print(" negative face", end="")
20  if part.isDegenerated() == True:
21  print(", degenerated")
22  else:
23  print("")
24 
25  for p in part.points():
26  if sys.float_info.max == p.z:
27  print("\t(%.3f," % p.x, " %.3f," % p.y, "---)")
28  else:
29  print("\t(%.3f," % p.x, " %.3f," % p.y, "%.3f)" % p.z)
30 
31  if part.sizePart() > 0:
32  print("part has ", part.sizePart(), "sub parts")
33  for part in part.parts():
34  output_polygon_part(level + 1, part)
35 
36  return
37 
38 
39 def output_polygon(poly, titel):
40  print(titel)
41  for part in poly.parts():
42  output_polygon_part(0, part)
43  return
44 
45 
46 f = pyDM.PolygonFactory()
47 
48 # simple positive polygon
49 f.addPoint(0, 0)
50 f.addPoint(10, 0)
51 f.addPoint(10, 10)
52 f.addPoint(0, 10)
53 f.closePart()
54 pSimple = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
55 
56 output_polygon(pSimple, "simple positiv polygon --------------------------------")
57 
58 # polygon with a hole
59 f.addPoint(0, 0)
60 f.addPoint(10, 0)
61 f.addPoint(10, 10)
62 f.addPoint(0, 10)
63 f.closePart()
64 f.addPoint(5, 5)
65 f.addPoint(5, 6)
66 f.addPoint(6, 6)
67 f.addPoint(6, 5)
68 f.closePart()
69 pHole = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
70 
71 output_polygon(pHole, "polygon with hole -------------------------------------")
72 
73 # geometric polygon operations =======================================
74 
75 # another simple positive polygon
76 f.addPoint(2, 2)
77 f.addPoint(8, 2)
78 f.addPoint(8, 8)
79 f.addPoint(2, 8)
80 f.closePart()
81 pSimple2 = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
82 
83 pDiff = pyDM.GeometricOperations.difference(pSimple, pSimple2) # results to a polygon with a hole
84 
85 output_polygon(pDiff, "polygon created by difference operation ---------------")
86 
87 # another simple positive polygon
88 f.addPoint(5, 5)
89 f.addPoint(6, 5)
90 f.addPoint(6, 6)
91 f.addPoint(5, 6)
92 f.closePart()
93 pSimple3 = f.getPolygon(pyDM.Orientation.ccw)
94 
95 pJoin = pyDM.GeometricOperations.join(pDiff, pSimple3) # results to a polygon with a hole containing another polgon
96 
97 output_polygon(pJoin, "polygon created by join operation ---------------------")