Skip to content
Snippets Groups Projects
Verified Commit eba57271 authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Add slerp

parent 7c45b837
No related branches found
No related tags found
1 merge request!32Orientations readout and interpolation
......@@ -12,3 +12,44 @@ Calculates the disance between two points.
"""
distance(a::Position, b::Position) = norm(a - b)
"""
Interpolate between two vectors (e.g. quaternions) using the slerp method. `t`
should be between 0 and 1, 0 will produce `q1` and `1` `q2`.
"""
function slerp(q₁, q₂, t::Real; dot_threshold=0.9995)
dot = q₁⋅q₂
if (dot < 0.0)
q₂ *= -1
dot = -dot
end
s₁ = t
s₀ = 1.0 - t
if dot <= dot_threshold
θ₀ = acos(dot)
θ₁ = θ₀ * t
s₁ = sin(θ₁) / sin(θ₀)
s₀ = cos(θ₁) - dot * s₁
end
normalize((s₀ * q₁) + (s₁ * q₂))
end
# Another implementation which yields slightly different results.
# Further reading: http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
#
# function slerp(q₁, q₂, t::Real; dot_threshold=0.9995)
# dot = acos(q₁⋅q₂)
# dot > dot_threshold && return normalize(q₁ + t*(q₂ - q₁))
# dot = clamp(dot, -1, 1)
# θ = acos(dot) * t
# q = normalize(q₂ - q₁*dot)
# q₁*cos(θ) + q*sin(θ)
# end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment