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
use crate::bounding_volume::{BoundingSphere, BoundingVolume, HasBoundingVolume};
use crate::math::{Isometry, Point, Vector, DIM};
use crate::utils::IsometryOps;
use na::{self, RealField};
#[inline]
pub fn aabb<N, G: ?Sized>(g: &G, m: &Isometry<N>) -> AABB<N>
where
N: RealField,
G: HasBoundingVolume<N, AABB<N>>,
{
g.bounding_volume(m)
}
#[inline]
pub fn local_aabb<N, G: ?Sized>(g: &G) -> AABB<N>
where
N: RealField,
G: HasBoundingVolume<N, AABB<N>>,
{
g.local_bounding_volume()
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct AABB<N: RealField> {
pub mins: Point<N>,
pub maxs: Point<N>,
}
impl<N: RealField> AABB<N> {
#[inline]
pub fn new(mins: Point<N>, maxs: Point<N>) -> AABB<N> {
AABB { mins, maxs }
}
#[inline]
pub fn new_invalid() -> Self {
Self::new(
Vector::repeat(N::max_value()).into(),
Vector::repeat(-N::max_value()).into(),
)
}
#[inline]
pub fn from_half_extents(center: Point<N>, half_extents: Vector<N>) -> Self {
Self::new(center - half_extents, center + half_extents)
}
pub fn from_points<'a, I>(pts: I) -> Self
where
I: IntoIterator<Item = &'a Point<N>>,
{
super::aabb_utils::local_point_cloud_aabb(pts)
}
#[inline]
#[deprecated(note = "use the `.mins` public field instead.")]
pub fn mins(&self) -> &Point<N> {
&self.mins
}
#[inline]
#[deprecated(note = "use the `.maxs` public field instead.")]
pub fn maxs(&self) -> &Point<N> {
&self.maxs
}
#[inline]
pub fn center(&self) -> Point<N> {
na::center(&self.mins, &self.maxs)
}
#[inline]
pub fn half_extents(&self) -> Vector<N> {
let half: N = na::convert(0.5);
(self.maxs - self.mins) * half
}
#[inline]
pub fn extents(&self) -> Vector<N> {
self.maxs - self.mins
}
pub fn take_point(&mut self, pt: Point<N>) {
self.mins = self.mins.coords.inf(&pt.coords).into();
self.maxs = self.maxs.coords.sup(&pt.coords).into();
}
#[inline]
pub fn transform_by(&self, m: &Isometry<N>) -> Self {
let ls_center = self.center();
let center = m * ls_center;
let ws_half_extents = m.absolute_transform_vector(&self.half_extents());
AABB::new(center + (-ws_half_extents), center + ws_half_extents)
}
#[inline]
pub fn bounding_sphere(&self) -> BoundingSphere<N> {
let center = self.center();
let rad = na::distance(&self.mins, &self.maxs);
BoundingSphere::new(center, rad)
}
#[inline]
pub fn contains_local_point(&self, point: &Point<N>) -> bool {
for i in 0..DIM {
if point[i] < self.mins[i] || point[i] > self.maxs[i] {
return false;
}
}
true
}
}
impl<N: RealField> BoundingVolume<N> for AABB<N> {
#[inline]
fn center(&self) -> Point<N> {
self.center()
}
#[inline]
fn intersects(&self, other: &AABB<N>) -> bool {
na::partial_le(&self.mins, &other.maxs) && na::partial_ge(&self.maxs, &other.mins)
}
#[inline]
fn contains(&self, other: &AABB<N>) -> bool {
na::partial_le(&self.mins, &other.mins) && na::partial_ge(&self.maxs, &other.maxs)
}
#[inline]
fn merge(&mut self, other: &AABB<N>) {
self.mins = self.mins.inf(&other.mins);
self.maxs = self.maxs.sup(&other.maxs);
}
#[inline]
fn merged(&self, other: &AABB<N>) -> AABB<N> {
AABB {
mins: self.mins.inf(&other.mins),
maxs: self.maxs.sup(&other.maxs),
}
}
#[inline]
fn loosen(&mut self, amount: N) {
assert!(
amount >= na::zero(),
"The loosening margin must be positive."
);
self.mins = self.mins + Vector::repeat(-amount);
self.maxs = self.maxs + Vector::repeat(amount);
}
#[inline]
fn loosened(&self, amount: N) -> AABB<N> {
assert!(
amount >= na::zero(),
"The loosening margin must be positive."
);
AABB {
mins: self.mins + Vector::repeat(-amount),
maxs: self.maxs + Vector::repeat(amount),
}
}
#[inline]
fn tighten(&mut self, amount: N) {
assert!(
amount >= na::zero(),
"The tightening margin must be positive."
);
self.mins = self.mins + Vector::repeat(amount);
self.maxs = self.maxs + Vector::repeat(-amount);
assert!(
na::partial_le(&self.mins, &self.maxs),
"The tightening margin is to large."
);
}
#[inline]
fn tightened(&self, amount: N) -> AABB<N> {
assert!(
amount >= na::zero(),
"The tightening margin must be positive."
);
AABB::new(
self.mins + Vector::repeat(amount),
self.maxs + Vector::repeat(-amount),
)
}
}