Linear.Quaternion
- Package
- purescript-linear
- Repository
- afcondon/purescript-linear
Quaternions for 3D rotations.
This module provides quaternions, which are the most efficient and numerically stable way to represent 3D rotations. Quaternions avoid gimbal lock and interpolate smoothly.
#Quaternion Source
data Quaternion aA quaternion representing a rotation in 3D space.
A quaternion q = w + xi + yj + zk is stored as Quaternion w (V3 x y z).
Unit quaternions (where |q| = 1) represent rotations.
identity = Quaternion 1.0 (V3 0.0 0.0 0.0)
Constructors
Quaternion a (V3 a)
Instances
(Eq a) => Eq (Quaternion a)(Show a) => Show (Quaternion a)Functor QuaternionApply QuaternionApplicative QuaternionFoldable QuaternionTraversable QuaternionAdditive QuaternionMetric Quaternion(Semiring a) => Semigroup (Quaternion a)(Semiring a) => Monoid (Quaternion a)(Epsilon a) => Epsilon (Quaternion a)
#conjugate Source
conjugate :: forall a. Ring a => Quaternion a -> Quaternion aThe conjugate of a quaternion.
For unit quaternions, the conjugate equals the inverse and represents the opposite rotation.
conjugate (Quaternion w (V3 x y z)) = Quaternion w (V3 (-x) (-y) (-z))
#inverse Source
inverse :: Quaternion Number -> Quaternion NumberThe multiplicative inverse of a quaternion.
For unit quaternions, this equals the conjugate.
qmul q (inverse q) ≈ identity
#slerp Source
slerp :: Number -> Quaternion Number -> Quaternion Number -> Quaternion NumberSpherical linear interpolation between two quaternions.
This produces smooth rotation interpolation along the shortest path.
slerp 0.0 q1 q2 = q1
slerp 1.0 q1 q2 = q2
slerp 0.5 q1 q2 -- halfway rotation
#getW Source
getW :: forall a. Quaternion a -> aGet the scalar (w) component.
#getXYZ Source
getXYZ :: forall a. Quaternion a -> V3 aGet the vector (xyz) components.
#getX Source
getX :: forall a. Quaternion a -> aGet the x component.
#getY Source
getY :: forall a. Quaternion a -> aGet the y component.
#getZ Source
getZ :: forall a. Quaternion a -> aGet the z component.
#qmul Source
qmul :: forall a. Ring a => Quaternion a -> Quaternion a -> Quaternion aQuaternion multiplication (Hamilton product).
This is NOT commutative: qmul q1 q2 /= qmul q2 q1 in general.
Composing rotations: to apply rotation q1 then q2, use qmul q2 q1.
qmul identity q = q
qmul q identity = q