Quaternions are an alternate way to describe orientation or rotations in 3D space using an ordered set of four numbers. They have the ability to uniquely describe any three-dimensional rotation about an arbitrary axis and do not suffer from gimbal lock
importnumpyasnpdefunit_quaternion(q):q=np.array(q,dtype=float)# q = [w, x, y, z]norm=np.linalg.norm(q)ifnorm==0:raiseValueError("Zero quaternion cannot be normalized")returnq/norm# Exampleq=[2,3,1,4]q_unit=unit_quaternion(q)print("Unit quaternion:",q_unit)print("Norm:",np.linalg.norm(q_unit))# should be 1
Conjugate
A quaternion is:
\[
q = (x, y, z, w)
\]
The conjugate is:
\[
q^* = (-x, -y, -z, w)
\]
flip the signs of x, y, z, keep w the same.
Slerp
Spherical Linear intERPolation (SLERP) is a method to smoothly interpolate between two orientations (unit quaternions).
- SLERP traces the shortest arc along that sphere between them
importnumpyasnpfromscipy.spatial.transformimportRotationasR,Slerp# Define two orientations (Euler angles in degrees)r0=R.from_euler('xyz',[0,0,0],degrees=True)# facing forwardr1=R.from_euler('xyz',[0,90,0],degrees=True)# rotate 90° around Y# Define key times and rotationskey_times=[0,1]# start=0, end=1key_rots=R.concatenate([r0,r1])# list of rotations# Create SLERP objectslerp=Slerp(key_times,key_rots)# Interpolation at multiple timestimes=np.linspace(0,1,5)# t=0.0,0.25,0.5,0.75,1.0interp_rots=slerp(times)# Print quaternionsprint("Interpolated quaternions [x, y, z, w]:")forqininterp_rots.as_quat():print(q.round(3))
Inverse
unit quaternion
If q is a unit quaternion (∥q∥=1), then the conjugate is also the inverse.