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