skrobot.coordinates.quaternion.Quaternion

class skrobot.coordinates.quaternion.Quaternion(w=1.0, x=0.0, y=0.0, z=0.0, q=None)[source]

Class for handling Quaternion.

Parameters:

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q
#<Quaternion 0x1283bde48 w: 1.0 x: 0.0 y: 0.0 z: 0.0>
>>> q = Quaternion([1, 2, 3, 4])
>>> q
#<Quaternion 0x1283cad68 w: 1.0 x: 2.0 y: 3.0 z: 4.0>
>>> q = Quaternion(q=[1, 2, 3, 4])
>>> q
#<Quaternion 0x1283bd438 w: 1.0 x: 2.0 y: 3.0 z: 4.0>
>>> q = Quaternion(1, 2, 3, 4)
>>> q
#<Quaternion 0x128400198 w: 1.0 x: 2.0 y: 3.0 z: 4.0>
>>> q = Quaternion(w=0.0, x=1.0, y=0.0, z=0.0)
>>> q
#<Quaternion 0x1283cc2e8 w: 0.0 x: 1.0 y: 0.0 z: 0.0>

Methods

T()[source]

Return 4x4 homogeneous transformation matrix.

Returns:

matrix – homogeneous transformation matrix shape of (4, 4)

Return type:

numpy.ndarray

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q.T()
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> q.q = [1, 2, 3, 4]
>>> q.T()
array([[-0.66666667,  0.13333333,  0.73333333,  0.        ],
       [ 0.66666667, -0.33333333,  0.66666667,  0.        ],
       [ 0.33333333,  0.93333333,  0.13333333,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  1.        ]])
copy()[source]

Return copy of this Quaternion

Returns:

Quaternion(q=self.q.copy()) – copy of this quaternion

Return type:

skrobot.coordinates.quaternion.Quaternion

normalize()[source]

Normalize this quaternion.

Note that this function changes wxyz property.

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion([1, 2, 3, 4])
>>> q.q
array([1., 2., 3., 4.])
>>> q.normalize()
>>> q.q
array([0.18257419, 0.36514837, 0.54772256, 0.73029674])
__eq__(value, /)

Return self==value.

__ne__(value, /)

Return self!=value.

__lt__(value, /)

Return self<value.

__le__(value, /)

Return self<=value.

__gt__(value, /)

Return self>value.

__ge__(value, /)

Return self>=value.

__neg__()[source]
__add__(cls)[source]
__sub__(cls)[source]
__mul__(cls)[source]
__rmul__(cls)[source]
__div__(cls)[source]
__truediv__(cls)[source]

Attributes

angle

Return rotation angle of this quaternion

Returns:

theta – rotation angle with respect to self.axis

Return type:

float

axis

Return axis of this quaternion.

Note that this property return normalized axis.

Returns:

axis – normalized axis vector

Return type:

numpy.ndarray

conjugate

Return conjugate of this quaternion

Returns:

Quaternion – new Quaternion class has this quaternion’s conjugate

Return type:

skrobot.coordinates.quaternion.Quaternion

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q.conjugate
#<Quaternion 0x12f2dfb38 w: 1.0 x: -0.0 y: -0.0 z: -0.0>
>>> q.q = [0, 1, 0, 0]
>>> q.conjugate
#<Quaternion 0x12f303c88 w: 0.0 x: -1.0 y: -0.0 z: -0.0>
inverse

Return inverse of this quaternion

Returns:

q – new Quaternion class has inverse of this quaternion

Return type:

skrobot.coordinates.quaternion.Quaternion

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q
#<Quaternion 0x127e6da58 w: 1.0 x: 0.0 y: 0.0 z: 0.0>
>>> q.inverse
#<Quaternion 0x1281bbda0 w: 1.0 x: -0.0 y: -0.0 z: -0.0>
>>> q.q = [0, 1, 0, 0]
>>> q.inverse
#<Quaternion 0x1282b0cc0 w: 0.0 x: -1.0 y: -0.0 z: -0.0>
norm

Return norm of this quaternion

Returns:

quaternion_norm(self.q) – norm of this quaternion

Return type:

float

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q.norm
1.0
>>> q = Quaternion([1, 2, 3, 4])
>>> q.norm
5.477225575051661
>>> q.normalized.norm
0.9999999999999999
normalized

Return Normalized quaternion.

Returns:

normalized quaternion – return quaternion which is norm == 1.0.

Return type:

skrobot.coordinates.quaternion.Quaternion

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion([1, 2, 3, 4])
>>> normalized_q = q.normalized
>>> normalized_q.q
array([0.18257419, 0.36514837, 0.54772256, 0.73029674])
>>> q.q
array([1., 2., 3., 4.])
q

Return quaternion

Returns:

self._q – [w, x, y, z] quaternion

Return type:

numpy.ndarray

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q.q
array([1., 0., 0., 0.])
>>> q = Quaternion(w=0.0, x=1.0, y=0.0, z=0.0)
>>> q.q
array([0., 1., 0., 0.])
rotation

Return rotation matrix.

Note that this property internally normalizes quaternion.

Returns:

quaternion2matrix(self.q) – 3x3 rotation matrix

Return type:

numpy.ndarray

Examples

>>> from skrobot.coordinates.quaternion import Quaternion
>>> q = Quaternion()
>>> q
#<Quaternion 0x12f1aa6a0 w: 1.0 x: 0.0 y: 0.0 z: 0.0>
>>> q.rotation
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> q.q = [0, 1, 0, 0]
>>> q
#<Quaternion 0x12f1aa6a0 w: 0.0 x: 1.0 y: 0.0 z: 0.0>
>>> q.rotation
array([[ 1.,  0.,  0.],
       [ 0., -1.,  0.],
       [ 0.,  0., -1.]])
>>> q.q = [1, 2, 3, 4]
>>> q
#<Quaternion 0x12f1aa6a0 w: 1.0 x: 2.0 y: 3.0 z: 4.0>
>>> q.rotation
array([[-0.66666667,  0.13333333,  0.73333333],
       [ 0.66666667, -0.33333333,  0.66666667],
       [ 0.33333333,  0.93333333,  0.13333333]])
w

Return w element

Returns:

self.q[0] – w element of this quaternion

Return type:

float

x

Return x element

Returns:

self.q[1] – x element of this quaternion

Return type:

float

xyz

Return xyz vector of this quaternion

Returns:

quaternion_xyz – xyz elements of this quaternion

Return type:

numpy.ndarray

y

Return y element

Returns:

self.q[2] – y element of this quaternion

Return type:

float

z

Return z element

Returns:

self.q[3] – z element of this quaternion

Return type:

float