geometry_algorithm/
is_empty.rs1use crate::num_points::{NumPointsStrategy, NumPointsStrategyForKind, num_points};
14use geometry_trait::Geometry;
15
16#[inline]
21#[must_use]
22pub fn is_empty<G>(g: &G) -> bool
23where
24 G: Geometry,
25 G::Kind: NumPointsStrategyForKind,
26 <G::Kind as NumPointsStrategyForKind>::S: NumPointsStrategy<G>,
27{
28 num_points(g) == 0
29}
30
31#[cfg(test)]
32mod tests {
33 use super::is_empty;
37 use geometry_cs::Cartesian;
38 use geometry_model::{Box, Linestring, MultiPoint, Point2D, Polygon, Segment, linestring};
39
40 type Pt = Point2D<f64, Cartesian>;
41
42 #[test]
44 fn point_is_never_empty() {
45 assert!(!is_empty(&Pt::new(0.0, 0.0)));
46 assert!(!is_empty(&Pt::new(1.0, 1.0)));
47 }
48
49 #[test]
51 fn segment_zero_length_is_not_empty() {
52 let s = Segment::new(Pt::new(0.0, 0.0), Pt::new(0.0, 0.0));
53 assert!(!is_empty(&s));
54 }
55
56 #[test]
58 fn box_is_never_empty() {
59 let b = Box::from_corners(Pt::new(0.0, 0.0), Pt::new(1.0, 1.0));
60 assert!(!is_empty(&b));
61 }
62
63 #[test]
65 fn empty_linestring_is_empty() {
66 let ls: Linestring<Pt> = Linestring::new();
67 assert!(is_empty(&ls));
68 }
69
70 #[test]
72 fn nonempty_linestring_is_not_empty() {
73 let ls: Linestring<Pt> = linestring![(0.0, 0.0), (1.0, 1.0)];
74 assert!(!is_empty(&ls));
75 }
76
77 #[test]
79 fn empty_multi_point_is_empty() {
80 let mp: MultiPoint<Pt> = MultiPoint(Vec::new());
81 assert!(is_empty(&mp));
82 }
83
84 #[test]
86 fn polygon_with_zero_point_outer_is_empty() {
87 let pg: Polygon<Pt> = Polygon::default();
88 assert!(is_empty(&pg));
89 }
90}