Quaternions
Operations and methods for the algebra of quaternions and rotations. This module is independent from the remainder of DeformableBodies.jl.
Quaternion algebra
DeformableBodies.Quaternions
— ModuleSubmodule of DeformableBodies.jl
implementing quaternion algebra.
Quaternions are a 4-dimensional normed division algebra which extend the complex numbers. They may be used as a representation of rotations on 3-dimensional space.
Exports
DeformableBodies.Quaternions.Quaternion
— TypeQuaternion{T<:Real} <: Number
Quaternion type with components of type T
.
This type overloads all the arithmetic operations as well as the methods defined for Complex numbers that still make sense for Quaternions.
DeformableBodies.Quaternions.components
— Functioncomponents(q)
Return an array with the components of a Quaternion
.
Example
julia> components(Quaternion(1.0, 2.0, 3.0, 4.0))
4-element Array{Float64,1}:
1.0
2.0
3.0
4.0
DeformableBodies.Quaternions.imagq
— Functionimagq(q)
Return imaginary part of Quaternion
as a Quaternion
with no real part.
Examples
julia> a = Quaternion(1,2,3,4)
1 + 2i + 3j + 4k
julia> imagq(a)
0 + 2i + 3j + 4k
DeformableBodies.Quaternions.axis
— Functionaxis(q)
Return the unit vector on the direction of the imaginary part of a Quaternion
.
Examples
julia> Quaternion(10,1,1,0.5)
10.0 + 1.0i + 1.0j + 0.5k
julia> axis(Quaternion(10,1,1,0.5))
3-element Array{Float64,1}:
0.6666666666666666
0.6666666666666666
0.3333333333333333
DeformableBodies.Quaternions.normalize
— Functionnormalize(q)
Return a Quaternion
with the same direction as q
but unit norm.
Examples
julia> q = Quaternion([1., 1., 1., 1.])
1.0 + 1.0i + 1.0j + 1.0k
julia> a = normalize(q)
0.5 + 0.5i + 0.5j + 0.5k
julia> abs(a)
1.0
Rotations
DeformableBodies.Quaternions.rotate
— Functionrotate(v::Vector, q::Quaternion, center=zeros(3))
rotate(v::Vector; axis, angle, center=zeros(3))
Rotate a vector v
by a quaternion q
around a central point center
. The quaternion may be given directly or as an axis and an angle. The point pt
is optional and defaults to the origin.
DeformableBodies.Quaternions.axistoquaternion
— Functionaxistoquaternion(axis, angle)
Receive an axis v
and angle θ
and return the Quaternion
corresponding to a rotation of θ
around v
.
DeformableBodies.Quaternions.quaterniontomatrix
— Functionquaterniontomatrix(q::Quaternion)
Return the rotation matrix associated with a Quaternion
.
DeformableBodies.Quaternions.matrixtoquaternion
— Functionmatrixtoquaternion(R)
Given a rotation matrix R
, return a unit quaternion q
such that rotate(v,q) = R*v
for all v
.
The matrix R
is assumed to be orthogonal but, for efficiency reasons, no check is made to guarantee that.
Since there are, in general, two unit quaternions representing the same rotation matrix, it is not guaranteed that matrixtoquaternion ∘ quaterniontomatrix
equals the identity.