QuaternionUtils

February 8, 2024 ยท View on GitHub

from tdw.quaternion_utils import QuaternionUtils

Helper functions for using quaternions.

Quaternions are always numpy arrays in the following order: [x, y, z, w].

This is the same order as any quaternion found in TDW's output data, e.g. Transforms.get_rotation(index).

Vectors are always numpy arrays in the following order: [x, y, z].


Class Variables

VariableTypeDescriptionValue
UPnp.ndarrayThe global up directional vector.np.array([0, 1, 0])
FORWARDnp.ndarrayThe global forward directional vector.np.array([0, 0, 1])
RIGHTnp.ndarrayThe global right directional vector.np.array([1, 0, 0])
IDENTITYnp.ndarrayThe quaternion identity rotation.np.array([0, 0, 0, 1])

Functions

get_inverse

QuaternionUtils.get_inverse(q)

(Static)

Source: https://referencesource.microsoft.com/#System.Numerics/System/Numerics/Quaternion.cs

ParameterTypeDefaultDescription
qnp.ndarrayThe quaternion.

Returns: The inverse of the quaternion.

multiply

QuaternionUtils.multiply(q1, q2)

(Static)

Multiply two quaternions.

Source: https://stackoverflow.com/questions/4870393/rotating-coordinate-system-via-a-quaternion

ParameterTypeDefaultDescription
q1np.ndarrayThe first quaternion.
q2np.ndarrayThe second quaternion.

Returns: The multiplied quaternion: q1 * q2

get_conjugate

QuaternionUtils.get_conjugate(q)

(Static)

Source: https://stackoverflow.com/questions/4870393/rotating-coordinate-system-via-a-quaternion

ParameterTypeDefaultDescription
qnp.ndarrayThe quaternion.

Returns: The conjugate of the quaternion: [-x, -y, -z, w]

multiply_by_vector

QuaternionUtils.multiply_by_vector(q, v)

(Static)

Multiply a quaternion by a vector.

Source: https://stackoverflow.com/questions/4870393/rotating-coordinate-system-via-a-quaternion

ParameterTypeDefaultDescription
qnp.ndarrayThe quaternion.
vnp.ndarrayThe vector.

Returns: A directional vector calculated from: q * v

world_to_local_vector

QuaternionUtils.world_to_local_vector(position, origin, rotation)

(Static)

Convert a vector position in absolute world coordinates to relative local coordinates.

Source: https://answers.unity.com/questions/601062/what-inversetransformpoint-does-need-explanation-p.html

ParameterTypeDefaultDescription
positionnp.ndarrayThe position vector in world coordinates.
originnp.ndarrayThe origin vector of the local space in world coordinates.
rotationnp.ndarrayThe rotation quaternion of the local coordinate space.

Returns: position in local coordinates.

world_to_local_rotation

QuaternionUtils.world_to_local_rotation(world_rotation, local_coord_rotation)

(Static)

Convert a rotation in absolute world coordinates to relative local coordinates.

Source: https://discussions.unity.com/t/what-is-the-rotation-equivalent-of-inversetransformpoint/45386

ParameterTypeDefaultDescription
world_rotationnp.ndarrayThe rotation vector in world coordinates that you want to convert to local coordinates.
local_coord_rotationnp.ndarrayThe rotation vector of the local coordinates in world coordinates.

Returns: rotation vector of world_rotation in local coordinates.

get_up_direction

QuaternionUtils.get_up_direction(q)

(Static)

ParameterTypeDefaultDescription
qnp.ndarrayThe rotation as a quaternion.

Returns: A directional vector corresponding to the "up" direction from the quaternion.

euler_angles_to_quaternion

QuaternionUtils.euler_angles_to_quaternion(euler)

(Static)

Convert Euler angles to a quaternion.

Source: https://pastebin.com/riRLRvch

ParameterTypeDefaultDescription
eulernp.ndarrayThe Euler angles vector.

Returns: The quaternion representation of the Euler angles.

quaternion_to_euler_angles

QuaternionUtils.quaternion_to_euler_angles(quaternion)

(Static)

Convert a quaternion to Euler angles.

Source: https://stackoverflow.com/a/12122899

ParameterTypeDefaultDescription
quaternionnp.ndarrayA quaternion as a nump array.

Returns: The Euler angles representation of the quaternion.

get_y_angle

QuaternionUtils.get_y_angle(q1, q2)

(Static)

Source: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

ParameterTypeDefaultDescription
q1np.ndarrayThe first quaternion.
q2np.ndarrayThe second quaternion.

Returns: The angle between the two quaternions in degrees around the y axis.

is_left_of

QuaternionUtils.is_left_of(origin, target, forward)

(Static)

ParameterTypeDefaultDescription
originnp.ndarrayThe origin position.
targetnp.ndarrayThe target position.
forwardnp.ndarrayThe forward directional vector.

Returns: True if target is to the left of origin by the forward vector; False if it's to the right.