-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rotation_between
for general dimensions (#257)
* deprecate rotation_between(::AbstractVector, ::AbstractVector) * move deprecated `rotation_between` to `src/deprecated.jl` * move `rotation_between(::SVector{3}, ::SVector{3})` to `src/rotation_between.jl` * update `src/rotation_between.jl` * add a method for `rotation_between(::SVector{2}, ::SVector{2})` * add a method for `rotation_between(::SVector{N}, ::SVector{N})` * add tests for `rotation_between` * fix `rotation_between` for general dimensions * fix `rotation_between(::SVector{2}, ::SVector{2})` * bump version to v1.5.0
- Loading branch information
Showing
6 changed files
with
54 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# Deprecate UnitQuaternion => QuatRotation | ||
Base.@deprecate_binding UnitQuaternion QuatRotation true | ||
|
||
Base.@deprecate rotation_between(from::AbstractVector, to::AbstractVector) rotation_between(SVector{3}(from), SVector{3}(to)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
rotation_between(u, v) | ||
Compute the quaternion that rotates vector `u` so that it aligns with vector | ||
`v`, along the geodesic (shortest path). | ||
""" | ||
function rotation_between end | ||
|
||
function rotation_between(u::SVector{2}, v::SVector{2}) | ||
c = complex(v[1], v[2]) / complex(u[1], u[2]) | ||
theta = Base.angle(c) | ||
return Angle2d(theta) | ||
end | ||
|
||
function rotation_between(u::SVector{3}, v::SVector{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) | ||
normprod < eps(T) && throw(ArgumentError("Input vectors must be nonzero.")) | ||
w = normprod + dot(u, v) | ||
v = abs(w) < 100 * eps(T) ? perpendicular_vector(u) : cross(u, v) | ||
@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 | ||
e1 = normalize(u) | ||
e2 = normalize(v - e1 * dot(e1, v)) | ||
c = dot(e1, normalize(v)) | ||
s = sqrt(1-c^2) | ||
P = [e1 e2 svd([e1 e2]'; full=true).Vt[StaticArrays.SUnitRange(3,N),:]'] | ||
Q = one(MMatrix{N,N}) | ||
Q[1,1] = c | ||
Q[1,2] = -s | ||
Q[2,1] = s | ||
Q[2,2] = c | ||
R = RotMatrix(P*Q*P') | ||
return R | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b04d039
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
b04d039
There was a problem hiding this comment.
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/83563
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: