def writeSdl(self, f): csg = Union() halfSegLength = 0.5*self.getSegmentLength() box = \ Box( core.Vec3(-halfSegLength, -self.getRadius(), -0.5*self.getHeight()), core.Vec3(halfSegLength, self.getRadius(), 0.5*self.getHeight()) ) csg.append(box) csg.append( Disk( center = core.Vec3(-halfSegLength, 0, 0), radius = self.getRadius(), height = self.getHeight(), direction = (0,0,1) ) ) csg.append( Disk( center = core.Vec3(halfSegLength, 0, 0), radius = self.getRadius(), height = self.getHeight(), direction = (0,0,1) ) ) csg.writeBegin(f) csg.writeObjects(f) Modifiable.writeSdl(self,f) f.write("\ntranslate <{0:s},{1:s},{2:s}>\n".format(*tuple(self.getCenter()))) csg.writeEnd(f)
def getVtkSource(self): cone = kwvtk.vtkConeSource() cone.SetRadius(self.getRadius()) cone.SetCenter(self.getCenter()) cone.SetHeight(self.getHeight()) cone.SetDirection( core.Vec3(self.getTipPt())-core.Vec3(self.getBasePt()) ) cone.SetResolution(10) return cone
def __init__(self, center, radius, height, direction=None): if (direction == None): direction = core.Vec3(0, 0, 1) direction = core.Vec3(direction) nrm = direction.norm() if (nrm == 0.0): direction = core.Vec3(0, 0, 1) nrm = 1.0 center = core.Vec3(center) direction = direction / nrm Cylinder.__init__(self, endPt1=center - (direction * (height * 0.5)), endPt2=center + (direction * (height * 0.5)), radius=radius)
def computeRotateWxyz(v1, v2): """ Returns the angle (in degrees) and axis of rotation required to rotate v1 onto v2. @type v1: sequence of 3 floats @param v1: Vector to be rotated. @type v2: sequence of 3 floats @param v2: C{v1} rotated parallel to this vector. @rtype: tuple @return: C{(angle, axis)} rotation which rotates C{v1} parallel to C{v2}. """ vv1 = core.Vec3(v1) vv2 = core.Vec3(v2) theta = (math.acos(vv2.dot(vv1)/(vv2.norm()*vv1.norm())))*(180.0/math.pi) rotAxis = vv2.cross(vv1) return (theta, rotAxis/(-(rotAxis.norm())))
def writeSdl(self, f): f.write("cylinder {") f.write("\n<" + ",".join( map(str, core.Vec3(self.getEndPt1()) - core.Vec3(self.getCenter()))) + "> ") f.write("\n<" + ",".join( map(str, core.Vec3(self.getEndPt2()) - core.Vec3(self.getCenter()))) + "> ") f.write(str(self.getRadius())) f.write(" ") Modifiable.writeSdl(self, f) f.write(" translate <") f.write(",".join(map(str, self.getCenter()))) f.write(">") f.write("}")
def __init__(self, minPt, sideLength): """ Constructs axis-aligned cube. @type minPt: sequence of 3 floats @param minPt: lower left back corner coordinate. @type sideLength: float @param sideLength: length of all sides. """ Box.__init__(self, minPt, core.Vec3(minPt) + sideLength)
def getActor(self): cylinder = self.getVtkSource() cylinderMapper = self.getVtkMapper() cylinderMapper.SetInput(cylinder.GetOutput()) cylinderActor = kwvtk.vtkActor() cylinderActor.SetMapper(cylinderMapper) cylinderActor.SetOrigin(self.getCenter()) yDir = core.Vec3(0, 1, 0) # original vtk cylinder-source axis direction axisDir = core.Vec3(self.getEndPt2()) - core.Vec3(self.getEndPt1()) (theta, rotAxis) = computeRotateWxyz(yDir, axisDir) cylinderActor.RotateWXYZ(theta, rotAxis[0], rotAxis[1], rotAxis[2]) self.applyModifiers(cylinderActor) return cylinderActor
def writeSdl(self, f): f.write("cone {") f.write("\n<" + ",".join( map(str, core.Vec3(self.getEndPt1()) - core.Vec3(self.getCenter()))) + "> ") f.write(" {0:s}".format(self.getRadius1())) f.write("\n<" + ",".join( map(str, core.Vec3(self.getEndPt2()) - core.Vec3(self.getCenter()))) + "> ") f.write(str(self.getRadius2())) f.write(" ") Modifiable.writeSdl(self, f) f.write("\ntranslate <") f.write(",".join(map(str, self.getCenter()))) f.write(">\n") f.write("}")
def getActor(self): arrow = self.getVtkSource() arrowMapper = self.getVtkMapper() arrowMapper.SetInput(arrow.GetOutput()) arrowActor = kwvtk.vtkActor() arrowActor.SetMapper(arrowMapper) arrowLength = self.getLength() arrowActor.SetScale([arrowLength] * 3) (theta, axis) = \ computeRotateWxyz( (1,0,0), core.Vec3(self.getHeadPt())-core.Vec3(self.getTailPt()) ) arrowActor.RotateWXYZ(theta, axis[0], axis[1], axis[2]) arrowActor.SetPosition(self.getTailPt()) self.applyModifiers(arrowActor) return arrowActor
def writeSdl(self, f): tailPt = core.Vec3(self.getTailPt()) tipPt = core.Vec3(self.getHeadPt()) dir = tipPt - tailPt arrowLength = dir.norm() if (arrowLength > 0.0): u = Union() dir = dir/arrowLength basePt = tipPt - dir*self.getHeadLength() if (arrowLength > self.getHeadLength()): u.append( Cylinder( endPt1=tailPt, endPt2=basePt, radius=self.getTailRadius() ) ) u.append( Cone(basePt=basePt, tipPt=tipPt, radius=self.getHeadRadius()) ) u.apply(self.getModifierList()) u.writeSdl(f)
def getRight(self): return core.Vec3(self.right) * (2.05 / self.zoomFactor)
def getUp(self): return core.Vec3(self.up) * (2.05 / self.zoomFactor)