The unit sphere in the space of quaternions has the group structure SU(2) coming from the quaternion multiplication, which is the double cover of the group SO(3) of rotations in R^3. Thus, unit quaternions can be used to encode rotations in 3D, which is a more compact encoding (4 floats) than a 3x3 matrix; however, there are two quaternions corresponding to each rotation.
See http://en.wikipedia.org/wiki/Quaternion and http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation for more information.
- newtype Quaternion = Q Vec4
- newtype UnitQuaternion = U Vec4
- type Q = Quaternion
- type U = UnitQuaternion
- unitQ :: Q
- zeroQ :: Q
- multQ :: Q -> Q -> Q
- negQ :: Q -> Q
- normalizeQ :: Q -> Q
- invQ :: Q -> Q
- fromQ :: Q -> Vec4
- toQ :: Vec4 -> Q
- unitU :: U
- multU :: U -> U -> U
- negU :: U -> U
- normalizeU :: U -> U
- invU :: U -> U
- fromU :: U -> Vec4
- fromU' :: U -> Normal4
- mkU :: Vec4 -> U
- toU :: Normal4 -> U
- unsafeToU :: Vec4 -> U
- actU :: U -> Vec3 -> Vec3
- rotU :: Vec3 -> Float -> U
- rotU' :: Normal3 -> Float -> U
- longSlerpU :: Float -> U -> U -> U
- slerpU :: Float -> U -> U -> U
- rightOrthoU :: U -> Ortho3
- leftOrthoU :: U -> Ortho3
types
newtype Quaternion Source
The type for quaternions.
newtype UnitQuaternion Source
The type for unit quaternions.
type Q = QuaternionSource
An abbreviated type synonym for quaternions
type U = UnitQuaternionSource
An abbreviated type synonym for unit quaternions
general quaternions
normalizeQ :: Q -> QSource
unit quaternions
normalizeU :: U -> USource
This is no-op, up to numerical imprecision. However, if you multiply together a large number of unit quaternions, it may be a good idea to normalize the end result.
unit quaternions as rotations
actU :: U -> Vec3 -> Vec3Source
The left action of unit quaternions on 3D vectors. That is,
actU q1 $ actU q2 v == actU (q1 `multU` q2) v
rotU :: Vec3 -> Float -> USource
The quaternion to encode rotation around an axis. Please note that quaternions act on the left, that is
rotU axis1 angl1 *. rotU axis2 angl2 *. v == (rotU axis1 angl1 .*. rotU axis2 angl2) *. v
longSlerpU :: Float -> U -> U -> USource
Interpolation of unit quaternions. Note that when applied to rotations,
this may be not what you want, since it is possible that the shortest path
in the space of unit quaternions is not the shortest path in the space of
rotations; see slerpU
!
slerpU :: Float -> U -> U -> USource
This is shortest path interpolation in the space of rotations; however
this is achieved by possibly flipping the first endpoint in the space of
quaternions. Thus slerpU 0.001 q1 q2
may be very far from q1
(and very
close to negU q1
) in the space of quaternions (but they are very close
in the space of rotations).
rightOrthoU :: U -> Ortho3Source
Makes a rotation matrix (to be multiplied with on the right) out of a unit quaternion:
v .* rightOrthoU (rotU axis angl) == v .* rotMatrix3 axis angl
Please note that while these matrices act on the right, quaternions act on the left; thus
rightOrthoU q1 .*. rightOrthoU q2 == rightOrthoU (q2 .*. q1)
leftOrthoU :: U -> Ortho3Source
Makes a rotation matrix (to be multiplied with on the left) out of a unit quaternion.
leftOrthoU (rotU axis angl) *. v == v .* rotMatrix3 axis angl