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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Port of `three.js/src/math/Box3.js`.
use crate::core::{BufferAttribute, ObjectRef};
use super::math_utils::{js_max, js_min};
use super::{Matrix4, Plane, Sphere, Triangle, Vector3};
/// Port of `three.js/src/math/Box3.js`'s `Box3` class: an axis-aligned
/// bounding box (AABB) in 3D space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Box3 {
/// The lower boundary of the box.
pub min: Vector3,
/// The upper boundary of the box.
pub max: Vector3,
}
impl Default for Box3 {
/// `new Box3()` — min `(+Infinity, +Infinity, +Infinity)`,
/// max `(-Infinity, -Infinity, -Infinity)`.
fn default() -> Self {
Self {
min: Vector3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY),
max: Vector3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY),
}
}
}
impl Box3 {
/// `Box3.isBox3`.
pub const IS_BOX3: bool = true;
/// `new Box3()`.
pub const fn new(min: Vector3, max: Vector3) -> Self {
Self { min, max }
}
/// `Box3.set()`.
pub fn set(&mut self, min: &Vector3, max: &Vector3) -> &mut Self {
self.min.copy(min);
self.max.copy(max);
self
}
/// `Box3.setFromArray()`.
pub fn set_from_array(&mut self, array: &[f64]) -> &mut Self {
self.make_empty();
let mut vector = Vector3::default();
let il = array.len();
let mut i = 0;
while i < il {
let point = *vector.from_array(array, i);
self.expand_by_point(&point);
i += 3;
}
self
}
/// `Box3.setFromBufferAttribute()`.
pub fn set_from_buffer_attribute(&mut self, attribute: &BufferAttribute) -> &mut Self {
self.make_empty();
for i in 0..attribute.count() {
let point = attribute.get_vector3(i);
self.expand_by_point(&point);
}
self
}
/// `Box3.setFromPoints()`.
pub fn set_from_points(&mut self, points: &[Vector3]) -> &mut Self {
self.make_empty();
for point in points {
self.expand_by_point(point);
}
self
}
/// `Box3.setFromCenterAndSize()`.
pub fn set_from_center_and_size(&mut self, center: &Vector3, size: &Vector3) -> &mut Self {
let mut vector = Vector3::default();
let half_size = *vector.copy(size).multiply_scalar(0.5);
self.min.copy(center).sub(&half_size);
self.max.copy(center).add(&half_size);
self
}
/// `Box3.setFromObject( object, precise )`: the world-axis-aligned box of a
/// scene-graph subtree.
///
/// `precise` puts every vertex of every geometry through its node's
/// `matrix_world`; the default walks each geometry's own bounding box
/// through the same matrix, which is cheaper and can be larger than
/// strictly necessary.
pub fn set_from_object(&mut self, object: &ObjectRef, precise: bool) -> &mut Self {
self.make_empty();
self.expand_by_object(object, precise)
}
/// `Box3.expandByObject( object, precise )`.
///
/// Two divergences from three.js, both because the port has no
/// `Object3D.getVertexPosition()` yet: the precise path reads the position
/// attribute directly, so it does not apply morph targets or skinning (the
/// non-precise path does get morphs, through
/// `BufferGeometry::compute_bounding_box()`), and there is no object-level
/// `boundingBox` to prefer over the geometry's, since only `SkinnedMesh`
/// has one here and it is not a [`Payload`](crate::objects::Payload)
/// variant. An `InstancedMesh` takes the conservative path in three.js too.
pub fn expand_by_object(&mut self, object: &ObjectRef, precise: bool) -> &mut Self {
// Computes the world-axis-aligned bounding box of an object (including
// its children), accounting for both the object's, and children's,
// world transforms.
object.update_world_matrix(false, false);
// Cloned out of the borrow: the `Rc` is cheap, and the recursion below
// must not hold a borrow on the node.
let geometry = {
let object = object.borrow();
object.payload.geometry().cloned()
};
if let Some(geometry) = geometry {
let (matrix_world, is_instanced_mesh) = {
let object = object.borrow();
(object.matrix_world, object.payload.is_instanced_mesh())
};
// precise AABB computation based on vertex data requires at least a
// position attribute. instancing isn't supported so far and uses the
// normal (conservative) code path.
let position = geometry.get_attribute("position");
match position {
Some(position) if precise && !is_instanced_mesh => {
for i in 0..position.count() {
let mut vector = position.get_vector3(i);
vector.apply_matrix4(&matrix_world);
self.expand_by_point(&vector);
}
}
_ => {
// geometry-level bounding box, computed on demand: the port's
// geometries do not cache one.
if let Some(bounding_box) = geometry.compute_bounding_box() {
let mut box_ = Self::new(bounding_box.min, bounding_box.max);
box_.apply_matrix4(&matrix_world);
self.union(&box_);
}
}
}
}
for child in object.children() {
self.expand_by_object(&child, precise);
}
self
}
/// `Box3.copy()`.
pub fn copy(&mut self, box_: &Self) -> &mut Self {
self.min.copy(&box_.min);
self.max.copy(&box_.max);
self
}
/// `Box3.makeEmpty()`.
pub fn make_empty(&mut self) -> &mut Self {
self.min.x = f64::INFINITY;
self.min.y = f64::INFINITY;
self.min.z = f64::INFINITY;
self.max.x = f64::NEG_INFINITY;
self.max.y = f64::NEG_INFINITY;
self.max.z = f64::NEG_INFINITY;
self
}
/// `Box3.isEmpty()`.
pub fn is_empty(&self) -> bool {
// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
(self.max.x < self.min.x) || (self.max.y < self.min.y) || (self.max.z < self.min.z)
}
/// `Box3.getCenter()`.
pub fn get_center(&self) -> Vector3 {
let mut target = Vector3::default();
if self.is_empty() {
*target.set(0.0, 0.0, 0.0)
} else {
*target
.add_vectors(&self.min, &self.max)
.multiply_scalar(0.5)
}
}
/// `Box3.getSize()`.
pub fn get_size(&self) -> Vector3 {
let mut target = Vector3::default();
if self.is_empty() {
*target.set(0.0, 0.0, 0.0)
} else {
*target.sub_vectors(&self.max, &self.min)
}
}
/// `Box3.expandByPoint()`.
pub fn expand_by_point(&mut self, point: &Vector3) -> &mut Self {
self.min.min(point);
self.max.max(point);
self
}
/// `Box3.expandByVector()`.
pub fn expand_by_vector(&mut self, vector: &Vector3) -> &mut Self {
self.min.sub(vector);
self.max.add(vector);
self
}
/// `Box3.expandByScalar()`.
pub fn expand_by_scalar(&mut self, scalar: f64) -> &mut Self {
self.min.add_scalar(-scalar);
self.max.add_scalar(scalar);
self
}
/// `Box3.containsPoint()`.
pub fn contains_point(&self, point: &Vector3) -> bool {
point.x >= self.min.x
&& point.x <= self.max.x
&& point.y >= self.min.y
&& point.y <= self.max.y
&& point.z >= self.min.z
&& point.z <= self.max.z
}
/// `Box3.containsBox()`.
pub fn contains_box(&self, box_: &Self) -> bool {
self.min.x <= box_.min.x
&& box_.max.x <= self.max.x
&& self.min.y <= box_.min.y
&& box_.max.y <= self.max.y
&& self.min.z <= box_.min.z
&& box_.max.z <= self.max.z
}
/// `Box3.getParameter()`.
pub fn get_parameter(&self, point: &Vector3) -> Vector3 {
// This can potentially have a divide by zero if the box
// has a size dimension of 0.
let mut target = Vector3::default();
*target.set(
(point.x - self.min.x) / (self.max.x - self.min.x),
(point.y - self.min.y) / (self.max.y - self.min.y),
(point.z - self.min.z) / (self.max.z - self.min.z),
)
}
/// `Box3.intersectsBox()`.
pub fn intersects_box(&self, box_: &Self) -> bool {
// using 6 splitting planes to rule out intersections.
box_.max.x >= self.min.x
&& box_.min.x <= self.max.x
&& box_.max.y >= self.min.y
&& box_.min.y <= self.max.y
&& box_.max.z >= self.min.z
&& box_.min.z <= self.max.z
}
/// `Box3.intersectsSphere()`.
pub fn intersects_sphere(&self, sphere: &Sphere) -> bool {
// Find the point on the AABB closest to the sphere center.
let vector = self.clamp_point(&sphere.center);
// If that point is inside the sphere, the AABB and sphere intersect.
vector.distance_to_squared(&sphere.center) <= (sphere.radius * sphere.radius)
}
/// `Box3.intersectsPlane()`.
pub fn intersects_plane(&self, plane: &Plane) -> bool {
// We compute the minimum and maximum dot product values. If those values
// are on the same side (back or front) of the plane, then there is no intersection.
let mut min;
let mut max;
if plane.normal.x > 0.0 {
min = plane.normal.x * self.min.x;
max = plane.normal.x * self.max.x;
} else {
min = plane.normal.x * self.max.x;
max = plane.normal.x * self.min.x;
}
if plane.normal.y > 0.0 {
min += plane.normal.y * self.min.y;
max += plane.normal.y * self.max.y;
} else {
min += plane.normal.y * self.max.y;
max += plane.normal.y * self.min.y;
}
if plane.normal.z > 0.0 {
min += plane.normal.z * self.min.z;
max += plane.normal.z * self.max.z;
} else {
min += plane.normal.z * self.max.z;
max += plane.normal.z * self.min.z;
}
(min <= -plane.constant) && (max >= -plane.constant)
}
/// `Box3.intersectsTriangle()`.
pub fn intersects_triangle(&self, triangle: &Triangle) -> bool {
if self.is_empty() {
return false;
}
// compute box center and extents
let center = self.get_center();
let mut extents = Vector3::default();
extents.sub_vectors(&self.max, ¢er);
// translate triangle to aabb origin
let mut v0 = Vector3::default();
let mut v1 = Vector3::default();
let mut v2 = Vector3::default();
v0.sub_vectors(&triangle.a, ¢er);
v1.sub_vectors(&triangle.b, ¢er);
v2.sub_vectors(&triangle.c, ¢er);
// compute edge vectors for triangle
let mut f0 = Vector3::default();
let mut f1 = Vector3::default();
let mut f2 = Vector3::default();
f0.sub_vectors(&v1, &v0);
f1.sub_vectors(&v2, &v1);
f2.sub_vectors(&v0, &v2);
// test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
// make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
// axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
let axes = [
0.0, -f0.z, f0.y, 0.0, -f1.z, f1.y, 0.0, -f2.z, f2.y, f0.z, 0.0, -f0.x, f1.z, 0.0,
-f1.x, f2.z, 0.0, -f2.x, -f0.y, f0.x, 0.0, -f1.y, f1.x, 0.0, -f2.y, f2.x, 0.0,
];
if !sat_for_axes(&axes, &v0, &v1, &v2, &extents) {
return false;
}
// test 3 face normals from the aabb
let axes = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
if !sat_for_axes(&axes, &v0, &v1, &v2, &extents) {
return false;
}
// finally testing the face normal of the triangle
// use already existing triangle edge vectors here
let mut triangle_normal = Vector3::default();
triangle_normal.cross_vectors(&f0, &f1);
let axes = [triangle_normal.x, triangle_normal.y, triangle_normal.z];
sat_for_axes(&axes, &v0, &v1, &v2, &extents)
}
/// `Box3.clampPoint()`.
pub fn clamp_point(&self, point: &Vector3) -> Vector3 {
let mut target = Vector3::default();
*target.copy(point).clamp(&self.min, &self.max)
}
/// `Box3.distanceToPoint()`.
pub fn distance_to_point(&self, point: &Vector3) -> f64 {
self.clamp_point(point).distance_to(point)
}
/// `Box3.getBoundingSphere()`.
pub fn get_bounding_sphere(&self) -> Sphere {
let mut target = Sphere::default();
if self.is_empty() {
target.make_empty();
} else {
target.center = self.get_center();
target.radius = self.get_size().length() * 0.5;
}
target
}
/// `Box3.intersect()`.
pub fn intersect(&mut self, box_: &Self) -> &mut Self {
self.min.max(&box_.min);
self.max.min(&box_.max);
// ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values.
if self.is_empty() {
self.make_empty();
}
self
}
/// `Box3.union()`.
pub fn union(&mut self, box_: &Self) -> &mut Self {
self.min.min(&box_.min);
self.max.max(&box_.max);
self
}
/// `Box3.applyMatrix4()`.
pub fn apply_matrix4(&mut self, matrix: &Matrix4) -> &mut Self {
// transform of empty box is an empty box.
if self.is_empty() {
return self;
}
let mut points = [Vector3::default(); 8];
// NOTE: I am using a binary pattern to specify all 2^3 combinations below
points[0]
.set(self.min.x, self.min.y, self.min.z)
.apply_matrix4(matrix); // 000
points[1]
.set(self.min.x, self.min.y, self.max.z)
.apply_matrix4(matrix); // 001
points[2]
.set(self.min.x, self.max.y, self.min.z)
.apply_matrix4(matrix); // 010
points[3]
.set(self.min.x, self.max.y, self.max.z)
.apply_matrix4(matrix); // 011
points[4]
.set(self.max.x, self.min.y, self.min.z)
.apply_matrix4(matrix); // 100
points[5]
.set(self.max.x, self.min.y, self.max.z)
.apply_matrix4(matrix); // 101
points[6]
.set(self.max.x, self.max.y, self.min.z)
.apply_matrix4(matrix); // 110
points[7]
.set(self.max.x, self.max.y, self.max.z)
.apply_matrix4(matrix); // 111
self.set_from_points(&points);
self
}
/// `Box3.translate()`.
pub fn translate(&mut self, offset: &Vector3) -> &mut Self {
self.min.add(offset);
self.max.add(offset);
self
}
/// `Box3.equals()`.
pub fn equals(&self, box_: &Self) -> bool {
box_.min.equals(&self.min) && box_.max.equals(&self.max)
}
}
/// `Box3.js` module-private `satForAxes()`.
fn sat_for_axes(axes: &[f64], v0: &Vector3, v1: &Vector3, v2: &Vector3, extents: &Vector3) -> bool {
let mut test_axis = Vector3::default();
let j = axes.len() - 3;
let mut i = 0;
while i <= j {
test_axis.from_array(axes, i);
// project the aabb onto the separating axis
let r = extents.x * test_axis.x.abs()
+ extents.y * test_axis.y.abs()
+ extents.z * test_axis.z.abs();
// project all 3 vertices of the triangle onto the separating axis
let p0 = v0.dot(&test_axis);
let p1 = v1.dot(&test_axis);
let p2 = v2.dot(&test_axis);
// actual test, basically see if either of the most extreme of the triangle points intersects r
if js_max(-js_max(p0, js_max(p1, p2)), js_min(p0, js_min(p1, p2))) > r {
// points of the projected triangle are outside the projected half-length of the aabb
// the axis is separating and we can exit
return false;
}
i += 3;
}
true
}