use alloc::vec::Vec;
use geometry_cs::{CoordinateSystem, SphericalFamily};
use geometry_tag::SameAs;
use geometry_trait::{Closure, Linestring, Point, Ring};
use crate::distance::DistanceStrategy;
use crate::length::LengthStrategy;
use crate::spherical::Haversine;
#[derive(Debug, Default, Clone, Copy)]
pub struct SphericalLength {
pub haversine: Haversine,
}
#[derive(Debug, Default, Clone, Copy)]
pub struct SphericalPerimeter {
pub haversine: Haversine,
}
impl<L> LengthStrategy<L> for SphericalLength
where
L: Linestring,
<L::Point as Point>::Cs: CoordinateSystem,
<<L::Point as Point>::Cs as CoordinateSystem>::Family: SameAs<SphericalFamily>,
Haversine: DistanceStrategy<L::Point, L::Point, Out = <L::Point as Point>::Scalar>,
{
type Out = <L::Point as Point>::Scalar;
#[inline]
fn length(&self, g: &L) -> Self::Out {
let pts: Vec<&L::Point> = g.points().collect();
let mut acc = <Self::Out as geometry_coords::CoordinateScalar>::ZERO;
for w in pts.windows(2) {
acc = acc + self.haversine.distance(w[0], w[1]);
}
acc
}
}
impl<R> LengthStrategy<R> for SphericalPerimeter
where
R: Ring,
<R::Point as Point>::Cs: CoordinateSystem,
<<R::Point as Point>::Cs as CoordinateSystem>::Family: SameAs<SphericalFamily>,
Haversine: DistanceStrategy<R::Point, R::Point, Out = <R::Point as Point>::Scalar>,
{
type Out = <R::Point as Point>::Scalar;
#[inline]
fn length(&self, g: &R) -> Self::Out {
let pts: Vec<&R::Point> = g.points().collect();
let mut acc = <Self::Out as geometry_coords::CoordinateScalar>::ZERO;
for w in pts.windows(2) {
acc = acc + self.haversine.distance(w[0], w[1]);
}
match (g.closure(), pts.first(), pts.last()) {
(Closure::Open, Some(first), Some(last)) => acc + self.haversine.distance(last, first),
_ => acc,
}
}
}
#[cfg(all(test, feature = "std"))]
mod tests {
#![allow(
clippy::float_cmp,
reason = "lengths are compared with an explicit absolute tolerance, not `==`"
)]
use super::{SphericalLength, SphericalPerimeter};
use crate::distance::DistanceStrategy;
use crate::length::LengthStrategy;
use crate::spherical::Haversine;
use geometry_adapt::{Adapt, WithCs};
use geometry_cs::{Degree, Spherical};
use geometry_model::{Linestring, Ring};
type Sp = WithCs<Adapt<[f64; 2]>, Spherical<Degree>>;
#[inline]
fn deg(lon: f64, lat: f64) -> Sp {
WithCs::new(Adapt([lon, lat]))
}
#[test]
fn ams_paris_haversine_length() {
let ls: Linestring<Sp> = Linestring(vec![deg(4.90, 52.37), deg(2.35, 48.86)]);
let got = SphericalLength::default().length(&ls);
let direct = Haversine::EARTH.distance(°(4.90, 52.37), °(2.35, 48.86));
assert!((got - direct).abs() < 1e-6);
}
#[test]
fn three_point_great_circle() {
let ls: Linestring<Sp> = Linestring(vec![deg(0., 0.), deg(0., 10.), deg(0., 20.)]);
let got = SphericalLength::default().length(&ls);
let segment = Haversine::EARTH.distance(°(0., 0.), °(0., 10.));
assert!((got - segment * 2.0).abs() < 1e-6);
}
#[test]
fn open_ring_closes_itself() {
let mut r = Ring::<Sp, true, false>::new();
r.push(deg(0., 0.));
r.push(deg(10., 0.));
r.push(deg(10., 10.));
r.push(deg(0., 10.));
let got = SphericalPerimeter::default().length(&r);
let h = Haversine::EARTH;
let expected = h.distance(°(0., 0.), °(10., 0.))
+ h.distance(°(10., 0.), °(10., 10.))
+ h.distance(°(10., 10.), °(0., 10.))
+ h.distance(°(0., 10.), °(0., 0.));
assert!((got - expected).abs() < 1e-6);
}
}