| 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 |
#![allow(clippy::just_underscores_and_digits)] |
| 11 |
|
| 12 |
use super::{Angle, UnknownUnit}; |
| 13 |
use crate::approxeq::ApproxEq; |
| 14 |
use crate::box2d::Box2D; |
| 15 |
use crate::num::{One, Zero}; |
| 16 |
use crate::point::{point2, Point2D}; |
| 17 |
use crate::rect::Rect; |
| 18 |
use crate::transform3d::Transform3D; |
| 19 |
use crate::trig::Trig; |
| 20 |
use crate::vector::{vec2, Vector2D}; |
| 21 |
use core::cmp::{Eq, PartialEq}; |
| 22 |
use core::fmt; |
| 23 |
use core::hash::Hash; |
| 24 |
use core::marker::PhantomData; |
| 25 |
use core::ops::{Add, Div, Mul, Sub}; |
| 26 |
|
| 27 |
#[cfg(feature = "bytemuck")] |
| 28 |
use bytemuck::{Pod, Zeroable}; |
| 29 |
#[cfg(feature = "mint")] |
| 30 |
use mint; |
| 31 |
use num_traits::NumCast; |
| 32 |
#[cfg(feature = "serde")] |
| 33 |
use serde::{Deserialize, Serialize}; |
| 34 |
|
| 35 |
/// A 2d transform represented by a column-major 3 by 3 matrix, compressed down to 3 by 2. |
| 36 |
/// |
| 37 |
/// Transforms can be parametrized over the source and destination units, to describe a |
| 38 |
/// transformation from a space to another. |
| 39 |
/// For example, `Transform2D<f32, WorldSpace, ScreenSpace>::transform_point4d` |
| 40 |
/// takes a `Point2D<f32, WorldSpace>` and returns a `Point2D<f32, ScreenSpace>`. |
| 41 |
/// |
| 42 |
/// Transforms expose a set of convenience methods for pre- and post-transformations. |
| 43 |
/// Pre-transformations (`pre_*` methods) correspond to adding an operation that is |
| 44 |
/// applied before the rest of the transformation, while post-transformations (`then_*` |
| 45 |
/// methods) add an operation that is applied after. |
| 46 |
/// |
| 47 |
/// The matrix representation is conceptually equivalent to a 3 by 3 matrix transformation |
| 48 |
/// compressed to 3 by 2 with the components that aren't needed to describe the set of 2d |
| 49 |
/// transformations we are interested in implicitly defined: |
| 50 |
/// |
| 51 |
/// ```text |
| 52 |
/// | m11 m21 m31 | |x| |x'| |
| 53 |
/// | m12 m22 m32 | x |y| = |y'| |
| 54 |
/// | 0 0 1 | |1| |1 | |
| 55 |
/// ``` |
| 56 |
/// |
| 57 |
/// When translating Transform2D into general matrix representations, consider that the |
| 58 |
/// representation follows the column-major notation with column vectors. |
| 59 |
/// |
| 60 |
/// The translation terms are m31 and m32. |
| 61 |
#[repr(C)] |
| 62 |
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 63 |
#[cfg_attr( |
| 64 |
feature = "serde", |
| 65 |
serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>")) |
| 66 |
)] |
| 67 |
#[rustfmt::skip] |
| 68 |
pub struct Transform2D<T, Src, Dst> { |
| 69 |
pub m11: T, pub m12: T, |
| 70 |
pub m21: T, pub m22: T, |
| 71 |
pub m31: T, pub m32: T, |
| 72 |
#[doc(hidden)] |
| 73 |
pub _unit: PhantomData<(Src, Dst)>, |
| 74 |
} |
| 75 |
|
| 76 |
#[cfg(feature = "arbitrary")] |
| 77 |
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Transform2D<T, Src, Dst> |
| 78 |
where |
| 79 |
T: arbitrary::Arbitrary<'a>, |
| 80 |
{ |
| 81 |
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { |
| 82 |
let (m11, m12, m21, m22, m31, m32) = arbitrary::Arbitrary::arbitrary(u)?; |
| 83 |
Ok(Transform2D { |
| 84 |
m11, |
| 85 |
m12, |
| 86 |
m21, |
| 87 |
m22, |
| 88 |
m31, |
| 89 |
m32, |
| 90 |
_unit: PhantomData, |
| 91 |
}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(feature = "bytemuck")] |
| 96 |
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Transform2D<T, Src, Dst> {} |
| 97 |
|
| 98 |
#[cfg(feature = "bytemuck")] |
| 99 |
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Transform2D<T, Src, Dst> {} |
| 100 |
|
| 101 |
impl<T: Copy, Src, Dst> Copy for Transform2D<T, Src, Dst> {} |
| 102 |
|
| 103 |
impl<T: Clone, Src, Dst> Clone for Transform2D<T, Src, Dst> { |
| 104 |
fn clone(&self) -> Self { |
| 105 |
Transform2D { |
| 106 |
m11: self.m11.clone(), |
| 107 |
m12: self.m12.clone(), |
| 108 |
m21: self.m21.clone(), |
| 109 |
m22: self.m22.clone(), |
| 110 |
m31: self.m31.clone(), |
| 111 |
m32: self.m32.clone(), |
| 112 |
_unit: PhantomData, |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl<T, Src, Dst> Eq for Transform2D<T, Src, Dst> where T: Eq {} |
| 118 |
|
| 119 |
impl<T, Src, Dst> PartialEq for Transform2D<T, Src, Dst> |
| 120 |
where |
| 121 |
T: PartialEq, |
| 122 |
{ |
| 123 |
fn eq(&self, other: &Self) -> bool { |
| 124 |
self.m11 == other.m11 |
| 125 |
&& self.m12 == other.m12 |
| 126 |
&& self.m21 == other.m21 |
| 127 |
&& self.m22 == other.m22 |
| 128 |
&& self.m31 == other.m31 |
| 129 |
&& self.m32 == other.m32 |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl<T, Src, Dst> Hash for Transform2D<T, Src, Dst> |
| 134 |
where |
| 135 |
T: Hash, |
| 136 |
{ |
| 137 |
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
| 138 |
self.m11.hash(h); |
| 139 |
self.m12.hash(h); |
| 140 |
self.m21.hash(h); |
| 141 |
self.m22.hash(h); |
| 142 |
self.m31.hash(h); |
| 143 |
self.m32.hash(h); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> { |
| 148 |
/// Create a transform specifying its components in using the column-major-column-vector |
| 149 |
/// matrix notation. |
| 150 |
/// |
| 151 |
/// For example, the translation terms m31 and m32 are the last two parameters parameters. |
| 152 |
/// |
| 153 |
/// ``` |
| 154 |
/// use euclid::default::Transform2D; |
| 155 |
/// let tx = 1.0; |
| 156 |
/// let ty = 2.0; |
| 157 |
/// let translation = Transform2D::new( |
| 158 |
/// 1.0, 0.0, |
| 159 |
/// 0.0, 1.0, |
| 160 |
/// tx, ty, |
| 161 |
/// ); |
| 162 |
/// ``` |
| 163 |
#[rustfmt::skip] |
| 164 |
pub const fn new(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> Self { |
| 165 |
Transform2D { |
| 166 |
m11, m12, |
| 167 |
m21, m22, |
| 168 |
m31, m32, |
| 169 |
_unit: PhantomData, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/// Returns true is this transform is approximately equal to the other one, using |
| 174 |
/// T's default epsilon value. |
| 175 |
/// |
| 176 |
/// The same as [`ApproxEq::approx_eq()`] but available without importing trait. |
| 177 |
/// |
| 178 |
/// [`ApproxEq::approx_eq()`]: ./approxeq/trait.ApproxEq.html#method.approx_eq |
| 179 |
#[inline] |
| 180 |
pub fn approx_eq(&self, other: &Self) -> bool |
| 181 |
where |
| 182 |
T: ApproxEq<T>, |
| 183 |
{ |
| 184 |
<Self as ApproxEq<T>>::approx_eq(&self, &other) |
| 185 |
} |
| 186 |
|
| 187 |
/// Returns true is this transform is approximately equal to the other one, using |
| 188 |
/// a provided epsilon value. |
| 189 |
/// |
| 190 |
/// The same as [`ApproxEq::approx_eq_eps()`] but available without importing trait. |
| 191 |
/// |
| 192 |
/// [`ApproxEq::approx_eq_eps()`]: ./approxeq/trait.ApproxEq.html#method.approx_eq_eps |
| 193 |
#[inline] |
| 194 |
pub fn approx_eq_eps(&self, other: &Self, eps: &T) -> bool |
| 195 |
where |
| 196 |
T: ApproxEq<T>, |
| 197 |
{ |
| 198 |
<Self as ApproxEq<T>>::approx_eq_eps(&self, &other, &eps) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl<T: Copy, Src, Dst> Transform2D<T, Src, Dst> { |
| 203 |
/// Returns an array containing this transform's terms. |
| 204 |
/// |
| 205 |
/// The terms are laid out in the same order as they are |
| 206 |
/// specified in `Transform2D::new`, that is following the |
| 207 |
/// column-major-column-vector matrix notation. |
| 208 |
/// |
| 209 |
/// For example the translation terms are found in the |
| 210 |
/// last two slots of the array. |
| 211 |
#[inline] |
| 212 |
#[rustfmt::skip] |
| 213 |
pub fn to_array(&self) -> [T; 6] { |
| 214 |
[ |
| 215 |
self.m11, self.m12, |
| 216 |
self.m21, self.m22, |
| 217 |
self.m31, self.m32 |
| 218 |
] |
| 219 |
} |
| 220 |
|
| 221 |
/// Returns an array containing this transform's terms transposed. |
| 222 |
/// |
| 223 |
/// The terms are laid out in transposed order from the same order of |
| 224 |
/// `Transform3D::new` and `Transform3D::to_array`, that is following |
| 225 |
/// the row-major-column-vector matrix notation. |
| 226 |
/// |
| 227 |
/// For example the translation terms are found at indices 2 and 5 |
| 228 |
/// in the array. |
| 229 |
#[inline] |
| 230 |
#[rustfmt::skip] |
| 231 |
pub fn to_array_transposed(&self) -> [T; 6] { |
| 232 |
[ |
| 233 |
self.m11, self.m21, self.m31, |
| 234 |
self.m12, self.m22, self.m32 |
| 235 |
] |
| 236 |
} |
| 237 |
|
| 238 |
/// Equivalent to `to_array` with elements packed two at a time |
| 239 |
/// in an array of arrays. |
| 240 |
#[inline] |
| 241 |
pub fn to_arrays(&self) -> [[T; 2]; 3] { |
| 242 |
[ |
| 243 |
[self.m11, self.m12], |
| 244 |
[self.m21, self.m22], |
| 245 |
[self.m31, self.m32], |
| 246 |
] |
| 247 |
} |
| 248 |
|
| 249 |
/// Create a transform providing its components via an array |
| 250 |
/// of 6 elements instead of as individual parameters. |
| 251 |
/// |
| 252 |
/// The order of the components corresponds to the |
| 253 |
/// column-major-column-vector matrix notation (the same order |
| 254 |
/// as `Transform2D::new`). |
| 255 |
#[inline] |
| 256 |
#[rustfmt::skip] |
| 257 |
pub fn from_array(array: [T; 6]) -> Self { |
| 258 |
Self::new( |
| 259 |
array[0], array[1], |
| 260 |
array[2], array[3], |
| 261 |
array[4], array[5], |
| 262 |
) |
| 263 |
} |
| 264 |
|
| 265 |
/// Equivalent to `from_array` with elements packed two at a time |
| 266 |
/// in an array of arrays. |
| 267 |
/// |
| 268 |
/// The order of the components corresponds to the |
| 269 |
/// column-major-column-vector matrix notation (the same order |
| 270 |
/// as `Transform3D::new`). |
| 271 |
#[inline] |
| 272 |
#[rustfmt::skip] |
| 273 |
pub fn from_arrays(array: [[T; 2]; 3]) -> Self { |
| 274 |
Self::new( |
| 275 |
array[0][0], array[0][1], |
| 276 |
array[1][0], array[1][1], |
| 277 |
array[2][0], array[2][1], |
| 278 |
) |
| 279 |
} |
| 280 |
|
| 281 |
/// Drop the units, preserving only the numeric value. |
| 282 |
#[inline] |
| 283 |
#[rustfmt::skip] |
| 284 |
pub fn to_untyped(&self) -> Transform2D<T, UnknownUnit, UnknownUnit> { |
| 285 |
Transform2D::new( |
| 286 |
self.m11, self.m12, |
| 287 |
self.m21, self.m22, |
| 288 |
self.m31, self.m32 |
| 289 |
) |
| 290 |
} |
| 291 |
|
| 292 |
/// Tag a unitless value with units. |
| 293 |
#[inline] |
| 294 |
#[rustfmt::skip] |
| 295 |
pub fn from_untyped(p: &Transform2D<T, UnknownUnit, UnknownUnit>) -> Self { |
| 296 |
Transform2D::new( |
| 297 |
p.m11, p.m12, |
| 298 |
p.m21, p.m22, |
| 299 |
p.m31, p.m32 |
| 300 |
) |
| 301 |
} |
| 302 |
|
| 303 |
/// Returns the same transform with a different source unit. |
| 304 |
#[inline] |
| 305 |
#[rustfmt::skip] |
| 306 |
pub fn with_source<NewSrc>(&self) -> Transform2D<T, NewSrc, Dst> { |
| 307 |
Transform2D::new( |
| 308 |
self.m11, self.m12, |
| 309 |
self.m21, self.m22, |
| 310 |
self.m31, self.m32, |
| 311 |
) |
| 312 |
} |
| 313 |
|
| 314 |
/// Returns the same transform with a different destination unit. |
| 315 |
#[inline] |
| 316 |
#[rustfmt::skip] |
| 317 |
pub fn with_destination<NewDst>(&self) -> Transform2D<T, Src, NewDst> { |
| 318 |
Transform2D::new( |
| 319 |
self.m11, self.m12, |
| 320 |
self.m21, self.m22, |
| 321 |
self.m31, self.m32, |
| 322 |
) |
| 323 |
} |
| 324 |
|
| 325 |
/// Create a 3D transform from the current transform |
| 326 |
pub fn to_3d(&self) -> Transform3D<T, Src, Dst> |
| 327 |
where |
| 328 |
T: Zero + One, |
| 329 |
{ |
| 330 |
Transform3D::new_2d(self.m11, self.m12, self.m21, self.m22, self.m31, self.m32) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
impl<T: NumCast + Copy, Src, Dst> Transform2D<T, Src, Dst> { |
| 335 |
/// Cast from one numeric representation to another, preserving the units. |
| 336 |
#[inline] |
| 337 |
pub fn cast<NewT: NumCast>(&self) -> Transform2D<NewT, Src, Dst> { |
| 338 |
self.try_cast().unwrap() |
| 339 |
} |
| 340 |
|
| 341 |
/// Fallible cast from one numeric representation to another, preserving the units. |
| 342 |
#[rustfmt::skip] |
| 343 |
pub fn try_cast<NewT: NumCast>(&self) -> Option<Transform2D<NewT, Src, Dst>> { |
| 344 |
match (NumCast::from(self.m11), NumCast::from(self.m12), |
| 345 |
NumCast::from(self.m21), NumCast::from(self.m22), |
| 346 |
NumCast::from(self.m31), NumCast::from(self.m32)) { |
| 347 |
(Some(m11), Some(m12), |
| 348 |
Some(m21), Some(m22), |
| 349 |
Some(m31), Some(m32)) => { |
| 350 |
Some(Transform2D::new( |
| 351 |
m11, m12, |
| 352 |
m21, m22, |
| 353 |
m31, m32 |
| 354 |
)) |
| 355 |
}, |
| 356 |
_ => None |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 362 |
where |
| 363 |
T: Zero + One, |
| 364 |
{ |
| 365 |
/// Create an identity matrix: |
| 366 |
/// |
| 367 |
/// ```text |
| 368 |
/// 1 0 |
| 369 |
/// 0 1 |
| 370 |
/// 0 0 |
| 371 |
/// ``` |
| 372 |
#[inline] |
| 373 |
pub fn identity() -> Self { |
| 374 |
Self::translation(T::zero(), T::zero()) |
| 375 |
} |
| 376 |
|
| 377 |
/// Intentional not public, because it checks for exact equivalence |
| 378 |
/// while most consumers will probably want some sort of approximate |
| 379 |
/// equivalence to deal with floating-point errors. |
| 380 |
fn is_identity(&self) -> bool |
| 381 |
where |
| 382 |
T: PartialEq, |
| 383 |
{ |
| 384 |
*self == Self::identity() |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/// Methods for combining generic transformations |
| 389 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 390 |
where |
| 391 |
T: Copy + Add<Output = T> + Mul<Output = T>, |
| 392 |
{ |
| 393 |
/// Returns the multiplication of the two matrices such that mat's transformation |
| 394 |
/// applies after self's transformation. |
| 395 |
#[must_use] |
| 396 |
#[rustfmt::skip] |
| 397 |
pub fn then<NewDst>(&self, mat: &Transform2D<T, Dst, NewDst>) -> Transform2D<T, Src, NewDst> { |
| 398 |
Transform2D::new( |
| 399 |
self.m11 * mat.m11 + self.m12 * mat.m21, |
| 400 |
self.m11 * mat.m12 + self.m12 * mat.m22, |
| 401 |
|
| 402 |
self.m21 * mat.m11 + self.m22 * mat.m21, |
| 403 |
self.m21 * mat.m12 + self.m22 * mat.m22, |
| 404 |
|
| 405 |
self.m31 * mat.m11 + self.m32 * mat.m21 + mat.m31, |
| 406 |
self.m31 * mat.m12 + self.m32 * mat.m22 + mat.m32, |
| 407 |
) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/// Methods for creating and combining translation transformations |
| 412 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 413 |
where |
| 414 |
T: Zero + One, |
| 415 |
{ |
| 416 |
/// Create a 2d translation transform: |
| 417 |
/// |
| 418 |
/// ```text |
| 419 |
/// 1 0 |
| 420 |
/// 0 1 |
| 421 |
/// x y |
| 422 |
/// ``` |
| 423 |
#[inline] |
| 424 |
#[rustfmt::skip] |
| 425 |
pub fn translation(x: T, y: T) -> Self { |
| 426 |
let _0 = || T::zero(); |
| 427 |
let _1 = || T::one(); |
| 428 |
|
| 429 |
Self::new( |
| 430 |
_1(), _0(), |
| 431 |
_0(), _1(), |
| 432 |
x, y, |
| 433 |
) |
| 434 |
} |
| 435 |
|
| 436 |
/// Applies a translation after self's transformation and returns the resulting transform. |
| 437 |
#[inline] |
| 438 |
#[must_use] |
| 439 |
pub fn then_translate(&self, v: Vector2D<T, Dst>) -> Self |
| 440 |
where |
| 441 |
T: Copy + Add<Output = T> + Mul<Output = T>, |
| 442 |
{ |
| 443 |
self.then(&Transform2D::translation(v.x, v.y)) |
| 444 |
} |
| 445 |
|
| 446 |
/// Applies a translation before self's transformation and returns the resulting transform. |
| 447 |
#[inline] |
| 448 |
#[must_use] |
| 449 |
pub fn pre_translate(&self, v: Vector2D<T, Src>) -> Self |
| 450 |
where |
| 451 |
T: Copy + Add<Output = T> + Mul<Output = T>, |
| 452 |
{ |
| 453 |
Transform2D::translation(v.x, v.y).then(self) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
/// Methods for creating and combining rotation transformations |
| 458 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 459 |
where |
| 460 |
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Zero + Trig, |
| 461 |
{ |
| 462 |
/// Returns a rotation transform. |
| 463 |
#[inline] |
| 464 |
#[rustfmt::skip] |
| 465 |
pub fn rotation(theta: Angle<T>) -> Self { |
| 466 |
let _0 = Zero::zero(); |
| 467 |
let cos = theta.get().cos(); |
| 468 |
let sin = theta.get().sin(); |
| 469 |
Transform2D::new( |
| 470 |
cos, sin, |
| 471 |
_0 - sin, cos, |
| 472 |
_0, _0 |
| 473 |
) |
| 474 |
} |
| 475 |
|
| 476 |
/// Applies a rotation after self's transformation and returns the resulting transform. |
| 477 |
#[inline] |
| 478 |
#[must_use] |
| 479 |
pub fn then_rotate(&self, theta: Angle<T>) -> Self { |
| 480 |
self.then(&Transform2D::rotation(theta)) |
| 481 |
} |
| 482 |
|
| 483 |
/// Applies a rotation before self's transformation and returns the resulting transform. |
| 484 |
#[inline] |
| 485 |
#[must_use] |
| 486 |
pub fn pre_rotate(&self, theta: Angle<T>) -> Self { |
| 487 |
Transform2D::rotation(theta).then(self) |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
/// Methods for creating and combining scale transformations |
| 492 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> { |
| 493 |
/// Create a 2d scale transform: |
| 494 |
/// |
| 495 |
/// ```text |
| 496 |
/// x 0 |
| 497 |
/// 0 y |
| 498 |
/// 0 0 |
| 499 |
/// ``` |
| 500 |
#[inline] |
| 501 |
#[rustfmt::skip] |
| 502 |
pub fn scale(x: T, y: T) -> Self |
| 503 |
where |
| 504 |
T: Zero, |
| 505 |
{ |
| 506 |
let _0 = || Zero::zero(); |
| 507 |
|
| 508 |
Self::new( |
| 509 |
x, _0(), |
| 510 |
_0(), y, |
| 511 |
_0(), _0(), |
| 512 |
) |
| 513 |
} |
| 514 |
|
| 515 |
/// Applies a scale after self's transformation and returns the resulting transform. |
| 516 |
#[inline] |
| 517 |
#[must_use] |
| 518 |
pub fn then_scale(&self, x: T, y: T) -> Self |
| 519 |
where |
| 520 |
T: Copy + Add<Output = T> + Mul<Output = T> + Zero, |
| 521 |
{ |
| 522 |
self.then(&Transform2D::scale(x, y)) |
| 523 |
} |
| 524 |
|
| 525 |
/// Applies a scale before self's transformation and returns the resulting transform. |
| 526 |
#[inline] |
| 527 |
#[must_use] |
| 528 |
#[rustfmt::skip] |
| 529 |
pub fn pre_scale(&self, x: T, y: T) -> Self |
| 530 |
where |
| 531 |
T: Copy + Mul<Output = T>, |
| 532 |
{ |
| 533 |
Transform2D::new( |
| 534 |
self.m11 * x, self.m12 * x, |
| 535 |
self.m21 * y, self.m22 * y, |
| 536 |
self.m31, self.m32 |
| 537 |
) |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/// Methods for apply transformations to objects |
| 542 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 543 |
where |
| 544 |
T: Copy + Add<Output = T> + Mul<Output = T>, |
| 545 |
{ |
| 546 |
/// Returns the given point transformed by this transform. |
| 547 |
#[inline] |
| 548 |
#[must_use] |
| 549 |
pub fn transform_point(&self, point: Point2D<T, Src>) -> Point2D<T, Dst> { |
| 550 |
Point2D::new( |
| 551 |
point.x * self.m11 + point.y * self.m21 + self.m31, |
| 552 |
point.x * self.m12 + point.y * self.m22 + self.m32, |
| 553 |
) |
| 554 |
} |
| 555 |
|
| 556 |
/// Returns the given vector transformed by this matrix. |
| 557 |
#[inline] |
| 558 |
#[must_use] |
| 559 |
pub fn transform_vector(&self, vec: Vector2D<T, Src>) -> Vector2D<T, Dst> { |
| 560 |
vec2( |
| 561 |
vec.x * self.m11 + vec.y * self.m21, |
| 562 |
vec.x * self.m12 + vec.y * self.m22, |
| 563 |
) |
| 564 |
} |
| 565 |
|
| 566 |
/// Returns a rectangle that encompasses the result of transforming the given rectangle by this |
| 567 |
/// transform. |
| 568 |
#[inline] |
| 569 |
#[must_use] |
| 570 |
pub fn outer_transformed_rect(&self, rect: &Rect<T, Src>) -> Rect<T, Dst> |
| 571 |
where |
| 572 |
T: Sub<Output = T> + Zero + PartialOrd, |
| 573 |
{ |
| 574 |
let min = rect.min(); |
| 575 |
let max = rect.max(); |
| 576 |
Rect::from_points(&[ |
| 577 |
self.transform_point(min), |
| 578 |
self.transform_point(max), |
| 579 |
self.transform_point(point2(max.x, min.y)), |
| 580 |
self.transform_point(point2(min.x, max.y)), |
| 581 |
]) |
| 582 |
} |
| 583 |
|
| 584 |
/// Returns a box that encompasses the result of transforming the given box by this |
| 585 |
/// transform. |
| 586 |
#[inline] |
| 587 |
#[must_use] |
| 588 |
pub fn outer_transformed_box(&self, b: &Box2D<T, Src>) -> Box2D<T, Dst> |
| 589 |
where |
| 590 |
T: Sub<Output = T> + Zero + PartialOrd, |
| 591 |
{ |
| 592 |
Box2D::from_points(&[ |
| 593 |
self.transform_point(b.min), |
| 594 |
self.transform_point(b.max), |
| 595 |
self.transform_point(point2(b.max.x, b.min.y)), |
| 596 |
self.transform_point(point2(b.min.x, b.max.y)), |
| 597 |
]) |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
impl<T, Src, Dst> Transform2D<T, Src, Dst> |
| 602 |
where |
| 603 |
T: Copy + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + PartialEq + Zero + One, |
| 604 |
{ |
| 605 |
/// Computes and returns the determinant of this transform. |
| 606 |
pub fn determinant(&self) -> T { |
| 607 |
self.m11 * self.m22 - self.m12 * self.m21 |
| 608 |
} |
| 609 |
|
| 610 |
/// Returns whether it is possible to compute the inverse transform. |
| 611 |
#[inline] |
| 612 |
pub fn is_invertible(&self) -> bool { |
| 613 |
self.determinant() != Zero::zero() |
| 614 |
} |
| 615 |
|
| 616 |
/// Returns the inverse transform if possible. |
| 617 |
#[must_use] |
| 618 |
pub fn inverse(&self) -> Option<Transform2D<T, Dst, Src>> { |
| 619 |
let det = self.determinant(); |
| 620 |
|
| 621 |
let _0: T = Zero::zero(); |
| 622 |
let _1: T = One::one(); |
| 623 |
|
| 624 |
if det == _0 { |
| 625 |
return None; |
| 626 |
} |
| 627 |
|
| 628 |
let inv_det = _1 / det; |
| 629 |
Some(Transform2D::new( |
| 630 |
inv_det * self.m22, |
| 631 |
inv_det * (_0 - self.m12), |
| 632 |
inv_det * (_0 - self.m21), |
| 633 |
inv_det * self.m11, |
| 634 |
inv_det * (self.m21 * self.m32 - self.m22 * self.m31), |
| 635 |
inv_det * (self.m31 * self.m12 - self.m11 * self.m32), |
| 636 |
)) |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
impl<T, Src, Dst> Default for Transform2D<T, Src, Dst> |
| 641 |
where |
| 642 |
T: Zero + One, |
| 643 |
{ |
| 644 |
/// Returns the [identity transform](#method.identity). |
| 645 |
fn default() -> Self { |
| 646 |
Self::identity() |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
impl<T: ApproxEq<T>, Src, Dst> ApproxEq<T> for Transform2D<T, Src, Dst> { |
| 651 |
#[inline] |
| 652 |
fn approx_epsilon() -> T { |
| 653 |
T::approx_epsilon() |
| 654 |
} |
| 655 |
|
| 656 |
/// Returns true is this transform is approximately equal to the other one, using |
| 657 |
/// a provided epsilon value. |
| 658 |
fn approx_eq_eps(&self, other: &Self, eps: &T) -> bool { |
| 659 |
self.m11.approx_eq_eps(&other.m11, eps) |
| 660 |
&& self.m12.approx_eq_eps(&other.m12, eps) |
| 661 |
&& self.m21.approx_eq_eps(&other.m21, eps) |
| 662 |
&& self.m22.approx_eq_eps(&other.m22, eps) |
| 663 |
&& self.m31.approx_eq_eps(&other.m31, eps) |
| 664 |
&& self.m32.approx_eq_eps(&other.m32, eps) |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
impl<T, Src, Dst> fmt::Debug for Transform2D<T, Src, Dst> |
| 669 |
where |
| 670 |
T: Copy + fmt::Debug + PartialEq + One + Zero, |
| 671 |
{ |
| 672 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 673 |
if self.is_identity() { |
| 674 |
write!(f, "[I]") |
| 675 |
} else { |
| 676 |
self.to_array().fmt(f) |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
#[cfg(feature = "mint")] |
| 682 |
impl<T, Src, Dst> From<mint::RowMatrix3x2<T>> for Transform2D<T, Src, Dst> { |
| 683 |
#[rustfmt::skip] |
| 684 |
fn from(m: mint::RowMatrix3x2<T>) -> Self { |
| 685 |
Transform2D { |
| 686 |
m11: m.x.x, m12: m.x.y, |
| 687 |
m21: m.y.x, m22: m.y.y, |
| 688 |
m31: m.z.x, m32: m.z.y, |
| 689 |
_unit: PhantomData, |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
#[cfg(feature = "mint")] |
| 694 |
impl<T, Src, Dst> From<Transform2D<T, Src, Dst>> for mint::RowMatrix3x2<T> { |
| 695 |
fn from(t: Transform2D<T, Src, Dst>) -> Self { |
| 696 |
mint::RowMatrix3x2 { |
| 697 |
x: mint::Vector2 { x: t.m11, y: t.m12 }, |
| 698 |
y: mint::Vector2 { x: t.m21, y: t.m22 }, |
| 699 |
z: mint::Vector2 { x: t.m31, y: t.m32 }, |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
#[cfg(test)] |
| 705 |
mod test { |
| 706 |
use super::*; |
| 707 |
use crate::approxeq::ApproxEq; |
| 708 |
use crate::default; |
| 709 |
#[cfg(feature = "mint")] |
| 710 |
use mint; |
| 711 |
|
| 712 |
use core::f32::consts::FRAC_PI_2; |
| 713 |
|
| 714 |
type Mat = default::Transform2D<f32>; |
| 715 |
|
| 716 |
fn rad(v: f32) -> Angle<f32> { |
| 717 |
Angle::radians(v) |
| 718 |
} |
| 719 |
|
| 720 |
#[test] |
| 721 |
pub fn test_translation() { |
| 722 |
let t1 = Mat::translation(1.0, 2.0); |
| 723 |
let t2 = Mat::identity().pre_translate(vec2(1.0, 2.0)); |
| 724 |
let t3 = Mat::identity().then_translate(vec2(1.0, 2.0)); |
| 725 |
assert_eq!(t1, t2); |
| 726 |
assert_eq!(t1, t3); |
| 727 |
|
| 728 |
assert_eq!( |
| 729 |
t1.transform_point(Point2D::new(1.0, 1.0)), |
| 730 |
Point2D::new(2.0, 3.0) |
| 731 |
); |
| 732 |
|
| 733 |
assert_eq!(t1.then(&t1), Mat::translation(2.0, 4.0)); |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
pub fn test_rotation() { |
| 738 |
let r1 = Mat::rotation(rad(FRAC_PI_2)); |
| 739 |
let r2 = Mat::identity().pre_rotate(rad(FRAC_PI_2)); |
| 740 |
let r3 = Mat::identity().then_rotate(rad(FRAC_PI_2)); |
| 741 |
assert_eq!(r1, r2); |
| 742 |
assert_eq!(r1, r3); |
| 743 |
|
| 744 |
assert!(r1 |
| 745 |
.transform_point(Point2D::new(1.0, 2.0)) |
| 746 |
.approx_eq(&Point2D::new(-2.0, 1.0))); |
| 747 |
|
| 748 |
assert!(r1.then(&r1).approx_eq(&Mat::rotation(rad(FRAC_PI_2 * 2.0)))); |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
pub fn test_scale() { |
| 753 |
let s1 = Mat::scale(2.0, 3.0); |
| 754 |
let s2 = Mat::identity().pre_scale(2.0, 3.0); |
| 755 |
let s3 = Mat::identity().then_scale(2.0, 3.0); |
| 756 |
assert_eq!(s1, s2); |
| 757 |
assert_eq!(s1, s3); |
| 758 |
|
| 759 |
assert!(s1 |
| 760 |
.transform_point(Point2D::new(2.0, 2.0)) |
| 761 |
.approx_eq(&Point2D::new(4.0, 6.0))); |
| 762 |
} |
| 763 |
|
| 764 |
#[test] |
| 765 |
pub fn test_pre_then_scale() { |
| 766 |
let m = Mat::rotation(rad(FRAC_PI_2)).then_translate(vec2(6.0, 7.0)); |
| 767 |
let s = Mat::scale(2.0, 3.0); |
| 768 |
assert_eq!(m.then(&s), m.then_scale(2.0, 3.0)); |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
pub fn test_inverse_simple() { |
| 773 |
let m1 = Mat::identity(); |
| 774 |
let m2 = m1.inverse().unwrap(); |
| 775 |
assert!(m1.approx_eq(&m2)); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
pub fn test_inverse_scale() { |
| 780 |
let m1 = Mat::scale(1.5, 0.3); |
| 781 |
let m2 = m1.inverse().unwrap(); |
| 782 |
assert!(m1.then(&m2).approx_eq(&Mat::identity())); |
| 783 |
assert!(m2.then(&m1).approx_eq(&Mat::identity())); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
pub fn test_inverse_translate() { |
| 788 |
let m1 = Mat::translation(-132.0, 0.3); |
| 789 |
let m2 = m1.inverse().unwrap(); |
| 790 |
assert!(m1.then(&m2).approx_eq(&Mat::identity())); |
| 791 |
assert!(m2.then(&m1).approx_eq(&Mat::identity())); |
| 792 |
} |
| 793 |
|
| 794 |
#[test] |
| 795 |
fn test_inverse_none() { |
| 796 |
assert!(Mat::scale(2.0, 0.0).inverse().is_none()); |
| 797 |
assert!(Mat::scale(2.0, 2.0).inverse().is_some()); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
pub fn test_pre_post() { |
| 802 |
let m1 = default::Transform2D::identity() |
| 803 |
.then_scale(1.0, 2.0) |
| 804 |
.then_translate(vec2(1.0, 2.0)); |
| 805 |
let m2 = default::Transform2D::identity() |
| 806 |
.pre_translate(vec2(1.0, 2.0)) |
| 807 |
.pre_scale(1.0, 2.0); |
| 808 |
assert!(m1.approx_eq(&m2)); |
| 809 |
|
| 810 |
let r = Mat::rotation(rad(FRAC_PI_2)); |
| 811 |
let t = Mat::translation(2.0, 3.0); |
| 812 |
|
| 813 |
let a = Point2D::new(1.0, 1.0); |
| 814 |
|
| 815 |
assert!(r |
| 816 |
.then(&t) |
| 817 |
.transform_point(a) |
| 818 |
.approx_eq(&Point2D::new(1.0, 4.0))); |
| 819 |
assert!(t |
| 820 |
.then(&r) |
| 821 |
.transform_point(a) |
| 822 |
.approx_eq(&Point2D::new(-4.0, 3.0))); |
| 823 |
assert!(t |
| 824 |
.then(&r) |
| 825 |
.transform_point(a) |
| 826 |
.approx_eq(&r.transform_point(t.transform_point(a)))); |
| 827 |
} |
| 828 |
|
| 829 |
#[test] |
| 830 |
fn test_size_of() { |
| 831 |
use core::mem::size_of; |
| 832 |
assert_eq!(size_of::<default::Transform2D<f32>>(), 6 * size_of::<f32>()); |
| 833 |
assert_eq!(size_of::<default::Transform2D<f64>>(), 6 * size_of::<f64>()); |
| 834 |
} |
| 835 |
|
| 836 |
#[test] |
| 837 |
pub fn test_is_identity() { |
| 838 |
let m1 = default::Transform2D::identity(); |
| 839 |
assert!(m1.is_identity()); |
| 840 |
let m2 = m1.then_translate(vec2(0.1, 0.0)); |
| 841 |
assert!(!m2.is_identity()); |
| 842 |
} |
| 843 |
|
| 844 |
#[test] |
| 845 |
pub fn test_transform_vector() { |
| 846 |
// Translation does not apply to vectors. |
| 847 |
let m1 = Mat::translation(1.0, 1.0); |
| 848 |
let v1 = vec2(10.0, -10.0); |
| 849 |
assert_eq!(v1, m1.transform_vector(v1)); |
| 850 |
} |
| 851 |
|
| 852 |
#[cfg(feature = "mint")] |
| 853 |
#[test] |
| 854 |
pub fn test_mint() { |
| 855 |
let m1 = Mat::rotation(rad(FRAC_PI_2)); |
| 856 |
let mm: mint::RowMatrix3x2<_> = m1.into(); |
| 857 |
let m2 = Mat::from(mm); |
| 858 |
|
| 859 |
assert_eq!(m1, m2); |
| 860 |
} |
| 861 |
} |