Skip to content

Commit

Permalink
Merge pull request #7 from themmes/dev_themmes
Browse files Browse the repository at this point in the history
Adding toXYZ() and rotate() for polygons
  • Loading branch information
tomturner authored Feb 22, 2021
2 parents 91d6eca + 388b68a commit ae1ec19
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cython_csg/_cython_csg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,39 @@ class Polygon(object):
map(lambda v: v.flip(), self.vertices)
self.plane.flip()

def rotate(self, axis, angleDeg):
"""
Rotate polygon. (copy from rotate geometry)
axis: axis of rotation (array of floats)
angleDeg: rotation angle in degrees
"""
ax = Vector(axis[0], axis[1], axis[2]).unit()
cosAngle = math.cos(math.pi * angleDeg / 180.)
sinAngle = math.sin(math.pi * angleDeg / 180.)

def newVector(v):
vA = v.dot(ax)
vPerp = v.minus(ax.times(vA))
vPerpLen = vPerp.length()
if vPerpLen == 0:
# vector is parallel to axis, no need to rotate
return v
u1 = vPerp.unit()
u2 = u1.cross(ax)
vCosA = vPerpLen*cosAngle
vSinA = vPerpLen*sinAngle
return ax.times(vA).plus(u1.times(vCosA).plus(u2.times(vSinA)))

for vert in self.vertices:
vert.pos = newVector(vert.pos)
normal = vert.normal
if normal.length() > 0:
vert.normal = newVector(vert.normal)

def toXYZ(self):
x, y, z = map(list, zip(*[(vert.pos.x, vert.pos.y, vert.pos.z) for vert in self.vertices]))
return x, y, z

def __repr__(self):
return reduce(lambda x, y: x + y,
['Polygon(['] + [repr(v) + ', ' \
Expand Down

0 comments on commit ae1ec19

Please sign in to comment.