Skip to content

Commit

Permalink
Fix rotation_between (#259)
Browse files Browse the repository at this point in the history
* add more tests for `rotation_between`

* fix methods for `rotation_between`

* bump version to v1.5.1
  • Loading branch information
hyrodium authored May 16, 2023
1 parent b04d039 commit f00b07d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Rotations"
uuid = "6038ab10-8711-5258-84ad-4b1120ba62dc"
version = "1.5.0"
version = "1.5.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 7 additions & 3 deletions src/rotation_between.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Compute the quaternion that rotates vector `u` so that it aligns with vector
"""
function rotation_between end

function rotation_between(u::SVector{2}, v::SVector{2})
function rotation_between(u::StaticVector{2}, v::StaticVector{2})
c = complex(v[1], v[2]) / complex(u[1], u[2])
iszero(c) && throw(ArgumentError("Input vectors must be nonzero and finite."))
isfinite(c) || throw(ArgumentError("Input vectors must be nonzero and finite."))
theta = Base.angle(c)
return Angle2d(theta)
end

function rotation_between(u::SVector{3}, v::SVector{3})
function rotation_between(u::StaticVector{3}, v::StaticVector{3})
# Robustified version of implementation from https://www.gamedev.net/topic/429507-finding-the-quaternion-betwee-two-vectors/#entry3856228
normprod = sqrt(dot(u, u) * dot(v, v))
T = typeof(normprod)
Expand All @@ -22,9 +24,11 @@ function rotation_between(u::SVector{3}, v::SVector{3})
@inbounds return QuatRotation(w, v[1], v[2], v[3]) # relies on normalization in constructor
end

function rotation_between(u::SVector{N}, v::SVector{N}) where N
function rotation_between(u::StaticVector{N}, v::StaticVector{N}) where N
e1 = normalize(u)
e2 = normalize(v - e1 * dot(e1, v))
any(isnan, e1) && throw(ArgumentError("Input vectors must be nonzero and finite."))
any(isnan, e2) && throw(ArgumentError("Input vectors must be nonzero and finite."))
c = dot(e1, normalize(v))
s = sqrt(1-c^2)
P = [e1 e2 svd([e1 e2]'; full=true).Vt[StaticArrays.SUnitRange(3,N),:]']
Expand Down
22 changes: 19 additions & 3 deletions test/rotation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,30 @@ all_types = (RotMatrix3, RotMatrix{3}, AngleAxis, RotationVec,
end

@testset "$(N)-dimensional rotation_between" for N in 2:7
for _ in 1:100
u = randn(SVector{N})
v = randn(SVector{N})
@testset "random check" begin
for _ in 1:100
u = randn(SVector{N})
v = randn(SVector{N})
R = rotation_between(u,v)
@test isrotation(R)
@test R isa Rotation
@test normalize(v) R * normalize(u)
end
end

@testset "mixed types" begin
u = randn(MVector{N})
v = randn(SizedVector{N})
R = rotation_between(u,v)
@test isrotation(R)
@test R isa Rotation
@test normalize(v) R * normalize(u)
end

@testset "zero-vector" begin
@test_throws ArgumentError rotation_between(zero(SVector{N}), rand(SVector{N}))
@test_throws ArgumentError rotation_between(rand(SVector{N}), zero(SVector{N}))
end
end

#########################################################################
Expand Down

3 comments on commit f00b07d

@hyrodium
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JulliaRegistrator register

@hyrodium
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83639

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.1 -m "<description of version>" f00b07d9d809717bfb6b1d9fc3966e6be059af22
git push origin v1.5.1

Please sign in to comment.