Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Slerp for 3D rotations #252

Merged
merged 5 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rotations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export
RotationVecGenerator,

# Quaternion math ops
logm, expm, ⊖, ⊕,
logm, expm, ⊖, ⊕, slerp,

# Quaternion maps
ExponentialMap, QuatVecMap, CayleyMap, MRPMap, IdentityMap,
Expand Down
3 changes: 3 additions & 0 deletions src/euler_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ for axis in [:X, :Y, :Z]

@inline Base.inv(r::$RotType) = $RotType(-r.theta)

# specialized slerp for single axis rotations
Quaternions.slerp(r1::$RotType, r2::$RotType, t::Real) = $RotType(r1.theta + (mod2pi(r2.theta - r1.theta + π) - π) * t)

# define identity rotations for convenience
@inline Base.one(::Type{$RotType}) = $RotType(0.0)
@inline Base.one(::Type{$RotType{T}}) where {T} = $RotType{T}(zero(T))
Expand Down
8 changes: 8 additions & 0 deletions src/unitquaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ function rotation_between(from::SVector{3}, to::SVector{3})
@inbounds return QuatRotation(w, v[1], v[2], v[3]) # relies on normalization in constructor
end

"""
slerp(R1::Rotaion{3}, R2::Rotaion{3}, t::Real)

Perform spherical linear interpolation (Slerp) between rotations `R1` and `R2`.
"""
Quaternions.slerp(q1::QuatRotation, q2::QuatRotation, t::Real) = QuatRotation(slerp(q1.q, q2.q, t))
Quaternions.slerp(r1::Rotation{3}, r2::Rotation{3}, t::Real) = slerp(QuatRotation(r1), QuatRotation(r2), t)

bicycle1885 marked this conversation as resolved.
Show resolved Hide resolved
# ~~~~~~~~~~~~~~~ Kinematics ~~~~~~~~~~~~~~~ $
"""
kinematics(R::Rotation{3}, ω::AbstractVector)
Expand Down
14 changes: 14 additions & 0 deletions test/rotation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ all_types = (RotMatrix3, RotMatrix{3}, AngleAxis, RotationVec,
end
end

@testset "Slerp rotations" begin
repeats = 100
@testset "slerp($(R1), $(R2), rand())" for R1 in all_types, R2 in all_types
Random.seed!(0)
for i in 1:repeats
r1 = rand(R1)
q1 = QuatRotation(r1)
r2 = rand(R2)
q2 = QuatRotation(r2)
t = rand()
@test slerp(r1, r2, t) ≈ slerp(q1, q2, t)
end
end
end

#########################################################################
# Test conversions between rotation types
Expand Down