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 (see
8//! `specs/open-tag-dispatch/`).
9
10use geometry_strategy::{EqualsPairStrategy, EqualsStrategy};
11use geometry_trait::Geometry;
12
13/// `true` iff `a` and `b` describe the same point set.
14///
15/// Mirrors `boost::geometry::equals(a, b)` from
16/// `boost/geometry/algorithms/equals.hpp`. Polygon equality is
17/// up-to-rotation and traversal direction; vertex-order normalisation
18/// happens inside the strategy kernel.
19#[inline]
20#[must_use]
21pub fn equals<A, B>(a: &A, b: &B) -> bool
22where
23    A: Geometry,
24    B: Geometry,
25    A::Kind: EqualsPairStrategy<B::Kind>,
26    <A::Kind as EqualsPairStrategy<B::Kind>>::S: EqualsStrategy<A, B>,
27{
28    <<A::Kind as EqualsPairStrategy<B::Kind>>::S as Default>::default().equals(a, b)
29}
30
31#[cfg(test)]
32mod tests {
33    use super::equals;
34    use geometry_cs::Cartesian;
35    use geometry_model::{Point2D, Polygon, polygon};
36
37    type P = Point2D<f64, Cartesian>;
38
39    fn pt(x: f64, y: f64) -> P {
40        Point2D::new(x, y)
41    }
42
43    #[test]
44    fn equals_same_point() {
45        assert!(equals(&pt(1.0, 2.0), &pt(1.0, 2.0)));
46        assert!(!equals(&pt(1.0, 2.0), &pt(1.0, 2.1)));
47    }
48
49    #[test]
50    fn equals_polygon_rotated_and_reversed() {
51        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)]];
52        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)]];
53        assert!(equals(&a, &b));
54    }
55}