kornia.geometry.quaternion¶
- class kornia.geometry.quaternion.Quaternion(data)[source]¶
Base class to represent a Quaternion.
A quaternion is a four dimensional vector representation of a rotation transformation in 3d. See more: https://en.wikipedia.org/wiki/Quaternion
The general definition of a quaternion is given by:
\[Q = a + b \cdot \mathbf{i} + c \cdot \mathbf{j} + d \cdot \mathbf{k}\]Thus, we represent a rotation quaternion as a contiguous torch.Tensor structure to perform rigid bodies transformations:
\[Q = \begin{bmatrix} q_w & q_x & q_y & q_z \end{bmatrix}\]Example
>>> q = Quaternion.identity(batch_size=4) >>> q.data tensor([[1., 0., 0., 0.], [1., 0., 0., 0.], [1., 0., 0., 0.], [1., 0., 0., 0.]]) >>> q.real tensor([1., 1., 1., 1.]) >>> q.vec tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
- __add__(right)[source]¶
Add a given quaternion, scalar, or torch.Tensor.
- Parameters:
right (
Union[Quaternion,Tensor,float]) – the quaternion, scalar, or torch.Tensor to add.- Return type:
Example
>>> q1 = Quaternion.identity() >>> q2 = Quaternion(torch.tensor([2., 0., 1., 1.])) >>> q3 = q1 + q2 >>> q3.data tensor([3., 0., 1., 1.])
- __init__(data)[source]¶
Construct a quaternion from torch.Tensor or parameter data.
- Parameters:
data (
Union[Tensor,Parameter]) – torch.Tensor or parameter containing the quaternion data with the shape of \((B, 4)\).
Example
>>> # Create with torch.tensor(no gradients tracked by default) >>> data = torch.tensor([1., 0., 0., 0.]) >>> q1 = Quaternion(data) >>> # Create with parameter (gradients tracked) >>> param_data = torch.nn.Parameter(torch.tensor([1., 0., 0., 0.])) >>> q2 = Quaternion(param_data)
- __neg__()[source]¶
Inverts the sign of the quaternion data.
- Return type:
Example
>>> q = Quaternion.identity() >>> -q.data tensor([-1., -0., -0., -0.])
- __pow__(t)[source]¶
Return the power of a quaternion raised to exponent t.
- Parameters:
t (
float) – raised exponent.- Return type:
Example
>>> q = Quaternion(torch.tensor([1., .5, 0., 0.])) >>> q_pow = q**2
- __radd__(left)[source]¶
Right addition (left + self) where left is a scalar or torch.Tensor.
- Return type:
- __rdiv__(left)[source]¶
Right division (left / self) where left is a scalar or torch.Tensor.
- Return type:
- __rmul__(left)[source]¶
Right multiplication (left * self) where left is a scalar or torch.Tensor.
- Return type:
- __rsub__(left)[source]¶
Right subtraction (left - self) where left is a scalar or torch.Tensor.
- Return type:
- __rtruediv__(left)[source]¶
Right division (left / self) where left is a scalar or torch.Tensor.
- Return type:
- __sub__(right)[source]¶
Subtract a given quaternion, scalar, or torch.Tensor.
- Parameters:
right (
Union[Quaternion,Tensor,float]) – the quaternion, scalar, or torch.Tensor to subtract.- Return type:
Example
>>> q1 = Quaternion(torch.tensor([2., 0., 1., 1.])) >>> q2 = Quaternion.identity() >>> q3 = q1 - q2 >>> q3.data tensor([1., 0., 1., 1.])
- property coeffs: Tuple[Tensor, Tensor, Tensor, Tensor]¶
Return a tuple with the underlying coefficients in WXYZ order.
- conj()[source]¶
Compute the conjugate of the quaternion.
- Return type:
- Returns:
The conjugate quaternion, with the vector part negated.
Example
>>> q = Quaternion(torch.tensor([1., 2., 3., 4.])) >>> q_conj = q.conj()
- classmethod from_axis_angle(axis_angle)[source]¶
Create a quaternion from axis-angle representation.
- Parameters:
axis_angle (
Tensor) – rotation vector of shape \((B, 3)\).- Return type:
Example
>>> axis_angle = torch.tensor([[1., 0., 0.]]) >>> q = Quaternion.from_axis_angle(axis_angle) >>> q.data tensor([[0.8776, 0.4794, 0.0000, 0.0000]])
- classmethod from_coeffs(w, x, y, z)[source]¶
Create a quaternion from the data coefficients.
- Parameters:
- Return type:
Example
>>> q = Quaternion.from_coeffs(1., 0., 0., 0.) >>> q.data tensor([1., 0., 0., 0.])
- classmethod from_euler(roll, pitch, yaw)[source]¶
Create a quaternion from Euler angles.
- Parameters:
- Return type:
Example
>>> roll, pitch, yaw = torch.tensor(0), torch.tensor(1), torch.tensor(0) >>> q = Quaternion.from_euler(roll, pitch, yaw) >>> q.data tensor([0.8776, 0.0000, 0.4794, 0.0000])
- classmethod from_matrix(matrix)[source]¶
Create a quaternion from a rotation matrix.
- Parameters:
matrix (
Tensor) – the rotation matrix to convert of shape \((B, 3, 3)\).- Return type:
Example
>>> m = torch.eye(3)[None] >>> q = Quaternion.from_matrix(m) >>> q.data tensor([[1., 0., 0., 0.]])
- classmethod identity(batch_size=None, device=None, dtype=None)[source]¶
Create a quaternion representing an identity rotation.
- Parameters:
- Return type:
Example
>>> q = Quaternion.identity() >>> q.data tensor([1., 0., 0., 0.])
- inv()[source]¶
Compute the inverse of the quaternion.
- Return type:
- Returns:
The inverse quaternion.
Example
>>> q = Quaternion.identity() >>> q_inv = q.inv()
- matrix()[source]¶
Convert the quaternion to a rotation matrix of shape \((B, 3, 3)\).
- Return type:
Example
>>> q = Quaternion.identity() >>> m = q.matrix() >>> m tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
- norm(keepdim=False)[source]¶
Compute the norm (magnitude) of the quaternion.
- Parameters:
keepdim (
bool, optional) – whether to retain the last dimension. Default:False- Return type:
- Returns:
The norm of the quaternion(s) as a torch.Tensor.
Example
>>> q = Quaternion.identity() >>> q.norm() tensor(1.)
- normalize()[source]¶
Return a normalized (unit) quaternion.
- Return type:
- Returns:
The normalized quaternion.
Example
>>> q = Quaternion(torch.tensor([2., 1., 0., 0.])) >>> q_norm = q.normalize()
- property polar_angle: Tensor¶
Return the polar angle with shape \((B,1)\).
Example
>>> q = Quaternion.identity() >>> q.polar_angle tensor(0.)
- classmethod random(batch_size=None, device=None, dtype=None)[source]¶
Create a random unit quaternion of shape \((B, 4)\).
Uniformly distributed across the rotation space as per: http://planning.cs.uiuc.edu/node198.html
- Parameters:
- Return type:
Example
>>> q = Quaternion.random() >>> q = Quaternion.random(batch_size=2)
- property real: Tensor¶
Return the real part with shape \((B,)\).
Alias for :func: ~kornia.geometry.quaternion.Quaternion.w
- property scalar: Tensor¶
Return a scalar with the real with shape \((B,)\).
Alias for :func: ~kornia.geometry.quaternion.Quaternion.w
- slerp(q1, t)[source]¶
Return a unit quaternion spherically interpolated between quaternions self.q and q1.
See more: https://en.wikipedia.org/wiki/Slerp
- Parameters:
q1 (
Quaternion) – second quaternion to be interpolated between.t (
float) – interpolation ratio, range [0-1]
- Return type:
Example
>>> q0 = Quaternion.identity() >>> q1 = Quaternion(torch.tensor([1., .5, 0., 0.])) >>> q2 = q0.slerp(q1, .3)
- squared_norm()[source]¶
Compute the squared norm (magnitude) of the quaternion.
- Return type:
- Returns:
The squared norm of the quaternion(s) as a torch.Tensor.
Example
>>> q = Quaternion.identity() >>> q.squared_norm() tensor(1.)
- to(*args, **kwargs)[source]¶
Move and/or cast the quaternion data.
- Parameters:
- Return type:
- Returns:
A new Quaternion with converted data.
- to_axis_angle()[source]¶
Convert the quaternion to an axis-angle representation.
- Return type:
Example
>>> q = Quaternion.identity() >>> axis_angle = q.to_axis_angle() >>> axis_angle tensor([0., 0., 0.])