Skip to content

Commit

Permalink
Simplified, and added dimension+measure
Browse files Browse the repository at this point in the history
  • Loading branch information
busstoptaktik committed Mar 20, 2024
1 parent 8fdbda2 commit 8800929
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 157 deletions.
4 changes: 2 additions & 2 deletions examples/01-geometric_geodesy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ fn main() -> Result<(), Error> {
// surface of the ellipsoid. Let's compute the distance and
// azimuth between the approximate locations of the airports
// of Copenhagen (CPH) and Paris (CDG).
let CPH = Coor4D::geo(55., 12., 0., 0.);
let CDG = Coor4D::geo(49., 2., 0., 0.);
let CPH = Coor2D::geo(55., 12.);
let CDG = Coor2D::geo(49., 2.);

// By historical convention the "from A to B" situation is considered
// the inverse sense of the geodesic problem - hence `geodesic_inv`:
Expand Down
5 changes: 3 additions & 2 deletions examples/08-user_defined_operators_using_proj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub fn proj_constructor(parameters: &RawParameters, _ctx: &dyn Context) -> Resul
fn main() -> anyhow::Result<()> {
let mut prv = geodesy::Minimal::new();
prv.register_op("proj", OpConstructor(proj_constructor));
let e = Ellipsoid::default();

// Check that we can access the `proj` binary - if not, just ignore
if Command::new("proj").stderr(Stdio::piped()).spawn().is_err() {
Expand All @@ -195,7 +196,7 @@ fn main() -> anyhow::Result<()> {
println!("projected: {:?}", geo[0]);

ctx.apply(op, Inv, &mut geo)?;
assert!(rtp[0].default_ellps_dist(&geo[0]) < 1e-5);
assert!(e.distance(&rtp[0], &geo[0]) < 1e-5);
println!("roundtrip: {:?}", geo[0].to_degrees());

// Inverted invocation - note "proj inv ..."
Expand All @@ -208,7 +209,7 @@ fn main() -> anyhow::Result<()> {

// Now, we get the inverse utm projection when calling the operator in the Fwd direction
ctx.apply(op, Fwd, &mut utm)?;
assert!(geo[0].default_ellps_dist(&utm[0]) < 1e-5);
assert!(e.distance(&utm[0], &geo[0]) < 1e-5);
// ...and roundtrip back to utm
ctx.apply(op, Inv, &mut utm)?;
assert!(rtp[0].hypot2(&utm[0]) < 1e-5);
Expand Down
12 changes: 3 additions & 9 deletions src/coordinate/coor2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,9 @@ impl Coor2D {
(self[0] - other[0]).hypot(self[1] - other[1])
}

/// The Geodesic distance on the default ellipsoid. Mostly a shortcut
/// for test authoring
pub fn default_ellps_dist(&self, other: &Self) -> f64 {
Ellipsoid::default().distance(
&Coor4D([self[0], self[1], 0., 0.]),
&Coor4D([other[0], other[1], 0., 0.]),
)
}
}


impl From<Coor2D> for Coor4D {
fn from(c: Coor2D) -> Self {
Coor4D([c[0], c[1], 0.0, 0.0])
Expand All @@ -193,11 +186,12 @@ mod tests {
use super::*;
#[test]
fn distances() {
let e = Ellipsoid::default();
let lat = angular::dms_to_dd(55, 30, 36.);
let lon = angular::dms_to_dd(12, 45, 36.);
let dms = Coor2D::geo(lat, lon);
let geo = Coor2D::geo(55.51, 12.76);
assert!(geo.default_ellps_dist(&dms) < 1e-10);
assert!(e.distance(&geo, &dms) < 1e-10);
}

#[test]
Expand Down
12 changes: 2 additions & 10 deletions src/coordinate/coor32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,6 @@ impl Coor32 {
pub fn hypot2(&self, other: &Self) -> f64 {
(self[0] as f64 - other[0] as f64).hypot(self[1] as f64 - other[1] as f64)
}

/// The Geodesic distance on the default ellipsoid. Mostly a shortcut
/// for test authoring
pub fn default_ellps_dist(&self, other: &Self) -> f64 {
Ellipsoid::default().distance(
&Coor4D([self[0] as f64, self[1] as f64, 0., 0.]),
&Coor4D([other[0] as f64, other[1] as f64, 0., 0.]),
)
}
}

// ----- T E S T S ---------------------------------------------------
Expand All @@ -199,7 +190,8 @@ mod tests {
let lon = angular::dms_to_dd(12, 45, 36.);
let dms = Coor32::geo(lat, lon);
let geo = Coor32::geo(55.51, 12.76);
assert!(geo.default_ellps_dist(&dms) < 1e-10);
let e = &Ellipsoid::default();
assert!(e.distance(&dms, &geo) < 1e-9);
}

#[test]
Expand Down
21 changes: 3 additions & 18 deletions src/coordinate/coor3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,37 +263,22 @@ impl Coor3D {
.hypot(self[2] - other[2])
}

/// The 3D distance between two points given as internal angular
/// coordinates. Mostly a shortcut for test authoring
pub fn default_ellps_3d_dist(&self, other: &Self) -> f64 {
let e = Ellipsoid::default();
let from = Coor4D([self[0], self[1], self[2], 0.]);
let to = Coor4D([other[0], other[1], other[2], 0.]);

e.cartesian(&from).hypot3(&e.cartesian(&to))
}

/// The Geodesic distance on the default ellipsoid. Mostly a shortcut
/// for test authoring
pub fn default_ellps_dist(&self, other: &Self) -> f64 {
let from = Coor4D([self[0], self[1], self[2], 0.]);
let to = Coor4D([other[0], other[1], other[2], 0.]);
Ellipsoid::default().distance(&from, &to)
}
}

// ----- T E S T S ---------------------------------------------------

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn distances() {
let e = Ellipsoid::default();
let lat = angular::dms_to_dd(55, 30, 36.);
let lon = angular::dms_to_dd(12, 45, 36.);
let dms = Coor3D::geo(lat, lon, 0.);
let geo = Coor3D::geo(55.51, 12.76, 0.);
assert!(geo.default_ellps_dist(&dms) < 1e-10);
assert!(e.distance(&geo, &dms) < 1e-10);
}

#[test]
Expand Down
17 changes: 3 additions & 14 deletions src/coordinate/coor4d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,33 +269,22 @@ impl Coor4D {
.hypot(self[1] - other[1])
.hypot(self[2] - other[2])
}

/// The 3D distance between two points given as internal angular
/// coordinates. Mostly a shortcut for test authoring
pub fn default_ellps_3d_dist(&self, other: &Self) -> f64 {
let e = Ellipsoid::default();
e.cartesian(self).hypot3(&e.cartesian(other))
}

/// The Geodesic distance on the default ellipsoid. Mostly a shortcut
/// for test authoring
pub fn default_ellps_dist(&self, other: &Self) -> f64 {
Ellipsoid::default().distance(self, other)
}
}

// ----- T E S T S ---------------------------------------------------

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn distances() {
let lat = angular::dms_to_dd(55, 30, 36.);
let lon = angular::dms_to_dd(12, 45, 36.);
let dms = Coor4D::geo(lat, lon, 0., 2020.);
let geo = Coor4D::geo(55.51, 12.76, 0., 2020.);
assert!(geo.default_ellps_dist(&dms) < 1e-10);
let e = Ellipsoid::default();
assert!(e.distance(&geo, &dms) < 1e-10);
}

#[test]
Expand Down
Loading

0 comments on commit 8800929

Please sign in to comment.