1 | // Copyright 2013 The Servo Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution. |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
7 | // option. This file may not be copied, modified, or distributed |
8 | // except according to those terms. |
9 | |
10 | use super::UnknownUnit; |
11 | use crate::approxord::{max, min}; |
12 | use crate::num::*; |
13 | use crate::point::{point3, Point3D}; |
14 | use crate::scale::Scale; |
15 | use crate::size::Size3D; |
16 | use crate::vector::Vector3D; |
17 | |
18 | use num_traits::{NumCast, Float}; |
19 | #[cfg (feature = "serde" )] |
20 | use serde::{Deserialize, Serialize}; |
21 | #[cfg (feature = "bytemuck" )] |
22 | use bytemuck::{Zeroable, Pod}; |
23 | |
24 | use core::borrow::Borrow; |
25 | use core::cmp::PartialOrd; |
26 | use core::fmt; |
27 | use core::hash::{Hash, Hasher}; |
28 | use core::ops::{Add, Div, DivAssign, Mul, MulAssign, Sub, Range}; |
29 | |
30 | /// An axis aligned 3D box represented by its minimum and maximum coordinates. |
31 | #[repr (C)] |
32 | #[cfg_attr (feature = "serde" , derive(Serialize, Deserialize))] |
33 | #[cfg_attr ( |
34 | feature = "serde" , |
35 | serde(bound(serialize = "T: Serialize" , deserialize = "T: Deserialize<'de>" )) |
36 | )] |
37 | pub struct Box3D<T, U> { |
38 | pub min: Point3D<T, U>, |
39 | pub max: Point3D<T, U>, |
40 | } |
41 | |
42 | impl<T: Hash, U> Hash for Box3D<T, U> { |
43 | fn hash<H: Hasher>(&self, h: &mut H) { |
44 | self.min.hash(state:h); |
45 | self.max.hash(state:h); |
46 | } |
47 | } |
48 | |
49 | impl<T: Copy, U> Copy for Box3D<T, U> {} |
50 | |
51 | impl<T: Clone, U> Clone for Box3D<T, U> { |
52 | fn clone(&self) -> Self { |
53 | Self::new(self.min.clone(), self.max.clone()) |
54 | } |
55 | } |
56 | |
57 | impl<T: PartialEq, U> PartialEq for Box3D<T, U> { |
58 | fn eq(&self, other: &Self) -> bool { |
59 | self.min.eq(&other.min) && self.max.eq(&other.max) |
60 | } |
61 | } |
62 | |
63 | impl<T: Eq, U> Eq for Box3D<T, U> {} |
64 | |
65 | impl<T: fmt::Debug, U> fmt::Debug for Box3D<T, U> { |
66 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
67 | f&mut DebugTuple<'_, '_>.debug_tuple(name:"Box3D" ) |
68 | .field(&self.min) |
69 | .field(&self.max) |
70 | .finish() |
71 | } |
72 | } |
73 | |
74 | #[cfg (feature = "bytemuck" )] |
75 | unsafe impl<T: Zeroable, U> Zeroable for Box3D<T, U> {} |
76 | |
77 | #[cfg (feature = "bytemuck" )] |
78 | unsafe impl<T: Pod, U: 'static> Pod for Box3D<T, U> {} |
79 | |
80 | impl<T, U> Box3D<T, U> { |
81 | /// Constructor. |
82 | #[inline ] |
83 | pub const fn new(min: Point3D<T, U>, max: Point3D<T, U>) -> Self { |
84 | Box3D { min, max } |
85 | } |
86 | |
87 | /// Creates a Box3D of the given size, at offset zero. |
88 | #[inline ] |
89 | pub fn from_size(size: Size3D<T, U>) -> Self where T: Zero { |
90 | Box3D { |
91 | min: Point3D::zero(), |
92 | max: point3(x:size.width, y:size.height, z:size.depth), |
93 | } |
94 | } |
95 | } |
96 | |
97 | impl<T, U> Box3D<T, U> |
98 | where |
99 | T: PartialOrd, |
100 | { |
101 | /// Returns true if the box has a negative volume. |
102 | /// |
103 | /// The common interpretation for a negative box is to consider it empty. It can be obtained |
104 | /// by calculating the intersection of two boxes that do not intersect. |
105 | #[inline ] |
106 | pub fn is_negative(&self) -> bool { |
107 | self.max.x < self.min.x || self.max.y < self.min.y || self.max.z < self.min.z |
108 | } |
109 | |
110 | /// Returns true if the size is zero, negative or NaN. |
111 | #[inline ] |
112 | pub fn is_empty(&self) -> bool { |
113 | !(self.max.x > self.min.x && self.max.y > self.min.y && self.max.z > self.min.z) |
114 | } |
115 | |
116 | #[inline ] |
117 | pub fn intersects(&self, other: &Self) -> bool { |
118 | self.min.x < other.max.x |
119 | && self.max.x > other.min.x |
120 | && self.min.y < other.max.y |
121 | && self.max.y > other.min.y |
122 | && self.min.z < other.max.z |
123 | && self.max.z > other.min.z |
124 | } |
125 | |
126 | /// Returns `true` if this box3d contains the point. Points are considered |
127 | /// in the box3d if they are on the front, left or top faces, but outside if they |
128 | /// are on the back, right or bottom faces. |
129 | #[inline ] |
130 | pub fn contains(&self, other: Point3D<T, U>) -> bool { |
131 | self.min.x <= other.x |
132 | && other.x < self.max.x |
133 | && self.min.y <= other.y |
134 | && other.y < self.max.y |
135 | && self.min.z <= other.z |
136 | && other.z < self.max.z |
137 | } |
138 | |
139 | /// Returns `true` if this box3d contains the interior of the other box3d. Always |
140 | /// returns `true` if other is empty, and always returns `false` if other is |
141 | /// nonempty but this box3d is empty. |
142 | #[inline ] |
143 | pub fn contains_box(&self, other: &Self) -> bool { |
144 | other.is_empty() |
145 | || (self.min.x <= other.min.x |
146 | && other.max.x <= self.max.x |
147 | && self.min.y <= other.min.y |
148 | && other.max.y <= self.max.y |
149 | && self.min.z <= other.min.z |
150 | && other.max.z <= self.max.z) |
151 | } |
152 | } |
153 | |
154 | impl<T, U> Box3D<T, U> |
155 | where |
156 | T: Copy + PartialOrd, |
157 | { |
158 | #[inline ] |
159 | pub fn to_non_empty(&self) -> Option<Self> { |
160 | if self.is_empty() { |
161 | return None; |
162 | } |
163 | |
164 | Some(*self) |
165 | } |
166 | |
167 | #[inline ] |
168 | pub fn intersection(&self, other: &Self) -> Option<Self> { |
169 | let b = self.intersection_unchecked(other); |
170 | |
171 | if b.is_empty() { |
172 | return None; |
173 | } |
174 | |
175 | Some(b) |
176 | } |
177 | |
178 | pub fn intersection_unchecked(&self, other: &Self) -> Self { |
179 | let intersection_min = Point3D::new( |
180 | max(self.min.x, other.min.x), |
181 | max(self.min.y, other.min.y), |
182 | max(self.min.z, other.min.z), |
183 | ); |
184 | |
185 | let intersection_max = Point3D::new( |
186 | min(self.max.x, other.max.x), |
187 | min(self.max.y, other.max.y), |
188 | min(self.max.z, other.max.z), |
189 | ); |
190 | |
191 | Box3D::new(intersection_min, intersection_max) |
192 | } |
193 | |
194 | /// Computes the union of two boxes. |
195 | /// |
196 | /// If either of the boxes is empty, the other one is returned. |
197 | #[inline ] |
198 | pub fn union(&self, other: &Self) -> Self { |
199 | if other.is_empty() { |
200 | return *self; |
201 | } |
202 | if self.is_empty() { |
203 | return *other; |
204 | } |
205 | |
206 | Box3D::new( |
207 | Point3D::new( |
208 | min(self.min.x, other.min.x), |
209 | min(self.min.y, other.min.y), |
210 | min(self.min.z, other.min.z), |
211 | ), |
212 | Point3D::new( |
213 | max(self.max.x, other.max.x), |
214 | max(self.max.y, other.max.y), |
215 | max(self.max.z, other.max.z), |
216 | ), |
217 | ) |
218 | } |
219 | } |
220 | |
221 | impl<T, U> Box3D<T, U> |
222 | where |
223 | T: Copy + Add<T, Output = T>, |
224 | { |
225 | /// Returns the same box3d, translated by a vector. |
226 | #[inline ] |
227 | #[must_use ] |
228 | pub fn translate(&self, by: Vector3D<T, U>) -> Self { |
229 | Box3D { |
230 | min: self.min + by, |
231 | max: self.max + by, |
232 | } |
233 | } |
234 | } |
235 | |
236 | impl<T, U> Box3D<T, U> |
237 | where |
238 | T: Copy + Sub<T, Output = T>, |
239 | { |
240 | #[inline ] |
241 | pub fn size(&self) -> Size3D<T, U> { |
242 | Size3D::new( |
243 | self.max.x - self.min.x, |
244 | self.max.y - self.min.y, |
245 | self.max.z - self.min.z, |
246 | ) |
247 | } |
248 | |
249 | #[inline ] |
250 | pub fn width(&self) -> T { |
251 | self.max.x - self.min.x |
252 | } |
253 | |
254 | #[inline ] |
255 | pub fn height(&self) -> T { |
256 | self.max.y - self.min.y |
257 | } |
258 | |
259 | #[inline ] |
260 | pub fn depth(&self) -> T { |
261 | self.max.z - self.min.z |
262 | } |
263 | } |
264 | |
265 | impl<T, U> Box3D<T, U> |
266 | where |
267 | T: Copy + Add<T, Output = T> + Sub<T, Output = T>, |
268 | { |
269 | /// Inflates the box by the specified sizes on each side respectively. |
270 | #[inline ] |
271 | #[must_use ] |
272 | pub fn inflate(&self, width: T, height: T, depth: T) -> Self { |
273 | Box3D::new( |
274 | min:Point3D::new(self.min.x - width, self.min.y - height, self.min.z - depth), |
275 | max:Point3D::new(self.max.x + width, self.max.y + height, self.max.z + depth), |
276 | ) |
277 | } |
278 | } |
279 | |
280 | impl<T, U> Box3D<T, U> |
281 | where |
282 | T: Copy + Zero + PartialOrd, |
283 | { |
284 | /// Returns the smallest box containing all of the provided points. |
285 | pub fn from_points<I>(points: I) -> Self |
286 | where |
287 | I: IntoIterator, |
288 | I::Item: Borrow<Point3D<T, U>>, |
289 | { |
290 | let mut points = points.into_iter(); |
291 | |
292 | let (mut min_x, mut min_y, mut min_z) = match points.next() { |
293 | Some(first) => first.borrow().to_tuple(), |
294 | None => return Box3D::zero(), |
295 | }; |
296 | let (mut max_x, mut max_y, mut max_z) = (min_x, min_y, min_z); |
297 | |
298 | for point in points { |
299 | let p = point.borrow(); |
300 | if p.x < min_x { |
301 | min_x = p.x |
302 | } |
303 | if p.x > max_x { |
304 | max_x = p.x |
305 | } |
306 | if p.y < min_y { |
307 | min_y = p.y |
308 | } |
309 | if p.y > max_y { |
310 | max_y = p.y |
311 | } |
312 | if p.z < min_z { |
313 | min_z = p.z |
314 | } |
315 | if p.z > max_z { |
316 | max_z = p.z |
317 | } |
318 | } |
319 | |
320 | Box3D { |
321 | min: point3(min_x, min_y, min_z), |
322 | max: point3(max_x, max_y, max_z), |
323 | } |
324 | } |
325 | } |
326 | |
327 | impl<T, U> Box3D<T, U> |
328 | where |
329 | T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>, |
330 | { |
331 | /// Linearly interpolate between this box3d and another box3d. |
332 | #[inline ] |
333 | pub fn lerp(&self, other: Self, t: T) -> Self { |
334 | Self::new(self.min.lerp(other.min, t), self.max.lerp(other:other.max, t)) |
335 | } |
336 | } |
337 | |
338 | impl<T, U> Box3D<T, U> |
339 | where |
340 | T: Copy + One + Add<Output = T> + Div<Output = T>, |
341 | { |
342 | pub fn center(&self) -> Point3D<T, U> { |
343 | let two: T = T::one() + T::one(); |
344 | (self.min + self.max.to_vector()) / two |
345 | } |
346 | } |
347 | |
348 | impl<T, U> Box3D<T, U> |
349 | where |
350 | T: Copy + Mul<T, Output = T> + Sub<T, Output = T>, |
351 | { |
352 | #[inline ] |
353 | pub fn volume(&self) -> T { |
354 | let size = self.size(); |
355 | size.width * size.height * size.depth |
356 | } |
357 | |
358 | #[inline ] |
359 | pub fn xy_area(&self) -> T { |
360 | let size = self.size(); |
361 | size.width * size.height |
362 | } |
363 | |
364 | #[inline ] |
365 | pub fn yz_area(&self) -> T { |
366 | let size = self.size(); |
367 | size.depth * size.height |
368 | } |
369 | |
370 | #[inline ] |
371 | pub fn xz_area(&self) -> T { |
372 | let size = self.size(); |
373 | size.depth * size.width |
374 | } |
375 | } |
376 | |
377 | impl<T, U> Box3D<T, U> |
378 | where |
379 | T: Zero, |
380 | { |
381 | /// Constructor, setting all sides to zero. |
382 | pub fn zero() -> Self { |
383 | Box3D::new(min:Point3D::zero(), max:Point3D::zero()) |
384 | } |
385 | } |
386 | |
387 | impl<T: Copy + Mul, U> Mul<T> for Box3D<T, U> { |
388 | type Output = Box3D<T::Output, U>; |
389 | |
390 | #[inline ] |
391 | fn mul(self, scale: T) -> Self::Output { |
392 | Box3D::new(self.min * scale, self.max * scale) |
393 | } |
394 | } |
395 | |
396 | impl<T: Copy + MulAssign, U> MulAssign<T> for Box3D<T, U> { |
397 | #[inline ] |
398 | fn mul_assign(&mut self, scale: T) { |
399 | self.min *= scale; |
400 | self.max *= scale; |
401 | } |
402 | } |
403 | |
404 | impl<T: Copy + Div, U> Div<T> for Box3D<T, U> { |
405 | type Output = Box3D<T::Output, U>; |
406 | |
407 | #[inline ] |
408 | fn div(self, scale: T) -> Self::Output { |
409 | Box3D::new(self.min / scale.clone(), self.max / scale) |
410 | } |
411 | } |
412 | |
413 | impl<T: Copy + DivAssign, U> DivAssign<T> for Box3D<T, U> { |
414 | #[inline ] |
415 | fn div_assign(&mut self, scale: T) { |
416 | self.min /= scale; |
417 | self.max /= scale; |
418 | } |
419 | } |
420 | |
421 | impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Box3D<T, U1> { |
422 | type Output = Box3D<T::Output, U2>; |
423 | |
424 | #[inline ] |
425 | fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output { |
426 | Box3D::new(self.min * scale.clone(), self.max * scale) |
427 | } |
428 | } |
429 | |
430 | impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Box3D<T, U> { |
431 | #[inline ] |
432 | fn mul_assign(&mut self, scale: Scale<T, U, U>) { |
433 | self.min *= scale.clone(); |
434 | self.max *= scale; |
435 | } |
436 | } |
437 | |
438 | impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Box3D<T, U2> { |
439 | type Output = Box3D<T::Output, U1>; |
440 | |
441 | #[inline ] |
442 | fn div(self, scale: Scale<T, U1, U2>) -> Self::Output { |
443 | Box3D::new(self.min / scale.clone(), self.max / scale) |
444 | } |
445 | } |
446 | |
447 | impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Box3D<T, U> { |
448 | #[inline ] |
449 | fn div_assign(&mut self, scale: Scale<T, U, U>) { |
450 | self.min /= scale.clone(); |
451 | self.max /= scale; |
452 | } |
453 | } |
454 | |
455 | impl<T, U> Box3D<T, U> |
456 | where |
457 | T: Copy, |
458 | { |
459 | #[inline ] |
460 | pub fn x_range(&self) -> Range<T> { |
461 | self.min.x..self.max.x |
462 | } |
463 | |
464 | #[inline ] |
465 | pub fn y_range(&self) -> Range<T> { |
466 | self.min.y..self.max.y |
467 | } |
468 | |
469 | #[inline ] |
470 | pub fn z_range(&self) -> Range<T> { |
471 | self.min.z..self.max.z |
472 | } |
473 | |
474 | /// Drop the units, preserving only the numeric value. |
475 | #[inline ] |
476 | pub fn to_untyped(&self) -> Box3D<T, UnknownUnit> { |
477 | Box3D { |
478 | min: self.min.to_untyped(), |
479 | max: self.max.to_untyped(), |
480 | } |
481 | } |
482 | |
483 | /// Tag a unitless value with units. |
484 | #[inline ] |
485 | pub fn from_untyped(c: &Box3D<T, UnknownUnit>) -> Box3D<T, U> { |
486 | Box3D { |
487 | min: Point3D::from_untyped(c.min), |
488 | max: Point3D::from_untyped(c.max), |
489 | } |
490 | } |
491 | |
492 | /// Cast the unit |
493 | #[inline ] |
494 | pub fn cast_unit<V>(&self) -> Box3D<T, V> { |
495 | Box3D::new(self.min.cast_unit(), self.max.cast_unit()) |
496 | } |
497 | |
498 | #[inline ] |
499 | pub fn scale<S: Copy>(&self, x: S, y: S, z: S) -> Self |
500 | where |
501 | T: Mul<S, Output = T>, |
502 | { |
503 | Box3D::new( |
504 | Point3D::new(self.min.x * x, self.min.y * y, self.min.z * z), |
505 | Point3D::new(self.max.x * x, self.max.y * y, self.max.z * z), |
506 | ) |
507 | } |
508 | } |
509 | |
510 | impl<T: NumCast + Copy, U> Box3D<T, U> { |
511 | /// Cast from one numeric representation to another, preserving the units. |
512 | /// |
513 | /// When casting from floating point to integer coordinates, the decimals are truncated |
514 | /// as one would expect from a simple cast, but this behavior does not always make sense |
515 | /// geometrically. Consider using round(), round_in or round_out() before casting. |
516 | #[inline ] |
517 | pub fn cast<NewT: NumCast>(&self) -> Box3D<NewT, U> { |
518 | Box3D::new(self.min.cast(), self.max.cast()) |
519 | } |
520 | |
521 | /// Fallible cast from one numeric representation to another, preserving the units. |
522 | /// |
523 | /// When casting from floating point to integer coordinates, the decimals are truncated |
524 | /// as one would expect from a simple cast, but this behavior does not always make sense |
525 | /// geometrically. Consider using round(), round_in or round_out() before casting. |
526 | pub fn try_cast<NewT: NumCast>(&self) -> Option<Box3D<NewT, U>> { |
527 | match (self.min.try_cast(), self.max.try_cast()) { |
528 | (Some(a), Some(b)) => Some(Box3D::new(a, b)), |
529 | _ => None, |
530 | } |
531 | } |
532 | |
533 | // Convenience functions for common casts |
534 | |
535 | /// Cast into an `f32` box3d. |
536 | #[inline ] |
537 | pub fn to_f32(&self) -> Box3D<f32, U> { |
538 | self.cast() |
539 | } |
540 | |
541 | /// Cast into an `f64` box3d. |
542 | #[inline ] |
543 | pub fn to_f64(&self) -> Box3D<f64, U> { |
544 | self.cast() |
545 | } |
546 | |
547 | /// Cast into an `usize` box3d, truncating decimals if any. |
548 | /// |
549 | /// When casting from floating point cuboids, it is worth considering whether |
550 | /// to `round()`, `round_in()` or `round_out()` before the cast in order to |
551 | /// obtain the desired conversion behavior. |
552 | #[inline ] |
553 | pub fn to_usize(&self) -> Box3D<usize, U> { |
554 | self.cast() |
555 | } |
556 | |
557 | /// Cast into an `u32` box3d, truncating decimals if any. |
558 | /// |
559 | /// When casting from floating point cuboids, it is worth considering whether |
560 | /// to `round()`, `round_in()` or `round_out()` before the cast in order to |
561 | /// obtain the desired conversion behavior. |
562 | #[inline ] |
563 | pub fn to_u32(&self) -> Box3D<u32, U> { |
564 | self.cast() |
565 | } |
566 | |
567 | /// Cast into an `i32` box3d, truncating decimals if any. |
568 | /// |
569 | /// When casting from floating point cuboids, it is worth considering whether |
570 | /// to `round()`, `round_in()` or `round_out()` before the cast in order to |
571 | /// obtain the desired conversion behavior. |
572 | #[inline ] |
573 | pub fn to_i32(&self) -> Box3D<i32, U> { |
574 | self.cast() |
575 | } |
576 | |
577 | /// Cast into an `i64` box3d, truncating decimals if any. |
578 | /// |
579 | /// When casting from floating point cuboids, it is worth considering whether |
580 | /// to `round()`, `round_in()` or `round_out()` before the cast in order to |
581 | /// obtain the desired conversion behavior. |
582 | #[inline ] |
583 | pub fn to_i64(&self) -> Box3D<i64, U> { |
584 | self.cast() |
585 | } |
586 | } |
587 | |
588 | impl<T: Float, U> Box3D<T, U> { |
589 | /// Returns true if all members are finite. |
590 | #[inline ] |
591 | pub fn is_finite(self) -> bool { |
592 | self.min.is_finite() && self.max.is_finite() |
593 | } |
594 | } |
595 | |
596 | impl<T, U> Box3D<T, U> |
597 | where |
598 | T: Round, |
599 | { |
600 | /// Return a box3d with edges rounded to integer coordinates, such that |
601 | /// the returned box3d has the same set of pixel centers as the original |
602 | /// one. |
603 | /// Values equal to 0.5 round up. |
604 | /// Suitable for most places where integral device coordinates |
605 | /// are needed, but note that any translation should be applied first to |
606 | /// avoid pixel rounding errors. |
607 | /// Note that this is *not* rounding to nearest integer if the values are negative. |
608 | /// They are always rounding as floor(n + 0.5). |
609 | #[must_use ] |
610 | pub fn round(&self) -> Self { |
611 | Box3D::new(self.min.round(), self.max.round()) |
612 | } |
613 | } |
614 | |
615 | impl<T, U> Box3D<T, U> |
616 | where |
617 | T: Floor + Ceil, |
618 | { |
619 | /// Return a box3d with faces/edges rounded to integer coordinates, such that |
620 | /// the original box3d contains the resulting box3d. |
621 | #[must_use ] |
622 | pub fn round_in(&self) -> Self { |
623 | Box3D { |
624 | min: self.min.ceil(), |
625 | max: self.max.floor(), |
626 | } |
627 | } |
628 | |
629 | /// Return a box3d with faces/edges rounded to integer coordinates, such that |
630 | /// the original box3d is contained in the resulting box3d. |
631 | #[must_use ] |
632 | pub fn round_out(&self) -> Self { |
633 | Box3D { |
634 | min: self.min.floor(), |
635 | max: self.max.ceil(), |
636 | } |
637 | } |
638 | } |
639 | |
640 | impl<T, U> From<Size3D<T, U>> for Box3D<T, U> |
641 | where |
642 | T: Copy + Zero + PartialOrd, |
643 | { |
644 | fn from(b: Size3D<T, U>) -> Self { |
645 | Self::from_size(b) |
646 | } |
647 | } |
648 | |
649 | impl<T: Default, U> Default for Box3D<T, U> { |
650 | fn default() -> Self { |
651 | Box3D { |
652 | min: Default::default(), |
653 | max: Default::default(), |
654 | } |
655 | } |
656 | } |
657 | |
658 | /// Shorthand for `Box3D::new(Point3D::new(x1, y1, z1), Point3D::new(x2, y2, z2))`. |
659 | pub fn box3d<T: Copy, U>( |
660 | min_x: T, |
661 | min_y: T, |
662 | min_z: T, |
663 | max_x: T, |
664 | max_y: T, |
665 | max_z: T, |
666 | ) -> Box3D<T, U> { |
667 | Box3D::new( |
668 | min:Point3D::new(min_x, min_y, min_z), |
669 | max:Point3D::new(max_x, max_y, max_z), |
670 | ) |
671 | } |
672 | |
673 | #[cfg (test)] |
674 | mod tests { |
675 | use crate::default::{Box3D, Point3D}; |
676 | use crate::{point3, size3, vec3}; |
677 | |
678 | #[test ] |
679 | fn test_new() { |
680 | let b = Box3D::new(point3(-1.0, -1.0, -1.0), point3(1.0, 1.0, 1.0)); |
681 | assert!(b.min.x == -1.0); |
682 | assert!(b.min.y == -1.0); |
683 | assert!(b.min.z == -1.0); |
684 | assert!(b.max.x == 1.0); |
685 | assert!(b.max.y == 1.0); |
686 | assert!(b.max.z == 1.0); |
687 | } |
688 | |
689 | #[test ] |
690 | fn test_size() { |
691 | let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)); |
692 | assert!(b.size().width == 20.0); |
693 | assert!(b.size().height == 20.0); |
694 | assert!(b.size().depth == 20.0); |
695 | } |
696 | |
697 | #[test ] |
698 | fn test_width_height_depth() { |
699 | let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)); |
700 | assert!(b.width() == 20.0); |
701 | assert!(b.height() == 20.0); |
702 | assert!(b.depth() == 20.0); |
703 | } |
704 | |
705 | #[test ] |
706 | fn test_center() { |
707 | let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)); |
708 | assert!(b.center() == Point3D::zero()); |
709 | } |
710 | |
711 | #[test ] |
712 | fn test_volume() { |
713 | let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)); |
714 | assert!(b.volume() == 8000.0); |
715 | } |
716 | |
717 | #[test ] |
718 | fn test_area() { |
719 | let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)); |
720 | assert!(b.xy_area() == 400.0); |
721 | assert!(b.yz_area() == 400.0); |
722 | assert!(b.xz_area() == 400.0); |
723 | } |
724 | |
725 | #[test ] |
726 | fn test_from_points() { |
727 | let b = Box3D::from_points(&[point3(50.0, 160.0, 12.5), point3(100.0, 25.0, 200.0)]); |
728 | assert!(b.min == point3(50.0, 25.0, 12.5)); |
729 | assert!(b.max == point3(100.0, 160.0, 200.0)); |
730 | } |
731 | |
732 | #[test ] |
733 | fn test_min_max() { |
734 | let b = Box3D::from_points(&[point3(50.0, 25.0, 12.5), point3(100.0, 160.0, 200.0)]); |
735 | assert!(b.min.x == 50.0); |
736 | assert!(b.min.y == 25.0); |
737 | assert!(b.min.z == 12.5); |
738 | assert!(b.max.x == 100.0); |
739 | assert!(b.max.y == 160.0); |
740 | assert!(b.max.z == 200.0); |
741 | } |
742 | |
743 | #[test ] |
744 | fn test_round_in() { |
745 | let b = |
746 | Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round_in(); |
747 | assert!(b.min.x == -25.0); |
748 | assert!(b.min.y == -40.0); |
749 | assert!(b.min.z == -70.0); |
750 | assert!(b.max.x == 60.0); |
751 | assert!(b.max.y == 36.0); |
752 | assert!(b.max.z == 89.0); |
753 | } |
754 | |
755 | #[test ] |
756 | fn test_round_out() { |
757 | let b = Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]) |
758 | .round_out(); |
759 | assert!(b.min.x == -26.0); |
760 | assert!(b.min.y == -41.0); |
761 | assert!(b.min.z == -71.0); |
762 | assert!(b.max.x == 61.0); |
763 | assert!(b.max.y == 37.0); |
764 | assert!(b.max.z == 90.0); |
765 | } |
766 | |
767 | #[test ] |
768 | fn test_round() { |
769 | let b = |
770 | Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round(); |
771 | assert!(b.min.x == -25.0); |
772 | assert!(b.min.y == -40.0); |
773 | assert!(b.min.z == -71.0); |
774 | assert!(b.max.x == 60.0); |
775 | assert!(b.max.y == 37.0); |
776 | assert!(b.max.z == 90.0); |
777 | } |
778 | |
779 | #[test ] |
780 | fn test_from_size() { |
781 | let b = Box3D::from_size(size3(30.0, 40.0, 50.0)); |
782 | assert!(b.min == Point3D::zero()); |
783 | assert!(b.size().width == 30.0); |
784 | assert!(b.size().height == 40.0); |
785 | assert!(b.size().depth == 50.0); |
786 | } |
787 | |
788 | #[test ] |
789 | fn test_translate() { |
790 | let size = size3(15.0, 15.0, 200.0); |
791 | let mut center = (size / 2.0).to_vector().to_point(); |
792 | let b = Box3D::from_size(size); |
793 | assert!(b.center() == center); |
794 | let translation = vec3(10.0, 2.5, 9.5); |
795 | let b = b.translate(translation); |
796 | center += translation; |
797 | assert!(b.center() == center); |
798 | assert!(b.max.x == 25.0); |
799 | assert!(b.max.y == 17.5); |
800 | assert!(b.max.z == 209.5); |
801 | assert!(b.min.x == 10.0); |
802 | assert!(b.min.y == 2.5); |
803 | assert!(b.min.z == 9.5); |
804 | } |
805 | |
806 | #[test ] |
807 | fn test_union() { |
808 | let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(0.0, 20.0, 20.0)]); |
809 | let b2 = Box3D::from_points(&[point3(0.0, 20.0, 20.0), point3(20.0, -20.0, -20.0)]); |
810 | let b = b1.union(&b2); |
811 | assert!(b.max.x == 20.0); |
812 | assert!(b.max.y == 20.0); |
813 | assert!(b.max.z == 20.0); |
814 | assert!(b.min.x == -20.0); |
815 | assert!(b.min.y == -20.0); |
816 | assert!(b.min.z == -20.0); |
817 | assert!(b.volume() == (40.0 * 40.0 * 40.0)); |
818 | } |
819 | |
820 | #[test ] |
821 | fn test_intersects() { |
822 | let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]); |
823 | let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]); |
824 | assert!(b1.intersects(&b2)); |
825 | } |
826 | |
827 | #[test ] |
828 | fn test_intersection_unchecked() { |
829 | let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]); |
830 | let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]); |
831 | let b = b1.intersection_unchecked(&b2); |
832 | assert!(b.max.x == 10.0); |
833 | assert!(b.max.y == 20.0); |
834 | assert!(b.max.z == 20.0); |
835 | assert!(b.min.x == -10.0); |
836 | assert!(b.min.y == -20.0); |
837 | assert!(b.min.z == -20.0); |
838 | assert!(b.volume() == (20.0 * 40.0 * 40.0)); |
839 | } |
840 | |
841 | #[test ] |
842 | fn test_intersection() { |
843 | let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]); |
844 | let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]); |
845 | assert!(b1.intersection(&b2).is_some()); |
846 | |
847 | let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(-10.0, 20.0, 20.0)]); |
848 | let b2 = Box3D::from_points(&[point3(10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]); |
849 | assert!(b1.intersection(&b2).is_none()); |
850 | } |
851 | |
852 | #[test ] |
853 | fn test_scale() { |
854 | let b = Box3D::from_points(&[point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)]); |
855 | let b = b.scale(0.5, 0.5, 0.5); |
856 | assert!(b.max.x == 5.0); |
857 | assert!(b.max.y == 5.0); |
858 | assert!(b.max.z == 5.0); |
859 | assert!(b.min.x == -5.0); |
860 | assert!(b.min.y == -5.0); |
861 | assert!(b.min.z == -5.0); |
862 | } |
863 | |
864 | #[test ] |
865 | fn test_zero() { |
866 | let b = Box3D::<f64>::zero(); |
867 | assert!(b.max.x == 0.0); |
868 | assert!(b.max.y == 0.0); |
869 | assert!(b.max.z == 0.0); |
870 | assert!(b.min.x == 0.0); |
871 | assert!(b.min.y == 0.0); |
872 | assert!(b.min.z == 0.0); |
873 | } |
874 | |
875 | #[test ] |
876 | fn test_lerp() { |
877 | let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(-10.0, -10.0, -10.0)]); |
878 | let b2 = Box3D::from_points(&[point3(10.0, 10.0, 10.0), point3(20.0, 20.0, 20.0)]); |
879 | let b = b1.lerp(b2, 0.5); |
880 | assert!(b.center() == Point3D::zero()); |
881 | assert!(b.size().width == 10.0); |
882 | assert!(b.size().height == 10.0); |
883 | assert!(b.size().depth == 10.0); |
884 | } |
885 | |
886 | #[test ] |
887 | fn test_contains() { |
888 | let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]); |
889 | assert!(b.contains(point3(-15.3, 10.5, 18.4))); |
890 | } |
891 | |
892 | #[test ] |
893 | fn test_contains_box() { |
894 | let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]); |
895 | let b2 = Box3D::from_points(&[point3(-14.3, -16.5, -19.3), point3(6.7, 17.6, 2.5)]); |
896 | assert!(b1.contains_box(&b2)); |
897 | } |
898 | |
899 | #[test ] |
900 | fn test_inflate() { |
901 | let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]); |
902 | let b = b.inflate(10.0, 5.0, 2.0); |
903 | assert!(b.size().width == 60.0); |
904 | assert!(b.size().height == 50.0); |
905 | assert!(b.size().depth == 44.0); |
906 | assert!(b.center() == Point3D::zero()); |
907 | } |
908 | |
909 | #[test ] |
910 | fn test_is_empty() { |
911 | for i in 0..3 { |
912 | let mut coords_neg = [-20.0, -20.0, -20.0]; |
913 | let mut coords_pos = [20.0, 20.0, 20.0]; |
914 | coords_neg[i] = 0.0; |
915 | coords_pos[i] = 0.0; |
916 | let b = Box3D::from_points(&[Point3D::from(coords_neg), Point3D::from(coords_pos)]); |
917 | assert!(b.is_empty()); |
918 | } |
919 | } |
920 | |
921 | #[test ] |
922 | fn test_nan_empty_or_negative() { |
923 | use std::f32::NAN; |
924 | assert!(Box3D { min: point3(NAN, 2.0, 1.0), max: point3(1.0, 3.0, 5.0) }.is_empty()); |
925 | assert!(Box3D { min: point3(0.0, NAN, 1.0), max: point3(1.0, 2.0, 5.0) }.is_empty()); |
926 | assert!(Box3D { min: point3(1.0, -2.0, NAN), max: point3(3.0, 2.0, 5.0) }.is_empty()); |
927 | assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(NAN, 2.0, 5.0) }.is_empty()); |
928 | assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, NAN, 5.0) }.is_empty()); |
929 | assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, 1.0, NAN) }.is_empty()); |
930 | } |
931 | } |
932 | |