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