Skip to main content

geometry_algorithm/
equals.rs

1//! `equals(&a, &b)` — see
2//! `boost/geometry/algorithms/equals.hpp`.
3//!
4//! Cartesian-only in v1. The per-pair strategy is selected by the
5//! tag-keyed [`geometry_strategy::EqualsPairStrategy`] picker on
6//! `(A::Kind, B::Kind)`, so any concept-adapted foreign type resolves
7//! through the same path as the equivalent `geometry-model` value.
8
9use geometry_strategy::{EqualsPairStrategy, EqualsStrategy};
10use geometry_trait::Geometry;
11
12/// `true` iff `a` and `b` describe the same point set.
13///
14/// Mirrors `boost::geometry::equals(a, b)` from
15/// `boost/geometry/algorithms/equals.hpp`. Polygon equality is
16/// up-to-rotation and traversal direction; vertex-order normalisation
17/// happens inside the strategy kernel.
18#[inline]
19#[must_use]
20pub fn equals<A, B>(a: &A, b: &B) -> bool
21where
22    A: Geometry,
23    B: Geometry,
24    A::Kind: EqualsPairStrategy<B::Kind>,
25    <A::Kind as EqualsPairStrategy<B::Kind>>::S: EqualsStrategy<A, B>,
26{
27    <<A::Kind as EqualsPairStrategy<B::Kind>>::S as Default>::default().equals(a, b)
28}
29
30#[cfg(test)]
31mod tests {
32    use super::equals;
33    use geometry_cs::Cartesian;
34    use geometry_model::{Point2D, Polygon, polygon};
35
36    type P = Point2D<f64, Cartesian>;
37
38    fn pt(x: f64, y: f64) -> P {
39        Point2D::new(x, y)
40    }
41
42    #[test]
43    fn equals_same_point() {
44        assert!(equals(&pt(1.0, 2.0), &pt(1.0, 2.0)));
45        assert!(!equals(&pt(1.0, 2.0), &pt(1.0, 2.1)));
46    }
47
48    #[test]
49    fn equals_polygon_rotated_and_reversed() {
50        let a: Polygon<P> = polygon![[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0), (0.0, 0.0)]];
51        let b: Polygon<P> = polygon![[(4.0, 4.0), (0.0, 4.0), (0.0, 0.0), (4.0, 0.0), (4.0, 4.0)]];
52        assert!(equals(&a, &b));
53    }
54}