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
//! Axis-aligned bounding boxes
use crate::{Real, RealConvert, ThreadSafe};
use nalgebra::{SVector, Scalar};
use rayon::prelude::*;
#[cfg(feature = "serde-serialize")]
use serde_derive::{Deserialize, Serialize};
/// Type representing an axis aligned bounding box in arbitrary dimensions
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct AxisAlignedBoundingBox<R: Scalar, const D: usize> {
min: SVector<R, D>,
max: SVector<R, D>,
}
/// Convenience type alias for an AABB in two dimensions
pub type Aabb2d<R> = AxisAlignedBoundingBox<R, 2>;
/// Convenience type alias for an AABB in three dimensions
pub type Aabb3d<R> = AxisAlignedBoundingBox<R, 3>;
impl<R, const D: usize> AxisAlignedBoundingBox<R, D>
where
R: Real,
SVector<R, D>: ThreadSafe,
{
/// Constructs the smallest AABB fitting around all the given points, parallel version
pub fn par_from_points(points: &[SVector<R, D>]) -> Self {
if points.is_empty() {
Self::zeros()
} else if points.len() == 1 {
Self::from_point(points[0])
} else {
let initial_aabb = Self::from_point(points[0]);
points[1..]
.par_iter()
.fold(
|| initial_aabb.clone(),
|mut aabb, next_point| {
aabb.join_with_point(next_point);
aabb
},
)
.reduce(
|| initial_aabb.clone(),
|mut final_aabb, aabb| {
final_aabb.join(&aabb);
final_aabb
},
)
}
}
}
impl<R, const D: usize> AxisAlignedBoundingBox<R, D>
where
R: Real,
{
/// Constructs a degenerate AABB with min and max set to zero
#[inline(always)]
pub fn zeros() -> Self {
Self::from_point(SVector::zeros())
}
/// Constructs an AABB with the given min and max bounding points
#[inline(always)]
pub fn new(min: SVector<R, D>, max: SVector<R, D>) -> Self {
Self { min, max }
}
/// Constructs a degenerate AABB with zero extents centered at the given point
#[inline(always)]
pub fn from_point(point: SVector<R, D>) -> Self {
Self {
min: point,
max: point,
}
}
/// Constructs the smallest AABB fitting around all the given points
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
///
/// assert_eq!(
/// Aabb3d::<f64>::from_points(&[]),
/// Aabb3d::<f64>::zeros()
/// );
/// assert_eq!(
/// Aabb3d::<f64>::from_points(&[Vector3::new(1.0, 1.0, 1.0)]),
/// Aabb3d::<f64>::from_point(Vector3::new(1.0, 1.0, 1.0))
/// );
///
/// let aabb = Aabb3d::<f64>::from_points(&[
/// Vector3::new(1.0, 1.0, 1.0),
/// Vector3::new(0.5, 3.0, 5.0),
/// Vector3::new(-1.0, 1.0, 1.0)
/// ]);
/// assert_eq!(aabb.min(), &Vector3::new(-1.0, 1.0, 1.0));
/// assert_eq!(aabb.max(), &Vector3::new(1.0, 3.0, 5.0));
/// ```
pub fn from_points(points: &[SVector<R, D>]) -> Self {
let mut point_iter = points.iter();
if let Some(first_point) = point_iter.next().cloned() {
let mut aabb = Self::from_point(first_point);
for next_point in point_iter {
aabb.join_with_point(next_point)
}
aabb
} else {
Self::zeros()
}
}
/// Tries to convert the AABB from one real type to another real type, returns None if conversion fails
pub fn try_convert<T>(&self) -> Option<AxisAlignedBoundingBox<T, D>>
where
T: Real,
{
Some(AxisAlignedBoundingBox::new(
self.min.try_convert()?,
self.max.try_convert()?,
))
}
/// Returns the min coordinate of the bounding box
#[inline(always)]
pub fn min(&self) -> &SVector<R, D> {
&self.min
}
/// Returns the max coordinate of the bounding box
#[inline(always)]
pub fn max(&self) -> &SVector<R, D> {
&self.max
}
/// Returns whether the AABB is consistent, i.e. `aabb.min()[i] <= aabb.max()[i]` for all `i`
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
/// assert_eq!(
/// Aabb3d::<f64>::zeros().is_consistent(), true);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, -1.0, -1.0), Vector3::new(1.0, 1.0, 1.0)).is_consistent(), true);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 1.0, -1.0), Vector3::new(1.0, -1.0, 1.0)).is_consistent(), false);
/// ```
pub fn is_consistent(&self) -> bool {
self.min <= self.max
}
/// Returns whether the AABB is degenerate in any dimension, i.e. `aabb.min()[i] == aabb.max()[i]` for any `i`
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
/// assert_eq!(Aabb3d::<f64>::zeros().is_degenerate(), true);
/// assert_eq!(Aabb3d::new(Vector3::new(1.0, 1.0, 1.0), Vector3::new(1.0, 1.0, 1.0)).is_degenerate(), true);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 0.0, -3.0), Vector3::new(2.0, 2.0, 4.0)).is_degenerate(), false);
/// ```
pub fn is_degenerate(&self) -> bool {
self.min == self.max
}
/// Returns the extents of the bounding box (vector connecting min and max point of the box)
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
/// assert_eq!(Aabb3d::<f64>::zeros().extents(), Vector3::new(0.0, 0.0, 0.0));
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, -1.0, -1.0), Vector3::new(1.0, 1.0, 1.0)).extents(), Vector3::new(2.0, 2.0, 2.0));
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 0.0, -3.0), Vector3::new(2.0, 2.0, 4.0)).extents(), Vector3::new(3.0, 2.0, 7.0));
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 5.0, -3.0), Vector3::new(2.0, 15.0, 4.0)).extents(), Vector3::new(3.0, 10.0, 7.0));
/// ```
#[inline(always)]
pub fn extents(&self) -> SVector<R, D> {
self.max - self.min
}
/// Returns the smallest scalar extent of the AABB over all of its dimensions
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
/// assert_eq!(Aabb3d::<f64>::zeros().min_extent(), 0.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, -2.0, -3.0), Vector3::new(2.0, 3.0, 4.0)).min_extent(), 3.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 0.0, -3.0), Vector3::new(2.0, 2.0, 4.0)).min_extent(), 2.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 0.0, 1.0), Vector3::new(2.0, 1.0, 1.0)).min_extent(), 0.0);
/// ```
#[inline(always)]
pub fn min_extent(&self) -> R {
let extents = self.extents();
// Use imin indirectly, because min is broken in nalgebra
extents[extents.imin()]
}
/// Returns the largest scalar extent of the AABB over all of its dimensions
/// ```
/// use crate::splashsurf_lib::Aabb3d;
/// use nalgebra::Vector3;
/// assert_eq!(Aabb3d::<f64>::zeros().max_extent(), 0.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-10.0, 0.0, -3.0), Vector3::new(2.0, 2.0, 4.0)).max_extent(), 12.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, -2.0, -3.0), Vector3::new(2.0, 3.0, 4.0)).max_extent(), 7.0);
/// assert_eq!(Aabb3d::new(Vector3::new(-1.0, 5.0, -3.0), Vector3::new(2.0, 15.0, 4.0)).max_extent(), 10.0);
/// ```
#[inline(always)]
pub fn max_extent(&self) -> R {
let extents = self.extents();
// Use imax indirectly, because max is broken in nalgebra
extents[extents.imax()]
}
/// Returns the geometric centroid of the AABB (mean of the corner points)
pub fn centroid(&self) -> SVector<R, D> {
self.min + (self.extents() / (R::one() + R::one()))
}
/// Checks if the given AABB is inside the AABB, the AABB is considered to be half-open to its max coordinate
pub fn contains_aabb(&self, other: &Self) -> bool {
self.contains_point(&other.min) || self.contains_point(&other.max)
}
/// Checks if the given point is inside the AABB, the AABB is considered to be half-open to its max coordinate
pub fn contains_point(&self, point: &SVector<R, D>) -> bool {
point >= &self.min && point < &self.max
}
/// Translates the AABB by the given vector
pub fn translate(&mut self, vector: &SVector<R, D>) {
self.min += vector;
self.max += vector;
}
/// Translates the AABB to center it at the coordinate origin (moves the centroid to the coordinate origin)
pub fn center_at_origin(&mut self) {
self.translate(&(self.centroid() * R::one().neg()));
}
/// Multiplies a uniform, local scaling to the AABB (i.e. multiplying its extents as if it was centered at the origin)
pub fn scale_uniformly(&mut self, scaling: R) {
let center = self.centroid();
self.translate(&(center * R::one().neg()));
self.min *= scaling;
self.max *= scaling;
self.translate(¢er);
}
/// Enlarges this AABB to the smallest AABB enclosing both itself and another AABB
pub fn join(&mut self, other: &Self) {
self.min = self.min.inf(&other.min);
self.max = self.max.sup(&other.max);
}
/// Enlarges this AABB to the smallest AABB enclosing both itself and another point
pub fn join_with_point(&mut self, point: &SVector<R, D>) {
self.min = self.min.inf(point);
self.max = self.max.sup(point);
}
/// Grows this AABB uniformly in all directions by the given scalar margin (i.e. adding the margin to min/max extents)
pub fn grow_uniformly(&mut self, margin: R) {
self.min -= SVector::repeat(margin);
self.max += SVector::repeat(margin);
}
/// Returns the smallest cubical AABB with the same center that encloses this AABB
pub fn enclosing_cube(&self) -> Self {
let center = self.centroid();
let half_max_extent = self.max_extent() / (R::one() + R::one());
let mut cube = Self::new(
SVector::repeat(half_max_extent.neg()),
SVector::repeat(half_max_extent),
);
cube.translate(¢er);
cube
}
}
#[test]
fn test_aabb_contains_point() {
use crate::nalgebra::Vector3;
let aabb = Aabb3d::<f64>::new(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0));
assert!(aabb.contains_point(&Vector3::new(0.5, 0.5, 0.5)));
assert!(aabb.contains_point(&Vector3::new(0.0, 0.5, 0.5)));
assert!(aabb.contains_point(&Vector3::new(0.5, 0.0, 0.5)));
assert!(aabb.contains_point(&Vector3::new(0.5, 0.5, 0.0)));
assert!(aabb.contains_point(&Vector3::new(0.0, 0.0, 0.0)));
assert!(!aabb.contains_point(&Vector3::new(1.0, 0.0, 0.0)));
assert!(!aabb.contains_point(&Vector3::new(0.0, 1.0, 0.0)));
assert!(!aabb.contains_point(&Vector3::new(0.0, 0.0, 1.0)));
assert!(!aabb.contains_point(&Vector3::new(1.0, 1.0, 1.0)));
}