Skip to content

Commit

Permalink
Merge b389ef2 into 95511f6
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriaenvc committed Nov 23, 2018
2 parents 95511f6 + b389ef2 commit d5c4b35
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 4 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
29 changes: 29 additions & 0 deletions src/e.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export eul2m

"""
eul2m(angle3, angle2, angle1, axis3, axis2, axis1)
Construct a rotation matrix from a set of Euler angles.
### Arguments ###
- `angle3`, `angle2`, `angle1`: Rotation angles about third, second, and first rotation axes (radians)
- `axis3`, `axis2`, `axis1`: Axis numbers of third, second, and first rotation axes
### Output ###
A rotation matrix corresponding to the product of the 3 rotations.
### References ###
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/eul2m_c.html)
"""
function eul2m(angle3, angle2, angle1, axis3, axis2, axis1)
r = Matrix{SpiceDouble}(undef, 3, 3)
ccall((:eul2m_c, libcspice), Cvoid,
(SpiceDouble, SpiceDouble, SpiceDouble, SpiceInt, SpiceInt, SpiceInt, Ptr{SpiceDouble}),
angle3, angle2, angle1, axis3, axis2, axis1, r)
handleerror()
permutedims(r)

end
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
30 changes: 29 additions & 1 deletion src/r.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export rav2xf
export
rav2xf,
rotate

"""
rav2xf(rot, av)
Expand Down Expand Up @@ -27,3 +29,29 @@ function rav2xf(rot, av)
xform
end

"""
rotate(angle, iaxis)
Calculate the 3x3 rotation matrix generated by a rotation
of a specified angle about a specified axis. This rotation
is thought of as rotating the coordinate system.
### Arguments ###
- `angle`: Angle of rotation (radians)
- `iaxis`: Axis of rotation (X=1, Y=2, Z=3)
### Output ###
Returns rotation matrix associated with `angle` and `iaxis`.
### References ###
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/rotate_c.html)
"""
function rotate(angle, iaxis)
r = Matrix{SpiceDouble}(undef, 3, 3)
ccall((:rotate_c, libcspice), Cvoid, (SpiceDouble, SpiceInt, Ptr{SpiceDouble}), angle, iaxis, r)
permutedims(r)
end

15 changes: 15 additions & 0 deletions test/e.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "E" begin
let
act = eul2m(3, 2, 1, 3, 2, 1)

@test size(act) == (3, 3)

exp = [0.411982245665683 -0.6812427202564033 0.6051272472413688;
0.05872664492762098 -0.642872836134547 -0.7637183366502791;
0.9092974268256817 0.35017548837401463 -0.2248450953661529]

@testset for i in eachindex(act, exp)
@test act[i] exp[i]
end
end
end
18 changes: 15 additions & 3 deletions test/m.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
@testset "M" begin
matrix = [1. 1. 1.
let
matrix = [1. 1. 1.
2. 3. 4.]
vecgood = [1., 2., 3.]
@test mxvg(matrix, vecgood) == [6.,20.]
vecgood = [1., 2., 3.]
@test mxvg(matrix, vecgood) == [6.,20.]
end

let
# r = spice.rotate(spice.halfpi(), 3)

# q = spice.m2q(r)

# expected = [np.sqrt(2) / 2.0, 0.0, 0.0, -np.sqrt(2) / 2.0]

# np.testing.assert_array_almost_equal(expected, q, decimal = 6)
end
end
40 changes: 40 additions & 0 deletions test/q.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@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

let angle = deg2rad.([-20.0, 50.0, -60.0])

m = eul2m(angle[3], angle[2], angle[1], 3, 1, 3)

# q = spice.m2q(m)

# expav = [1.0, 2.0, 3.0]

# qav = [0.0, 1.0, 2.0, 3.0]

# dq = spice.qxq(q, qav)

# dq = [-0.5 * x for x in dq]

# av = spice.qdq2av(q, dq)

# npt.assert_array_almost_equal(av, expav)
end

end
15 changes: 15 additions & 0 deletions test/r.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "R" begin
let
act = rotate/4, 3)

exp = [sqrt(2)/2.0 sqrt(2)/2.0 0.0;
-sqrt(2)/2.0 sqrt(2)/2.0 0.0;
0.0 0.0 1.0]

@testset for i in eachindex(act, exp)
@test act[i] exp[i]
end
end


end

0 comments on commit d5c4b35

Please sign in to comment.