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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use super::*;
/// 2d Axis aligned bounding box.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Aabb2<T> {
/// Minimum coordinates
pub min: vec2<T>,
/// Maximum coordinates
pub max: vec2<T>,
}
impl<T: Copy> Aabb2<T> {
/// Get the bottom-left corner of the [Aabb2].
pub fn bottom_left(&self) -> vec2<T> {
self.min
}
/// Get the bottom-right corner of the [Aabb2].
pub fn bottom_right(&self) -> vec2<T> {
vec2(self.max.x, self.min.y)
}
/// Get the top-left corner of the [Aabb2].
pub fn top_left(&self) -> vec2<T> {
vec2(self.min.x, self.max.y)
}
/// Get the top-right corner of the [Aabb2].
pub fn top_right(&self) -> vec2<T> {
vec2(self.max.x, self.max.y)
}
/// Get an array of all four corner points
pub fn corners(&self) -> [vec2<T>; 4] {
[
self.bottom_left(),
self.bottom_right(),
self.top_right(),
self.top_left(),
]
}
}
impl<T: UNum> Aabb2<T> {
/// An [Aabb2] with both position and size equal to (0, 0).
pub const ZERO: Self = Aabb2 {
min: vec2::ZERO,
max: vec2::ZERO,
};
/// Construct an [Aabb2] from two opposite corners. The two corners can be given in any order.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let aabb = Aabb2::from_corners(vec2(-5.0, -5.0), vec2(5.0, 5.0));
/// let same = Aabb2::from_corners(vec2(5.0, -5.0), vec2(-5.0, 5.0));
/// assert_eq!(aabb, same);
/// ```
pub fn from_corners(p1: vec2<T>, p2: vec2<T>) -> Self {
let (min_x, max_x) = partial_min_max(p1.x, p2.x);
let (min_y, max_y) = partial_min_max(p1.y, p2.y);
Self {
min: vec2(min_x, min_y),
max: vec2(max_x, max_y),
}
}
/// Create an [Aabb2] at given position of size (0, 0).
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// assert_eq!(Aabb2::<f32>::ZERO, Aabb2::point(vec2::ZERO));
/// ```
pub fn point(point: vec2<T>) -> Self {
Self {
min: point,
max: point,
}
}
/// Extend boundaries of the [Aabb2] by a given value in each direction.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let aabb = Aabb2::point(vec2(5, 5)).extend_uniform(10);
/// assert_eq!(aabb, Aabb2::from_corners(vec2(-5, -5), vec2(15, 15)));
/// ```
pub fn extend_uniform(self, extend: T) -> Self {
Self {
min: self.min.map(|x| x - extend),
max: self.max.map(|x| x + extend),
}
}
/// Extend the boundaries equally right and left and equally up and down
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let aabb = Aabb2::ZERO.extend_symmetric(vec2(10, 5));
/// let same = Aabb2::from_corners(vec2(-10, -5), vec2(10, 5));
/// assert_eq!(aabb, same);
/// ```
pub fn extend_symmetric(self, extend: vec2<T>) -> Self {
Self {
min: self.min - extend,
max: self.max + extend,
}
}
/// Extend the boundaries to the right and up by the given values
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let aabb = Aabb2::point(vec2(-10.0, -5.0)).extend_positive(vec2(20.0, 10.0));
/// let same = Aabb2::ZERO.extend_symmetric(vec2(10.0, 5.0));
/// assert_eq!(aabb, same);
/// ```
pub fn extend_positive(self, extend: vec2<T>) -> Self {
Self {
min: self.min,
max: self.max + extend,
}
}
/// Extend the left edge of the [Aabb2] by a given value.
pub fn extend_left(self, extend: T) -> Self {
let mut res = self;
res.min.x -= extend;
res
}
/// Extend the right edge of the [Aabb2] by a given value.
pub fn extend_right(self, extend: T) -> Self {
let mut res = self;
res.max.x += extend;
res
}
/// Extend the top edge of the [Aabb2] by a given value.
pub fn extend_up(self, extend: T) -> Self {
let mut res = self;
res.max.y += extend;
res
}
/// Extend the bottom edge of the [Aabb2] by a given value.
pub fn extend_down(self, extend: T) -> Self {
let mut res = self;
res.min.y -= extend;
res
}
/// Ensure that the [Aabb2] has positive size
/// # Examples
/// ```
/// # use batbox_la::*;
/// let original = Aabb2::point(vec2(10.0, 5.0)).extend_positive(vec2(-20.0, -10.0));
/// let normalized = Aabb2::ZERO.extend_symmetric(vec2(10.0, 5.0));
/// assert_eq!(original.normalized(), normalized);
/// ```
pub fn normalized(self) -> Self {
Self::from_corners(self.bottom_left(), self.top_right())
}
/// Get the center position of the [Aabb2].
pub fn center(&self) -> vec2<T> {
let two: T = T::ONE + T::ONE;
vec2(
(self.min.x + self.max.x) / two,
(self.min.y + self.max.y) / two,
)
}
/// Map every value (coordinate) of the [Aabb2].
pub fn map<U: UNum, F: Fn(T) -> U>(self, f: F) -> Aabb2<U> {
Aabb2 {
min: self.min.map(&f),
max: self.max.map(&f),
}
}
/// Zip every value (coordinate) with another [Aabb2]
pub fn zip<U>(self, other: Aabb2<U>) -> Aabb2<(T, U)> {
Aabb2 {
min: self.min.zip(other.min),
max: self.max.zip(other.max),
}
}
/// Map bounds (min & max vectors)
pub fn map_bounds<U: UNum, F: Fn(vec2<T>) -> vec2<U>>(self, f: F) -> Aabb2<U> {
Aabb2 {
min: f(self.min),
max: f(self.max),
}
}
/// Returns the width of the [Aabb2].
pub fn width(&self) -> T {
self.max.x - self.min.x
}
/// Returns the height of the [Aabb2].
pub fn height(&self) -> T {
self.max.y - self.min.y
}
/// Return the size of the [Aabb2].
pub fn size(&self) -> vec2<T> {
vec2(self.width(), self.height())
}
/// Check if a point is inside the [Aabb2].
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let rect = Aabb2::from_corners(vec2(1, 2), vec2(3, 4));
/// assert!(rect.contains(vec2(2, 3)));
/// assert!(!rect.contains(vec2(5, 5)));
/// ```
pub fn contains(&self, point: vec2<T>) -> bool {
self.min.x <= point.x
&& point.x < self.max.x
&& self.min.y <= point.y
&& point.y < self.max.y
}
/// Checks whether two [Aabb2]'s intersect.
pub fn intersects(&self, other: &Self) -> bool {
self.max.x > other.min.x
&& self.max.y > other.min.y
&& self.min.x < other.max.x
&& self.min.y < other.max.y
}
/// Moves the [Aabb2] by a given vector.
pub fn translate(self, v: vec2<T>) -> Self {
Self {
min: self.min + v,
max: self.max + v,
}
}
/// Returns an iterator over points inside the [Aabb2].
pub fn points(self) -> impl Iterator<Item = vec2<T>>
where
Range<T>: Iterator<Item = T>,
{
(self.min.x..self.max.x)
.flat_map(move |x| (self.min.y..self.max.y).map(move |y| vec2(x, y)))
}
/// Returns the smallest possible [Aabb2] such that it contains all the points.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let aabb = Aabb2::points_bounding_box([
/// vec2(3, 0),
/// vec2(0, -2),
/// vec2(1, 4),
/// vec2(-1, -1),
/// vec2(0, 3),
/// ]).unwrap();
/// assert_eq!(aabb, Aabb2 { min: vec2(-1, -2), max: vec2(3, 4) });
/// ```
pub fn points_bounding_box(points: impl IntoIterator<Item = vec2<T>>) -> Option<Self> {
let mut points = points.into_iter();
let vec2(mut min_x, mut min_y) = points.next()?;
let mut max_x = min_x;
let mut max_y = min_y;
for vec2(x, y) in points {
// TODO: disallow partials?
min_x = partial_min(min_x, x);
min_y = partial_min(min_y, y);
max_x = partial_max(max_x, x);
max_y = partial_max(max_y, y);
}
Some(Aabb2 {
min: vec2(min_x, min_y),
max: vec2(max_x, max_y),
})
}
}