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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use crate::bounding_volume::Aabb;
use crate::math::{Isometry, Point, Real, SimdBool, SimdReal, Vector, DIM, SIMD_WIDTH};
use crate::query::SimdRay;
use crate::utils::{self, IsometryOps};
use num::{One, Zero};
use simba::simd::{SimdPartialOrd, SimdValue};
#[derive(Debug, Copy, Clone)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
pub struct SimdAabb {
pub mins: Point<SimdReal>,
pub maxs: Point<SimdReal>,
}
#[cfg(feature = "serde-serialize")]
impl serde::Serialize for SimdAabb {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
let mins: Point<[Real; SIMD_WIDTH]> = Point::from(
self.mins
.coords
.map(|e| array![|ii| e.extract(ii); SIMD_WIDTH]),
);
let maxs: Point<[Real; SIMD_WIDTH]> = Point::from(
self.maxs
.coords
.map(|e| array![|ii| e.extract(ii); SIMD_WIDTH]),
);
let mut simd_aabb = serializer.serialize_struct("simd_aabb", 2)?;
simd_aabb.serialize_field("mins", &mins)?;
simd_aabb.serialize_field("maxs", &maxs)?;
simd_aabb.end()
}
}
#[cfg(feature = "serde-serialize")]
impl<'de> serde::Deserialize<'de> for SimdAabb {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor {}
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = SimdAabb;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
formatter,
"two arrays containing at least {} floats",
SIMD_WIDTH * DIM * 2
)
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mins: Point<[Real; SIMD_WIDTH]> = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
let maxs: Point<[Real; SIMD_WIDTH]> = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
let mins = Point::from(mins.coords.map(|e| SimdReal::from(e)));
let maxs = Point::from(maxs.coords.map(|e| SimdReal::from(e)));
Ok(SimdAabb { mins, maxs })
}
}
deserializer.deserialize_struct("SimdAabb", &["mins", "maxs"], Visitor {})
}
}
impl SimdAabb {
pub fn new_invalid() -> Self {
Self::splat(Aabb::new_invalid())
}
pub fn splat(aabb: Aabb) -> Self {
Self {
mins: Point::splat(aabb.mins),
maxs: Point::splat(aabb.maxs),
}
}
pub fn center(&self) -> Point<SimdReal> {
na::center(&self.mins, &self.maxs)
}
pub fn half_extents(&self) -> Vector<SimdReal> {
(self.maxs - self.mins) * SimdReal::splat(0.5)
}
pub fn radius(&self) -> SimdReal {
(self.maxs - self.mins).norm()
}
pub fn transform_by(&self, transform: &Isometry<SimdReal>) -> Self {
let ls_center = self.center();
let center = transform * ls_center;
let ws_half_extents = transform.absolute_transform_vector(&self.half_extents());
Self {
mins: center + (-ws_half_extents),
maxs: center + ws_half_extents,
}
}
#[inline]
pub fn scaled(self, scale: &Vector<SimdReal>) -> Self {
let a = self.mins.coords.component_mul(&scale);
let b = self.maxs.coords.component_mul(&scale);
Self {
mins: a.inf(&b).into(),
maxs: a.sup(&b).into(),
}
}
pub fn loosen(&mut self, margin: SimdReal) {
self.mins -= Vector::repeat(margin);
self.maxs += Vector::repeat(margin);
}
pub fn dilate_by_factor(&mut self, factor: SimdReal) {
let is_valid = self.mins.x.simd_le(self.maxs.x);
let factor = factor.select(is_valid, SimdReal::zero());
let dilation = self.maxs * factor - self.mins * factor;
self.mins -= dilation;
self.maxs += dilation;
}
pub fn replace(&mut self, i: usize, aabb: Aabb) {
self.mins.replace(i, aabb.mins);
self.maxs.replace(i, aabb.maxs);
}
pub fn cast_local_ray(&self, ray: &SimdRay, max_toi: SimdReal) -> (SimdBool, SimdReal) {
let zero = SimdReal::zero();
let one = SimdReal::one();
let infinity = SimdReal::splat(Real::MAX);
let mut hit = SimdBool::splat(true);
let mut tmin = SimdReal::zero();
let mut tmax = max_toi;
for i in 0usize..DIM {
let is_not_zero = ray.dir[i].simd_ne(zero);
let is_zero_test =
ray.origin[i].simd_ge(self.mins[i]) & ray.origin[i].simd_le(self.maxs[i]);
let is_not_zero_test = {
let denom = one / ray.dir[i];
let mut inter_with_near_plane =
((self.mins[i] - ray.origin[i]) * denom).select(is_not_zero, -infinity);
let mut inter_with_far_plane =
((self.maxs[i] - ray.origin[i]) * denom).select(is_not_zero, infinity);
let gt = inter_with_near_plane.simd_gt(inter_with_far_plane);
utils::simd_swap(gt, &mut inter_with_near_plane, &mut inter_with_far_plane);
tmin = tmin.simd_max(inter_with_near_plane);
tmax = tmax.simd_min(inter_with_far_plane);
tmin.simd_le(tmax)
};
hit = hit & is_not_zero_test.select(is_not_zero, is_zero_test);
}
(hit, tmin)
}
pub fn distance_to_local_point(&self, point: &Point<SimdReal>) -> SimdReal {
let mins_point = self.mins - point;
let point_maxs = point - self.maxs;
let shift = mins_point.sup(&point_maxs).sup(&na::zero());
shift.norm()
}
pub fn distance_to_origin(&self) -> SimdReal {
self.mins
.coords
.sup(&-self.maxs.coords)
.sup(&Vector::zeros())
.norm()
}
pub fn contains_local_point(&self, point: &Point<SimdReal>) -> SimdBool {
#[cfg(feature = "dim2")]
return self.mins.x.simd_le(point.x)
& self.mins.y.simd_le(point.y)
& self.maxs.x.simd_ge(point.x)
& self.maxs.y.simd_ge(point.y);
#[cfg(feature = "dim3")]
return self.mins.x.simd_le(point.x)
& self.mins.y.simd_le(point.y)
& self.mins.z.simd_le(point.z)
& self.maxs.x.simd_ge(point.x)
& self.maxs.y.simd_ge(point.y)
& self.maxs.z.simd_ge(point.z);
}
#[cfg(feature = "dim2")]
pub fn contains(&self, other: &SimdAabb) -> SimdBool {
self.mins.x.simd_le(other.mins.x)
& self.mins.y.simd_le(other.mins.y)
& self.maxs.x.simd_ge(other.maxs.x)
& self.maxs.y.simd_ge(other.maxs.y)
}
#[cfg(feature = "dim3")]
pub fn contains(&self, other: &SimdAabb) -> SimdBool {
self.mins.x.simd_le(other.mins.x)
& self.mins.y.simd_le(other.mins.y)
& self.mins.z.simd_le(other.mins.z)
& self.maxs.x.simd_ge(other.maxs.x)
& self.maxs.y.simd_ge(other.maxs.y)
& self.maxs.z.simd_ge(other.maxs.z)
}
#[cfg(feature = "dim2")]
pub fn intersects(&self, other: &SimdAabb) -> SimdBool {
self.mins.x.simd_le(other.maxs.x)
& other.mins.x.simd_le(self.maxs.x)
& self.mins.y.simd_le(other.maxs.y)
& other.mins.y.simd_le(self.maxs.y)
}
#[cfg(feature = "dim3")]
pub fn intersects(&self, other: &SimdAabb) -> SimdBool {
self.mins.x.simd_le(other.maxs.x)
& other.mins.x.simd_le(self.maxs.x)
& self.mins.y.simd_le(other.maxs.y)
& other.mins.y.simd_le(self.maxs.y)
& self.mins.z.simd_le(other.maxs.z)
& other.mins.z.simd_le(self.maxs.z)
}
pub fn intersects_permutations(&self, other: &SimdAabb) -> [SimdBool; SIMD_WIDTH] {
let mut result = [SimdBool::splat(false); SIMD_WIDTH];
for ii in 0..SIMD_WIDTH {
let extracted = SimdAabb::splat(self.extract(ii));
result[ii] = extracted.intersects(other);
}
result
}
pub fn to_merged_aabb(&self) -> Aabb {
Aabb::new(
self.mins.coords.map(|e| e.simd_horizontal_min()).into(),
self.maxs.coords.map(|e| e.simd_horizontal_max()).into(),
)
}
pub fn extract(&self, lane: usize) -> Aabb {
Aabb::new(self.mins.extract(lane), self.maxs.extract(lane))
}
}
impl From<[Aabb; SIMD_WIDTH]> for SimdAabb {
fn from(aabbs: [Aabb; SIMD_WIDTH]) -> Self {
let mins = array![|ii| aabbs[ii].mins; SIMD_WIDTH];
let maxs = array![|ii| aabbs[ii].maxs; SIMD_WIDTH];
SimdAabb {
mins: Point::from(mins),
maxs: Point::from(maxs),
}
}
}