CoordinateTransformations.jl usage arguments?

I’m trying to transform from a Spherical type to an SVector using the CartersianFromSpherical() function, but I get the following error:

julia> CartesianFromSpherical(S)
ERROR: MethodError: Cannot `convert` an object of type CoordinateTransformations.Spherical{Float64} to an object of type Coo
rdinateTransformations.CartesianFromSpherical
This may have arisen from a call to the constructor CoordinateTransformations.CartesianFromSpherical(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] CoordinateTransformations.CartesianFromSpherical(::CoordinateTransformations.Spherical{Float64}) at .\sysimg.jl:24

I take it I’m not calling the function correctly? Paging @andyferris?

CartesianFromSpherical is a type, not a function, so you have to actually instantiate it:

julia> using CoordinateTransformations

julia> CartesianFromSpherical()(Spherical(1., 1., 1.))
3-element SVector{3,Float64}:
 0.291927
 0.454649
 0.841471

More typical usage would be something like:

julia> tform = CartesianFromSpherical()
CartesianFromSpherical()

julia> tform(Spherical(1., 1., 1.))
3-element SVector{3,Float64}:
 0.291927
 0.454649
 0.841471
1 Like

Thanks @rdeits! Since I’m new to types in Julia, would you mind explaining the rationale for having the transform as a type versus a function?

Many transformations have parameters, like the angle of a rotation or the distance of a translation, so it makes sense to construct them like r = RotX(0.2) and then use r(p). Transformations without parameters are just treated the same way (presumably for consistency of the API).

4 Likes

Yeah, that’s right. Arguably one could export a const cartesianfromspherical = CartesianFromSpherical() function (transformation) from CoordianteTransformations, but it didn’t seem that worth it.

Also maybe worth keeping in mind that the spherical/Cartesian/cylindrical stuff was simply a prototype for myself to prove that the method (manipulations on transformations, using composition and inverse, etc) would actually work. To me the useful stuff are the Affine transformations, camera transformations, the transformations in Geodesy.jl, image transformations, and so-on.

1 Like