copapy.quaternion#

class copapy.quaternion(w=1.0, x=0.0, y=0.0, z=0.0)#

Bases: ArrayType[float]

Mathematical quaternion class for representing 3D rotations.

values#

Internal storage of the (w, x, y, z) components.

Type:

tuple[unifloat, …]

w, x, y, z

Property accessors to individual components.

Type:

unifloat

Create a quaternion with given components.

Parameters:
  • w (value[float] | float | Iterable[value[float] | float]) – w component, or an iterable of 4 components.

  • x (value[float] | float) – x component (ignored if w is an iterable).

  • y (value[float] | float) – y component (ignored if w is an iterable).

  • z (value[float] | float) – z component (ignored if w is an iterable).

conjugate()#

Return the conjugate of the quaternion.

Return type:

quaternion

Returns:

The conjugate quaternion (negates x, y, z components).

classmethod from_euler(roll, pitch, yaw)#

Create a quaternion from Euler angles (roll, pitch, yaw).

Parameters:
  • roll (value[int] | value[float] | int | float) – Rotation around the x-axis in radians.

  • pitch (value[int] | value[float] | int | float) – Rotation around the y-axis in radians.

  • yaw (value[int] | value[float] | int | float) – Rotation around the z-axis in radians.

Return type:

quaternion

Returns:

A quaternion representing the rotation.

classmethod identity()#

Return the identity quaternion (no rotation).

Return type:

quaternion

Returns:

The identity quaternion (x=0, y=0, z=0, w=1).

inverse()#

Return the inverse of the quaternion.

Return type:

quaternion

Returns:

The inverse quaternion. Returns identity if the norm is zero.

map(func)#

Applies a function to each element of the quaternion and returns a new quaternion.

Parameters:

func (Callable[[Any], value[float] | float]) – A function that takes a single argument.

Return type:

quaternion

Returns:

A new quaternion with the function applied to each element.

norm()#

Calculate the norm (magnitude) of the quaternion.

Return type:

value[float] | float

Returns:

The norm (square root of the sum of squared components).

normalize()#

Normalize the quaternion to unit length.

Return type:

quaternion

Returns:

A normalized (unit) quaternion. Returns identity if the norm is zero.

rotate_vector(vec)#

Rotate a 3D vector by this quaternion.

Parameters:

vec (vector[float]) – A 3D vector to rotate.

Return type:

vector[float]

Returns:

The rotated vector.

toAxisAngle()#

Convert the quaternion to axis-angle representation.

Return type:

tuple[vector[float], value[float] | float]

Returns:

A tuple of (axis, angle) where axis is a unit vector and angle is in radians.

toEulerAngles()#

Convert the quaternion to Euler angles (roll, pitch, yaw).

Return type:

vector[float]

Returns:

A vector of [roll, pitch, yaw] in radians.

toRotationMatrix()#

Convert the quaternion to a 4x4 rotation matrix.

Return type:

tensor[float]

Returns:

A 4x4 tensor representing the rotation matrix.

property w: value[float] | float#
property x: value[float] | float#
property y: value[float] | float#
property z: value[float] | float#