| 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::approxeq::ApproxEq; |
| 12 |
use crate::approxord::{max, min}; |
| 13 |
use crate::length::Length; |
| 14 |
use crate::num::*; |
| 15 |
use crate::scale::Scale; |
| 16 |
use crate::size::{Size2D, Size3D}; |
| 17 |
use crate::vector::{vec2, vec3, Vector2D, Vector3D}; |
| 18 |
use core::cmp::{Eq, PartialEq}; |
| 19 |
use core::fmt; |
| 20 |
use core::hash::Hash; |
| 21 |
use core::marker::PhantomData; |
| 22 |
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
| 23 |
#[cfg(feature = "mint")] |
| 24 |
use mint; |
| 25 |
use num_traits::real::Real; |
| 26 |
use num_traits::{Euclid, Float, NumCast}; |
| 27 |
#[cfg(feature = "serde")] |
| 28 |
use serde; |
| 29 |
|
| 30 |
#[cfg(feature = "bytemuck")] |
| 31 |
use bytemuck::{Pod, Zeroable}; |
| 32 |
|
| 33 |
/// A 2d Point tagged with a unit. |
| 34 |
#[repr(C)] |
| 35 |
pub struct Point2D<T, U> { |
| 36 |
pub x: T, |
| 37 |
pub y: T, |
| 38 |
#[doc(hidden)] |
| 39 |
pub _unit: PhantomData<U>, |
| 40 |
} |
| 41 |
|
| 42 |
impl<T: Copy, U> Copy for Point2D<T, U> {} |
| 43 |
|
| 44 |
impl<T: Clone, U> Clone for Point2D<T, U> { |
| 45 |
fn clone(&self) -> Self { |
| 46 |
Point2D { |
| 47 |
x: self.x.clone(), |
| 48 |
y: self.y.clone(), |
| 49 |
_unit: PhantomData, |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
#[cfg(feature = "serde")] |
| 55 |
impl<'de, T, U> serde::Deserialize<'de> for Point2D<T, U> |
| 56 |
where |
| 57 |
T: serde::Deserialize<'de>, |
| 58 |
{ |
| 59 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 60 |
where |
| 61 |
D: serde::Deserializer<'de>, |
| 62 |
{ |
| 63 |
let (x, y) = serde::Deserialize::deserialize(deserializer)?; |
| 64 |
Ok(Point2D { |
| 65 |
x, |
| 66 |
y, |
| 67 |
_unit: PhantomData, |
| 68 |
}) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(feature = "serde")] |
| 73 |
impl<T, U> serde::Serialize for Point2D<T, U> |
| 74 |
where |
| 75 |
T: serde::Serialize, |
| 76 |
{ |
| 77 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 78 |
where |
| 79 |
S: serde::Serializer, |
| 80 |
{ |
| 81 |
(&self.x, &self.y).serialize(serializer) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
#[cfg(feature = "arbitrary")] |
| 86 |
impl<'a, T, U> arbitrary::Arbitrary<'a> for Point2D<T, U> |
| 87 |
where |
| 88 |
T: arbitrary::Arbitrary<'a>, |
| 89 |
{ |
| 90 |
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { |
| 91 |
let (x, y) = arbitrary::Arbitrary::arbitrary(u)?; |
| 92 |
Ok(Point2D { |
| 93 |
x, |
| 94 |
y, |
| 95 |
_unit: PhantomData, |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(feature = "bytemuck")] |
| 101 |
unsafe impl<T: Zeroable, U> Zeroable for Point2D<T, U> {} |
| 102 |
|
| 103 |
#[cfg(feature = "bytemuck")] |
| 104 |
unsafe impl<T: Pod, U: 'static> Pod for Point2D<T, U> {} |
| 105 |
|
| 106 |
impl<T, U> Eq for Point2D<T, U> where T: Eq {} |
| 107 |
|
| 108 |
impl<T, U> PartialEq for Point2D<T, U> |
| 109 |
where |
| 110 |
T: PartialEq, |
| 111 |
{ |
| 112 |
fn eq(&self, other: &Self) -> bool { |
| 113 |
self.x == other.x && self.y == other.y |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl<T, U> Hash for Point2D<T, U> |
| 118 |
where |
| 119 |
T: Hash, |
| 120 |
{ |
| 121 |
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
| 122 |
self.x.hash(h); |
| 123 |
self.y.hash(h); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
mint_vec!(Point2D[x, y] = Point2); |
| 128 |
|
| 129 |
impl<T: fmt::Debug, U> fmt::Debug for Point2D<T, U> { |
| 130 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 131 |
f.debug_tuple("").field(&self.x).field(&self.y).finish() |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
impl<T: Default, U> Default for Point2D<T, U> { |
| 136 |
fn default() -> Self { |
| 137 |
Point2D::new(Default::default(), Default::default()) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl<T, U> Point2D<T, U> { |
| 142 |
/// Constructor, setting all components to zero. |
| 143 |
#[inline] |
| 144 |
pub fn origin() -> Self |
| 145 |
where |
| 146 |
T: Zero, |
| 147 |
{ |
| 148 |
point2(Zero::zero(), Zero::zero()) |
| 149 |
} |
| 150 |
|
| 151 |
/// The same as [`origin()`](#method.origin). |
| 152 |
#[inline] |
| 153 |
pub fn zero() -> Self |
| 154 |
where |
| 155 |
T: Zero, |
| 156 |
{ |
| 157 |
Self::origin() |
| 158 |
} |
| 159 |
|
| 160 |
/// Constructor taking scalar values directly. |
| 161 |
#[inline] |
| 162 |
pub const fn new(x: T, y: T) -> Self { |
| 163 |
Point2D { |
| 164 |
x, |
| 165 |
y, |
| 166 |
_unit: PhantomData, |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/// Constructor taking properly Lengths instead of scalar values. |
| 171 |
#[inline] |
| 172 |
pub fn from_lengths(x: Length<T, U>, y: Length<T, U>) -> Self { |
| 173 |
point2(x.0, y.0) |
| 174 |
} |
| 175 |
|
| 176 |
/// Constructor setting all components to the same value. |
| 177 |
#[inline] |
| 178 |
pub fn splat(v: T) -> Self |
| 179 |
where |
| 180 |
T: Clone, |
| 181 |
{ |
| 182 |
Point2D { |
| 183 |
x: v.clone(), |
| 184 |
y: v, |
| 185 |
_unit: PhantomData, |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Tag a unitless value with units. |
| 190 |
#[inline] |
| 191 |
pub fn from_untyped(p: Point2D<T, UnknownUnit>) -> Self { |
| 192 |
point2(p.x, p.y) |
| 193 |
} |
| 194 |
|
| 195 |
/// Apply the function `f` to each component of this point. |
| 196 |
/// |
| 197 |
/// # Example |
| 198 |
/// |
| 199 |
/// This may be used to perform unusual arithmetic which is not already offered as methods. |
| 200 |
/// |
| 201 |
/// ``` |
| 202 |
/// use euclid::default::Point2D; |
| 203 |
/// |
| 204 |
/// let p = Point2D::<u32>::new(5, 15); |
| 205 |
/// assert_eq!(p.map(|coord| coord.saturating_sub(10)), Point2D::new(0, 5)); |
| 206 |
/// ``` |
| 207 |
#[inline] |
| 208 |
pub fn map<V, F: FnMut(T) -> V>(self, mut f: F) -> Point2D<V, U> { |
| 209 |
point2(f(self.x), f(self.y)) |
| 210 |
} |
| 211 |
|
| 212 |
/// Apply the function `f` to each pair of components of this point and `rhs`. |
| 213 |
/// |
| 214 |
/// # Example |
| 215 |
/// |
| 216 |
/// This may be used to perform unusual arithmetic which is not already offered as methods. |
| 217 |
/// |
| 218 |
/// ``` |
| 219 |
/// use euclid::{default::{Point2D, Vector2D}, point2}; |
| 220 |
/// |
| 221 |
/// let a: Point2D<u32> = point2(50, 200); |
| 222 |
/// let b: Point2D<u32> = point2(100, 100); |
| 223 |
/// assert_eq!(a.zip(b, u32::saturating_sub), Vector2D::new(0, 100)); |
| 224 |
/// ``` |
| 225 |
#[inline] |
| 226 |
pub fn zip<V, F: FnMut(T, T) -> V>(self, rhs: Self, mut f: F) -> Vector2D<V, U> { |
| 227 |
vec2(f(self.x, rhs.x), f(self.y, rhs.y)) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
impl<T: Copy, U> Point2D<T, U> { |
| 232 |
/// Create a 3d point from this one, using the specified z value. |
| 233 |
#[inline] |
| 234 |
pub fn extend(self, z: T) -> Point3D<T, U> { |
| 235 |
point3(self.x, self.y, z) |
| 236 |
} |
| 237 |
|
| 238 |
/// Cast this point into a vector. |
| 239 |
/// |
| 240 |
/// Equivalent to subtracting the origin from this point. |
| 241 |
#[inline] |
| 242 |
pub fn to_vector(self) -> Vector2D<T, U> { |
| 243 |
Vector2D { |
| 244 |
x: self.x, |
| 245 |
y: self.y, |
| 246 |
_unit: PhantomData, |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/// Swap x and y. |
| 251 |
/// |
| 252 |
/// # Example |
| 253 |
/// |
| 254 |
/// ```rust |
| 255 |
/// # use euclid::{Point2D, point2}; |
| 256 |
/// enum Mm {} |
| 257 |
/// |
| 258 |
/// let point: Point2D<_, Mm> = point2(1, -8); |
| 259 |
/// |
| 260 |
/// assert_eq!(point.yx(), point2(-8, 1)); |
| 261 |
/// ``` |
| 262 |
#[inline] |
| 263 |
pub fn yx(self) -> Self { |
| 264 |
point2(self.y, self.x) |
| 265 |
} |
| 266 |
|
| 267 |
/// Drop the units, preserving only the numeric value. |
| 268 |
/// |
| 269 |
/// # Example |
| 270 |
/// |
| 271 |
/// ```rust |
| 272 |
/// # use euclid::{Point2D, point2}; |
| 273 |
/// enum Mm {} |
| 274 |
/// |
| 275 |
/// let point: Point2D<_, Mm> = point2(1, -8); |
| 276 |
/// |
| 277 |
/// assert_eq!(point.x, point.to_untyped().x); |
| 278 |
/// assert_eq!(point.y, point.to_untyped().y); |
| 279 |
/// ``` |
| 280 |
#[inline] |
| 281 |
pub fn to_untyped(self) -> Point2D<T, UnknownUnit> { |
| 282 |
point2(self.x, self.y) |
| 283 |
} |
| 284 |
|
| 285 |
/// Cast the unit, preserving the numeric value. |
| 286 |
/// |
| 287 |
/// # Example |
| 288 |
/// |
| 289 |
/// ```rust |
| 290 |
/// # use euclid::{Point2D, point2}; |
| 291 |
/// enum Mm {} |
| 292 |
/// enum Cm {} |
| 293 |
/// |
| 294 |
/// let point: Point2D<_, Mm> = point2(1, -8); |
| 295 |
/// |
| 296 |
/// assert_eq!(point.x, point.cast_unit::<Cm>().x); |
| 297 |
/// assert_eq!(point.y, point.cast_unit::<Cm>().y); |
| 298 |
/// ``` |
| 299 |
#[inline] |
| 300 |
pub fn cast_unit<V>(self) -> Point2D<T, V> { |
| 301 |
point2(self.x, self.y) |
| 302 |
} |
| 303 |
|
| 304 |
/// Cast into an array with x and y. |
| 305 |
/// |
| 306 |
/// # Example |
| 307 |
/// |
| 308 |
/// ```rust |
| 309 |
/// # use euclid::{Point2D, point2}; |
| 310 |
/// enum Mm {} |
| 311 |
/// |
| 312 |
/// let point: Point2D<_, Mm> = point2(1, -8); |
| 313 |
/// |
| 314 |
/// assert_eq!(point.to_array(), [1, -8]); |
| 315 |
/// ``` |
| 316 |
#[inline] |
| 317 |
pub fn to_array(self) -> [T; 2] { |
| 318 |
[self.x, self.y] |
| 319 |
} |
| 320 |
|
| 321 |
/// Cast into a tuple with x and y. |
| 322 |
/// |
| 323 |
/// # Example |
| 324 |
/// |
| 325 |
/// ```rust |
| 326 |
/// # use euclid::{Point2D, point2}; |
| 327 |
/// enum Mm {} |
| 328 |
/// |
| 329 |
/// let point: Point2D<_, Mm> = point2(1, -8); |
| 330 |
/// |
| 331 |
/// assert_eq!(point.to_tuple(), (1, -8)); |
| 332 |
/// ``` |
| 333 |
#[inline] |
| 334 |
pub fn to_tuple(self) -> (T, T) { |
| 335 |
(self.x, self.y) |
| 336 |
} |
| 337 |
|
| 338 |
/// Convert into a 3d point with z-coordinate equals to zero. |
| 339 |
#[inline] |
| 340 |
pub fn to_3d(self) -> Point3D<T, U> |
| 341 |
where |
| 342 |
T: Zero, |
| 343 |
{ |
| 344 |
point3(self.x, self.y, Zero::zero()) |
| 345 |
} |
| 346 |
|
| 347 |
/// Rounds each component to the nearest integer value. |
| 348 |
/// |
| 349 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 350 |
/// |
| 351 |
/// ```rust |
| 352 |
/// # use euclid::point2; |
| 353 |
/// enum Mm {} |
| 354 |
/// |
| 355 |
/// assert_eq!(point2::<_, Mm>(-0.1, -0.8).round(), point2::<_, Mm>(0.0, -1.0)) |
| 356 |
/// ``` |
| 357 |
#[inline] |
| 358 |
#[must_use] |
| 359 |
pub fn round(self) -> Self |
| 360 |
where |
| 361 |
T: Round, |
| 362 |
{ |
| 363 |
point2(self.x.round(), self.y.round()) |
| 364 |
} |
| 365 |
|
| 366 |
/// Rounds each component to the smallest integer equal or greater than the original value. |
| 367 |
/// |
| 368 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 369 |
/// |
| 370 |
/// ```rust |
| 371 |
/// # use euclid::point2; |
| 372 |
/// enum Mm {} |
| 373 |
/// |
| 374 |
/// assert_eq!(point2::<_, Mm>(-0.1, -0.8).ceil(), point2::<_, Mm>(0.0, 0.0)) |
| 375 |
/// ``` |
| 376 |
#[inline] |
| 377 |
#[must_use] |
| 378 |
pub fn ceil(self) -> Self |
| 379 |
where |
| 380 |
T: Ceil, |
| 381 |
{ |
| 382 |
point2(self.x.ceil(), self.y.ceil()) |
| 383 |
} |
| 384 |
|
| 385 |
/// Rounds each component to the biggest integer equal or lower than the original value. |
| 386 |
/// |
| 387 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 388 |
/// |
| 389 |
/// ```rust |
| 390 |
/// # use euclid::point2; |
| 391 |
/// enum Mm {} |
| 392 |
/// |
| 393 |
/// assert_eq!(point2::<_, Mm>(-0.1, -0.8).floor(), point2::<_, Mm>(-1.0, -1.0)) |
| 394 |
/// ``` |
| 395 |
#[inline] |
| 396 |
#[must_use] |
| 397 |
pub fn floor(self) -> Self |
| 398 |
where |
| 399 |
T: Floor, |
| 400 |
{ |
| 401 |
point2(self.x.floor(), self.y.floor()) |
| 402 |
} |
| 403 |
|
| 404 |
/// Linearly interpolate between this point and another point. |
| 405 |
/// |
| 406 |
/// # Example |
| 407 |
/// |
| 408 |
/// ```rust |
| 409 |
/// use euclid::point2; |
| 410 |
/// use euclid::default::Point2D; |
| 411 |
/// |
| 412 |
/// let from: Point2D<_> = point2(0.0, 10.0); |
| 413 |
/// let to: Point2D<_> = point2(8.0, -4.0); |
| 414 |
/// |
| 415 |
/// assert_eq!(from.lerp(to, -1.0), point2(-8.0, 24.0)); |
| 416 |
/// assert_eq!(from.lerp(to, 0.0), point2( 0.0, 10.0)); |
| 417 |
/// assert_eq!(from.lerp(to, 0.5), point2( 4.0, 3.0)); |
| 418 |
/// assert_eq!(from.lerp(to, 1.0), point2( 8.0, -4.0)); |
| 419 |
/// assert_eq!(from.lerp(to, 2.0), point2(16.0, -18.0)); |
| 420 |
/// ``` |
| 421 |
#[inline] |
| 422 |
pub fn lerp(self, other: Self, t: T) -> Self |
| 423 |
where |
| 424 |
T: One + Sub<Output = T> + Mul<Output = T> + Add<Output = T>, |
| 425 |
{ |
| 426 |
let one_t = T::one() - t; |
| 427 |
point2(one_t * self.x + t * other.x, one_t * self.y + t * other.y) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
impl<T: PartialOrd, U> Point2D<T, U> { |
| 432 |
#[inline] |
| 433 |
pub fn min(self, other: Self) -> Self { |
| 434 |
point2(min(self.x, other.x), min(self.y, other.y)) |
| 435 |
} |
| 436 |
|
| 437 |
#[inline] |
| 438 |
pub fn max(self, other: Self) -> Self { |
| 439 |
point2(max(self.x, other.x), max(self.y, other.y)) |
| 440 |
} |
| 441 |
|
| 442 |
/// Returns the point each component of which clamped by corresponding |
| 443 |
/// components of `start` and `end`. |
| 444 |
/// |
| 445 |
/// Shortcut for `self.max(start).min(end)`. |
| 446 |
#[inline] |
| 447 |
pub fn clamp(self, start: Self, end: Self) -> Self |
| 448 |
where |
| 449 |
T: Copy, |
| 450 |
{ |
| 451 |
self.max(start).min(end) |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
impl<T: NumCast + Copy, U> Point2D<T, U> { |
| 456 |
/// Cast from one numeric representation to another, preserving the units. |
| 457 |
/// |
| 458 |
/// When casting from floating point to integer coordinates, the decimals are truncated |
| 459 |
/// as one would expect from a simple cast, but this behavior does not always make sense |
| 460 |
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. |
| 461 |
#[inline] |
| 462 |
pub fn cast<NewT: NumCast>(self) -> Point2D<NewT, U> { |
| 463 |
self.try_cast().unwrap() |
| 464 |
} |
| 465 |
|
| 466 |
/// Fallible cast from one numeric representation to another, preserving the units. |
| 467 |
/// |
| 468 |
/// When casting from floating point to integer coordinates, the decimals are truncated |
| 469 |
/// as one would expect from a simple cast, but this behavior does not always make sense |
| 470 |
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. |
| 471 |
pub fn try_cast<NewT: NumCast>(self) -> Option<Point2D<NewT, U>> { |
| 472 |
match (NumCast::from(self.x), NumCast::from(self.y)) { |
| 473 |
(Some(x), Some(y)) => Some(point2(x, y)), |
| 474 |
_ => None, |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// Convenience functions for common casts |
| 479 |
|
| 480 |
/// Cast into an `f32` point. |
| 481 |
#[inline] |
| 482 |
pub fn to_f32(self) -> Point2D<f32, U> { |
| 483 |
self.cast() |
| 484 |
} |
| 485 |
|
| 486 |
/// Cast into an `f64` point. |
| 487 |
#[inline] |
| 488 |
pub fn to_f64(self) -> Point2D<f64, U> { |
| 489 |
self.cast() |
| 490 |
} |
| 491 |
|
| 492 |
/// Cast into an `usize` point, truncating decimals if any. |
| 493 |
/// |
| 494 |
/// When casting from floating point points, it is worth considering whether |
| 495 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 496 |
/// the desired conversion behavior. |
| 497 |
#[inline] |
| 498 |
pub fn to_usize(self) -> Point2D<usize, U> { |
| 499 |
self.cast() |
| 500 |
} |
| 501 |
|
| 502 |
/// Cast into an `u32` point, truncating decimals if any. |
| 503 |
/// |
| 504 |
/// When casting from floating point points, it is worth considering whether |
| 505 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 506 |
/// the desired conversion behavior. |
| 507 |
#[inline] |
| 508 |
pub fn to_u32(self) -> Point2D<u32, U> { |
| 509 |
self.cast() |
| 510 |
} |
| 511 |
|
| 512 |
/// Cast into an i32 point, truncating decimals if any. |
| 513 |
/// |
| 514 |
/// When casting from floating point points, it is worth considering whether |
| 515 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 516 |
/// the desired conversion behavior. |
| 517 |
#[inline] |
| 518 |
pub fn to_i32(self) -> Point2D<i32, U> { |
| 519 |
self.cast() |
| 520 |
} |
| 521 |
|
| 522 |
/// Cast into an i64 point, truncating decimals if any. |
| 523 |
/// |
| 524 |
/// When casting from floating point points, it is worth considering whether |
| 525 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 526 |
/// the desired conversion behavior. |
| 527 |
#[inline] |
| 528 |
pub fn to_i64(self) -> Point2D<i64, U> { |
| 529 |
self.cast() |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
impl<T: Float, U> Point2D<T, U> { |
| 534 |
/// Returns true if all members are finite. |
| 535 |
#[inline] |
| 536 |
pub fn is_finite(self) -> bool { |
| 537 |
self.x.is_finite() && self.y.is_finite() |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
impl<T: Copy + Add<T, Output = T>, U> Point2D<T, U> { |
| 542 |
#[inline] |
| 543 |
pub fn add_size(self, other: &Size2D<T, U>) -> Self { |
| 544 |
point2(self.x + other.width, self.y + other.height) |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
impl<T: Real + Sub<T, Output = T>, U> Point2D<T, U> { |
| 549 |
#[inline] |
| 550 |
pub fn distance_to(self, other: Self) -> T { |
| 551 |
(self - other).length() |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
impl<T: Neg, U> Neg for Point2D<T, U> { |
| 556 |
type Output = Point2D<T::Output, U>; |
| 557 |
|
| 558 |
#[inline] |
| 559 |
fn neg(self) -> Self::Output { |
| 560 |
point2(-self.x, -self.y) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
impl<T: Add, U> Add<Size2D<T, U>> for Point2D<T, U> { |
| 565 |
type Output = Point2D<T::Output, U>; |
| 566 |
|
| 567 |
#[inline] |
| 568 |
fn add(self, other: Size2D<T, U>) -> Self::Output { |
| 569 |
point2(self.x + other.width, self.y + other.height) |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
impl<T: AddAssign, U> AddAssign<Size2D<T, U>> for Point2D<T, U> { |
| 574 |
#[inline] |
| 575 |
fn add_assign(&mut self, other: Size2D<T, U>) { |
| 576 |
self.x += other.width; |
| 577 |
self.y += other.height; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
impl<T: Add, U> Add<Vector2D<T, U>> for Point2D<T, U> { |
| 582 |
type Output = Point2D<T::Output, U>; |
| 583 |
|
| 584 |
#[inline] |
| 585 |
fn add(self, other: Vector2D<T, U>) -> Self::Output { |
| 586 |
point2(self.x + other.x, self.y + other.y) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
impl<T: Copy + Add<T, Output = T>, U> AddAssign<Vector2D<T, U>> for Point2D<T, U> { |
| 591 |
#[inline] |
| 592 |
fn add_assign(&mut self, other: Vector2D<T, U>) { |
| 593 |
*self = *self + other |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
impl<T: Sub, U> Sub for Point2D<T, U> { |
| 598 |
type Output = Vector2D<T::Output, U>; |
| 599 |
|
| 600 |
#[inline] |
| 601 |
fn sub(self, other: Self) -> Self::Output { |
| 602 |
vec2(self.x - other.x, self.y - other.y) |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
impl<T: Sub, U> Sub<Size2D<T, U>> for Point2D<T, U> { |
| 607 |
type Output = Point2D<T::Output, U>; |
| 608 |
|
| 609 |
#[inline] |
| 610 |
fn sub(self, other: Size2D<T, U>) -> Self::Output { |
| 611 |
point2(self.x - other.width, self.y - other.height) |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
impl<T: SubAssign, U> SubAssign<Size2D<T, U>> for Point2D<T, U> { |
| 616 |
#[inline] |
| 617 |
fn sub_assign(&mut self, other: Size2D<T, U>) { |
| 618 |
self.x -= other.width; |
| 619 |
self.y -= other.height; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
impl<T: Sub, U> Sub<Vector2D<T, U>> for Point2D<T, U> { |
| 624 |
type Output = Point2D<T::Output, U>; |
| 625 |
|
| 626 |
#[inline] |
| 627 |
fn sub(self, other: Vector2D<T, U>) -> Self::Output { |
| 628 |
point2(self.x - other.x, self.y - other.y) |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector2D<T, U>> for Point2D<T, U> { |
| 633 |
#[inline] |
| 634 |
fn sub_assign(&mut self, other: Vector2D<T, U>) { |
| 635 |
*self = *self - other |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
impl<T: Copy + Mul, U> Mul<T> for Point2D<T, U> { |
| 640 |
type Output = Point2D<T::Output, U>; |
| 641 |
|
| 642 |
#[inline] |
| 643 |
fn mul(self, scale: T) -> Self::Output { |
| 644 |
point2(self.x * scale, self.y * scale) |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
impl<T: Copy + Mul<T, Output = T>, U> MulAssign<T> for Point2D<T, U> { |
| 649 |
#[inline] |
| 650 |
fn mul_assign(&mut self, scale: T) { |
| 651 |
*self = *self * scale |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Point2D<T, U1> { |
| 656 |
type Output = Point2D<T::Output, U2>; |
| 657 |
|
| 658 |
#[inline] |
| 659 |
fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output { |
| 660 |
point2(self.x * scale.0, self.y * scale.0) |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Point2D<T, U> { |
| 665 |
#[inline] |
| 666 |
fn mul_assign(&mut self, scale: Scale<T, U, U>) { |
| 667 |
self.x *= scale.0; |
| 668 |
self.y *= scale.0; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
impl<T: Copy + Div, U> Div<T> for Point2D<T, U> { |
| 673 |
type Output = Point2D<T::Output, U>; |
| 674 |
|
| 675 |
#[inline] |
| 676 |
fn div(self, scale: T) -> Self::Output { |
| 677 |
point2(self.x / scale, self.y / scale) |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
impl<T: Copy + Div<T, Output = T>, U> DivAssign<T> for Point2D<T, U> { |
| 682 |
#[inline] |
| 683 |
fn div_assign(&mut self, scale: T) { |
| 684 |
*self = *self / scale |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Point2D<T, U2> { |
| 689 |
type Output = Point2D<T::Output, U1>; |
| 690 |
|
| 691 |
#[inline] |
| 692 |
fn div(self, scale: Scale<T, U1, U2>) -> Self::Output { |
| 693 |
point2(self.x / scale.0, self.y / scale.0) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Point2D<T, U> { |
| 698 |
#[inline] |
| 699 |
fn div_assign(&mut self, scale: Scale<T, U, U>) { |
| 700 |
self.x /= scale.0; |
| 701 |
self.y /= scale.0; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
impl<T: Zero, U> Zero for Point2D<T, U> { |
| 706 |
#[inline] |
| 707 |
fn zero() -> Self { |
| 708 |
Self::origin() |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
impl<T: Round, U> Round for Point2D<T, U> { |
| 713 |
/// See [Point2D::round()](#method.round) |
| 714 |
#[inline] |
| 715 |
fn round(self) -> Self { |
| 716 |
self.round() |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
impl<T: Ceil, U> Ceil for Point2D<T, U> { |
| 721 |
/// See [Point2D::ceil()](#method.ceil) |
| 722 |
#[inline] |
| 723 |
fn ceil(self) -> Self { |
| 724 |
self.ceil() |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
impl<T: Floor, U> Floor for Point2D<T, U> { |
| 729 |
/// See [Point2D::floor()](#method.floor) |
| 730 |
#[inline] |
| 731 |
fn floor(self) -> Self { |
| 732 |
self.floor() |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
impl<T: ApproxEq<T>, U> ApproxEq<Point2D<T, U>> for Point2D<T, U> { |
| 737 |
#[inline] |
| 738 |
fn approx_epsilon() -> Self { |
| 739 |
point2(T::approx_epsilon(), T::approx_epsilon()) |
| 740 |
} |
| 741 |
|
| 742 |
#[inline] |
| 743 |
fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool { |
| 744 |
self.x.approx_eq_eps(&other.x, &eps.x) && self.y.approx_eq_eps(&other.y, &eps.y) |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
impl<T: Euclid, U> Point2D<T, U> { |
| 749 |
/// Calculates the least nonnegative remainder of `self (mod other)`. |
| 750 |
/// |
| 751 |
/// # Example |
| 752 |
/// |
| 753 |
/// ```rust |
| 754 |
/// use euclid::point2; |
| 755 |
/// use euclid::default::{Point2D, Size2D}; |
| 756 |
/// |
| 757 |
/// let p = Point2D::new(7.0, -7.0); |
| 758 |
/// let s = Size2D::new(4.0, -4.0); |
| 759 |
/// |
| 760 |
/// assert_eq!(p.rem_euclid(&s), point2(3.0, 1.0)); |
| 761 |
/// assert_eq!((-p).rem_euclid(&s), point2(1.0, 3.0)); |
| 762 |
/// assert_eq!(p.rem_euclid(&-s), point2(3.0, 1.0)); |
| 763 |
/// ``` |
| 764 |
#[inline] |
| 765 |
pub fn rem_euclid(&self, other: &Size2D<T, U>) -> Self { |
| 766 |
point2( |
| 767 |
self.x.rem_euclid(&other.width), |
| 768 |
self.y.rem_euclid(&other.height), |
| 769 |
) |
| 770 |
} |
| 771 |
|
| 772 |
/// Calculates Euclidean division, the matching method for `rem_euclid`. |
| 773 |
/// |
| 774 |
/// # Example |
| 775 |
/// |
| 776 |
/// ```rust |
| 777 |
/// use euclid::point2; |
| 778 |
/// use euclid::default::{Point2D, Size2D}; |
| 779 |
/// |
| 780 |
/// let p = Point2D::new(7.0, -7.0); |
| 781 |
/// let s = Size2D::new(4.0, -4.0); |
| 782 |
/// |
| 783 |
/// assert_eq!(p.div_euclid(&s), point2(1.0, 2.0)); |
| 784 |
/// assert_eq!((-p).div_euclid(&s), point2(-2.0, -1.0)); |
| 785 |
/// assert_eq!(p.div_euclid(&-s), point2(-1.0, -2.0)); |
| 786 |
/// ``` |
| 787 |
#[inline] |
| 788 |
pub fn div_euclid(&self, other: &Size2D<T, U>) -> Self { |
| 789 |
point2( |
| 790 |
self.x.div_euclid(&other.width), |
| 791 |
self.y.div_euclid(&other.height), |
| 792 |
) |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
impl<T, U> From<Point2D<T, U>> for [T; 2] { |
| 797 |
fn from(p: Point2D<T, U>) -> Self { |
| 798 |
[p.x, p.y] |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
impl<T, U> From<[T; 2]> for Point2D<T, U> { |
| 803 |
fn from([x, y]: [T; 2]) -> Self { |
| 804 |
point2(x, y) |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
impl<T, U> From<Point2D<T, U>> for (T, T) { |
| 809 |
fn from(p: Point2D<T, U>) -> Self { |
| 810 |
(p.x, p.y) |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
impl<T, U> From<(T, T)> for Point2D<T, U> { |
| 815 |
fn from(tuple: (T, T)) -> Self { |
| 816 |
point2(tuple.0, tuple.1) |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
/// A 3d Point tagged with a unit. |
| 821 |
#[repr(C)] |
| 822 |
pub struct Point3D<T, U> { |
| 823 |
pub x: T, |
| 824 |
pub y: T, |
| 825 |
pub z: T, |
| 826 |
#[doc(hidden)] |
| 827 |
pub _unit: PhantomData<U>, |
| 828 |
} |
| 829 |
|
| 830 |
mint_vec!(Point3D[x, y, z] = Point3); |
| 831 |
|
| 832 |
impl<T: Copy, U> Copy for Point3D<T, U> {} |
| 833 |
|
| 834 |
impl<T: Clone, U> Clone for Point3D<T, U> { |
| 835 |
fn clone(&self) -> Self { |
| 836 |
Point3D { |
| 837 |
x: self.x.clone(), |
| 838 |
y: self.y.clone(), |
| 839 |
z: self.z.clone(), |
| 840 |
_unit: PhantomData, |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
#[cfg(feature = "serde")] |
| 846 |
impl<'de, T, U> serde::Deserialize<'de> for Point3D<T, U> |
| 847 |
where |
| 848 |
T: serde::Deserialize<'de>, |
| 849 |
{ |
| 850 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 851 |
where |
| 852 |
D: serde::Deserializer<'de>, |
| 853 |
{ |
| 854 |
let (x, y, z) = serde::Deserialize::deserialize(deserializer)?; |
| 855 |
Ok(Point3D { |
| 856 |
x, |
| 857 |
y, |
| 858 |
z, |
| 859 |
_unit: PhantomData, |
| 860 |
}) |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
#[cfg(feature = "serde")] |
| 865 |
impl<T, U> serde::Serialize for Point3D<T, U> |
| 866 |
where |
| 867 |
T: serde::Serialize, |
| 868 |
{ |
| 869 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 870 |
where |
| 871 |
S: serde::Serializer, |
| 872 |
{ |
| 873 |
(&self.x, &self.y, &self.z).serialize(serializer) |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
#[cfg(feature = "bytemuck")] |
| 878 |
unsafe impl<T: Zeroable, U> Zeroable for Point3D<T, U> {} |
| 879 |
|
| 880 |
#[cfg(feature = "bytemuck")] |
| 881 |
unsafe impl<T: Pod, U: 'static> Pod for Point3D<T, U> {} |
| 882 |
|
| 883 |
impl<T, U> Eq for Point3D<T, U> where T: Eq {} |
| 884 |
|
| 885 |
impl<T, U> PartialEq for Point3D<T, U> |
| 886 |
where |
| 887 |
T: PartialEq, |
| 888 |
{ |
| 889 |
fn eq(&self, other: &Self) -> bool { |
| 890 |
self.x == other.x && self.y == other.y && self.z == other.z |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
impl<T, U> Hash for Point3D<T, U> |
| 895 |
where |
| 896 |
T: Hash, |
| 897 |
{ |
| 898 |
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
| 899 |
self.x.hash(h); |
| 900 |
self.y.hash(h); |
| 901 |
self.z.hash(h); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
impl<T: fmt::Debug, U> fmt::Debug for Point3D<T, U> { |
| 906 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 907 |
f.debug_tuple("") |
| 908 |
.field(&self.x) |
| 909 |
.field(&self.y) |
| 910 |
.field(&self.z) |
| 911 |
.finish() |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
impl<T: Default, U> Default for Point3D<T, U> { |
| 916 |
fn default() -> Self { |
| 917 |
Point3D::new(Default::default(), Default::default(), Default::default()) |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
impl<T, U> Point3D<T, U> { |
| 922 |
/// Constructor, setting all components to zero. |
| 923 |
#[inline] |
| 924 |
pub fn origin() -> Self |
| 925 |
where |
| 926 |
T: Zero, |
| 927 |
{ |
| 928 |
point3(Zero::zero(), Zero::zero(), Zero::zero()) |
| 929 |
} |
| 930 |
|
| 931 |
/// The same as [`origin()`](#method.origin). |
| 932 |
#[inline] |
| 933 |
pub fn zero() -> Self |
| 934 |
where |
| 935 |
T: Zero, |
| 936 |
{ |
| 937 |
Self::origin() |
| 938 |
} |
| 939 |
|
| 940 |
/// Constructor taking scalar values directly. |
| 941 |
#[inline] |
| 942 |
pub const fn new(x: T, y: T, z: T) -> Self { |
| 943 |
Point3D { |
| 944 |
x, |
| 945 |
y, |
| 946 |
z, |
| 947 |
_unit: PhantomData, |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
/// Constructor taking properly Lengths instead of scalar values. |
| 952 |
#[inline] |
| 953 |
pub fn from_lengths(x: Length<T, U>, y: Length<T, U>, z: Length<T, U>) -> Self { |
| 954 |
point3(x.0, y.0, z.0) |
| 955 |
} |
| 956 |
|
| 957 |
/// Constructor setting all components to the same value. |
| 958 |
#[inline] |
| 959 |
pub fn splat(v: T) -> Self |
| 960 |
where |
| 961 |
T: Clone, |
| 962 |
{ |
| 963 |
Point3D { |
| 964 |
x: v.clone(), |
| 965 |
y: v.clone(), |
| 966 |
z: v, |
| 967 |
_unit: PhantomData, |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
/// Tag a unitless value with units. |
| 972 |
#[inline] |
| 973 |
pub fn from_untyped(p: Point3D<T, UnknownUnit>) -> Self { |
| 974 |
point3(p.x, p.y, p.z) |
| 975 |
} |
| 976 |
|
| 977 |
/// Apply the function `f` to each component of this point. |
| 978 |
/// |
| 979 |
/// # Example |
| 980 |
/// |
| 981 |
/// This may be used to perform unusual arithmetic which is not already offered as methods. |
| 982 |
/// |
| 983 |
/// ``` |
| 984 |
/// use euclid::default::Point3D; |
| 985 |
/// |
| 986 |
/// let p = Point3D::<u32>::new(5, 11, 15); |
| 987 |
/// assert_eq!(p.map(|coord| coord.saturating_sub(10)), Point3D::new(0, 1, 5)); |
| 988 |
/// ``` |
| 989 |
#[inline] |
| 990 |
pub fn map<V, F: FnMut(T) -> V>(self, mut f: F) -> Point3D<V, U> { |
| 991 |
point3(f(self.x), f(self.y), f(self.z)) |
| 992 |
} |
| 993 |
|
| 994 |
/// Apply the function `f` to each pair of components of this point and `rhs`. |
| 995 |
/// |
| 996 |
/// # Example |
| 997 |
/// |
| 998 |
/// This may be used to perform unusual arithmetic which is not already offered as methods. |
| 999 |
/// |
| 1000 |
/// ``` |
| 1001 |
/// use euclid::{default::{Point3D, Vector3D}, point2}; |
| 1002 |
/// |
| 1003 |
/// let a: Point3D<u32> = Point3D::new(50, 200, 400); |
| 1004 |
/// let b: Point3D<u32> = Point3D::new(100, 100, 150); |
| 1005 |
/// assert_eq!(a.zip(b, u32::saturating_sub), Vector3D::new(0, 100, 250)); |
| 1006 |
/// ``` |
| 1007 |
#[inline] |
| 1008 |
pub fn zip<V, F: FnMut(T, T) -> V>(self, rhs: Self, mut f: F) -> Vector3D<V, U> { |
| 1009 |
vec3(f(self.x, rhs.x), f(self.y, rhs.y), f(self.z, rhs.z)) |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
impl<T: Copy, U> Point3D<T, U> { |
| 1014 |
/// Cast this point into a vector. |
| 1015 |
/// |
| 1016 |
/// Equivalent to subtracting the origin to this point. |
| 1017 |
#[inline] |
| 1018 |
pub fn to_vector(self) -> Vector3D<T, U> { |
| 1019 |
Vector3D { |
| 1020 |
x: self.x, |
| 1021 |
y: self.y, |
| 1022 |
z: self.z, |
| 1023 |
_unit: PhantomData, |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
/// Returns a 2d point using this point's x and y coordinates |
| 1028 |
#[inline] |
| 1029 |
pub fn xy(self) -> Point2D<T, U> { |
| 1030 |
point2(self.x, self.y) |
| 1031 |
} |
| 1032 |
|
| 1033 |
/// Returns a 2d point using this point's x and z coordinates |
| 1034 |
#[inline] |
| 1035 |
pub fn xz(self) -> Point2D<T, U> { |
| 1036 |
point2(self.x, self.z) |
| 1037 |
} |
| 1038 |
|
| 1039 |
/// Returns a 2d point using this point's x and z coordinates |
| 1040 |
#[inline] |
| 1041 |
pub fn yz(self) -> Point2D<T, U> { |
| 1042 |
point2(self.y, self.z) |
| 1043 |
} |
| 1044 |
|
| 1045 |
/// Cast into an array with x, y and z. |
| 1046 |
/// |
| 1047 |
/// # Example |
| 1048 |
/// |
| 1049 |
/// ```rust |
| 1050 |
/// # use euclid::{Point3D, point3}; |
| 1051 |
/// enum Mm {} |
| 1052 |
/// |
| 1053 |
/// let point: Point3D<_, Mm> = point3(1, -8, 0); |
| 1054 |
/// |
| 1055 |
/// assert_eq!(point.to_array(), [1, -8, 0]); |
| 1056 |
/// ``` |
| 1057 |
#[inline] |
| 1058 |
pub fn to_array(self) -> [T; 3] { |
| 1059 |
[self.x, self.y, self.z] |
| 1060 |
} |
| 1061 |
|
| 1062 |
#[inline] |
| 1063 |
pub fn to_array_4d(self) -> [T; 4] |
| 1064 |
where |
| 1065 |
T: One, |
| 1066 |
{ |
| 1067 |
[self.x, self.y, self.z, One::one()] |
| 1068 |
} |
| 1069 |
|
| 1070 |
/// Cast into a tuple with x, y and z. |
| 1071 |
/// |
| 1072 |
/// # Example |
| 1073 |
/// |
| 1074 |
/// ```rust |
| 1075 |
/// # use euclid::{Point3D, point3}; |
| 1076 |
/// enum Mm {} |
| 1077 |
/// |
| 1078 |
/// let point: Point3D<_, Mm> = point3(1, -8, 0); |
| 1079 |
/// |
| 1080 |
/// assert_eq!(point.to_tuple(), (1, -8, 0)); |
| 1081 |
/// ``` |
| 1082 |
#[inline] |
| 1083 |
pub fn to_tuple(self) -> (T, T, T) { |
| 1084 |
(self.x, self.y, self.z) |
| 1085 |
} |
| 1086 |
|
| 1087 |
#[inline] |
| 1088 |
pub fn to_tuple_4d(self) -> (T, T, T, T) |
| 1089 |
where |
| 1090 |
T: One, |
| 1091 |
{ |
| 1092 |
(self.x, self.y, self.z, One::one()) |
| 1093 |
} |
| 1094 |
|
| 1095 |
/// Drop the units, preserving only the numeric value. |
| 1096 |
/// |
| 1097 |
/// # Example |
| 1098 |
/// |
| 1099 |
/// ```rust |
| 1100 |
/// # use euclid::{Point3D, point3}; |
| 1101 |
/// enum Mm {} |
| 1102 |
/// |
| 1103 |
/// let point: Point3D<_, Mm> = point3(1, -8, 0); |
| 1104 |
/// |
| 1105 |
/// assert_eq!(point.x, point.to_untyped().x); |
| 1106 |
/// assert_eq!(point.y, point.to_untyped().y); |
| 1107 |
/// assert_eq!(point.z, point.to_untyped().z); |
| 1108 |
/// ``` |
| 1109 |
#[inline] |
| 1110 |
pub fn to_untyped(self) -> Point3D<T, UnknownUnit> { |
| 1111 |
point3(self.x, self.y, self.z) |
| 1112 |
} |
| 1113 |
|
| 1114 |
/// Cast the unit, preserving the numeric value. |
| 1115 |
/// |
| 1116 |
/// # Example |
| 1117 |
/// |
| 1118 |
/// ```rust |
| 1119 |
/// # use euclid::{Point3D, point3}; |
| 1120 |
/// enum Mm {} |
| 1121 |
/// enum Cm {} |
| 1122 |
/// |
| 1123 |
/// let point: Point3D<_, Mm> = point3(1, -8, 0); |
| 1124 |
/// |
| 1125 |
/// assert_eq!(point.x, point.cast_unit::<Cm>().x); |
| 1126 |
/// assert_eq!(point.y, point.cast_unit::<Cm>().y); |
| 1127 |
/// assert_eq!(point.z, point.cast_unit::<Cm>().z); |
| 1128 |
/// ``` |
| 1129 |
#[inline] |
| 1130 |
pub fn cast_unit<V>(self) -> Point3D<T, V> { |
| 1131 |
point3(self.x, self.y, self.z) |
| 1132 |
} |
| 1133 |
|
| 1134 |
/// Convert into a 2d point. |
| 1135 |
#[inline] |
| 1136 |
pub fn to_2d(self) -> Point2D<T, U> { |
| 1137 |
self.xy() |
| 1138 |
} |
| 1139 |
|
| 1140 |
/// Rounds each component to the nearest integer value. |
| 1141 |
/// |
| 1142 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 1143 |
/// |
| 1144 |
/// ```rust |
| 1145 |
/// # use euclid::point3; |
| 1146 |
/// enum Mm {} |
| 1147 |
/// |
| 1148 |
/// assert_eq!(point3::<_, Mm>(-0.1, -0.8, 0.4).round(), point3::<_, Mm>(0.0, -1.0, 0.0)) |
| 1149 |
/// ``` |
| 1150 |
#[inline] |
| 1151 |
#[must_use] |
| 1152 |
pub fn round(self) -> Self |
| 1153 |
where |
| 1154 |
T: Round, |
| 1155 |
{ |
| 1156 |
point3(self.x.round(), self.y.round(), self.z.round()) |
| 1157 |
} |
| 1158 |
|
| 1159 |
/// Rounds each component to the smallest integer equal or greater than the original value. |
| 1160 |
/// |
| 1161 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 1162 |
/// |
| 1163 |
/// ```rust |
| 1164 |
/// # use euclid::point3; |
| 1165 |
/// enum Mm {} |
| 1166 |
/// |
| 1167 |
/// assert_eq!(point3::<_, Mm>(-0.1, -0.8, 0.4).ceil(), point3::<_, Mm>(0.0, 0.0, 1.0)) |
| 1168 |
/// ``` |
| 1169 |
#[inline] |
| 1170 |
#[must_use] |
| 1171 |
pub fn ceil(self) -> Self |
| 1172 |
where |
| 1173 |
T: Ceil, |
| 1174 |
{ |
| 1175 |
point3(self.x.ceil(), self.y.ceil(), self.z.ceil()) |
| 1176 |
} |
| 1177 |
|
| 1178 |
/// Rounds each component to the biggest integer equal or lower than the original value. |
| 1179 |
/// |
| 1180 |
/// This behavior is preserved for negative values (unlike the basic cast). |
| 1181 |
/// |
| 1182 |
/// ```rust |
| 1183 |
/// # use euclid::point3; |
| 1184 |
/// enum Mm {} |
| 1185 |
/// |
| 1186 |
/// assert_eq!(point3::<_, Mm>(-0.1, -0.8, 0.4).floor(), point3::<_, Mm>(-1.0, -1.0, 0.0)) |
| 1187 |
/// ``` |
| 1188 |
#[inline] |
| 1189 |
#[must_use] |
| 1190 |
pub fn floor(self) -> Self |
| 1191 |
where |
| 1192 |
T: Floor, |
| 1193 |
{ |
| 1194 |
point3(self.x.floor(), self.y.floor(), self.z.floor()) |
| 1195 |
} |
| 1196 |
|
| 1197 |
/// Linearly interpolate between this point and another point. |
| 1198 |
/// |
| 1199 |
/// # Example |
| 1200 |
/// |
| 1201 |
/// ```rust |
| 1202 |
/// use euclid::point3; |
| 1203 |
/// use euclid::default::Point3D; |
| 1204 |
/// |
| 1205 |
/// let from: Point3D<_> = point3(0.0, 10.0, -1.0); |
| 1206 |
/// let to: Point3D<_> = point3(8.0, -4.0, 0.0); |
| 1207 |
/// |
| 1208 |
/// assert_eq!(from.lerp(to, -1.0), point3(-8.0, 24.0, -2.0)); |
| 1209 |
/// assert_eq!(from.lerp(to, 0.0), point3( 0.0, 10.0, -1.0)); |
| 1210 |
/// assert_eq!(from.lerp(to, 0.5), point3( 4.0, 3.0, -0.5)); |
| 1211 |
/// assert_eq!(from.lerp(to, 1.0), point3( 8.0, -4.0, 0.0)); |
| 1212 |
/// assert_eq!(from.lerp(to, 2.0), point3(16.0, -18.0, 1.0)); |
| 1213 |
/// ``` |
| 1214 |
#[inline] |
| 1215 |
pub fn lerp(self, other: Self, t: T) -> Self |
| 1216 |
where |
| 1217 |
T: One + Sub<Output = T> + Mul<Output = T> + Add<Output = T>, |
| 1218 |
{ |
| 1219 |
let one_t = T::one() - t; |
| 1220 |
point3( |
| 1221 |
one_t * self.x + t * other.x, |
| 1222 |
one_t * self.y + t * other.y, |
| 1223 |
one_t * self.z + t * other.z, |
| 1224 |
) |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
impl<T: PartialOrd, U> Point3D<T, U> { |
| 1229 |
#[inline] |
| 1230 |
pub fn min(self, other: Self) -> Self { |
| 1231 |
point3( |
| 1232 |
min(self.x, other.x), |
| 1233 |
min(self.y, other.y), |
| 1234 |
min(self.z, other.z), |
| 1235 |
) |
| 1236 |
} |
| 1237 |
|
| 1238 |
#[inline] |
| 1239 |
pub fn max(self, other: Self) -> Self { |
| 1240 |
point3( |
| 1241 |
max(self.x, other.x), |
| 1242 |
max(self.y, other.y), |
| 1243 |
max(self.z, other.z), |
| 1244 |
) |
| 1245 |
} |
| 1246 |
|
| 1247 |
/// Returns the point each component of which clamped by corresponding |
| 1248 |
/// components of `start` and `end`. |
| 1249 |
/// |
| 1250 |
/// Shortcut for `self.max(start).min(end)`. |
| 1251 |
#[inline] |
| 1252 |
pub fn clamp(self, start: Self, end: Self) -> Self |
| 1253 |
where |
| 1254 |
T: Copy, |
| 1255 |
{ |
| 1256 |
self.max(start).min(end) |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
impl<T: NumCast + Copy, U> Point3D<T, U> { |
| 1261 |
/// Cast from one numeric representation to another, preserving the units. |
| 1262 |
/// |
| 1263 |
/// When casting from floating point to integer coordinates, the decimals are truncated |
| 1264 |
/// as one would expect from a simple cast, but this behavior does not always make sense |
| 1265 |
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. |
| 1266 |
#[inline] |
| 1267 |
pub fn cast<NewT: NumCast>(self) -> Point3D<NewT, U> { |
| 1268 |
self.try_cast().unwrap() |
| 1269 |
} |
| 1270 |
|
| 1271 |
/// Fallible cast from one numeric representation to another, preserving the units. |
| 1272 |
/// |
| 1273 |
/// When casting from floating point to integer coordinates, the decimals are truncated |
| 1274 |
/// as one would expect from a simple cast, but this behavior does not always make sense |
| 1275 |
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. |
| 1276 |
pub fn try_cast<NewT: NumCast>(self) -> Option<Point3D<NewT, U>> { |
| 1277 |
match ( |
| 1278 |
NumCast::from(self.x), |
| 1279 |
NumCast::from(self.y), |
| 1280 |
NumCast::from(self.z), |
| 1281 |
) { |
| 1282 |
(Some(x), Some(y), Some(z)) => Some(point3(x, y, z)), |
| 1283 |
_ => None, |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
// Convenience functions for common casts |
| 1288 |
|
| 1289 |
/// Cast into an `f32` point. |
| 1290 |
#[inline] |
| 1291 |
pub fn to_f32(self) -> Point3D<f32, U> { |
| 1292 |
self.cast() |
| 1293 |
} |
| 1294 |
|
| 1295 |
/// Cast into an `f64` point. |
| 1296 |
#[inline] |
| 1297 |
pub fn to_f64(self) -> Point3D<f64, U> { |
| 1298 |
self.cast() |
| 1299 |
} |
| 1300 |
|
| 1301 |
/// Cast into an `usize` point, truncating decimals if any. |
| 1302 |
/// |
| 1303 |
/// When casting from floating point points, it is worth considering whether |
| 1304 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 1305 |
/// the desired conversion behavior. |
| 1306 |
#[inline] |
| 1307 |
pub fn to_usize(self) -> Point3D<usize, U> { |
| 1308 |
self.cast() |
| 1309 |
} |
| 1310 |
|
| 1311 |
/// Cast into an `u32` point, truncating decimals if any. |
| 1312 |
/// |
| 1313 |
/// When casting from floating point points, it is worth considering whether |
| 1314 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 1315 |
/// the desired conversion behavior. |
| 1316 |
#[inline] |
| 1317 |
pub fn to_u32(self) -> Point3D<u32, U> { |
| 1318 |
self.cast() |
| 1319 |
} |
| 1320 |
|
| 1321 |
/// Cast into an `i32` point, truncating decimals if any. |
| 1322 |
/// |
| 1323 |
/// When casting from floating point points, it is worth considering whether |
| 1324 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 1325 |
/// the desired conversion behavior. |
| 1326 |
#[inline] |
| 1327 |
pub fn to_i32(self) -> Point3D<i32, U> { |
| 1328 |
self.cast() |
| 1329 |
} |
| 1330 |
|
| 1331 |
/// Cast into an `i64` point, truncating decimals if any. |
| 1332 |
/// |
| 1333 |
/// When casting from floating point points, it is worth considering whether |
| 1334 |
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain |
| 1335 |
/// the desired conversion behavior. |
| 1336 |
#[inline] |
| 1337 |
pub fn to_i64(self) -> Point3D<i64, U> { |
| 1338 |
self.cast() |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
impl<T: Float, U> Point3D<T, U> { |
| 1343 |
/// Returns true if all members are finite. |
| 1344 |
#[inline] |
| 1345 |
pub fn is_finite(self) -> bool { |
| 1346 |
self.x.is_finite() && self.y.is_finite() && self.z.is_finite() |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
impl<T: Copy + Add<T, Output = T>, U> Point3D<T, U> { |
| 1351 |
#[inline] |
| 1352 |
pub fn add_size(self, other: Size3D<T, U>) -> Self { |
| 1353 |
point3( |
| 1354 |
self.x + other.width, |
| 1355 |
self.y + other.height, |
| 1356 |
self.z + other.depth, |
| 1357 |
) |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
impl<T: Real + Sub<T, Output = T>, U> Point3D<T, U> { |
| 1362 |
#[inline] |
| 1363 |
pub fn distance_to(self, other: Self) -> T { |
| 1364 |
(self - other).length() |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
impl<T: Neg, U> Neg for Point3D<T, U> { |
| 1369 |
type Output = Point3D<T::Output, U>; |
| 1370 |
|
| 1371 |
#[inline] |
| 1372 |
fn neg(self) -> Self::Output { |
| 1373 |
point3(-self.x, -self.y, -self.z) |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
impl<T: Add, U> Add<Size3D<T, U>> for Point3D<T, U> { |
| 1378 |
type Output = Point3D<T::Output, U>; |
| 1379 |
|
| 1380 |
#[inline] |
| 1381 |
fn add(self, other: Size3D<T, U>) -> Self::Output { |
| 1382 |
point3( |
| 1383 |
self.x + other.width, |
| 1384 |
self.y + other.height, |
| 1385 |
self.z + other.depth, |
| 1386 |
) |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
impl<T: AddAssign, U> AddAssign<Size3D<T, U>> for Point3D<T, U> { |
| 1391 |
#[inline] |
| 1392 |
fn add_assign(&mut self, other: Size3D<T, U>) { |
| 1393 |
self.x += other.width; |
| 1394 |
self.y += other.height; |
| 1395 |
self.z += other.depth; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
impl<T: Add, U> Add<Vector3D<T, U>> for Point3D<T, U> { |
| 1400 |
type Output = Point3D<T::Output, U>; |
| 1401 |
|
| 1402 |
#[inline] |
| 1403 |
fn add(self, other: Vector3D<T, U>) -> Self::Output { |
| 1404 |
point3(self.x + other.x, self.y + other.y, self.z + other.z) |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
impl<T: Copy + Add<T, Output = T>, U> AddAssign<Vector3D<T, U>> for Point3D<T, U> { |
| 1409 |
#[inline] |
| 1410 |
fn add_assign(&mut self, other: Vector3D<T, U>) { |
| 1411 |
*self = *self + other |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
impl<T: Sub, U> Sub for Point3D<T, U> { |
| 1416 |
type Output = Vector3D<T::Output, U>; |
| 1417 |
|
| 1418 |
#[inline] |
| 1419 |
fn sub(self, other: Self) -> Self::Output { |
| 1420 |
vec3(self.x - other.x, self.y - other.y, self.z - other.z) |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
impl<T: Sub, U> Sub<Size3D<T, U>> for Point3D<T, U> { |
| 1425 |
type Output = Point3D<T::Output, U>; |
| 1426 |
|
| 1427 |
#[inline] |
| 1428 |
fn sub(self, other: Size3D<T, U>) -> Self::Output { |
| 1429 |
point3( |
| 1430 |
self.x - other.width, |
| 1431 |
self.y - other.height, |
| 1432 |
self.z - other.depth, |
| 1433 |
) |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
impl<T: SubAssign, U> SubAssign<Size3D<T, U>> for Point3D<T, U> { |
| 1438 |
#[inline] |
| 1439 |
fn sub_assign(&mut self, other: Size3D<T, U>) { |
| 1440 |
self.x -= other.width; |
| 1441 |
self.y -= other.height; |
| 1442 |
self.z -= other.depth; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
impl<T: Sub, U> Sub<Vector3D<T, U>> for Point3D<T, U> { |
| 1447 |
type Output = Point3D<T::Output, U>; |
| 1448 |
|
| 1449 |
#[inline] |
| 1450 |
fn sub(self, other: Vector3D<T, U>) -> Self::Output { |
| 1451 |
point3(self.x - other.x, self.y - other.y, self.z - other.z) |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector3D<T, U>> for Point3D<T, U> { |
| 1456 |
#[inline] |
| 1457 |
fn sub_assign(&mut self, other: Vector3D<T, U>) { |
| 1458 |
*self = *self - other |
| 1459 |
} |
| 1460 |
} |
| 1461 |
|
| 1462 |
impl<T: Copy + Mul, U> Mul<T> for Point3D<T, U> { |
| 1463 |
type Output = Point3D<T::Output, U>; |
| 1464 |
|
| 1465 |
#[inline] |
| 1466 |
fn mul(self, scale: T) -> Self::Output { |
| 1467 |
point3(self.x * scale, self.y * scale, self.z * scale) |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
impl<T: Copy + MulAssign, U> MulAssign<T> for Point3D<T, U> { |
| 1472 |
#[inline] |
| 1473 |
fn mul_assign(&mut self, scale: T) { |
| 1474 |
self.x *= scale; |
| 1475 |
self.y *= scale; |
| 1476 |
self.z *= scale; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Point3D<T, U1> { |
| 1481 |
type Output = Point3D<T::Output, U2>; |
| 1482 |
|
| 1483 |
#[inline] |
| 1484 |
fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output { |
| 1485 |
point3(self.x * scale.0, self.y * scale.0, self.z * scale.0) |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Point3D<T, U> { |
| 1490 |
#[inline] |
| 1491 |
fn mul_assign(&mut self, scale: Scale<T, U, U>) { |
| 1492 |
*self *= scale.0; |
| 1493 |
} |
| 1494 |
} |
| 1495 |
|
| 1496 |
impl<T: Copy + Div, U> Div<T> for Point3D<T, U> { |
| 1497 |
type Output = Point3D<T::Output, U>; |
| 1498 |
|
| 1499 |
#[inline] |
| 1500 |
fn div(self, scale: T) -> Self::Output { |
| 1501 |
point3(self.x / scale, self.y / scale, self.z / scale) |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
impl<T: Copy + DivAssign, U> DivAssign<T> for Point3D<T, U> { |
| 1506 |
#[inline] |
| 1507 |
fn div_assign(&mut self, scale: T) { |
| 1508 |
self.x /= scale; |
| 1509 |
self.y /= scale; |
| 1510 |
self.z /= scale; |
| 1511 |
} |
| 1512 |
} |
| 1513 |
|
| 1514 |
impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Point3D<T, U2> { |
| 1515 |
type Output = Point3D<T::Output, U1>; |
| 1516 |
|
| 1517 |
#[inline] |
| 1518 |
fn div(self, scale: Scale<T, U1, U2>) -> Self::Output { |
| 1519 |
point3(self.x / scale.0, self.y / scale.0, self.z / scale.0) |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Point3D<T, U> { |
| 1524 |
#[inline] |
| 1525 |
fn div_assign(&mut self, scale: Scale<T, U, U>) { |
| 1526 |
*self /= scale.0; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
impl<T: Zero, U> Zero for Point3D<T, U> { |
| 1531 |
#[inline] |
| 1532 |
fn zero() -> Self { |
| 1533 |
Self::origin() |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
impl<T: Round, U> Round for Point3D<T, U> { |
| 1538 |
/// See [Point3D::round()](#method.round) |
| 1539 |
#[inline] |
| 1540 |
fn round(self) -> Self { |
| 1541 |
self.round() |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
impl<T: Ceil, U> Ceil for Point3D<T, U> { |
| 1546 |
/// See [Point3D::ceil()](#method.ceil) |
| 1547 |
#[inline] |
| 1548 |
fn ceil(self) -> Self { |
| 1549 |
self.ceil() |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
impl<T: Floor, U> Floor for Point3D<T, U> { |
| 1554 |
/// See [Point3D::floor()](#method.floor) |
| 1555 |
#[inline] |
| 1556 |
fn floor(self) -> Self { |
| 1557 |
self.floor() |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
impl<T: ApproxEq<T>, U> ApproxEq<Point3D<T, U>> for Point3D<T, U> { |
| 1562 |
#[inline] |
| 1563 |
fn approx_epsilon() -> Self { |
| 1564 |
point3( |
| 1565 |
T::approx_epsilon(), |
| 1566 |
T::approx_epsilon(), |
| 1567 |
T::approx_epsilon(), |
| 1568 |
) |
| 1569 |
} |
| 1570 |
|
| 1571 |
#[inline] |
| 1572 |
fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool { |
| 1573 |
self.x.approx_eq_eps(&other.x, &eps.x) |
| 1574 |
&& self.y.approx_eq_eps(&other.y, &eps.y) |
| 1575 |
&& self.z.approx_eq_eps(&other.z, &eps.z) |
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
impl<T: Euclid, U> Point3D<T, U> { |
| 1580 |
/// Calculates the least nonnegative remainder of `self (mod other)`. |
| 1581 |
/// |
| 1582 |
/// # Example |
| 1583 |
/// |
| 1584 |
/// ```rust |
| 1585 |
/// use euclid::point3; |
| 1586 |
/// use euclid::default::{Point3D, Size3D}; |
| 1587 |
/// |
| 1588 |
/// let p = Point3D::new(7.0, -7.0, 0.0); |
| 1589 |
/// let s = Size3D::new(4.0, -4.0, 12.0); |
| 1590 |
|
| 1591 |
/// assert_eq!(p.rem_euclid(&s), point3(3.0, 1.0, 0.0)); |
| 1592 |
/// assert_eq!((-p).rem_euclid(&s), point3(1.0, 3.0, 0.0)); |
| 1593 |
/// assert_eq!(p.rem_euclid(&-s), point3(3.0, 1.0, 0.0)); |
| 1594 |
/// ``` |
| 1595 |
#[inline] |
| 1596 |
pub fn rem_euclid(&self, other: &Size3D<T, U>) -> Self { |
| 1597 |
point3( |
| 1598 |
self.x.rem_euclid(&other.width), |
| 1599 |
self.y.rem_euclid(&other.height), |
| 1600 |
self.z.rem_euclid(&other.depth), |
| 1601 |
) |
| 1602 |
} |
| 1603 |
|
| 1604 |
/// Calculates Euclidean division, the matching method for `rem_euclid`. |
| 1605 |
/// |
| 1606 |
/// # Example |
| 1607 |
/// |
| 1608 |
/// ```rust |
| 1609 |
/// use euclid::point3; |
| 1610 |
/// use euclid::default::{Point3D, Size3D}; |
| 1611 |
/// |
| 1612 |
/// let p = Point3D::new(7.0, -7.0, 0.0); |
| 1613 |
/// let s = Size3D::new(4.0, -4.0, 12.0); |
| 1614 |
/// |
| 1615 |
/// assert_eq!(p.div_euclid(&s), point3(1.0, 2.0, 0.0)); |
| 1616 |
/// assert_eq!((-p).div_euclid(&s), point3(-2.0, -1.0, 0.0)); |
| 1617 |
/// assert_eq!(p.div_euclid(&-s), point3(-1.0, -2.0, 0.0)); |
| 1618 |
/// ``` |
| 1619 |
#[inline] |
| 1620 |
pub fn div_euclid(&self, other: &Size3D<T, U>) -> Self { |
| 1621 |
point3( |
| 1622 |
self.x.div_euclid(&other.width), |
| 1623 |
self.y.div_euclid(&other.height), |
| 1624 |
self.z.div_euclid(&other.depth), |
| 1625 |
) |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
impl<T, U> From<Point3D<T, U>> for [T; 3] { |
| 1630 |
fn from(p: Point3D<T, U>) -> Self { |
| 1631 |
[p.x, p.y, p.z] |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
impl<T, U> From<[T; 3]> for Point3D<T, U> { |
| 1636 |
fn from([x, y, z]: [T; 3]) -> Self { |
| 1637 |
point3(x, y, z) |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
impl<T, U> From<Point3D<T, U>> for (T, T, T) { |
| 1642 |
fn from(p: Point3D<T, U>) -> Self { |
| 1643 |
(p.x, p.y, p.z) |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
impl<T, U> From<(T, T, T)> for Point3D<T, U> { |
| 1648 |
fn from(tuple: (T, T, T)) -> Self { |
| 1649 |
point3(tuple.0, tuple.1, tuple.2) |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
/// Shorthand for `Point2D::new(x, y)`. |
| 1654 |
#[inline] |
| 1655 |
pub const fn point2<T, U>(x: T, y: T) -> Point2D<T, U> { |
| 1656 |
Point2D { |
| 1657 |
x, |
| 1658 |
y, |
| 1659 |
_unit: PhantomData, |
| 1660 |
} |
| 1661 |
} |
| 1662 |
|
| 1663 |
/// Shorthand for `Point3D::new(x, y)`. |
| 1664 |
#[inline] |
| 1665 |
pub const fn point3<T, U>(x: T, y: T, z: T) -> Point3D<T, U> { |
| 1666 |
Point3D { |
| 1667 |
x, |
| 1668 |
y, |
| 1669 |
z, |
| 1670 |
_unit: PhantomData, |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
#[cfg(test)] |
| 1675 |
mod point2d { |
| 1676 |
use crate::default::Point2D; |
| 1677 |
use crate::point2; |
| 1678 |
|
| 1679 |
#[cfg(feature = "mint")] |
| 1680 |
use mint; |
| 1681 |
|
| 1682 |
#[test] |
| 1683 |
pub fn test_min() { |
| 1684 |
let p1 = Point2D::new(1.0, 3.0); |
| 1685 |
let p2 = Point2D::new(2.0, 2.0); |
| 1686 |
|
| 1687 |
let result = p1.min(p2); |
| 1688 |
|
| 1689 |
assert_eq!(result, Point2D::new(1.0, 2.0)); |
| 1690 |
} |
| 1691 |
|
| 1692 |
#[test] |
| 1693 |
pub fn test_max() { |
| 1694 |
let p1 = Point2D::new(1.0, 3.0); |
| 1695 |
let p2 = Point2D::new(2.0, 2.0); |
| 1696 |
|
| 1697 |
let result = p1.max(p2); |
| 1698 |
|
| 1699 |
assert_eq!(result, Point2D::new(2.0, 3.0)); |
| 1700 |
} |
| 1701 |
|
| 1702 |
#[cfg(feature = "mint")] |
| 1703 |
#[test] |
| 1704 |
pub fn test_mint() { |
| 1705 |
let p1 = Point2D::new(1.0, 3.0); |
| 1706 |
let pm: mint::Point2<_> = p1.into(); |
| 1707 |
let p2 = Point2D::from(pm); |
| 1708 |
|
| 1709 |
assert_eq!(p1, p2); |
| 1710 |
} |
| 1711 |
|
| 1712 |
#[test] |
| 1713 |
pub fn test_conv_vector() { |
| 1714 |
for i in 0..100 { |
| 1715 |
// We don't care about these values as long as they are not the same. |
| 1716 |
let x = i as f32 * 0.012345; |
| 1717 |
let y = i as f32 * 0.987654; |
| 1718 |
let p: Point2D<f32> = point2(x, y); |
| 1719 |
assert_eq!(p.to_vector().to_point(), p); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
#[test] |
| 1724 |
pub fn test_swizzling() { |
| 1725 |
let p: Point2D<i32> = point2(1, 2); |
| 1726 |
assert_eq!(p.yx(), point2(2, 1)); |
| 1727 |
} |
| 1728 |
|
| 1729 |
#[test] |
| 1730 |
pub fn test_distance_to() { |
| 1731 |
let p1 = Point2D::new(1.0, 2.0); |
| 1732 |
let p2 = Point2D::new(2.0, 2.0); |
| 1733 |
|
| 1734 |
assert_eq!(p1.distance_to(p2), 1.0); |
| 1735 |
|
| 1736 |
let p1 = Point2D::new(1.0, 2.0); |
| 1737 |
let p2 = Point2D::new(1.0, 4.0); |
| 1738 |
|
| 1739 |
assert_eq!(p1.distance_to(p2), 2.0); |
| 1740 |
} |
| 1741 |
|
| 1742 |
mod ops { |
| 1743 |
use crate::default::Point2D; |
| 1744 |
use crate::scale::Scale; |
| 1745 |
use crate::{size2, vec2, Vector2D}; |
| 1746 |
|
| 1747 |
pub enum Mm {} |
| 1748 |
pub enum Cm {} |
| 1749 |
|
| 1750 |
pub type Point2DMm<T> = crate::Point2D<T, Mm>; |
| 1751 |
pub type Point2DCm<T> = crate::Point2D<T, Cm>; |
| 1752 |
|
| 1753 |
#[test] |
| 1754 |
pub fn test_neg() { |
| 1755 |
assert_eq!(-Point2D::new(1.0, 2.0), Point2D::new(-1.0, -2.0)); |
| 1756 |
assert_eq!(-Point2D::new(0.0, 0.0), Point2D::new(-0.0, -0.0)); |
| 1757 |
assert_eq!(-Point2D::new(-1.0, -2.0), Point2D::new(1.0, 2.0)); |
| 1758 |
} |
| 1759 |
|
| 1760 |
#[test] |
| 1761 |
pub fn test_add_size() { |
| 1762 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1763 |
let p2 = size2(3.0, 4.0); |
| 1764 |
|
| 1765 |
let result = p1 + p2; |
| 1766 |
|
| 1767 |
assert_eq!(result, Point2DMm::new(4.0, 6.0)); |
| 1768 |
} |
| 1769 |
|
| 1770 |
#[test] |
| 1771 |
pub fn test_add_assign_size() { |
| 1772 |
let mut p1 = Point2DMm::new(1.0, 2.0); |
| 1773 |
|
| 1774 |
p1 += size2(3.0, 4.0); |
| 1775 |
|
| 1776 |
assert_eq!(p1, Point2DMm::new(4.0, 6.0)); |
| 1777 |
} |
| 1778 |
|
| 1779 |
#[test] |
| 1780 |
pub fn test_add_vec() { |
| 1781 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1782 |
let p2 = vec2(3.0, 4.0); |
| 1783 |
|
| 1784 |
let result = p1 + p2; |
| 1785 |
|
| 1786 |
assert_eq!(result, Point2DMm::new(4.0, 6.0)); |
| 1787 |
} |
| 1788 |
|
| 1789 |
#[test] |
| 1790 |
pub fn test_add_assign_vec() { |
| 1791 |
let mut p1 = Point2DMm::new(1.0, 2.0); |
| 1792 |
|
| 1793 |
p1 += vec2(3.0, 4.0); |
| 1794 |
|
| 1795 |
assert_eq!(p1, Point2DMm::new(4.0, 6.0)); |
| 1796 |
} |
| 1797 |
|
| 1798 |
#[test] |
| 1799 |
pub fn test_sub() { |
| 1800 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1801 |
let p2 = Point2DMm::new(3.0, 4.0); |
| 1802 |
|
| 1803 |
let result = p1 - p2; |
| 1804 |
|
| 1805 |
assert_eq!(result, Vector2D::<_, Mm>::new(-2.0, -2.0)); |
| 1806 |
} |
| 1807 |
|
| 1808 |
#[test] |
| 1809 |
pub fn test_sub_size() { |
| 1810 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1811 |
let p2 = size2(3.0, 4.0); |
| 1812 |
|
| 1813 |
let result = p1 - p2; |
| 1814 |
|
| 1815 |
assert_eq!(result, Point2DMm::new(-2.0, -2.0)); |
| 1816 |
} |
| 1817 |
|
| 1818 |
#[test] |
| 1819 |
pub fn test_sub_assign_size() { |
| 1820 |
let mut p1 = Point2DMm::new(1.0, 2.0); |
| 1821 |
|
| 1822 |
p1 -= size2(3.0, 4.0); |
| 1823 |
|
| 1824 |
assert_eq!(p1, Point2DMm::new(-2.0, -2.0)); |
| 1825 |
} |
| 1826 |
|
| 1827 |
#[test] |
| 1828 |
pub fn test_sub_vec() { |
| 1829 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1830 |
let p2 = vec2(3.0, 4.0); |
| 1831 |
|
| 1832 |
let result = p1 - p2; |
| 1833 |
|
| 1834 |
assert_eq!(result, Point2DMm::new(-2.0, -2.0)); |
| 1835 |
} |
| 1836 |
|
| 1837 |
#[test] |
| 1838 |
pub fn test_sub_assign_vec() { |
| 1839 |
let mut p1 = Point2DMm::new(1.0, 2.0); |
| 1840 |
|
| 1841 |
p1 -= vec2(3.0, 4.0); |
| 1842 |
|
| 1843 |
assert_eq!(p1, Point2DMm::new(-2.0, -2.0)); |
| 1844 |
} |
| 1845 |
|
| 1846 |
#[test] |
| 1847 |
pub fn test_mul_scalar() { |
| 1848 |
let p1: Point2D<f32> = Point2D::new(3.0, 5.0); |
| 1849 |
|
| 1850 |
let result = p1 * 5.0; |
| 1851 |
|
| 1852 |
assert_eq!(result, Point2D::new(15.0, 25.0)); |
| 1853 |
} |
| 1854 |
|
| 1855 |
#[test] |
| 1856 |
pub fn test_mul_assign_scalar() { |
| 1857 |
let mut p1 = Point2D::new(3.0, 5.0); |
| 1858 |
|
| 1859 |
p1 *= 5.0; |
| 1860 |
|
| 1861 |
assert_eq!(p1, Point2D::new(15.0, 25.0)); |
| 1862 |
} |
| 1863 |
|
| 1864 |
#[test] |
| 1865 |
pub fn test_mul_scale() { |
| 1866 |
let p1 = Point2DMm::new(1.0, 2.0); |
| 1867 |
let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
| 1868 |
|
| 1869 |
let result = p1 * cm_per_mm; |
| 1870 |
|
| 1871 |
assert_eq!(result, Point2DCm::new(0.1, 0.2)); |
| 1872 |
} |
| 1873 |
|
| 1874 |
#[test] |
| 1875 |
pub fn test_mul_assign_scale() { |
| 1876 |
let mut p1 = Point2DMm::new(1.0, 2.0); |
| 1877 |
let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
| 1878 |
|
| 1879 |
p1 *= scale; |
| 1880 |
|
| 1881 |
assert_eq!(p1, Point2DMm::new(0.1, 0.2)); |
| 1882 |
} |
| 1883 |
|
| 1884 |
#[test] |
| 1885 |
pub fn test_div_scalar() { |
| 1886 |
let p1: Point2D<f32> = Point2D::new(15.0, 25.0); |
| 1887 |
|
| 1888 |
let result = p1 / 5.0; |
| 1889 |
|
| 1890 |
assert_eq!(result, Point2D::new(3.0, 5.0)); |
| 1891 |
} |
| 1892 |
|
| 1893 |
#[test] |
| 1894 |
pub fn test_div_assign_scalar() { |
| 1895 |
let mut p1: Point2D<f32> = Point2D::new(15.0, 25.0); |
| 1896 |
|
| 1897 |
p1 /= 5.0; |
| 1898 |
|
| 1899 |
assert_eq!(p1, Point2D::new(3.0, 5.0)); |
| 1900 |
} |
| 1901 |
|
| 1902 |
#[test] |
| 1903 |
pub fn test_div_scale() { |
| 1904 |
let p1 = Point2DCm::new(0.1, 0.2); |
| 1905 |
let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
| 1906 |
|
| 1907 |
let result = p1 / cm_per_mm; |
| 1908 |
|
| 1909 |
assert_eq!(result, Point2DMm::new(1.0, 2.0)); |
| 1910 |
} |
| 1911 |
|
| 1912 |
#[test] |
| 1913 |
pub fn test_div_assign_scale() { |
| 1914 |
let mut p1 = Point2DMm::new(0.1, 0.2); |
| 1915 |
let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
| 1916 |
|
| 1917 |
p1 /= scale; |
| 1918 |
|
| 1919 |
assert_eq!(p1, Point2DMm::new(1.0, 2.0)); |
| 1920 |
} |
| 1921 |
|
| 1922 |
#[test] |
| 1923 |
pub fn test_point_debug_formatting() { |
| 1924 |
let n = 1.23456789; |
| 1925 |
let p1 = Point2D::new(n, -n); |
| 1926 |
let should_be = format!("({:.4}, {:.4})", n, -n); |
| 1927 |
|
| 1928 |
let got = format!("{:.4?}", p1); |
| 1929 |
|
| 1930 |
assert_eq!(got, should_be); |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
mod euclid { |
| 1935 |
use crate::default::{Point2D, Size2D}; |
| 1936 |
use crate::point2; |
| 1937 |
|
| 1938 |
#[test] |
| 1939 |
pub fn test_rem_euclid() { |
| 1940 |
let p = Point2D::new(7.0, -7.0); |
| 1941 |
let s = Size2D::new(4.0, -4.0); |
| 1942 |
|
| 1943 |
assert_eq!(p.rem_euclid(&s), point2(3.0, 1.0)); |
| 1944 |
assert_eq!((-p).rem_euclid(&s), point2(1.0, 3.0)); |
| 1945 |
assert_eq!(p.rem_euclid(&-s), point2(3.0, 1.0)); |
| 1946 |
} |
| 1947 |
|
| 1948 |
#[test] |
| 1949 |
pub fn test_div_euclid() { |
| 1950 |
let p = Point2D::new(7.0, -7.0); |
| 1951 |
let s = Size2D::new(4.0, -4.0); |
| 1952 |
|
| 1953 |
assert_eq!(p.div_euclid(&s), point2(1.0, 2.0)); |
| 1954 |
assert_eq!((-p).div_euclid(&s), point2(-2.0, -1.0)); |
| 1955 |
assert_eq!(p.div_euclid(&-s), point2(-1.0, -2.0)); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
#[cfg(test)] |
| 1961 |
mod point3d { |
| 1962 |
use crate::default; |
| 1963 |
use crate::default::Point3D; |
| 1964 |
use crate::{point2, point3}; |
| 1965 |
#[cfg(feature = "mint")] |
| 1966 |
use mint; |
| 1967 |
|
| 1968 |
#[test] |
| 1969 |
pub fn test_min() { |
| 1970 |
let p1 = Point3D::new(1.0, 3.0, 5.0); |
| 1971 |
let p2 = Point3D::new(2.0, 2.0, -1.0); |
| 1972 |
|
| 1973 |
let result = p1.min(p2); |
| 1974 |
|
| 1975 |
assert_eq!(result, Point3D::new(1.0, 2.0, -1.0)); |
| 1976 |
} |
| 1977 |
|
| 1978 |
#[test] |
| 1979 |
pub fn test_max() { |
| 1980 |
let p1 = Point3D::new(1.0, 3.0, 5.0); |
| 1981 |
let p2 = Point3D::new(2.0, 2.0, -1.0); |
| 1982 |
|
| 1983 |
let result = p1.max(p2); |
| 1984 |
|
| 1985 |
assert_eq!(result, Point3D::new(2.0, 3.0, 5.0)); |
| 1986 |
} |
| 1987 |
|
| 1988 |
#[test] |
| 1989 |
pub fn test_conv_vector() { |
| 1990 |
use crate::point3; |
| 1991 |
for i in 0..100 { |
| 1992 |
// We don't care about these values as long as they are not the same. |
| 1993 |
let x = i as f32 * 0.012345; |
| 1994 |
let y = i as f32 * 0.987654; |
| 1995 |
let z = x * y; |
| 1996 |
let p: Point3D<f32> = point3(x, y, z); |
| 1997 |
assert_eq!(p.to_vector().to_point(), p); |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
#[test] |
| 2002 |
pub fn test_swizzling() { |
| 2003 |
let p: default::Point3D<i32> = point3(1, 2, 3); |
| 2004 |
assert_eq!(p.xy(), point2(1, 2)); |
| 2005 |
assert_eq!(p.xz(), point2(1, 3)); |
| 2006 |
assert_eq!(p.yz(), point2(2, 3)); |
| 2007 |
} |
| 2008 |
|
| 2009 |
#[test] |
| 2010 |
pub fn test_distance_to() { |
| 2011 |
let p1 = Point3D::new(1.0, 2.0, 3.0); |
| 2012 |
let p2 = Point3D::new(2.0, 2.0, 3.0); |
| 2013 |
|
| 2014 |
assert_eq!(p1.distance_to(p2), 1.0); |
| 2015 |
|
| 2016 |
let p1 = Point3D::new(1.0, 2.0, 3.0); |
| 2017 |
let p2 = Point3D::new(1.0, 4.0, 3.0); |
| 2018 |
|
| 2019 |
assert_eq!(p1.distance_to(p2), 2.0); |
| 2020 |
|
| 2021 |
let p1 = Point3D::new(1.0, 2.0, 3.0); |
| 2022 |
let p2 = Point3D::new(1.0, 2.0, 6.0); |
| 2023 |
|
| 2024 |
assert_eq!(p1.distance_to(p2), 3.0); |
| 2025 |
} |
| 2026 |
|
| 2027 |
#[cfg(feature = "mint")] |
| 2028 |
#[test] |
| 2029 |
pub fn test_mint() { |
| 2030 |
let p1 = Point3D::new(1.0, 3.0, 5.0); |
| 2031 |
let pm: mint::Point3<_> = p1.into(); |
| 2032 |
let p2 = Point3D::from(pm); |
| 2033 |
|
| 2034 |
assert_eq!(p1, p2); |
| 2035 |
} |
| 2036 |
|
| 2037 |
mod ops { |
| 2038 |
use crate::default::Point3D; |
| 2039 |
use crate::scale::Scale; |
| 2040 |
use crate::{size3, vec3, Vector3D}; |
| 2041 |
|
| 2042 |
pub enum Mm {} |
| 2043 |
pub enum Cm {} |
| 2044 |
|
| 2045 |
pub type Point3DMm<T> = crate::Point3D<T, Mm>; |
| 2046 |
pub type Point3DCm<T> = crate::Point3D<T, Cm>; |
| 2047 |
|
| 2048 |
#[test] |
| 2049 |
pub fn test_neg() { |
| 2050 |
assert_eq!(-Point3D::new(1.0, 2.0, 3.0), Point3D::new(-1.0, -2.0, -3.0)); |
| 2051 |
assert_eq!(-Point3D::new(0.0, 0.0, 0.0), Point3D::new(-0.0, -0.0, -0.0)); |
| 2052 |
assert_eq!(-Point3D::new(-1.0, -2.0, -3.0), Point3D::new(1.0, 2.0, 3.0)); |
| 2053 |
} |
| 2054 |
|
| 2055 |
#[test] |
| 2056 |
pub fn test_add_size() { |
| 2057 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2058 |
let p2 = size3(4.0, 5.0, 6.0); |
| 2059 |
|
| 2060 |
let result = p1 + p2; |
| 2061 |
|
| 2062 |
assert_eq!(result, Point3DMm::new(5.0, 7.0, 9.0)); |
| 2063 |
} |
| 2064 |
|
| 2065 |
#[test] |
| 2066 |
pub fn test_add_assign_size() { |
| 2067 |
let mut p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2068 |
|
| 2069 |
p1 += size3(4.0, 5.0, 6.0); |
| 2070 |
|
| 2071 |
assert_eq!(p1, Point3DMm::new(5.0, 7.0, 9.0)); |
| 2072 |
} |
| 2073 |
|
| 2074 |
#[test] |
| 2075 |
pub fn test_add_vec() { |
| 2076 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2077 |
let p2 = vec3(4.0, 5.0, 6.0); |
| 2078 |
|
| 2079 |
let result = p1 + p2; |
| 2080 |
|
| 2081 |
assert_eq!(result, Point3DMm::new(5.0, 7.0, 9.0)); |
| 2082 |
} |
| 2083 |
|
| 2084 |
#[test] |
| 2085 |
pub fn test_add_assign_vec() { |
| 2086 |
let mut p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2087 |
|
| 2088 |
p1 += vec3(4.0, 5.0, 6.0); |
| 2089 |
|
| 2090 |
assert_eq!(p1, Point3DMm::new(5.0, 7.0, 9.0)); |
| 2091 |
} |
| 2092 |
|
| 2093 |
#[test] |
| 2094 |
pub fn test_sub() { |
| 2095 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2096 |
let p2 = Point3DMm::new(4.0, 5.0, 6.0); |
| 2097 |
|
| 2098 |
let result = p1 - p2; |
| 2099 |
|
| 2100 |
assert_eq!(result, Vector3D::<_, Mm>::new(-3.0, -3.0, -3.0)); |
| 2101 |
} |
| 2102 |
|
| 2103 |
#[test] |
| 2104 |
pub fn test_sub_size() { |
| 2105 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2106 |
let p2 = size3(4.0, 5.0, 6.0); |
| 2107 |
|
| 2108 |
let result = p1 - p2; |
| 2109 |
|
| 2110 |
assert_eq!(result, Point3DMm::new(-3.0, -3.0, -3.0)); |
| 2111 |
} |
| 2112 |
|
| 2113 |
#[test] |
| 2114 |
pub fn test_sub_assign_size() { |
| 2115 |
let mut p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2116 |
|
| 2117 |
p1 -= size3(4.0, 5.0, 6.0); |
| 2118 |
|
| 2119 |
assert_eq!(p1, Point3DMm::new(-3.0, -3.0, -3.0)); |
| 2120 |
} |
| 2121 |
|
| 2122 |
#[test] |
| 2123 |
pub fn test_sub_vec() { |
| 2124 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2125 |
let p2 = vec3(4.0, 5.0, 6.0); |
| 2126 |
|
| 2127 |
let result = p1 - p2; |
| 2128 |
|
| 2129 |
assert_eq!(result, Point3DMm::new(-3.0, -3.0, -3.0)); |
| 2130 |
} |
| 2131 |
|
| 2132 |
#[test] |
| 2133 |
pub fn test_sub_assign_vec() { |
| 2134 |
let mut p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2135 |
|
| 2136 |
p1 -= vec3(4.0, 5.0, 6.0); |
| 2137 |
|
| 2138 |
assert_eq!(p1, Point3DMm::new(-3.0, -3.0, -3.0)); |
| 2139 |
} |
| 2140 |
|
| 2141 |
#[test] |
| 2142 |
pub fn test_mul_scalar() { |
| 2143 |
let p1: Point3D<f32> = Point3D::new(3.0, 5.0, 7.0); |
| 2144 |
|
| 2145 |
let result = p1 * 5.0; |
| 2146 |
|
| 2147 |
assert_eq!(result, Point3D::new(15.0, 25.0, 35.0)); |
| 2148 |
} |
| 2149 |
|
| 2150 |
#[test] |
| 2151 |
pub fn test_mul_assign_scalar() { |
| 2152 |
let mut p1: Point3D<f32> = Point3D::new(3.0, 5.0, 7.0); |
| 2153 |
|
| 2154 |
p1 *= 5.0; |
| 2155 |
|
| 2156 |
assert_eq!(p1, Point3D::new(15.0, 25.0, 35.0)); |
| 2157 |
} |
| 2158 |
|
| 2159 |
#[test] |
| 2160 |
pub fn test_mul_scale() { |
| 2161 |
let p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2162 |
let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
| 2163 |
|
| 2164 |
let result = p1 * cm_per_mm; |
| 2165 |
|
| 2166 |
assert_eq!(result, Point3DCm::new(0.1, 0.2, 0.3)); |
| 2167 |
} |
| 2168 |
|
| 2169 |
#[test] |
| 2170 |
pub fn test_mul_assign_scale() { |
| 2171 |
let mut p1 = Point3DMm::new(1.0, 2.0, 3.0); |
| 2172 |
let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
| 2173 |
|
| 2174 |
p1 *= scale; |
| 2175 |
|
| 2176 |
assert_eq!(p1, Point3DMm::new(0.1, 0.2, 0.3)); |
| 2177 |
} |
| 2178 |
|
| 2179 |
#[test] |
| 2180 |
pub fn test_div_scalar() { |
| 2181 |
let p1: Point3D<f32> = Point3D::new(15.0, 25.0, 35.0); |
| 2182 |
|
| 2183 |
let result = p1 / 5.0; |
| 2184 |
|
| 2185 |
assert_eq!(result, Point3D::new(3.0, 5.0, 7.0)); |
| 2186 |
} |
| 2187 |
|
| 2188 |
#[test] |
| 2189 |
pub fn test_div_assign_scalar() { |
| 2190 |
let mut p1: Point3D<f32> = Point3D::new(15.0, 25.0, 35.0); |
| 2191 |
|
| 2192 |
p1 /= 5.0; |
| 2193 |
|
| 2194 |
assert_eq!(p1, Point3D::new(3.0, 5.0, 7.0)); |
| 2195 |
} |
| 2196 |
|
| 2197 |
#[test] |
| 2198 |
pub fn test_div_scale() { |
| 2199 |
let p1 = Point3DCm::new(0.1, 0.2, 0.3); |
| 2200 |
let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
| 2201 |
|
| 2202 |
let result = p1 / cm_per_mm; |
| 2203 |
|
| 2204 |
assert_eq!(result, Point3DMm::new(1.0, 2.0, 3.0)); |
| 2205 |
} |
| 2206 |
|
| 2207 |
#[test] |
| 2208 |
pub fn test_div_assign_scale() { |
| 2209 |
let mut p1 = Point3DMm::new(0.1, 0.2, 0.3); |
| 2210 |
let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
| 2211 |
|
| 2212 |
p1 /= scale; |
| 2213 |
|
| 2214 |
assert_eq!(p1, Point3DMm::new(1.0, 2.0, 3.0)); |
| 2215 |
} |
| 2216 |
} |
| 2217 |
|
| 2218 |
mod euclid { |
| 2219 |
use crate::default::{Point3D, Size3D}; |
| 2220 |
use crate::point3; |
| 2221 |
|
| 2222 |
#[test] |
| 2223 |
pub fn test_rem_euclid() { |
| 2224 |
let p = Point3D::new(7.0, -7.0, 0.0); |
| 2225 |
let s = Size3D::new(4.0, -4.0, 12.0); |
| 2226 |
|
| 2227 |
assert_eq!(p.rem_euclid(&s), point3(3.0, 1.0, 0.0)); |
| 2228 |
assert_eq!((-p).rem_euclid(&s), point3(1.0, 3.0, 0.0)); |
| 2229 |
assert_eq!(p.rem_euclid(&-s), point3(3.0, 1.0, 0.0)); |
| 2230 |
} |
| 2231 |
|
| 2232 |
#[test] |
| 2233 |
pub fn test_div_euclid() { |
| 2234 |
let p = Point3D::new(7.0, -7.0, 0.0); |
| 2235 |
let s = Size3D::new(4.0, -4.0, 12.0); |
| 2236 |
|
| 2237 |
assert_eq!(p.div_euclid(&s), point3(1.0, 2.0, 0.0)); |
| 2238 |
assert_eq!((-p).div_euclid(&s), point3(-2.0, -1.0, 0.0)); |
| 2239 |
assert_eq!(p.div_euclid(&-s), point3(-1.0, -2.0, 0.0)); |
| 2240 |
} |
| 2241 |
} |
| 2242 |
} |