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
use std::fmt;
use std::fmt::Debug;
use nalgebra::allocator::Allocator;
use nalgebra::{DefaultAllocator, DimName, VectorN, U2, U3};
use crate::Real;
#[derive(Clone, PartialEq)]
pub struct AxisAlignedBoundingBox<R: Real, D: DimName>
where
DefaultAllocator: Allocator<R, D>,
{
min: VectorN<R, D>,
max: VectorN<R, D>,
}
pub type AxisAlignedBoundingBox2d<R> = AxisAlignedBoundingBox<R, U2>;
pub type AxisAlignedBoundingBox3d<R> = AxisAlignedBoundingBox<R, U3>;
impl<R, D> AxisAlignedBoundingBox<R, D>
where
R: Real,
D: DimName,
DefaultAllocator: Allocator<R, D>,
{
#[inline(always)]
pub fn zeros() -> Self {
Self {
min: VectorN::zeros(),
max: VectorN::zeros(),
}
}
#[inline(always)]
pub fn new(min: VectorN<R, D>, max: VectorN<R, D>) -> Self {
Self { min, max }
}
#[inline(always)]
pub fn from_point(point: &VectorN<R, D>) -> Self {
Self {
min: point.clone(),
max: point.clone(),
}
}
pub fn from_points(points: &[VectorN<R, D>]) -> Self {
let mut point_iter = points.iter();
if let Some(first_point) = point_iter.next() {
let mut aabb = Self::from_point(first_point);
while let Some(next_point) = point_iter.next() {
aabb.join_with_point(next_point)
}
aabb
} else {
Self::zeros()
}
}
pub fn try_convert<T>(&self) -> Option<AxisAlignedBoundingBox<T, D>>
where
T: Real,
DefaultAllocator: Allocator<T, D>,
{
Some(AxisAlignedBoundingBox::new(
T::try_convert_vec_from(&self.min)?,
T::try_convert_vec_from(&self.max)?,
))
}
#[inline(always)]
pub fn min(&self) -> &VectorN<R, D> {
&self.min
}
#[inline(always)]
pub fn max(&self) -> &VectorN<R, D> {
&self.max
}
pub fn is_consistent(&self) -> bool {
for i in 0..D::dim() {
if !(self.min[i] <= self.max[i]) {
return false;
}
}
return true;
}
pub fn is_degenerate(&self) -> bool {
for i in 0..D::dim() {
if self.min[i] == self.max[i] {
return true;
}
}
return false;
}
#[inline(always)]
pub fn extents(&self) -> VectorN<R, D> {
&self.max - &self.min
}
#[inline(always)]
pub fn min_extent(&self) -> R {
let extents = self.extents();
extents[extents.imin()]
}
#[inline(always)]
pub fn max_extent(&self) -> R {
let extents = self.extents();
extents[extents.imax()]
}
pub fn centroid(&self) -> VectorN<R, D> {
&self.min + (self.extents() / (R::one() + R::one()))
}
pub fn contains_point(&self, point: &VectorN<R, D>) -> bool {
for i in 0..D::dim() {
if point[i] <= self.min[i] || point[i] >= self.max[i] {
return false;
}
}
return true;
}
pub fn translate(&mut self, vector: &VectorN<R, D>) {
self.min += vector;
self.max += vector;
}
pub fn center_at_origin(&mut self) {
self.translate(&(self.centroid() * R::one().neg()));
}
pub fn scale_uniformly(&mut self, scaling: R) {
let center = self.centroid();
self.translate(&(¢er * R::one().neg()));
self.min *= scaling;
self.max *= scaling;
self.translate(¢er);
}
pub fn join(&mut self, other: &Self) {
for i in 0..D::dim() {
self.min[i] = self.min[i].min(other.min[i]);
self.max[i] = self.max[i].max(other.max[i]);
}
}
pub fn join_with_point(&mut self, point: &VectorN<R, D>) {
for i in 0..D::dim() {
self.min[i] = self.min[i].min(point[i]);
self.max[i] = self.max[i].max(point[i]);
}
}
pub fn grow_uniformly(&mut self, margin: R) {
self.min = &self.min - &VectorN::repeat(margin);
self.max = &self.max + &VectorN::repeat(margin);
}
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(
VectorN::repeat(half_max_extent.neg()),
VectorN::repeat(half_max_extent),
);
cube.translate(¢er);
cube
}
}
impl<R, D> Debug for AxisAlignedBoundingBox<R, D>
where
R: Real,
D: DimName,
DefaultAllocator: Allocator<R, D>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AxisAlignedBoundingBox {{ min: [{:.7}, {:.7}, {:.7}], max: [{:.7}, {:.7}, {:.7}] }}",
self.min[0], self.min[1], self.min[2], self.max[0], self.max[1], self.max[2]
)
}
}