Skip to main content

geometry_algorithm/
clear.rs

1//! `clear(&mut g)` — empty the backing storage in place.
2//!
3//! Mirrors `boost::geometry::clear` from
4//! `boost/geometry/algorithms/clear.hpp`. Per-kind:
5//!
6//! * `Linestring`, `Ring`, `MultiPoint` → `Vec::clear` on the backing.
7//! * `Polygon` → clear the outer ring and drop every inner ring.
8//! * `MultiLinestring`, `MultiPolygon` → `Vec::clear` on the member
9//!   vector.
10//! * `Point`, `Box`, `Segment` → no impl (fixed-size storage — there
11//!   is nothing to clear).
12//!
13//! Boost ships the fixed-size case as a silent no-op fall-through
14//! (`clear.hpp`); the Rust port instead simply provides no [`Clear`]
15//! impl for `Point` / `Box` / `Segment`, so `clear(&mut point)` is a
16//! compile error — the type system says "call this on something that
17//! *has* clearable storage."
18
19use geometry_model::{Linestring, MultiLinestring, MultiPoint, MultiPolygon, Polygon, Ring};
20
21/// Empty the backing storage of `g` in place.
22///
23/// Mirrors `boost::geometry::clear(g)` from
24/// `boost/geometry/algorithms/clear.hpp`. Not defined for `Point`,
25/// `Box`, or `Segment` (fixed-size storage — see the module docs).
26pub fn clear<G: Clear>(g: &mut G) {
27    g.clear();
28}
29
30/// Per-kind clear dispatch. Implemented only for the variable-length
31/// kinds; fixed-size kinds (`Point`, `Box`, `Segment`) are
32/// intentionally absent so clearing them fails to compile.
33#[doc(hidden)]
34pub trait Clear {
35    fn clear(&mut self);
36}
37
38impl<P: geometry_trait::Point> Clear for Linestring<P> {
39    fn clear(&mut self) {
40        self.0.clear();
41    }
42}
43
44impl<P: geometry_trait::Point, const CW: bool, const CL: bool> Clear for Ring<P, CW, CL> {
45    fn clear(&mut self) {
46        self.0.clear();
47    }
48}
49
50impl<P: geometry_trait::Point, const CW: bool, const CL: bool> Clear for Polygon<P, CW, CL> {
51    fn clear(&mut self) {
52        self.outer.0.clear();
53        self.inners.clear();
54    }
55}
56
57impl<P: geometry_trait::Point> Clear for MultiPoint<P> {
58    fn clear(&mut self) {
59        self.0.clear();
60    }
61}
62
63impl<L: geometry_trait::Linestring> Clear for MultiLinestring<L> {
64    fn clear(&mut self) {
65        self.0.clear();
66    }
67}
68
69impl<Pg: geometry_trait::Polygon> Clear for MultiPolygon<Pg> {
70    fn clear(&mut self) {
71        self.0.clear();
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    //! Behaviour cross-checked against
78    //! [`crate::is_empty`]: a cleared geometry reports
79    //! empty, and `Polygon::clear` drops both the outer ring's points
80    //! and every inner ring.
81
82    use super::clear;
83    use crate::is_empty::is_empty;
84    use geometry_cs::Cartesian;
85    use geometry_model::{
86        Linestring, MultiLinestring, MultiPoint, MultiPolygon, Point2D, Polygon, Ring, linestring,
87        polygon,
88    };
89
90    type Pt = Point2D<f64, Cartesian>;
91
92    #[test]
93    fn clear_linestring() {
94        let mut ls: Linestring<Pt> = linestring![(0., 0.), (1., 1.), (2., 2.)];
95        clear(&mut ls);
96        assert!(is_empty(&ls));
97    }
98
99    #[test]
100    fn clear_polygon_drops_both_rings_and_holes() {
101        let mut pg: Polygon<Pt> = polygon![
102            [(0., 0.), (5., 0.), (5., 5.), (0., 0.)],
103            [(1., 1.), (2., 1.), (2., 2.), (1., 1.)],
104        ];
105        clear(&mut pg);
106        assert!(is_empty(&pg));
107        assert_eq!(pg.inners.len(), 0);
108    }
109
110    #[test]
111    fn clear_multi_point() {
112        let mut mp = MultiPoint(alloc::vec![Pt::new(0., 0.), Pt::new(1., 1.)]);
113        clear(&mut mp);
114        assert_eq!(mp.0.len(), 0);
115    }
116
117    #[test]
118    fn clear_ring() {
119        let mut r: Ring<Pt> = Ring::from_vec(alloc::vec![
120            Pt::new(0., 0.),
121            Pt::new(1., 0.),
122            Pt::new(1., 1.)
123        ]);
124        clear(&mut r);
125        assert!(is_empty(&r));
126    }
127
128    #[test]
129    fn clear_multi_linestring_drops_all_members() {
130        let mut mls: MultiLinestring<Linestring<Pt>> = MultiLinestring(alloc::vec![
131            linestring![(0., 0.), (1., 1.)],
132            linestring![(2., 2.), (3., 3.)],
133        ]);
134        clear(&mut mls);
135        assert_eq!(mls.0.len(), 0);
136        assert!(is_empty(&mls));
137    }
138
139    #[test]
140    fn clear_multi_polygon_drops_all_members() {
141        let member: Polygon<Pt> = polygon![[(0., 0.), (5., 0.), (5., 5.), (0., 0.)]];
142        let mut mpg: MultiPolygon<Polygon<Pt>> = MultiPolygon(alloc::vec![member.clone(), member]);
143        clear(&mut mpg);
144        assert_eq!(mpg.0.len(), 0);
145        assert!(is_empty(&mpg));
146    }
147}