DM_polygon_test.py
"""
Small example script showing how to create and manage polygons
(script works in python 2 and 3)
"""
from __future__ import print_function # print function syntax as in python 3
import sys
# from opals import pyDM_d as pyDM #for pyDM in debug mode
from opals import pyDM # for pyDM in release mode
# raw_input("Press Enter to continue...")
def output_polygon_part(level, part):
print("level", level, "part: ", end="")
if part.positveFace() == True:
print(" positive face", end="")
else:
print(" negative face", end="")
if part.isDegenerated() == True:
print(", degenerated")
else:
print("")
for p in part.points():
if sys.float_info.max == p.z:
print("\t(%.3f," % p.x, " %.3f," % p.y, "---)")
else:
print("\t(%.3f," % p.x, " %.3f," % p.y, "%.3f)" % p.z)
if part.sizePart() > 0:
print("part has ", part.sizePart(), "sub parts")
for part in part.parts():
output_polygon_part(level + 1, part)
return
def output_polygon(poly, titel):
print(titel)
for part in poly.parts():
output_polygon_part(0, part)
return
f = pyDM.PolygonFactory()
# simple positive polygon
f.addPoint(0, 0)
f.addPoint(10, 0)
f.addPoint(10, 10)
f.addPoint(0, 10)
f.closePart()
pSimple = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
output_polygon(pSimple, "simple positiv polygon --------------------------------")
# polygon with a hole
f.addPoint(0, 0)
f.addPoint(10, 0)
f.addPoint(10, 10)
f.addPoint(0, 10)
f.closePart()
f.addPoint(5, 5)
f.addPoint(5, 6)
f.addPoint(6, 6)
f.addPoint(6, 5)
f.closePart()
pHole = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
output_polygon(pHole, "polygon with hole -------------------------------------")
# geometric polygon operations =======================================
# another simple positive polygon
f.addPoint(2, 2)
f.addPoint(8, 2)
f.addPoint(8, 8)
f.addPoint(2, 8)
f.closePart()
pSimple2 = f.getPolygon(pyDM.Orientation.ccw) # positive parts are orienated counterclockwise (ccw)
pDiff = pyDM.GeometricOperations.difference(pSimple, pSimple2) # results to a polygon with a hole
output_polygon(pDiff, "polygon created by difference operation ---------------")
# another simple positive polygon
f.addPoint(5, 5)
f.addPoint(6, 5)
f.addPoint(6, 6)
f.addPoint(5, 6)
f.closePart()
pSimple3 = f.getPolygon(pyDM.Orientation.ccw)
pJoin = pyDM.GeometricOperations.join(pDiff, pSimple3) # results to a polygon with a hole containing another polgon
output_polygon(pJoin, "polygon created by join operation ---------------------")
