def transform_point(point, transform): """transform a point from from_cs to to_cs""" hpoint = Vec(point) hpoint.append(1.0) hres = transform.mmul(hpoint) res = vector(hres[1:-1]) / hres[-1] return res
def rotationFromMove(self, vFrom, vTo): rotAxis = vTo.cross(vFrom) d = Vec(vFrom - vTo) t = d.norm() / (2.0 * 1.0) phi = 2.0 * math.asin(t) rotQuat = Quaternion() rotQuat.fromAxisAngle(rotAxis, phi) rotQuat.normalize() return rotQuat
def axis(self): result = Vec([self.quaternion[0], self.quaternion[1], self.quaternion[2]]) sinus = result.norm() if sinus > 1E-8: result /= sinus if math.acos(self.quaternion[3]) <= (math.pi/2.0): return result else: return -result
def axis(self): result = Vec( [self.quaternion[0], self.quaternion[1], self.quaternion[2]]) sinus = result.norm() if sinus > 1E-8: result /= sinus if math.acos(self.quaternion[3]) <= (math.pi / 2.0): return result else: return -result
def transform(self, t): """returns a new configuration, which is this one transformed by matrix t""" newmap = {} for v in self.map: p = self.map[v] ph = Vec(p) ph.append(1.0) ph = t.mmul(ph) p = vector(ph[0:-1]) / ph[-1] newmap[v] = p return Configuration(newmap)
def setViewDirection(self, direction): if direction.normSquared() < 1E-10: return xAxis = Vec(direction.cross(self.upVec)) if xAxis.normSquared() < 1E-10: xAxis = Vec([1.0, 0.0, 0.0]) #piet = xAxis.cross(direction) #print "xAxis: ", xAxis, " direction: ", direction, " cross: " , piet q = Quaternion() q.setFromRotatedBasis(xAxis, xAxis.cross(direction), -direction) self.setAltOrientation(q)
def __init__(self, glViewport, parent=None): """ Initialization of the camerhandler. Parameters: glViewport: the viewport which is controlled by the camera parent: parent of the camerahandler """ QtCore.QObject.__init__(self, parent) self.resolvePoint = Vec([0.0, 0.0, 0.0]) self.lastMousePos = QtCore.QPoint(0, 0) self.lastMousePosOnSphere = Vec([0.0, 0.0, 0.0]) self.activeMouseAction = None self.glViewport = glViewport self.spinningQuaternion = Quaternion()
def execute(self, mouseEvent): """ Process the mouse move event and update the text in the taskbar Parameters: mouseEvent: new position of the mouse in 2D """ camera = self.glViewport.getCamera() """ convert the 2D viewport coordinates into 3D projected on a Y-plane in 3D and for the other viewports to the orthogonal plane. """ translation = Vec( camera.unprojectedCoordinatesOf( [mouseEvent.x(), mouseEvent.y(), 1.0])) if camera.getCameraType() == CameraType.PERSPECTIVE: position = camera.getPositionOnYPlane(translation) else: position = translation self.glViewport.mousePosition[0] = position[0] if self.glViewport.viewportType == ViewportType.SIDE: position[0] = 0.0 self.glViewport.mousePosition[1] = position[1] if self.glViewport.viewportType == ViewportType.TOP: position[1] = 0.0 self.glViewport.mousePosition[2] = position[2] if self.glViewport.viewportType == ViewportType.FRONT: position[2] = 0.0 """ Update the text of the statusbar """ translation[0] = "%.4f" % position[0] translation[1] = "%.4f" % position[1] translation[2] = "%.4f" % position[2] text = QtCore.QString("x:%1, y:%2, z:%3").arg(position[0]).arg( position[1]).arg(position[2]) self.mainWindow.statusBar().showMessage(text)
def handleMouseMove(self, camera, dx, dy): """ Handle the translation of the camera based on the current and last mouse position. Parameters: camera - the camera that needs to be translated dx - current x mouseposition in the view dy - current y mouseposition in the view """ translation = Vec([dx, -dy, 0.0]) if camera.getCameraType() == CameraType.PERSPECTIVE: # check = camera.coordinatesOf([0.0,0.0,0.0]) # print "check: " , check[2] # choke = 2.0 * math.tan(check[2]) / camera.getScreenHeight() # print "choke: " , choke # translation *= choke # print translation translation[0] *= 2.0 translation[1] *= 2.0 # cameraPos[2] += dy*trans else: cameraPos = camera.getPosition() translation[1] *= 2.0 translation[0] *= 2.0 self.translate(camera, translation)
def handleMouseMove(self, mouseEvent, camera, viewPortType, selection): # Rick 20090519 # was: pass # now adding a ghost! if selection != -1: self.prototypeManager.setSelectedObject(selection) if self.ghostPoint != None: translation = Vec(camera.unprojectedCoordinatesOf([mouseEvent.x(), mouseEvent.y(), 1.0])) if viewPortType == ViewportType.PERSPECTIVE: position = camera.getPositionOnYPlane(translation) else: position = translation if viewPortType == ViewportType.FRONT: position[2] = 0.0 elif viewPortType == ViewportType.SIDE: position[0] = 0.0 elif viewPortType == ViewportType.TOP: position[1] = 0.0 self.ghostPoint.setPosition(position) self.needUpdate = True if selection != -1 and self.prototypeManager.objectSelected != None and ( self.prototypeManager.objectSelected.objType == ObjectType.POINT or self.prototypeManager.objectSelected.objType == ObjectType.FIXED_POINT): selectedObject = self.prototypeManager.objectSelected selectedObject.selected = True self.needUpdate = True elif self.prototypeManager.objectSelected != None: self.prototypeManager.deselectObject() self.needUpdate = True
def handleMouseZoom(self, camera, dx, dy): """ Handle the zooming of the camera based on the current and last mouse position. Parameters: camera - the camera that needs to zoom dx - difference between the last and current x mouse position dy - difference between the last and current y mouse position """ self.translate(camera, Vec([0.0, 0.0, dy * 2.0]))
def createGhosts(self, point): pName = "p" + str(self.prototypeManager.objectNr) self.ghostPoint = Point(pName, Vec([point.position[0], point.position[1], point.position[2]]), 5.0) self.ghostPoint.ghost = True self.prototypeManager.addObject(self.ghostPoint) dcName = "d" + str(self.prototypeManager.objectNr) self.ghostDistance = DistanceConstraint(dcName, point, self.ghostPoint) self.ghostDistance.ghost = True self.prototypeManager.addObject(self.ghostDistance)
def __init__(self): Singleton.__init__(self) self.prototypeObject = None self.prototypeManager = PrototypeManager() self.toolType = None self.lastPosition = Vec([0.0,0.0,0.0]) self.needUpdate = False self.needPicking = False self.multipleSelect = False
def getPositionOnZPlane(self, pointPosition): positionOnPlane = Vec([0.0, 0.0, 0.0]) if self.position[2] == pointPosition[2]: t = -pointPosition[2] else: t = -pointPosition[2] / (self.position[2] - pointPosition[2]) positionOnPlane[0] = self.position[0] * t + (1 - t) * pointPosition[0] positionOnPlane[1] = self.position[1] * t + (1 - t) * pointPosition[1] return positionOnPlane
def handleMousePress(self, mouseEvent, camera, viewPortType, selection): translation = Vec(camera.unprojectedCoordinatesOf([mouseEvent.x(), mouseEvent.y(), 1.0])) if viewPortType == ViewportType.PERSPECTIVE: position = camera.getPositionOnYPlane(translation) else: position = translation if viewPortType == ViewportType.FRONT: position[2] = 0.0 elif viewPortType == ViewportType.SIDE: position[0] = 0.0 elif viewPortType == ViewportType.TOP: position[1] = 0.0 #print " position position: " , position[0], position[1], position[2] pName = "p" + str(self.prototypeManager.objectNr) self.newPoint = Point(pName, Vec([position[0], position[1], position[2]]), 5.0) self.prototypeManager.addObject(self.newPoint) self.needUpdate = True
def computeModelView(self): self.orientation.getRotationMatrix(self.modelViewMatrix) #print self.orientation.quaternion v = Vec([0.0, 0.0, 0.0]) v = self.orientation.inverseRotate(self.position) self.modelViewMatrix[3][0] = -v[0] self.modelViewMatrix[3][1] = -v[1] self.modelViewMatrix[3][2] = -v[2] self.modelViewMatrix[3][3] = 1.0
def __init__(self): Tool.__init__(self) self.toolType = ToolType.MOVE self.needPicking = True self.lastPosition = Vec([0.0, 0.0, 0.0]) self.selectionStarted = False self.beginSelPoint = QtCore.QPoint() self.endSelPoint = QtCore.QPoint() self.holdShift = False self.multipleSelect = True
def handleMousePress(self, mouseEvent, camera, viewPortType, selection): translation = Vec(camera.unprojectedCoordinatesOf([mouseEvent.x(), mouseEvent.y(), 1.0])) if viewPortType == ViewportType.PERSPECTIVE: position = camera.getPositionOnYPlane(translation) else: position = translation if viewPortType == ViewportType.FRONT: position[2] = 0.0 elif viewPortType == ViewportType.SIDE: position[0] = 0.0 elif viewPortType == ViewportType.TOP: position[1] = 0.0 fixedPoints = filter(lambda x:x.objType == ObjectType.FIXED_POINT, self.prototypeManager.prtObjects) pName = "f" + str(self.prototypeManager.objectNr) newFixedConstraint = FixedPoint(pName, Vec([position[0], position[1], position[2]]), 5.0) self.prototypeManager.addObject(newFixedConstraint) self.needUpdate = True
def getPositionOnYPlane(self, pointPosition): positionOnPlane = Vec([0.0, 0.0, 0.0]) #print " camera position: ", self.position[0], self.position[1], self.position[2] if self.position[1] == pointPosition[1]: t = -pointPosition[1] else: t = -pointPosition[1] / (self.position[1] - pointPosition[1]) positionOnPlane[0] = self.position[0] * t + (1 - t) * pointPosition[0] positionOnPlane[2] = self.position[2] * t + (1 - t) * pointPosition[2] return positionOnPlane
def projectToTrackball(self, camera, newX, newY): """ Project the mouseposition onto a trackball to rotate the camera. Parameters: newX - new x position on the trackball newY - new y position on the trackball Return: vector - position on the trackball """ x = newX / (camera.getWindowWidth() / 2.0) y = newY / (camera.getWindowHeight() / 2.0) x = x - 1 y = 1 - y z2 = 1 - (x * x + y * y) z = 0.0 if z2 > 0: z = math.sqrt(z2) # print " x, y, z on sphere: " , x, y, z p = Vec([x, y, z]) p.normalize() return p
def __init__(self, glViewport, camType): self.cameraType = camType self.setScreenWidthAndHeight(800, 600) self.farPlane = 2000.0 self.nearPlane = 0.01 self.zFarCoef = math.sqrt(3.0) self.zNearCoef = 0.005 self.position = Vec([0.0, 0.0, 0.0]) self.orientation = Quaternion() self.sceneCenter = Vec([0.0, 0.0, 0.0]) self.target = self.sceneCenter self.upVec = Vec([0.0, 1.0, 0.0]) self.sceneRadius = 1.0 self.aspectRatio = 0.0 self.cameraIsEdited = False self.fieldOfView = 45.0 self.orthoCoef = math.tan(self.fieldOfView / 2.0) self.sceneRadius = 300.0 self.revolveAroundPoint = Vec([0.0, 0.0, 0.0]) self.glViewport = glViewport self.modelViewMatrix = Numeric.identity(4)
def drawViewportAxis(self): glPushMatrix() #glScalef(self.scaleAxis,self.scaleAxis,1.0) glTranslatef(20.0, self.camera.getWindowHeight() - 40.0, -50.0) rotAxis = Vec([0.0, 0.0, 0.0]) rotAngle = self.camera.getOrientation().getAxisAngle( self.camera.getOrientation(), rotAxis) glRotatef(rotAngle, rotAxis[0], -rotAxis[1], rotAxis[2]) self.sceneObjects.drawXAxis() self.sceneObjects.drawYAxis() self.sceneObjects.drawZAxis() glPopMatrix()
def handleMouseRotate(self, camera, newMouseX, newMouseY, dx, dy, projection): """ Handle the rotation of the camera based on the current and last mouse position. Parameters: camera - the camera that needs to be rotated newMouseX - current x mouseposition in the view newMouseY - current y mouseposition in the view dx - difference between the last and current x mouse position dy - difference between the last and current y mouse position projection - projection of the mouseposition on a trackball """ # camera.orientation = camera.orientation * rotQuat self.translate(camera, Vec([dx, dy, 0.0])) camera.lookAt(self.resolvePoint)
def createGhosts(self, point): pName = "p" + str(self.prototypeManager.objectNr) self.ghostPoint = Point(pName, Vec([point.position[0], point.position[1], point.position[2]]), 5.0) self.ghostPoint.ghost = True self.prototypeManager.addObject(self.ghostPoint) dcName = "d" + str(self.prototypeManager.objectNr) self.ghostDistance = Distance(dcName, point, self.ghostPoint) self.ghostDistance.ghost = True self.prototypeManager.addObject(self.ghostDistance) # Rick 20090519 if self.pointMiddle != None: acName = "ghost_angle" + str(self.prototypeManager.objectNr) self.ghostAngle = AngleConstraint(acName, self.pointBegin, self.pointMiddle, self.ghostPoint) self.ghostAngle.ghost = True self.prototypeManager.addObject(self.ghostAngle)
def fromDirection(self, vFrom, vTo): vFrom = Vec(vFrom) vTo = Vec(vTo) epsilon = 1E-10 fromLengthSq = vFrom.normSquared() toLengthSq = vTo.normSquared() if (fromLengthSq < epsilon) or (toLengthSq < epsilon): self.quaternion[0] = self.quaternion[1] = self.quaternion[2] = 0.0 self.quaternion[3] = 1.0 else: axis = vTo.cross(vFrom) axisLengthSq = axis.normSquared() if axisLengthSq < epsilon: if (math.fabs(vFrom[1]) >= math.fabs(vFrom[0])) and (math.fabs( vFrom[2]) >= math.fabs(vFrom[0])): axis[0] = 0.0 axis[1] = -vFrom[2] axis[2] = vFrom[0] elif (math.fabs(vFrom[0]) >= math.fabs(vFrom[1])) and ( math.fabs(vFrom[2]) >= math.fabs(vFrom[1])): axis[0] = -vFrom[2] axis[1] = 0.0 axis[2] = vFrom[0] else: axis[0] = -vFrom[1] axis[1] = vFrom[0] axis[2] = 0.0 squareroot = math.sqrt(axisLengthSq / (fromLengthSq * toLengthSq)) if squareroot > 1.0: squareroot = 1.0 elif squareroot < -1.0: squareroot = -1.0 angle = math.asin(squareroot) if vFrom.dot(vTo) < 0.0: angle = math.pi - angle self.fromAxisAngle(axis, angle)
def handleFit(self, camera): center = camera.getSceneCenter() radius = camera.getSceneRadius() distance = 0.0 cameraType = camera.getCameraType() if cameraType == CameraType.PERSPECTIVE: yView = radius / math.sin(camera.getFieldOfView() / 2.0) xView = radius / math.sin(camera.getHorizontalFov() / 2.0) if xView >= yView: distance = xView else: distance = yView elif cameraType == CameraType.ORTHOGRAPHIC: distance = (( center - camera.getRevolveAroundPoint()) * camera.getViewDirection()) + ( radius / camera.orthoCoef) position = Vec(center - distance * camera.getViewDirection()) camera.setPosition(position)
def __init__(self, viewport, vpType, shareWidget=None, format=None, parent=None): QtOpenGL.QGLWidget.__init__(self, format, parent) self.gridColor = QtGui.QColor(190, 190, 190) #self.gridVisible = True self.viewport = viewport self.sceneObjects = SceneObject(self) self.setMouseTracking(True) self.windowWidth = 0 self.windowHeight = 0 self.zoomfactor = 1.0 self.scaleAxis = 1.0 self.viewportType = vpType self.camera = None self.cameraHandler = CameraHandler(self) self.selectionHandler = SelectionHandler() self.settings = Settings() self.bindings = {} self.bufferSize = 4000 self.selectRegionWidth = 3 self.selectRegionHeight = 3 self.selectionRect = QtCore.QRect() self.selectedName = -1 self.texture = None self.mousePosition = Vec([0.0, 0.0, 0.0]) self.prtManager = PrototypeManager() self.currentTool = None self.updateStatusBar = UpdateTaskbarCommand(self, self.getMainWindow()) if self.viewportType == ViewportType.PERSPECTIVE or self.viewportType == ViewportType.SOLUTION: self.camera = Camera(self, CameraType.PERSPECTIVE) else: self.camera = Camera(self, CameraType.ORTHOGRAPHIC) self.createTriggers() self.setCameraView() self.setBindings()
def rotate(self, vec): quat00 = 2.0 * self.quaternion[0] * self.quaternion[0] quat11 = 2.0 * self.quaternion[1] * self.quaternion[1] quat22 = 2.0 * self.quaternion[2] * self.quaternion[2] quat01 = 2.0 * self.quaternion[0] * self.quaternion[1] quat02 = 2.0 * self.quaternion[0] * self.quaternion[2] quat03 = 2.0 * self.quaternion[0] * self.quaternion[3] quat12 = 2.0 * self.quaternion[1] * self.quaternion[2] quat13 = 2.0 * self.quaternion[1] * self.quaternion[3] quat23 = 2.0 * self.quaternion[2] * self.quaternion[3] rotation = Vec([0.0, 0.0, 0.0]) rotation[0] = (1.0 - quat11 - quat22) * vec[0] + ( quat01 - quat23) * vec[1] + (quat02 + quat13) * vec[2] rotation[1] = (quat01 + quat23) * vec[0] + ( 1.0 - quat22 - quat00) * vec[1] + (quat12 - quat03) * vec[2] rotation[2] = (quat02 - quat13) * vec[0] + ( quat12 + quat03) * vec[1] + (1.0 - quat11 - quat00) * vec[2] #print "rot: ", rotation return rotation
def fromDirection(self, vFrom, vTo): vFrom = Vec(vFrom) vTo = Vec(vTo) epsilon = 1E-10 fromLengthSq = vFrom.normSquared() toLengthSq = vTo.normSquared() if (fromLengthSq < epsilon) or (toLengthSq < epsilon): self.quaternion[0] = self.quaternion[1] = self.quaternion[2] = 0.0 self.quaternion[3] = 1.0 else: axis = vTo.cross(vFrom) axisLengthSq = axis.normSquared() if axisLengthSq < epsilon: if (math.fabs(vFrom[1]) >= math.fabs(vFrom[0])) and (math.fabs(vFrom[2]) >= math.fabs(vFrom[0])): axis[0] = 0.0 axis[1] = -vFrom[2] axis[2] = vFrom[0] elif (math.fabs(vFrom[0]) >= math.fabs(vFrom[1])) and (math.fabs(vFrom[2]) >= math.fabs(vFrom[1])): axis[0] = -vFrom[2] axis[1] = 0.0 axis[2] = vFrom[0] else: axis[0] = -vFrom[1] axis[1] = vFrom[0] axis[2] = 0.0 squareroot = math.sqrt(axisLengthSq / (fromLengthSq * toLengthSq)) if squareroot > 1.0: squareroot = 1.0 elif squareroot < -1.0: squareroot = -1.0 angle = math.asin(squareroot) if vFrom.dot(vTo) < 0.0: angle = math.pi - angle self.fromAxisAngle(axis, angle)
def setPosition(self, position): self.position = Vec(position)
def coordinatesOf(self, src): tempCoOf = Vec([0.0, 0.0, 0.0]) tempCoOf[0] = src[0] - self.position[0] tempCoOf[1] = src[1] - self.position[1] tempCoOf[2] = src[2] - self.position[2] return self.getOrientation().inverseRotate(tempCoOf)
def __init__(self, x=0.0, y=0.0, z=0.0, w=1.0): #self.quaternion = Numeric.array([x, y, z, w]) self.quaternion = Vec([x, y, z, w])