diff --git a/src/r.jl b/src/r.jl index e69de29..49717d9 100644 --- a/src/r.jl +++ b/src/r.jl @@ -0,0 +1,29 @@ +export rav2xf + +""" + rav2xf(rot, av) + +Determine a state transformation matrix from a rotation matrix and the angular +velocity of the rotation. + +### Arguments ### + +- `rot`: Rotation matrix +- `av`: Angular velocity vector + +### Output ### + +Returns state transformation matrix associated with `rot` and `av`. + +### References ### + +- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/rav2xf_c.html) +""" +function rav2xf(rot, av) + xform = Array{SpiceDouble}(undef, 6, 6) + ccall((:rav2xf_c, libcspice), Cvoid, + (Ptr{SpiceDouble}, Ptr{SpiceDouble}, Ptr{SpiceDouble}), + rot, av, xform) + xform +end + diff --git a/src/x.jl b/src/x.jl index 5da5c02..4f5827b 100644 --- a/src/x.jl +++ b/src/x.jl @@ -1,5 +1,32 @@ -export xf2eul +export xf2eul, + xf2rav, + xfmsta, + xpose6, + xpose, + xposeg +""" + xf2eul(xform, axisa, axisb, axisc) + +Convert a state transformation matrix to Euler angles and their derivatives +with respect to a specified set of axes. + +### Arguments ### + +- `xform`: A state transformation matrix +- `axisa`: Axis A of the Euler angle factorization +- `axisb`: Axis B of the Euler angle factorization +- `axisc`: Axis C of the Euler angle factorization + +### Output ### + +Returns a tuple of an array of Euler angles and their derivatives and a boolean +that indicates whether these are a unique representation. + +### References ### + +- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/xf2eul_c.html) +""" function xf2eul(xform, axisa, axisb, axisc) eulang = Array{SpiceDouble}(undef, 6) unique = Ref{SpiceBoolean}() @@ -7,5 +34,89 @@ function xf2eul(xform, axisa, axisb, axisc) (Ptr{SpiceDouble}, SpiceInt, SpiceInt, SpiceInt, Ptr{SpiceDouble}, Ref{SpiceBoolean}), permutedims(xform), axisa, axisb, axisc, eulang, unique) handleerror() - eulang, unique[] + eulang, Bool(unique[]) +end + +""" + xf2rav(xform) + +Determines the rotation matrix and angular velocity of the rotation from a +state transformation matrix. + +### Arguments ### + +- `xform`: State transformation matrix + +### Output ### + +Returns a tuple of the rotation matrix and the angular velocity vector +associated with `xform`. + +### References ### + +- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/xf2rav_c.html) +""" +function xf2rav(xform) + rot = Array{SpiceDouble}(undef, 3, 3) + av = Array{SpiceDouble}(undef, 3) + ccall((:xf2rav_c, libcspice), Cvoid, + (Ptr{SpiceDouble}, Ptr{SpiceDouble}, Ptr{SpiceDouble}), + xform, rot, av) + rot, av +end + +""" + xfmsta(input_state, input_coord_sys, output_coord_sys, body) + +Transform a state between coordinate systems. + +### Arguments ### + +- `input_state`: Input state +- `input_coord_sys`: Current (input) coordinate system +- `output_coord_sys: Desired (output) coordinate system +- `body`: Name or NAIF ID of body with which coordinates are associated (if applicable) + +### Output ### + +Returns the converted output state. + +### References ### + +- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/xfmsta_c.html) +""" +function xfmsta(input_state, input_coord_sys, output_coord_sys, body) + output_state = Array{SpiceDouble}(undef, 6) + ccall((:xfmsta_c, libcspice), Cvoid, + (Ptr{SpiceDouble}, Cstring, Cstring, Cstring, Ptr{SpiceDouble}), + input_state, input_coord_sys, output_coord_sys, body, output_state) + handleerror() + output_state +end + +function _xpose6(m) + mout = Array{SpiceDouble}(undef, 6, 6) + ccall((:xpose6_c, libcspice), Cvoid, (Ptr{SpiceDouble}, Ptr{SpiceDouble}), m, mout) + mout end + +@deprecate xpose6 transpose + +function _xpose(m) + mout = Array{SpiceDouble}(undef, 3, 3) + ccall((:xpose_c, libcspice), Cvoid, (Ptr{SpiceDouble}, Ptr{SpiceDouble}), m, mout) + mout +end + +@deprecate xpose transpose + +function _xposeg(matrix) + m, n = size(matrix) + mout = Array{SpiceDouble}(undef, n, m) + ccall((:xposeg_c, libcspice), Cvoid, + (Ptr{SpiceDouble}, SpiceInt, SpiceInt, Ptr{SpiceDouble}), matrix, n, m, mout) + mout +end + +@deprecate xposeg transpose + diff --git a/test/x.jl b/test/x.jl index 3121f29..4a0905c 100644 --- a/test/x.jl +++ b/test/x.jl @@ -1,8 +1,46 @@ -@testset "X" begin - furnsh(path(CORE, :pck)) - sx = sxform("J2000", "IAU_JUPITER", 0.0) - eulang, unique = xf2eul(sx, 3, 1, 3) - @test round.(eulang.*100000)./100000 == [-3.10768, 0.44513, -1.83172, -0.0, 0.0, 0.0] - @test_throws SpiceException xf2eul(sx, 4, 1, 4) - unload(path(CORE, :pck)) -end \ No newline at end of file +@testset "X" begin + let + furnsh(path(CORE, :pck)) + sx = sxform("J2000", "IAU_JUPITER", 0.0) + eulang, unique = xf2eul(sx, 3, 1, 3) + @test eulang ≈ [-3.10768, 0.44513, -1.83172, -0.0, 0.0, 0.0] rtol=1e-5 + @test_throws SpiceException xf2eul(sx, 4, 1, 4) + unload(path(CORE, :pck)) + end + let + e1 = [1.0, 0.0, 0.0] + rz1 = [0.0 1.0 0.0; -1.0 0.0 0.0; 0.0 0.0 1.0] + xform = rav2xf(rz1, e1) + rz2, e2 = xf2rav(xform) + @test e1 ≈ e2 + @test rz1 ≈ rz2 + end + let + furnsh(path(CORE, :lsk), path(CORE, :spk)) + et = str2et("July 4, 2003 11:00 AM PST") + state, lt = spkezr("Mars", et, "J2000", "Earth", abcorr="LT+S") + expected_lt = 269.6898813661505 + expected_state = [7.38222353105354905128e+07, -2.71279189984722770751e+07, + -1.87413063014898747206e+07, -6.80851334001380692484e+00, + 7.51399612408221173609e+00, 3.00129849265935222391e+00] + @test lt ≈ expected_lt + @test state ≈ expected_state + state_lat = xfmsta(state, "rectangular", "latitudinal", " ") + expected_lat_state = [8.08509924324866235256e+07, -3.52158255331780634112e-01, + -2.33928262716770696272e-01, -9.43348972618204761886e+00, + 5.98157681117165682860e-08, 1.03575559016377728336e-08] + @test state_lat ≈ expected_lat_state + kclear() + end + let a = randn(6, 6) + @test SPICE._xpose6(a) == transpose(a) + @test SPICE._xposeg(a) == transpose(a) + end + let a = randn(3, 3) + @test SPICE._xpose(a) == transpose(a) + @test SPICE._xposeg(a) == transpose(a) + end + let a = randn(3, 5) + @test SPICE._xposeg(a) == transpose(a) + end +end