Skip to content

Commit 529d950

Browse files
author
Andy Ferris
committed
Added convenience constructors to convert Spherical etc
1 parent e08c2bf commit 529d950

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ differentiates with respect to components of `x`:
6363
```
6464

6565
Or perhaps we want to know how `y` will change with respect to changes of
66-
rotation angle:
66+
to the translation parameters:
6767
```julia
68-
∂y_∂θ = transform_deriv_params(rot, x)
68+
∂y_∂θ = transform_deriv_params(trans, x)
6969
```
7070

7171
### The interface
@@ -101,12 +101,9 @@ transformation chain.
101101

102102
#### Coordinate types
103103

104-
The package does not assume any specific coordinate types for Cartesian
105-
coordinates, and aims to accept any indexable container (such as `Vector`,
106-
`Tuple`, *FixedSizeArrays*' `FixedSizeVector{N}` or any other duck-typed vector).
107-
For speed, we recommend using a statically-sized container such as `Point{N}` or
108-
`Vec{N}` from *FixedSizeArrays*, or even an `NTuple{N}`. However, it is
109-
attempted that the package will not change your data type.
104+
The package accepts any `AbstractVector` type for Cartesian coordinates (as
105+
well as *FixedSizeArrays* types in Julia v0.4 only). For speed, we recommend
106+
using a statically-sized container such as `SVector{N}` from *StaticArrays*.
110107

111108
We do provide a few specialist coordinate types. The `Polar(r, θ)` type is a 2D
112109
polar representation of a point, and similarly in 3D we have defined
@@ -130,6 +127,9 @@ transformations:
130127
5. `CartesianFromCylindrical()`
131128
6. `CylindricalFromCartesian()`
132129

130+
However, you may find it simpler to use the convenience constructors like
131+
`Polar(SVector(1.0, 2.0))`.
132+
133133
#### Translations
134134

135135
Translations can be be applied to Cartesian coordinates in arbitrary dimensions,

src/coordinatesystems.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Base.inv(::CartesianFromPolar) = PolarFromCartesian()
5555
compose(::PolarFromCartesian, ::CartesianFromPolar) = IdentityTransformation()
5656
compose(::CartesianFromPolar, ::PolarFromCartesian) = IdentityTransformation()
5757

58+
# For convenience
59+
Base.convert(::Type{Polar}, v::AbstractVector) = PolarFromCartesian()(v)
60+
@inline Base.convert{V <: AbstractVector}(::Type{V}, p::Polar) = convert(V, CartesianFromPolar()(p))
61+
@inline Base.convert{V <: StaticVector}(::Type{V}, p::Polar) = convert(V, CartesianFromPolar()(p))
62+
63+
5864
#############################
5965
### 3D Coordinate Systems ###
6066
#############################
@@ -216,3 +222,15 @@ compose(::CylindricalFromCartesian, ::CartesianFromSpherical) = CylindricalFro
216222
compose(::CartesianFromCylindrical, ::CylindricalFromSpherical) = CartesianFromSpherical()
217223
compose(::CylindricalFromSpherical, ::SphericalFromCartesian) = CylindricalFromCartesian()
218224
compose(::SphericalFromCylindrical, ::CylindricalFromCartesian) = SphericalFromCartesian()
225+
226+
# For convenience
227+
Base.convert(::Type{Spherical}, v::AbstractVector) = SphericalFromCartesian()(v)
228+
Base.convert(::Type{Cylindrical}, v::AbstractVector) = CylindricalFromCartesian()(v)
229+
230+
Base.convert{V <: AbstractVector}(::Type{V}, s::Spherical) = convert(V, CartesianFromSpherical()(s))
231+
Base.convert{V <: AbstractVector}(::Type{V}, c::Cylindrical) = convert(V, CartesianFromCylindrical()(c))
232+
Base.convert{V <: StaticVector}(::Type{V}, s::Spherical) = convert(V, CartesianFromSpherical()(s))
233+
Base.convert{V <: StaticVector}(::Type{V}, c::Cylindrical) = convert(V, CartesianFromCylindrical()(c))
234+
235+
Base.convert(::Type{Spherical}, c::Cylindrical) = SphericalFromCylindrical()(c)
236+
Base.convert(::Type{Cylindrical}, s::Spherical) = CylindricalFromSpherical()(s)

0 commit comments

Comments
 (0)