Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding toXYZ() and rotate() for polygons #7

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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