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