Skip to content

Commit

Permalink
Merge 776bd9b into 95511f6
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriaenvc committed Nov 23, 2018
2 parents 95511f6 + 776bd9b commit 36a81d5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ deps/windows/usr/
deps/windows/x64
docs/build/
test/kernels/
deps/build.log
27 changes: 27 additions & 0 deletions src/q.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export q2m

"""
q2m(q...)
Find the rotation matrix corresponding to a specified unit quaternion.
### Arguments ###
- `q`: A unit quaternion (as any kind of iterable with four elements)
### Output ###
A rotation matrix corresponding to `q`.
### References ###
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/q2m_c.html)
"""
q2m(q...) = q2m(collect(q))

function q2m(q)
length(q) != 4 && throw(ArgumentError("`q` needs to be an iterable with four elements."))
r = Matrix{SpiceDouble}(undef, 3, 3)
ccall((:q2m_c, libcspice), Cvoid, (Ptr{SpiceDouble}, Ptr{SpiceDouble}), collect(q), r)
permutedims(r)
end
20 changes: 20 additions & 0 deletions test/q.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@testset "Q" begin
let expected = [0.607843137254902 0.27450980392156854 0.7450980392156862;
0.6666666666666666 0.33333333333333326 -0.6666666666666666;
-0.43137254901960775 0.9019607843137255 0.019607843137254832]
actual = q2m(0.5, 0.4, 0.3, 0.1)
@testset for i in eachindex(actual, expected)
@test actual[i] expected[i]
end
q = [0.5, 0.4, 0.3, 0.1]
actual = q2m(q)
@testset for i in eachindex(actual, expected)
@test actual[i] expected[i]
end
q = (0.5, 0.4, 0.3, 0.1)
actual = q2m(q)
@testset for i in eachindex(actual, expected)
@test actual[i] expected[i]
end
end
end

0 comments on commit 36a81d5

Please sign in to comment.