d3_geo_rs/path/
bounds.rs

1use geo::CoordFloat;
2use geo::Coord;
3
4use crate::stream::Stream;
5
6use super::Result;
7
8/// Stream endpoint: Compute the bounding box.
9#[derive(Clone, Debug, PartialEq)]
10pub struct Bounds<T>
11where
12    T: CoordFloat,
13{
14    p0: Coord<T>,
15    p1: Coord<T>,
16}
17
18impl<T> Default for Bounds<T>
19where
20    T: CoordFloat,
21{
22    #[inline]
23    fn default() -> Self {
24        Self {
25            p0: Coord {
26                x: T::infinity(),
27                y: T::infinity(),
28            },
29            p1: Coord {
30                x: T::neg_infinity(),
31                y: T::neg_infinity(),
32            },
33        }
34    }
35}
36
37impl<T> Result for Bounds<T>
38where
39    T: CoordFloat,
40{
41    type Out = [Coord<T>; 2];
42
43    /// Return the result, reseting the Bounds.
44    fn result(&mut self) -> Self::Out {
45        let bounds = [self.p0, self.p1];
46        *self = Self::default();
47        bounds
48    }
49}
50
51impl<T> Stream for Bounds<T>
52where
53    T: CoordFloat,
54{
55    type EP = Self;
56    type T = T;
57
58    #[inline]
59    fn endpoint(&mut self) -> &mut Self {
60        self
61    }
62
63    #[inline]
64    fn point(&mut self, p: &Coord<T>, _m: Option<u8>) {
65        if p.x < self.p0.x {
66            self.p0.x = p.x;
67        }
68        if p.x > self.p1.x {
69            self.p1.x = p.x;
70        }
71        if p.y < self.p0.y {
72            self.p0.y = p.y;
73        }
74        if p.y > self.p1.y {
75            self.p1.y = p.y;
76        }
77    }
78}