2019-02-04 23:20:32 -05:00
|
|
|
// pathfinder/geometry/src/basic/transform2d.rs
|
2018-01-24 15:08:39 -05:00
|
|
|
//
|
2019-01-10 13:01:21 -05:00
|
|
|
// Copyright © 2019 The Pathfinder Project Developers.
|
2018-01-24 15:08:39 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2019-02-04 23:20:32 -05:00
|
|
|
//! 2D affine transforms.
|
2018-01-24 15:08:39 -05:00
|
|
|
|
2019-06-21 13:06:19 -04:00
|
|
|
use crate::line_segment::LineSegment2F;
|
|
|
|
use crate::vector::Vector2F;
|
|
|
|
use crate::rect::RectF;
|
2019-07-11 17:35:06 -04:00
|
|
|
use crate::transform3d::Transform4F;
|
2019-05-29 15:12:12 -04:00
|
|
|
use crate::unit_vector::UnitVector;
|
2019-02-01 14:48:10 -05:00
|
|
|
use pathfinder_simd::default::F32x4;
|
2019-07-12 14:26:09 -04:00
|
|
|
use std::ops::{Mul, MulAssign, Sub};
|
2019-01-25 17:28:53 -05:00
|
|
|
|
|
|
|
/// A 2x2 matrix, optimized with SIMD, in column-major order.
|
2019-02-06 21:09:37 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub struct Matrix2x2F(pub F32x4);
|
2019-01-25 17:28:53 -05:00
|
|
|
|
2019-05-29 22:13:42 -04:00
|
|
|
impl Default for Matrix2x2F {
|
2019-01-25 17:28:53 -05:00
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
fn default() -> Matrix2x2F {
|
2019-06-03 15:39:29 -04:00
|
|
|
Self::from_scale(Vector2F::splat(1.0))
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 22:13:42 -04:00
|
|
|
impl Matrix2x2F {
|
2019-01-25 17:28:53 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn from_scale(scale: Vector2F) -> Matrix2x2F {
|
2019-05-29 22:13:42 -04:00
|
|
|
Matrix2x2F(F32x4::new(scale.x(), 0.0, 0.0, scale.y()))
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn from_rotation(theta: f32) -> Matrix2x2F {
|
|
|
|
Matrix2x2F::from_rotation_vector(UnitVector::from_angle(theta))
|
2019-05-29 15:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn from_rotation_vector(vector: UnitVector) -> Matrix2x2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Matrix2x2F((vector.0).0.to_f32x4().xyyx() * F32x4::new(1.0, 1.0, -1.0, 1.0))
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-12 14:26:09 -04:00
|
|
|
pub fn row_major(m00: f32, m01: f32, m10: f32, m11: f32) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(F32x4::new(m00, m10, m01, m11))
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn entrywise_mul(&self, other: &Matrix2x2F) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(self.0 * other.0)
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn adjugate(&self) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(self.0.wyzx() * F32x4::new(1.0, -1.0, -1.0, 1.0))
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn det(&self) -> f32 {
|
|
|
|
self.0[0] * self.0[3] - self.0[2] * self.0[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn inverse(&self) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(F32x4::splat(1.0 / self.det()) * self.adjugate().0)
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m11(&self) -> f32 {
|
|
|
|
self.0[0]
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m21(&self) -> f32 {
|
|
|
|
self.0[1]
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m12(&self) -> f32 {
|
|
|
|
self.0[2]
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m22(&self) -> f32 {
|
|
|
|
self.0[3]
|
|
|
|
}
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:13:42 -04:00
|
|
|
impl Sub<Matrix2x2F> for Matrix2x2F {
|
|
|
|
type Output = Matrix2x2F;
|
2019-01-25 17:28:53 -05:00
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
fn sub(self, other: Matrix2x2F) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(self.0 - other.0)
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-24 15:08:39 -05:00
|
|
|
|
2019-07-12 14:26:09 -04:00
|
|
|
impl Mul<Matrix2x2F> for Matrix2x2F {
|
|
|
|
type Output = Matrix2x2F;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, other: Matrix2x2F) -> Matrix2x2F {
|
|
|
|
Matrix2x2F(self.0.xyxy() * other.0.xxzz() + self.0.zwzw() * other.0.yyww())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:55:01 -04:00
|
|
|
impl Mul<Vector2F> for Matrix2x2F {
|
|
|
|
type Output = Vector2F;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, vector: Vector2F) -> Vector2F {
|
|
|
|
let halves = self.0 * vector.0.to_f32x4().xxyy();
|
|
|
|
Vector2F(halves.xy() + halves.zw())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:07:08 -05:00
|
|
|
/// An affine transform, optimized with SIMD.
|
2019-02-06 21:09:37 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub struct Transform2F {
|
2019-01-14 14:07:08 -05:00
|
|
|
// Row-major order.
|
2019-06-24 17:03:26 -04:00
|
|
|
pub matrix: Matrix2x2F,
|
|
|
|
pub vector: Vector2F,
|
2019-01-14 14:07:08 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:35:06 -04:00
|
|
|
impl Default for Transform2F {
|
2019-01-14 14:07:08 -05:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
fn default() -> Transform2F {
|
2019-06-03 15:39:29 -04:00
|
|
|
Self::from_scale(Vector2F::splat(1.0))
|
2019-01-14 14:07:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:35:06 -04:00
|
|
|
impl Transform2F {
|
2019-01-14 14:07:08 -05:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn from_scale(scale: Vector2F) -> Transform2F {
|
|
|
|
Transform2F {
|
2019-05-29 22:13:42 -04:00
|
|
|
matrix: Matrix2x2F::from_scale(scale),
|
2019-06-03 15:39:29 -04:00
|
|
|
vector: Vector2F::default(),
|
2019-01-14 14:07:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:26:09 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn from_uniform_scale(scale: f32) -> Transform2F {
|
|
|
|
Transform2F::from_scale(Vector2F::splat(scale))
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:49:26 -05:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn from_rotation(theta: f32) -> Transform2F {
|
|
|
|
Transform2F {
|
2019-05-29 22:13:42 -04:00
|
|
|
matrix: Matrix2x2F::from_rotation(theta),
|
2019-06-03 15:39:29 -04:00
|
|
|
vector: Vector2F::default(),
|
2019-01-15 16:49:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:12:12 -04:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn from_rotation_vector(vector: UnitVector) -> Transform2F {
|
|
|
|
Transform2F {
|
2019-05-29 22:13:42 -04:00
|
|
|
matrix: Matrix2x2F::from_rotation_vector(vector),
|
2019-06-03 15:39:29 -04:00
|
|
|
vector: Vector2F::default(),
|
2019-05-29 15:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:11:47 -05:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn from_translation(vector: Vector2F) -> Transform2F {
|
|
|
|
Transform2F { matrix: Matrix2x2F::default(), vector }
|
2019-01-16 20:11:47 -05:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn from_scale_rotation_translation(
|
2019-06-03 15:39:29 -04:00
|
|
|
scale: Vector2F,
|
2019-04-29 19:46:35 -04:00
|
|
|
theta: f32,
|
2019-06-03 15:39:29 -04:00
|
|
|
translation: Vector2F,
|
2019-07-11 17:35:06 -04:00
|
|
|
) -> Transform2F {
|
|
|
|
let rotation = Transform2F::from_rotation(theta);
|
|
|
|
let translation = Transform2F::from_translation(translation);
|
2019-07-12 14:26:09 -04:00
|
|
|
Transform2F::from_scale(scale) * rotation * translation
|
2019-02-13 19:05:28 -05:00
|
|
|
}
|
|
|
|
|
2019-01-14 14:07:08 -05:00
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn row_major(m11: f32, m12: f32, m21: f32, m22: f32, m31: f32, m32: f32) -> Transform2F {
|
|
|
|
Transform2F {
|
2019-05-29 22:13:42 -04:00
|
|
|
matrix: Matrix2x2F::row_major(m11, m12, m21, m22),
|
2019-06-03 15:39:29 -04:00
|
|
|
vector: Vector2F::new(m31, m32),
|
2019-01-14 14:07:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 17:28:53 -05:00
|
|
|
// TODO(pcwalton): Optimize better with SIMD.
|
|
|
|
#[inline]
|
2019-07-11 17:35:06 -04:00
|
|
|
pub fn to_3d(&self) -> Transform4F {
|
|
|
|
Transform4F::row_major(
|
2019-04-29 19:46:35 -04:00
|
|
|
self.matrix.0[0],
|
|
|
|
self.matrix.0[1],
|
|
|
|
0.0,
|
|
|
|
self.vector.x(),
|
|
|
|
self.matrix.0[2],
|
|
|
|
self.matrix.0[3],
|
|
|
|
0.0,
|
|
|
|
self.vector.y(),
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
)
|
2019-01-25 17:28:53 -05:00
|
|
|
}
|
2019-02-06 21:09:37 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_identity(&self) -> bool {
|
2019-07-11 17:35:06 -04:00
|
|
|
*self == Transform2F::default()
|
2019-02-06 21:09:37 -05:00
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m11(&self) -> f32 {
|
|
|
|
self.matrix.m11()
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m21(&self) -> f32 {
|
|
|
|
self.matrix.m21()
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m12(&self) -> f32 {
|
|
|
|
self.matrix.m12()
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
#[inline]
|
2019-04-29 19:46:35 -04:00
|
|
|
pub fn m22(&self) -> f32 {
|
|
|
|
self.matrix.m22()
|
|
|
|
}
|
2019-02-13 19:05:28 -05:00
|
|
|
|
|
|
|
#[inline]
|
2019-07-12 14:34:48 -04:00
|
|
|
pub fn translate(&self, vector: Vector2F) -> Transform2F {
|
|
|
|
Transform2F::from_translation(vector) * *self
|
2019-02-13 19:05:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-12 14:34:48 -04:00
|
|
|
pub fn rotate(&self, theta: f32) -> Transform2F {
|
|
|
|
Transform2F::from_rotation(theta) * *self
|
2019-02-13 19:05:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-12 14:34:48 -04:00
|
|
|
pub fn scale(&self, scale: Vector2F) -> Transform2F {
|
|
|
|
Transform2F::from_scale(scale) * *self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn uniform_scale(&self, scale: f32) -> Transform2F {
|
|
|
|
self.scale(Vector2F::splat(scale))
|
2019-02-13 19:05:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the translation part of this matrix.
|
|
|
|
///
|
|
|
|
/// This decomposition assumes that scale, rotation, and translation are applied in that order.
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn translation(&self) -> Vector2F {
|
2019-02-13 19:05:28 -05:00
|
|
|
self.vector
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the rotation angle of this matrix.
|
|
|
|
///
|
|
|
|
/// This decomposition assumes that scale, rotation, and translation are applied in that order.
|
|
|
|
#[inline]
|
|
|
|
pub fn rotation(&self) -> f32 {
|
|
|
|
f32::atan2(self.m21(), self.m11())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the scale factor of this matrix.
|
|
|
|
///
|
|
|
|
/// This decomposition assumes that scale, rotation, and translation are applied in that order.
|
|
|
|
#[inline]
|
|
|
|
pub fn scale_factor(&self) -> f32 {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.matrix.0.zw()).length()
|
2019-02-13 19:05:28 -05:00
|
|
|
}
|
2019-01-14 14:07:08 -05:00
|
|
|
}
|
2019-07-12 14:26:09 -04:00
|
|
|
|
|
|
|
impl Mul<Transform2F> for Transform2F {
|
|
|
|
type Output = Transform2F;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, other: Transform2F) -> Transform2F {
|
|
|
|
Transform2F {
|
|
|
|
matrix: self.matrix * other.matrix,
|
2019-07-12 14:55:01 -04:00
|
|
|
vector: self * other.vector,
|
2019-07-12 14:26:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:55:01 -04:00
|
|
|
impl Mul<Vector2F> for Transform2F {
|
|
|
|
type Output = Vector2F;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, vector: Vector2F) -> Vector2F {
|
|
|
|
self.matrix * vector + self.vector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:59:04 -04:00
|
|
|
impl Mul<LineSegment2F> for Transform2F {
|
|
|
|
type Output = LineSegment2F;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, line_segment: LineSegment2F) -> LineSegment2F {
|
|
|
|
LineSegment2F::new(self * line_segment.from(), self * line_segment.to())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<RectF> for Transform2F {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, rect: RectF) -> RectF {
|
|
|
|
let (upper_left, upper_right) = (self * rect.origin(), self * rect.upper_right());
|
|
|
|
let (lower_left, lower_right) = (self * rect.lower_left(), self * rect.lower_right());
|
|
|
|
let min_point = upper_left.min(upper_right).min(lower_left).min(lower_right);
|
|
|
|
let max_point = upper_left.max(upper_right).max(lower_left).max(lower_right);
|
|
|
|
RectF::from_points(min_point, max_point)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:26:09 -04:00
|
|
|
impl MulAssign for Transform2F {
|
|
|
|
#[inline]
|
|
|
|
fn mul_assign(&mut self, other: Transform2F) {
|
|
|
|
*self = *self * other
|
|
|
|
}
|
|
|
|
}
|