Skip to content

Commit

Permalink
Fix custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Mar 3, 2019
1 parent 2149d04 commit 06c71d6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
31 changes: 17 additions & 14 deletions src/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ export
cgv2el,
cidfrm,
ckcls,
ckcov,
ckcov!,
ckobj,
ckobj!,
ckcov,
ckgp,
ckgpav,
cklpf,
ckobj!,
ckobj,
ckopn,
ckupf,
ckw01,
Expand Down Expand Up @@ -38,7 +38,7 @@ Return the frame name, frame ID, and center associated with a given frame class
### Output ###
Return the tuple `(frcode, frname, center)`.
Returns `nothing` if no frame was found or
- `frcode`: ID code of the frame
- `frname`: Name of the frame
Expand All @@ -51,22 +51,22 @@ Return the tuple `(frcode, frname, center)`.
function ccifrm(frclss, clssid)
lenout = 33
frcode = Ref{SpiceInt}()
frname = Array{UInt8}(undef, lenout)
frname = Array{SpiceChar}(undef, lenout)
center = Ref{SpiceInt}()
found = Ref{SpiceBoolean}()
ccall((:ccifrm_c, libcspice), Cvoid,
(SpiceInt, SpiceInt, SpiceInt, Ref{SpiceInt}, Ref{UInt8}, Ref{SpiceInt}, Ref{SpiceBoolean}),
frclss, clssid, lenout, frcode, frname, center, found,
)
(SpiceInt, SpiceInt, SpiceInt, Ref{SpiceInt}, Ref{SpiceChar},
Ref{SpiceInt}, Ref{SpiceBoolean}),
frclss, clssid, lenout, frcode, frname, center, found)
handleerror()
found[] == 0 && throw(SpiceError("No frame with class $frclss and class ID $clssid found."))
frcode[], chararray_to_string(frname), center[]
Bool(found[]) || return nothing
Int(frcode[]), chararray_to_string(frname), Int(center[])
end

"""
cgv2el(center, vec1, vec2)
Form a CSPICE ellipse from a center vector and two generating vectors.
Form an ellipse from a center vector and two generating vectors.
### Arguments ###
Expand All @@ -76,18 +76,21 @@ Form a CSPICE ellipse from a center vector and two generating vectors.
### Output ###
The CSPICE ellipse defined by the input vectors.
Returns the ellipse defined by the input vectors.
### References ###
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/cgv2el_c.html)
"""
function cgv2el(center, vec1, vec2)
ellipse = Ellipse()
length(center) != 3 && throw(ArgumentError("`center` must have three elements."))
length(vec1) != 3 && throw(ArgumentError("`vec1` must have three elements."))
length(vec2) != 3 && throw(ArgumentError("`vec2` must have three elements."))
ellipse = Ref{Ellipse}()
ccall((:cgv2el_c, libcspice), Cvoid,
(Ref{SpiceDouble}, Ref{SpiceDouble}, Ref{SpiceDouble}, Ref{Ellipse}),
center, vec1, vec2, ellipse)
ellipse
ellipse[]
end

"""
Expand Down
4 changes: 2 additions & 2 deletions src/e.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ Returns the limb of the ellipsoid as seen from the viewing point.
"""
function edlimb(a, b, c, viewpt)
length(viewpt) != 3 && throw(ArgumentError("Length of `viewpt` must be 3."))
limb = Ellipse()
limb = Ref{Ellipse}()
ccall((:edlimb_c, libcspice), Cvoid,
(SpiceDouble, SpiceDouble, SpiceDouble, Ref{SpiceDouble}, Ref{Ellipse}),
a, b, c, viewpt, limb)
handleerror()
limb
limb[]
end

"""
Expand Down
4 changes: 2 additions & 2 deletions src/i.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ Returns `nothing` if no ellipse could be found.
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/inedpl_c.html)
"""
function inedpl(a, b, c, plane)
ellipse = Ellipse()
ellipse = Ref{Ellipse}()
found = Ref{SpiceBoolean}()
ccall((:inedpl_c, libcspice), Cvoid,
(SpiceDouble, SpiceDouble, SpiceDouble, Ref{Plane}, Ref{Ellipse}, Ref{SpiceBoolean}),
a, b, c, plane, ellipse, found)
handleerror()
!Bool(found[]) && return nothing
ellipse
ellipse[]
end

"""
Expand Down
4 changes: 2 additions & 2 deletions src/n.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Returns a struct representing the plane.
"""
function nvc2pl(norm, constant)
length(norm) != 3 && throw(ArgumentError("`norm` must be an iterable with three elements."))
plane = Ref{Plane}(Plane())
plane = Ref{Plane}()
ccall((:nvc2pl_c, libcspice), Cvoid,
(Ref{SpiceDouble}, SpiceDouble, Ref{Plane}), collect(norm), constant, plane)
handleerror()
Expand Down Expand Up @@ -285,7 +285,7 @@ Returns a struct representing the plane.
function nvp2pl(norm, orig)
length(norm) != 3 && throw(ArgumentError("`norm` must be an iterable with three elements."))
length(orig) != 3 && throw(ArgumentError("`orig` must be an iterable with three elements."))
plane = Ref{Plane}(Plane())
plane = Ref{Plane}()
ccall((:nvp2pl_c, libcspice), Cvoid,
(Ref{SpiceDouble}, Ref{SpiceDouble}, Ref{Plane}),
collect(norm), collect(orig), plane)
Expand Down
9 changes: 4 additions & 5 deletions src/p.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,12 @@ Returns the ellipse resulting from the projection.
- [NAIF Documentation](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/pjelpl_c.html)
"""
function pjelpl(elin, plane)
elin, plane
elout = Ellipse()
elout = Ref{Ellipse}()
ccall((:pjelpl_c, libcspice), Cvoid,
(Ref{Ellipse}, Ref{Plane}, Ref{Ellipse}),
elin, plane, elout)
handleerror()
elout
elout[]
end

"""
Expand Down Expand Up @@ -668,12 +667,12 @@ function psv2pl(point, span1, span2)
length(point) != 3 && throw(ArgumentError("Length of `point` needs to be 3."))
length(span1) != 3 && throw(ArgumentError("Length of `span1` needs to be 3."))
length(span2) != 3 && throw(ArgumentError("Length of `span2` needs to be 3."))
plane = Plane()
plane = Ref{Plane}()
ccall((:psv2pl_c, libcspice), Cvoid,
(Ref{SpiceDouble}, Ref{SpiceDouble}, Ref{SpiceDouble}, Ref{Plane}),
point, span1, span2, plane)
handleerror()
plane
plane[]
end

"""
Expand Down
15 changes: 4 additions & 11 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,23 @@ export
semi_minor,
Plane

mutable struct Ellipse
struct Ellipse
center::NTuple{3, SpiceDouble}
semi_major::NTuple{3, SpiceDouble}
semi_minor::NTuple{3, SpiceDouble}
end

function Ellipse()
Ellipse(
(0.0, 0.0, 0.0),
(0.0, 0.0, 0.0),
(0.0, 0.0, 0.0),
)
end

center(e::Ellipse) = collect(e.center)
semi_major(e::Ellipse) = collect(e.semi_major)
semi_minor(e::Ellipse) = collect(e.semi_minor)

mutable struct Plane
struct Plane
normal::NTuple{3, SpiceDouble}
constant::SpiceDouble
end

Plane() = Plane((0.0, 0.0, 0.0), 0.0)
normal(p::Plane) = collect(p.normal)
constant(p::Plane) = p.constant

struct DLADescr
bwdptr::SpiceInt
Expand Down
22 changes: 12 additions & 10 deletions test/c.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
using Random: randstring

@testset "C" begin
@test ccifrm(2, 3000) == (13000, "ITRF93", 399)
@test_throws SpiceError ccifrm(9999, 3000)

@test cidfrm(399) == (10013, "IAU_EARTH")
@test_throws SpiceError cidfrm(999999)

@test clight() == 299792.458

let vec1 = [1.0, 1.0, 1.0],
vec2 = [1.0, -1.0, 1.0],
@testset "ccifrm" begin
@test ccifrm(2, 3000) == (13000, "ITRF93", 399)
@test ccifrm(9999, 3000) === nothing
end
@testset "cgv2el" begin
vec1 = [1.0, 1.0, 1.0]
vec2 = [1.0, -1.0, 1.0]
cent = [-1.0, 1.0, -1.0]
ellipse = cgv2el(cent, vec1, vec2)
@test center(ellipse) [-1.0, 1.0, -1.0]
@test semi_major(ellipse) [sqrt(2.0), 0.0, sqrt(2.0)]
@test semi_minor(ellipse) [0.0, sqrt(2.0), 0.0]
end

@test cidfrm(399) == (10013, "IAU_EARTH")
@test_throws SpiceError cidfrm(999999)

@test clight() == 299792.458

try
sclk = path(CASSINI, :sclk)
ck = path(CASSINI, :ck)
Expand Down

0 comments on commit 06c71d6

Please sign in to comment.