def addTransform(self): # correct orientation self.scannerTransform.setScale3D(QVector3D(100, 100, 100)) self.orientation = QQuaternion(self.qw, self.qx, self.qy, self.qz) self.axisCorrection = QQuaternion.fromEulerAngles(0, 180, 90) self.modelCorrection = QQuaternion.fromEulerAngles(-90, 0, 90) self.modelRotation = self.orientation * self.axisCorrection self.correctedOrientation = self.modelCorrection * self.modelRotation self.scannerTransform.setRotation(self.correctedOrientation) self.scannerEntity.addComponent(self.scannerTransform)
def rotate(self, lastPosition, currentPosition, dt): angle, direction = self.createRotation(lastPosition, currentPosition) rotatedAxis = self._camera.transform().rotation().rotatedVector( direction) angle *= self._rotationSpeed * dt self._camera.rotateAboutViewCenter( QQuaternion.fromAxisAndAngle(rotatedAxis, angle * pi * 180))
def anglesSphToQuaternion(phi, theta): x = sin(radians(theta)) * cos(radians(phi)) y = sin(radians(theta)) * sin(radians(phi)) z = cos(radians(theta)) fromVec = QVector3D(1, 0, 0) toVec = QVector3D(x, y, z) return QQuaternion.rotationTo(fromVec, toVec)
def quaternionfromAxisAndAngle(theta, qvector3D=QVector3D(0, 0, 0)): provVect = (qvector3D.normalized()) s = sin(radians(theta / 2)) directionRot = QVector3D(s * provVect.x(), s * provVect.y(), s * provVect.z()) quat = QQuaternion(cos(radians(theta / 2)), directionRot.x(), directionRot.y(), directionRot.z()) return quat
def rotation(self): """ Get the camera rotation as a quaternion. """ if self._R is None: return None rot = QMatrix3x3([ self._R[0], -self._R[1], -self._R[2], self._R[3], -self._R[4], -self._R[5], self._R[6], -self._R[7], -self._R[8] ]) return QQuaternion.fromRotationMatrix(rot)
def origin(self): super(ViewCamera, self).origin() t = self.view.position() self.move(t[0], t[1], t[2]) r = self.view.rotation[0:3, 0:3].copy() r[1:3, :] = -r[1:3, :] euler = QQuaternion.fromRotationMatrix(QMatrix3x3(list( r.T.flat))).toEulerAngles() self.orient(-euler.y(), -euler.x(), -euler.z()) self.atOrigin = True self.changed = True self.moved = True
def createScene(self): # Root entity self.rootEntity = Qt3DCore.QEntity() # Material self.material = Qt3DExtras.QPhongMaterial(self.rootEntity) # Torus self.torusEntity = Qt3DCore.QEntity(self.rootEntity) self.torusMesh = Qt3DExtras.QTorusMesh() self.torusMesh.setRadius(5) self.torusMesh.setMinorRadius(1) self.torusMesh.setRings(100) self.torusMesh.setSlices(20) self.torusTransform = Qt3DCore.QTransform() self.torusTransform.setScale3D(QVector3D(1.5, 1, 0.5)) self.torusTransform.setRotation( QQuaternion.fromAxisAndAngle(QVector3D(1, 0, 0), 45)) self.torusEntity.addComponent(self.torusMesh) self.torusEntity.addComponent(self.torusTransform) self.torusEntity.addComponent(self.material) # Sphere self.sphereEntity = Qt3DCore.QEntity(self.rootEntity) self.sphereMesh = Qt3DExtras.QSphereMesh() self.sphereMesh.setRadius(3) self.sphereTransform = Qt3DCore.QTransform() self.controller = OrbitTransformController(self.sphereTransform) self.controller.setTarget(self.sphereTransform) self.controller.setRadius(20) self.sphereRotateTransformAnimation = QPropertyAnimation( self.sphereTransform) self.sphereRotateTransformAnimation.setTargetObject(self.controller) self.sphereRotateTransformAnimation.setPropertyName("angle") self.sphereRotateTransformAnimation.setStartValue(0) self.sphereRotateTransformAnimation.setEndValue(360) self.sphereRotateTransformAnimation.setDuration(10000) self.sphereRotateTransformAnimation.setLoopCount(-1) self.sphereRotateTransformAnimation.start() self.sphereEntity.addComponent(self.sphereMesh) self.sphereEntity.addComponent(self.sphereTransform) self.sphereEntity.addComponent(self.material)
def createScene(self): # Root entity self.rootEntity = Qt3DCore.QEntity() # Material self.material = Qt3DExtras.QPhongMaterial(self.rootEntity) # Torus self.torusEntity = Qt3DCore.QEntity(self.rootEntity) self.torusMesh = Qt3DExtras.QTorusMesh() self.torusMesh.setRadius(5) self.torusMesh.setMinorRadius(1) self.torusMesh.setRings(100) self.torusMesh.setSlices(20) self.torusTransform = Qt3DCore.QTransform() self.torusTransform.setScale3D(QVector3D(1.5, 1, 0.5)) self.torusTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(1, 0, 0), 45)) self.torusEntity.addComponent(self.torusMesh) self.torusEntity.addComponent(self.torusTransform) self.torusEntity.addComponent(self.material) # Sphere self.sphereEntity = Qt3DCore.QEntity(self.rootEntity) self.sphereMesh = Qt3DExtras.QSphereMesh() self.sphereMesh.setRadius(3) self.sphereTransform = Qt3DCore.QTransform() self.controller = OrbitTransformController(self.sphereTransform) self.controller.setTarget(self.sphereTransform) self.controller.setRadius(20) self.sphereRotateTransformAnimation = QPropertyAnimation(self.sphereTransform) self.sphereRotateTransformAnimation.setTargetObject(self.controller) self.sphereRotateTransformAnimation.setPropertyName(b"angle") self.sphereRotateTransformAnimation.setStartValue(0) self.sphereRotateTransformAnimation.setEndValue(360) self.sphereRotateTransformAnimation.setDuration(10000) self.sphereRotateTransformAnimation.setLoopCount(-1) self.sphereRotateTransformAnimation.start() self.sphereEntity.addComponent(self.sphereMesh) self.sphereEntity.addComponent(self.sphereTransform) self.sphereEntity.addComponent(self.material)
def decomposeModelMatrix(self, modelMat): """ Decompose a model matrix into individual component. Args: modelMat (QMatrix4x4): model matrix to decompose Returns: QVariant: object containing translation and scale vectors + rotation quaternion """ translation = modelMat.column(3).toVector3D() quaternion = QQuaternion.fromDirection( modelMat.column(2).toVector3D(), modelMat.column(1).toVector3D()) scale = QVector3D( modelMat.column(0).length(), modelMat.column(1).length(), modelMat.column(2).length()) return { "translation": translation, "quaternion": quaternion, "scale": scale }
def computeModelMatrixWithEuler(self, translation, rotation, scale): """ Compute a model matrix from three Vector3D. Args: translation (QVector3D): position in space (x, y, z) rotation (QVector3D): Euler angles in degrees (x, y, z) scale (QVector3D): scale of the object (x, y, z) Returns: QMatrix4x4: corresponding model matrix """ posMat = QMatrix4x4() posMat.translate(translation) quaternion = QQuaternion.fromEulerAngles(rotation) rotMat = self.quaternionToRotationMatrix(quaternion) scaleMat = QMatrix4x4() scaleMat.scale(scale) modelMat = posMat * rotMat * scaleMat return modelMat
def relativeLocalRotate(self, transformQtInstance, initialPosMat, initialRotQuat, initialScaleMat, axis, degree): """ Rotate the QTransform in its local space relatively to an initial state. Args: transformQtInstance (QTransform): reference to the Transform to modify initialPosMat (QMatrix4x4): initial position matrix initialRotQuat (QQuaternion): initial rotation quaternion initialScaleMat (QMatrix4x4): initial scale matrix axis (QVector3D): axis to rotate around degree (int): angle of rotation in degree """ # Compute the transformation quaternion from axis and angle in degrees transformQuat = QQuaternion.fromAxisAndAngle(axis, degree) # Compute the new rotation quaternion and then calculate the matrix newRotQuat = initialRotQuat * transformQuat # Order is important newRotationMat = self.quaternionToRotationMatrix(newRotQuat) # Compute the new model matrix (POSITION * NEW_COMPUTED_ROTATION * SCALE) and set it to the Transform mat = initialPosMat * newRotationMat * initialScaleMat transformQtInstance.setMatrix(mat)
def createScene(self): # Root entity self.rootEntity = Qt3DCore.QEntity() # Material self.material = Qt3DExtras.QPhongMaterial(self.rootEntity) # Torus self.torusEntity = Qt3DCore.QEntity(self.rootEntity) self.torusMesh = Qt3DExtras.QTorusMesh() self.torusMesh.setRadius(5) self.torusMesh.setMinorRadius(1) self.torusMesh.setRings(100) self.torusMesh.setSlices(20) self.torusTransform = Qt3DCore.QTransform() self.torusTransform.setScale3D(QVector3D(1.5, 1, 0.5)) self.torusTransform.setRotation( QQuaternion.fromAxisAndAngle(QVector3D(1, 0, 0), 45)) self.torusEntity.addComponent(self.torusMesh) self.torusEntity.addComponent(self.torusTransform) self.torusEntity.addComponent(self.material) # Sphere self.sphereEntity = Qt3DCore.QEntity(self.rootEntity) self.sphereMesh = Qt3DExtras.QSphereMesh() self.sphereMesh.setRadius(3) self.sphereTransform = Qt3DCore.QTransform() self.controller = OrbitTransformController(self.sphereTransform) self.controller.setTarget(self.sphereTransform) self.controller.setRadius(20) self.sphereEntity.addComponent(self.sphereMesh) self.sphereEntity.addComponent(self.sphereTransform) self.sphereEntity.addComponent(self.material)
def setUp(self): self.original = QQuaternion(1, 2, 3, 4)
def rotateAround(v1, v2, angle): q = QQuaternion.fromAxisAndAngle(v2, angle) return q.rotatedVector(v1)