1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use serde::{Deserialize, Serialize};

use aabb_quadtree::geom::{Point, Rect};

use crate::{Distance, LonLat, Polygon, Pt2D, Ring};

/// Represents a rectangular boundary of `Pt2D` points.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Bounds {
    pub min_x: f64,
    pub min_y: f64,
    pub max_x: f64,
    pub max_y: f64,
}

impl Bounds {
    /// A boundary including no points.
    pub fn new() -> Bounds {
        Bounds {
            min_x: f64::MAX,
            min_y: f64::MAX,
            max_x: f64::MIN,
            max_y: f64::MIN,
        }
    }

    pub fn zero() -> Self {
        Bounds {
            min_x: 0.0,
            min_y: 0.0,
            max_x: 0.0,
            max_y: 0.0,
        }
    }

    /// Create a boundary covering some points.
    pub fn from(pts: &[Pt2D]) -> Bounds {
        let mut b = Bounds::new();
        for pt in pts {
            b.update(*pt);
        }
        b
    }

    /// Update the boundary to include this point.
    pub fn update(&mut self, pt: Pt2D) {
        self.min_x = self.min_x.min(pt.x());
        self.max_x = self.max_x.max(pt.x());
        self.min_y = self.min_y.min(pt.y());
        self.max_y = self.max_y.max(pt.y());
    }

    /// Unions two boundaries.
    pub fn union(&mut self, other: Bounds) {
        self.update(Pt2D::new(other.min_x, other.min_y));
        self.update(Pt2D::new(other.max_x, other.max_y));
    }

    /// Expand the existing boundary by some distance evenly on all sides.
    pub fn add_buffer(&mut self, sides: Distance) {
        self.min_x -= sides.inner_meters();
        self.max_x += sides.inner_meters();
        self.min_y -= sides.inner_meters();
        self.max_y += sides.inner_meters();
    }

    /// Transform the boundary by scaling its corners.
    pub fn scale(mut self, factor: f64) -> Self {
        self.min_x *= factor;
        self.min_y *= factor;
        self.max_x *= factor;
        self.max_y *= factor;
        self
    }

    /// True if the point is within the boundary.
    pub fn contains(&self, pt: Pt2D) -> bool {
        pt.x() >= self.min_x && pt.x() <= self.max_x && pt.y() >= self.min_y && pt.y() <= self.max_y
    }

    /// Converts the boundary to the format used by `aabb_quadtree`.
    pub fn as_bbox(&self) -> Rect {
        Rect {
            top_left: Point {
                x: self.min_x as f32,
                y: self.min_y as f32,
            },
            bottom_right: Point {
                x: self.max_x as f32,
                y: self.max_y as f32,
            },
        }
    }

    /// Creates a rectangle covering this boundary.
    pub fn get_rectangle(&self) -> Polygon {
        Ring::must_new(vec![
            Pt2D::new(self.min_x, self.min_y),
            Pt2D::new(self.max_x, self.min_y),
            Pt2D::new(self.max_x, self.max_y),
            Pt2D::new(self.min_x, self.max_y),
            Pt2D::new(self.min_x, self.min_y),
        ])
        .into_polygon()
    }

    /// The width of this boundary.
    // TODO Really should be Distance
    pub fn width(&self) -> f64 {
        self.max_x - self.min_x
    }

    /// The height of this boundary.
    pub fn height(&self) -> f64 {
        self.max_y - self.min_y
    }

    /// The center point of this boundary.
    pub fn center(&self) -> Pt2D {
        Pt2D::new(
            self.min_x + self.width() / 2.0,
            self.min_y + self.height() / 2.0,
        )
    }
}

/// Represents a rectangular boundary of `LonLat` points. After building one of these, `LonLat`s
/// can be transformed into `Pt2D`s, treating the top-left of the boundary as (0, 0), and growing
/// to the right and down (screen-drawing order, not Cartesian) in meters.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GPSBounds {
    pub min_lon: f64,
    pub min_lat: f64,
    pub max_lon: f64,
    pub max_lat: f64,
}

impl GPSBounds {
    /// A boundary including no points.
    pub fn new() -> GPSBounds {
        GPSBounds {
            min_lon: f64::MAX,
            min_lat: f64::MAX,
            max_lon: f64::MIN,
            max_lat: f64::MIN,
        }
    }

    /// Create a boundary covering some points.
    pub fn from(pts: Vec<LonLat>) -> GPSBounds {
        let mut b = GPSBounds::new();
        for pt in pts {
            b.update(pt);
        }
        b
    }

    /// Update the boundary to include this point.
    pub fn update(&mut self, pt: LonLat) {
        self.min_lon = self.min_lon.min(pt.x());
        self.max_lon = self.max_lon.max(pt.x());
        self.min_lat = self.min_lat.min(pt.y());
        self.max_lat = self.max_lat.max(pt.y());
    }

    /// True if the point is within the boundary.
    pub fn contains(&self, pt: LonLat) -> bool {
        pt.x() >= self.min_lon
            && pt.x() <= self.max_lon
            && pt.y() >= self.min_lat
            && pt.y() <= self.max_lat
    }

    /// The bottom-right corner of the boundary, in map-space.
    // TODO cache this
    pub fn get_max_world_pt(&self) -> Pt2D {
        let width = LonLat::new(self.min_lon, self.min_lat)
            .gps_dist(LonLat::new(self.max_lon, self.min_lat));
        let height = LonLat::new(self.min_lon, self.min_lat)
            .gps_dist(LonLat::new(self.min_lon, self.max_lat));
        Pt2D::new(width.inner_meters(), height.inner_meters())
    }

    /// Converts the boundary to map-space.
    pub fn to_bounds(&self) -> Bounds {
        let mut b = Bounds::new();
        b.update(Pt2D::new(0.0, 0.0));
        b.update(self.get_max_world_pt());
        b
    }

    /// Convert all points to map-space, failing if any points are outside this boundary.
    pub fn try_convert(&self, pts: &[LonLat]) -> Option<Vec<Pt2D>> {
        let mut result = Vec::new();
        for gps in pts {
            if !self.contains(*gps) {
                return None;
            }
            result.push(gps.to_pt(self));
        }
        Some(result)
    }

    /// Convert all points to map-space. The points may be outside this boundary.
    pub fn convert(&self, pts: &[LonLat]) -> Vec<Pt2D> {
        pts.iter().map(|gps| gps.to_pt(self)).collect()
    }

    /// Convert map-space points back to `LonLat`s. This is only valid if the `GPSBounds` used
    /// is the same as the one used to originally produce the `Pt2D`s.
    pub fn convert_back(&self, pts: &[Pt2D]) -> Vec<LonLat> {
        pts.iter().map(|pt| pt.to_gps(self)).collect()
    }

    /// Returns points in order covering this boundary.
    pub fn get_rectangle(&self) -> Vec<LonLat> {
        vec![
            LonLat::new(self.min_lon, self.min_lat),
            LonLat::new(self.max_lon, self.min_lat),
            LonLat::new(self.max_lon, self.max_lat),
            LonLat::new(self.min_lon, self.max_lat),
            LonLat::new(self.min_lon, self.min_lat),
        ]
    }
}