Skip to main content

geometry_algorithm/
distance.rs

1//! User-facing `distance` / `distance_with` / `comparable_distance`
2//! entry points.
3//!
4//! Mirrors the three free-function overloads Boost.Geometry users
5//! reach for:
6//!
7//! * `boost::geometry::distance(g1, g2)` — strategy-less, picks the
8//!   default strategy for the coordinate-system pair. See
9//!   `boost/geometry/algorithms/distance.hpp` and the dispatch
10//!   plumbing in
11//!   `boost/geometry/algorithms/detail/distance/interface.hpp`.
12//! * `boost::geometry::distance(g1, g2, strategy)` — explicit-strategy
13//!   companion, same file.
14//! * `boost::geometry::comparable_distance(g1, g2)` — the
15//!   "skip the sqrt" form, from
16//!   `boost/geometry/algorithms/comparable_distance.hpp`.
17//!
18//! The strategy trait, default-strategy projection, and
19//! reverse-dispatch wrapper all live in `geometry-strategy::distance`
20//! (T21/T22). T23 is the thin layer that turns them into ordinary
21//! Rust functions.
22
23use geometry_cs::CoordinateSystem;
24use geometry_strategy::distance::{DefaultDistance, DefaultDistanceStrategy, DistanceStrategy};
25use geometry_trait::{Geometry, Point};
26
27/// Shorthand for the CS family of `G`'s point type. Used purely to
28/// keep the `where` clauses on the three free functions readable;
29/// matches the family projection spelled out long-form in the
30/// definition of
31/// [`geometry_strategy::distance::DefaultDistanceStrategy`].
32type Family<G> = <<<G as Geometry>::Point as Point>::Cs as CoordinateSystem>::Family;
33
34/// Distance between two geometries using the default strategy for
35/// their coordinate-system pair.
36///
37/// Mirrors the no-strategy overload of `boost::geometry::distance(g1, g2)`
38/// from `boost/geometry/algorithms/distance.hpp` and the dispatch
39/// in `boost/geometry/algorithms/detail/distance/interface.hpp`. The
40/// default strategy is resolved via
41/// [`DefaultDistanceStrategy`], which is the Rust analogue of Boost's
42/// `boost::geometry::strategy::distance::services::default_strategy<…>::type`.
43#[inline]
44pub fn distance<A, B>(
45    a: &A,
46    b: &B,
47) -> <DefaultDistanceStrategy<A, B> as DistanceStrategy<A, B>>::Out
48where
49    A: Geometry,
50    B: Geometry,
51    Family<A>: DefaultDistance<Family<B>>,
52    DefaultDistanceStrategy<A, B>: DistanceStrategy<A, B> + Default,
53{
54    DefaultDistanceStrategy::<A, B>::default().distance(a, b)
55}
56
57/// Distance between two geometries using an explicit strategy.
58///
59/// Mirrors the with-strategy overload
60/// `boost::geometry::distance(g1, g2, strategy)` from
61/// `boost/geometry/algorithms/distance.hpp` and the dispatch
62/// in `boost/geometry/algorithms/detail/distance/interface.hpp`.
63///
64/// The C++ overload is spelled the same as the strategy-less one and
65/// disambiguated by SFINAE on the strategy concept; on the Rust side
66/// the two overloads become two distinct names ([`distance`] and
67/// `distance_with`) so the strategy argument never clashes with the
68/// strategy-less form during type inference.
69///
70/// Taking the strategy by value (rather than by reference, as Boost
71/// does with `Strategy const&` at
72/// `boost/geometry/algorithms/detail/distance/interface.hpp:68`)
73/// keeps the call site free of an `&` everyone would forget. Concrete
74/// strategies are zero-sized or pointer-sized configuration objects
75/// (`Pythagoras`, `Haversine`, `Andoyer { spheroid }`), so passing
76/// them by value monomorphises into nothing.
77#[inline]
78#[allow(
79    clippy::needless_pass_by_value,
80    reason = "Strategies are ZST/small Copy configuration objects; by-value matches the user-facing call shape."
81)]
82pub fn distance_with<A, B, S>(a: &A, b: &B, strategy: S) -> S::Out
83where
84    A: Geometry,
85    B: Geometry,
86    S: DistanceStrategy<A, B>,
87{
88    strategy.distance(a, b)
89}
90
91/// Comparable distance — equivalent ordering, cheaper math.
92///
93/// Mirrors `boost::geometry::comparable_distance(g1, g2)` from
94/// `boost/geometry/algorithms/comparable_distance.hpp`. The result
95/// preserves the ordering of [`distance`] but skips work the ordering
96/// does not need (e.g. the final `sqrt` for Pythagoras). The
97/// comparable companion of the default strategy is selected via
98/// [`DistanceStrategy::Comparable`], the Rust analogue of Boost's
99/// `boost::geometry::strategy::distance::services::comparable_type<…>::type`.
100#[inline]
101pub fn comparable_distance<A, B>(
102    a: &A,
103    b: &B,
104) -> <<DefaultDistanceStrategy<A, B> as DistanceStrategy<A, B>>::Comparable as DistanceStrategy<
105    A,
106    B,
107>>::Out
108where
109    A: Geometry,
110    B: Geometry,
111    Family<A>: DefaultDistance<Family<B>>,
112    DefaultDistanceStrategy<A, B>: DistanceStrategy<A, B> + Default,
113{
114    DefaultDistanceStrategy::<A, B>::default()
115        .comparable()
116        .distance(a, b)
117}
118
119#[cfg(test)]
120mod tests {
121    //! Canonical 3-4-5 triangle, exercised through every entry point.
122    //! Reference value matches `geometry/test/strategies/pythagoras.cpp`
123    //! (lines 50-66) and `doc/quickstart.qbk` (Cartesian distance
124    //! example).
125
126    use super::{comparable_distance, distance, distance_with};
127    use geometry_cs::Cartesian;
128    use geometry_model::Point2D;
129    use geometry_strategy::cartesian::Pythagoras;
130
131    // 3² + 4² = 25 exactly and √25 = 5 exactly in IEEE-754; `assert_eq!`
132    // is the spec's chosen shape (T23 spec, `Tests` block). The
133    // pedantic `float_cmp` warning does not apply to integer-valued
134    // floats from finite arithmetic on small integers.
135    #[allow(clippy::float_cmp, reason = "3-4-5 is exact in IEEE-754 f64.")]
136    #[test]
137    fn cartesian_quickstart_3_4_5() {
138        let a = Point2D::<f64, Cartesian>::new(0.0, 0.0);
139        let b = Point2D::<f64, Cartesian>::new(3.0, 4.0);
140        assert_eq!(distance(&a, &b), 5.0);
141        assert_eq!(distance_with(&a, &b, Pythagoras), 5.0);
142        assert_eq!(comparable_distance(&a, &b), 25.0);
143    }
144}