use super::{UnknownUnit, Radians};
use num::{One, Zero};
use point::TypedPoint2D;
use rect::TypedRect;
use size::TypedSize2D;
use std::ops::{Add, Mul, Div, Sub};
use std::marker::PhantomData;
use approxeq::ApproxEq;
use trig::Trig;
define_matrix! {
pub struct TypedMatrix2D<T, Src, Dst> {
pub m11: T, pub m12: T,
pub m21: T, pub m22: T,
pub m31: T, pub m32: T,
}
}
pub type Matrix2D<T> = TypedMatrix2D<T, UnknownUnit, UnknownUnit>;
impl<T: Copy, Src, Dst> TypedMatrix2D<T, Src, Dst> {
pub fn row_major(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> TypedMatrix2D<T, Src, Dst> {
TypedMatrix2D {
m11: m11, m12: m12,
m21: m21, m22: m22,
m31: m31, m32: m32,
_unit: PhantomData,
}
}
pub fn column_major(m11: T, m21: T, m31: T, m12: T, m22: T, m32: T) -> TypedMatrix2D<T, Src, Dst> {
TypedMatrix2D {
m11: m11, m12: m12,
m21: m21, m22: m22,
m31: m31, m32: m32,
_unit: PhantomData,
}
}
pub fn to_row_major_array(&self) -> [T; 6] {
[
self.m11, self.m12,
self.m21, self.m22,
self.m31, self.m32
]
}
pub fn to_column_major_array(&self) -> [T; 6] {
[
self.m11, self.m21, self.m31,
self.m12, self.m22, self.m32
]
}
pub fn to_untyped(&self) -> Matrix2D<T> {
Matrix2D::row_major(
self.m11, self.m12,
self.m21, self.m22,
self.m31, self.m32
)
}
pub fn from_untyped(p: &Matrix2D<T>) -> TypedMatrix2D<T, Src, Dst> {
TypedMatrix2D::row_major(
p.m11, p.m12,
p.m21, p.m22,
p.m31, p.m32
)
}
}
impl<T, Src, Dst> TypedMatrix2D<T, Src, Dst>
where T: Copy + Clone +
Add<T, Output=T> +
Mul<T, Output=T> +
Div<T, Output=T> +
Sub<T, Output=T> +
Trig +
PartialOrd +
One + Zero {
pub fn identity() -> TypedMatrix2D<T, Src, Dst> {
let (_0, _1) = (Zero::zero(), One::one());
TypedMatrix2D::row_major(
_1, _0,
_0, _1,
_0, _0
)
}
pub fn post_mul<NewDst>(&self, mat: &TypedMatrix2D<T, Dst, NewDst>) -> TypedMatrix2D<T, Src, NewDst> {
TypedMatrix2D::row_major(
self.m11 * mat.m11 + self.m12 * mat.m21,
self.m11 * mat.m12 + self.m12 * mat.m22,
self.m21 * mat.m11 + self.m22 * mat.m21,
self.m21 * mat.m12 + self.m22 * mat.m22,
self.m31 * mat.m11 + self.m32 * mat.m21 + mat.m31,
self.m31 * mat.m12 + self.m32 * mat.m22 + mat.m32,
)
}
pub fn pre_mul<NewSrc>(&self, mat: &TypedMatrix2D<T, NewSrc, Src>) -> TypedMatrix2D<T, NewSrc, Dst> {
mat.post_mul(self)
}
pub fn create_translation(x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
TypedMatrix2D::row_major(
_1, _0,
_0, _1,
x, y
)
}
pub fn post_translated(&self, x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
self.post_mul(&TypedMatrix2D::create_translation(x, y))
}
pub fn pre_translated(&self, x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
self.pre_mul(&TypedMatrix2D::create_translation(x, y))
}
pub fn create_scale(x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
let _0 = Zero::zero();
TypedMatrix2D::row_major(
x, _0,
_0, y,
_0, _0
)
}
pub fn post_scaled(&self, x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
self.post_mul(&TypedMatrix2D::create_scale(x, y))
}
pub fn pre_scaled(&self, x: T, y: T) -> TypedMatrix2D<T, Src, Dst> {
TypedMatrix2D::row_major(
self.m11 * x, self.m12,
self.m21, self.m22 * y,
self.m31, self.m32
)
}
pub fn create_rotation(theta: Radians<T>) -> TypedMatrix2D<T, Src, Dst> {
let _0 = Zero::zero();
let cos = theta.get().cos();
let sin = theta.get().sin();
TypedMatrix2D::row_major(
cos, _0 - sin,
sin, cos,
_0, _0
)
}
pub fn post_rotated(&self, theta: Radians<T>) -> TypedMatrix2D<T, Src, Dst> {
self.post_mul(&TypedMatrix2D::create_rotation(theta))
}
pub fn pre_rotated(&self, theta: Radians<T>) -> TypedMatrix2D<T, Src, Dst> {
self.pre_mul(&TypedMatrix2D::create_rotation(theta))
}
#[inline]
pub fn transform_point(&self, point: &TypedPoint2D<T, Src>) -> TypedPoint2D<T, Dst> {
TypedPoint2D::new(point.x * self.m11 + point.y * self.m21 + self.m31,
point.x * self.m12 + point.y * self.m22 + self.m32)
}
#[inline]
pub fn transform_rect(&self, rect: &TypedRect<T, Src>) -> TypedRect<T, Dst> {
let top_left = self.transform_point(&rect.origin);
let top_right = self.transform_point(&rect.top_right());
let bottom_left = self.transform_point(&rect.bottom_left());
let bottom_right = self.transform_point(&rect.bottom_right());
let (mut min_x, mut min_y) = (top_left.x, top_left.y);
let (mut max_x, mut max_y) = (min_x, min_y);
for point in &[top_right, bottom_left, bottom_right] {
if point.x < min_x {
min_x = point.x
}
if point.x > max_x {
max_x = point.x
}
if point.y < min_y {
min_y = point.y
}
if point.y > max_y {
max_y = point.y
}
}
TypedRect::new(TypedPoint2D::new(min_x, min_y),
TypedSize2D::new(max_x - min_x, max_y - min_y))
}
pub fn determinant(&self) -> T {
self.m11 * self.m22 - self.m12 * self.m21
}
pub fn inverse(&self) -> Option<TypedMatrix2D<T, Dst, Src>> {
let det = self.determinant();
let _0: T = Zero::zero();
let _1: T = One::one();
if det == _0 {
return None;
}
let inv_det = _1 / det;
Some(TypedMatrix2D::row_major(
inv_det * self.m22,
inv_det * (_0 - self.m12),
inv_det * (_0 - self.m21),
inv_det * self.m11,
inv_det * (self.m21 * self.m32 - self.m22 * self.m31),
inv_det * (self.m31 * self.m12 - self.m11 * self.m32),
))
}
#[inline]
pub fn with_destination<NewDst>(&self) -> TypedMatrix2D<T, Src, NewDst> {
TypedMatrix2D::row_major(
self.m11, self.m12,
self.m21, self.m22,
self.m31, self.m32,
)
}
#[inline]
pub fn with_source<NewSrc>(&self) -> TypedMatrix2D<T, NewSrc, Dst> {
TypedMatrix2D::row_major(
self.m11, self.m12,
self.m21, self.m22,
self.m31, self.m32,
)
}
}
impl<T: ApproxEq<T>, Src, Dst> TypedMatrix2D<T, Src, Dst> {
pub fn approx_eq(&self, other: &Self) -> bool {
self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) &&
self.m21.approx_eq(&other.m21) && self.m22.approx_eq(&other.m22) &&
self.m31.approx_eq(&other.m31) && self.m32.approx_eq(&other.m32)
}
}
#[cfg(test)]
mod test {
use super::*;
type Mat = Matrix2D<f32>;
#[test]
pub fn test_inverse_simple() {
let m1 = Mat::identity();
let m2 = m1.inverse().unwrap();
assert!(m1.approx_eq(&m2));
}
#[test]
pub fn test_inverse_scale() {
let m1 = Mat::create_scale(1.5, 0.3);
let m2 = m1.inverse().unwrap();
assert!(m1.pre_mul(&m2).approx_eq(&Mat::identity()));
}
#[test]
pub fn test_inverse_translate() {
let m1 = Mat::create_translation(-132.0, 0.3);
let m2 = m1.inverse().unwrap();
assert!(m1.pre_mul(&m2).approx_eq(&Mat::identity()));
}
#[test]
pub fn test_pre_post() {
let m1 = Matrix2D::identity().post_scaled(1.0, 2.0).post_translated(1.0, 2.0);
let m2 = Matrix2D::identity().pre_translated(1.0, 2.0).pre_scaled(1.0, 2.0);
assert!(m1.approx_eq(&m2));
}
}