Quaternion

Quaternion

Represents a Quaternion rotation.

The Quaternion class provides a number of convenient functions and conversions.

import numpy as np
from pyrr import Quaternion, Matrix33, Matrix44, Vector3, Vector4

q = Quaternion()

# explicit creation
q = Quaternion.from_x_rotation(np.pi / 2.0)
q = Quaternion.from_matrix(Matrix33.identity())
q = Quaternion.from_matrix(Matrix44.identity())

# inferred conversions
q = Quaternion(Quaternion())
q = Quaternion(Matrix33.identity())
q = Quaternion(Matrix44.identity())

# apply one quaternion to another
q1 = Quaternion.from_y_rotation(np.pi / 2.0)
q2 = Quaternion.from_x_rotation(np.pi / 2.0)
q3 = q1 * q2

# extract a matrix from the quaternion
m33 = q3.matrix33
m44 = q3.matrix44

# convert from matrix back to quaternion
q4 = Quaternion(m44)

# rotate a quaternion by a matrix
q = Quaternion() * Matrix33.identity()
q = Quaternion() * Matrix44.identity()

# apply quaternion to a vector
v3 = Quaternion() * Vector3()
v4 = Quaternion() * Vector4()

# undo a rotation
q = Quaternion.from_x_rotation(np.pi / 2.0)
v = q * Vector3([1.,1.,1.])
# ~q is the same as q.conjugate
original = ~q * v
assert np.allclose(original, v)

# get the dot product of 2 Quaternions
dot = Quaternion() | Quaternion.from_x_rotation(np.pi / 2.0)
class pyrr.objects.quaternion.Quaternion
angle

Returns the angle around the axis of rotation of this Quaternion as a float.

axis

Returns the axis of rotation of this Quaternion as a Vector3.

conjugate

Returns the conjugate of this Quaternion.

This is a Quaternion with the opposite rotation.

cross(other)

Returns the cross of this Quaternion and another.

This is the equivalent of combining Quaternion rotations (like Matrix multiplication).

dot(other)

Returns the dot of this Quaternion and another.

exp()

Returns a new Quaternion representing the exponentional of this Quaternion

classmethod from_axis(axis, dtype=None)

Creates a new Quaternion from an axis with angle magnitude.

classmethod from_axis_rotation(axis, theta, dtype=None)

Creates a new Quaternion with a rotation around the specified axis.

classmethod from_eulers(eulers, dtype=None)

Creates a Quaternion from the specified Euler angles.

classmethod from_inverse_of_eulers(eulers, dtype=None)

Creates a Quaternion from the inverse of the specified Euler angles.

classmethod from_matrix(matrix, dtype=None)

Creates a Quaternion from the specified Matrix (Matrix33 or Matrix44).

classmethod from_x_rotation(theta, dtype=None)

Creates a new Quaternion with a rotation around the X-axis.

classmethod from_y_rotation(theta, dtype=None)

Creates a new Quaternion with a rotation around the Y-axis.

classmethod from_z_rotation(theta, dtype=None)

Creates a new Quaternion with a rotation around the Z-axis.

inverse

Returns the inverse of this quaternion.

is_identity

Returns True if the Quaternion has no rotation (0.,0.,0.,1.).

length

Returns the length of this Quaternion.

lerp(other, t)

Interpolates between quat1 and quat2 by t. The parameter t is clamped to the range [0, 1]

matrix33

Returns a Matrix33 representation of this Quaternion.

matrix44

Returns a Matrix44 representation of this Quaternion.

negative

Returns the negative of the Quaternion.

normalise()

normalizes this Quaternion in-place.

normalised

Returns a normalized version of this Quaternion as a new Quaternion.

normalize()

normalizes this Quaternion in-place.

normalized

Returns a normalized version of this Quaternion as a new Quaternion.

power(exponent)

Returns a new Quaternion representing this Quaternion to the power of the exponent.

slerp(other, t)

Spherically interpolates between quat1 and quat2 by t. The parameter t is clamped to the range [0, 1]

w
x
xw
xy
xyw
xyz
xyzw
xz
xzw
y
z