Quaternion Math Utilities

NumPy quaternion math, [w, x, y, z] (MuJoCo) convention.

Quaternion and vector helpers broadcast over leading batch axes (last axis = components), like the torch and JAX twins in quat_math_torch.py / quat_math_jax.py.

myosuite.physics.quat_math.mul_quat(qa, qb)

Hamilton product qa * qb.

myosuite.physics.quat_math.neg_quat(quat)

Conjugate (the inverse of a unit quaternion).

myosuite.physics.quat_math.quat2Vel(quat, dt=1)

Angular velocity that applies rotation quat over dt, as (speed, axis).

speed * axis equals mujoco.mju_quat2Vel: rotations by more than pi are taken the short way round, so q and -q (the same rotation) give the same velocity.

myosuite.physics.quat_math.diff_quat(quat1, quat2)
myosuite.physics.quat_math.quat_diff_to_vel(quat1, quat2, dt)
myosuite.physics.quat_math.axis_angle2quat(axis, angle)

Quaternion of a rotation by angle about the unit vector axis.

myosuite.physics.quat_math.euler2mat(euler)

Euler angles to rotation matrix, intrinsic X-Y’-Z’’ (scipy "XYZ").

myosuite.physics.quat_math.euler2quat(euler)

Euler angles to quaternion, intrinsic X-Y’-Z’’ (scipy "XYZ").

myosuite.physics.quat_math.mat2euler(mat)

Rotation matrix to intrinsic X-Y’-Z’’ Euler angles (scipy "XYZ").

myosuite.physics.quat_math.mat2quat(mat)

Convert Rotation Matrix to Quaternion

myosuite.physics.quat_math.quat2euler(quat)

Quaternion to intrinsic X-Y’-Z’’ Euler angles (scipy "XYZ").

myosuite.physics.quat_math.quat2mat(quat)

Quaternion to rotation matrix (identity for a near-zero quaternion).

myosuite.physics.quat_math.quat2yaw(quat)

Yaw of one quaternion: quat2euler(quat)[2] without the full conversion.

Runs only the operations of quat2mat() and mat2euler() that the yaw depends on, in the same order and with numpy’s sum and arctan2, so the result is bit-identical; the per-step heightmap needs only the yaw.

Parameters:

quat (ndarray) – Quaternion (w, x, y, z), shape (4,).

Return type:

float64

Returns:

The yaw angle in radians.

myosuite.physics.quat_math.rot_vec_mat_t(vec, mat)

Multiply vec by the transpose of the rotation matrix mat.

myosuite.physics.quat_math.rot_vec_mat(vec, mat)

Multiply vec by the rotation matrix mat.

myosuite.physics.quat_math.rot_vec_quat(vec, quat)

Rotate vec by the quaternion quat.

myosuite.physics.quat_math.quat2euler_intrinsic(quat)

Quaternion to [roll, pitch, yaw], the inverse of intrinsic_euler2quat().

Despite the name, the angles are extrinsic x-y-z (scipy "xyz"); pitch lies in [-pi/2, pi/2].

myosuite.physics.quat_math.intrinsic_euler2quat(euler)

[roll, pitch, yaw] to quaternion.

Despite the name, this is the extrinsic x-y-z convention (scipy "xyz"): roll about the fixed X axis, then pitch about the fixed Y, then yaw about the fixed Z (equivalently intrinsic Z-Y’-X’’: yaw, pitch, roll).

myosuite.physics.quat_math.quat_from_euler_xyz_deg(euler_deg)

Convert [roll, pitch, yaw] in degrees to a quaternion.

Uses intrinsic_euler2quat, i.e. extrinsic x-y-z rotations (scipy "xyz"), the convention of the historical half-angle composition used for glove/helmet mesh placement. Do not use euler2quat here: that helper is intrinsic X-Y’-Z’’ (scipy "XYZ").

Parameters:

euler_deg (list[float]) – [roll_deg, pitch_deg, yaw_deg] about the fixed X, Y, Z axes.

Return type:

list[float]

Returns:

Quaternion as [w, x, y, z].

myosuite.physics.quat_math.calculate_cosine(vec1, vec2)

Return cos(theta) between two vectors, supporting batch dimensions.

Parameters:
  • vec1 (ndarray) – First vector (optionally batched).

  • vec2 (ndarray) – Second vector with the same shape as vec1.

Return type:

ndarray

Returns:

Cosine similarity with the same batch shape as the inputs.