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 #6

Merged
merged 1 commit into from
Nov 28, 2015
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
25 changes: 10 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
language: cpp
compiler:
- gcc
language: julia
os:
- linux
- osx
julia:
- 0.3
- 0.4
- nightly
notifications:
email: false
before_install:
- sudo add-apt-repository ppa:staticfloat/julia-deps -y
- sudo add-apt-repository ppa:staticfloat/julianightlies -y
- sudo apt-get update -qq -y
- sudo apt-get install git libpcre3-dev julia -y
- git config --global user.name "Travis User"
- git config --global user.email "travis@example.net"
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
script:
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd())'
- julia ./test/runtests.jl
email: false
# uncomment the following lines to override the default test script
36 changes: 36 additions & 0 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,39 @@ function normalize{T}(v::Vector{T})
end
zeros(promote_type(T,typeof(nv)), length(v))
end


function slerp{T}(qa::Quaternion{T}, qb::Quaternion{T}, t::T)
# http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
coshalftheta = qa.s * qb.s + qa.v1 * qb.v1 + qa.v2 * qb.v2 + qa.v3 * qb.v3;

if coshalftheta < 0
qm = -qb
coshalftheta = -coshalftheta
else
qm = qb
end
abs(coshalftheta) >= 1.0 && return qa

halftheta = acos(coshalftheta)
sinhalftheta = sqrt(one(T) - coshalftheta * coshalftheta)

if abs(sinhalftheta) < 0.001
return Quaternion(
T(0.5) * (qa.s + qb.s),
T(0.5) * (qa.v1 + qb.v1),
T(0.5) * (qa.v2 + qb.v2),
T(0.5) * (qa.v3 + qb.v3),
)
end

ratio_a = sin((one(T) - t) * halftheta) / sinhalftheta
ratio_b = sin(t * halftheta) / sinhalftheta

Quaternion(
qa.s * ratio_a + qm.s * ratio_b,
qa.v1 * ratio_a + qm.v1 * ratio_b,
qa.v2 * ratio_a + qm.v2 * ratio_b,
qa.v3 * ratio_a + qm.v3 * ratio_b,
)
end
3 changes: 2 additions & 1 deletion src/Quaternions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Quaternions
importall Base

include("Quaternion.jl")
include("Octonion.jl")
include("DualQuaternion.jl")
Expand All @@ -25,4 +25,5 @@ module Quaternions
export ndualquatrand
export qrotation
export rotationmatrix
export slerp
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ Rz = [c -s 0; s c 0; 0 0 1]
@test_approx_eq rotationmatrix(qx*qy*qz) Rx*Ry*Rz
@test_approx_eq rotationmatrix(qy*qx*qz) Ry*Rx*Rz
@test_approx_eq rotationmatrix(qz*qx*qy) Rz*Rx*Ry

a, b = qrotation([0,0,1], deg2rad(0)), qrotation([0,0,1], deg2rad(180))
@test_approx_eq slerp(a,b,0.0) a
@test_approx_eq slerp(a,b,1.0) b
@test_approx_eq slerp(a,b,0.5) qrotation([0,0,1], deg2rad(90))