From b34e41ea7627ff9b035eab22cbc1418855b4bc27 Mon Sep 17 00:00:00 2001 From: Tom Hemmes Date: Wed, 31 Oct 2018 11:00:55 +0100 Subject: [PATCH 1/2] Add polygon rotation and toXYZ function --- cython_csg/_cython_csg.pyx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cython_csg/_cython_csg.pyx b/cython_csg/_cython_csg.pyx index 66b9a3e..e569604 100755 --- a/cython_csg/_cython_csg.pyx +++ b/cython_csg/_cython_csg.pyx @@ -305,6 +305,38 @@ 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): + return map(list, zip(*[(vert.pos.x, vert.pos.y, vert.pos.z) for vert in self.vertices])) + def __repr__(self): return reduce(lambda x, y: x + y, ['Polygon(['] + [repr(v) + ', ' \ From 388b68a347707e3dcdb4dcb646d18efc7f2cf10d Mon Sep 17 00:00:00 2001 From: Tom Hemmes Date: Wed, 31 Oct 2018 11:10:02 +0100 Subject: [PATCH 2/2] Update toXYZ for polygon --- cython_csg/_cython_csg.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cython_csg/_cython_csg.pyx b/cython_csg/_cython_csg.pyx index e569604..bc1c1ee 100755 --- a/cython_csg/_cython_csg.pyx +++ b/cython_csg/_cython_csg.pyx @@ -335,7 +335,8 @@ class Polygon(object): vert.normal = newVector(vert.normal) def toXYZ(self): - return map(list, zip(*[(vert.pos.x, vert.pos.y, vert.pos.z) for vert in self.vertices])) + 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,