2018-09-22 19:18:59 +08:00
|
|
|
use na::{U2, U3, U4, Matrix3, Matrix4};
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
use traits::Number;
|
|
|
|
use aliases::{Mat, Vec};
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Build planar projection matrix along normal axis and right-multiply it to `m`.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn proj2d<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let mut res = Matrix3::identity();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
|
|
|
|
part -= normal * normal.transpose();
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
m * res
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Build planar projection matrix along normal axis, and right-multiply it to `m`.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn proj<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let mut res = Matrix4::identity();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
|
|
|
|
part -= normal * normal.transpose();
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
m * res
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Builds a reflection matrix and right-multiply it to `m`.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn reflect2d<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let mut res = Matrix3::identity();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
|
|
|
|
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
m * res
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Builds a reflection matrix, and right-multiply it to `m`.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn reflect<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let mut res = Matrix4::identity();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
|
|
|
|
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
m * res
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Builds a scale-bias matrix.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn scale_bias_matrix<N: Number>(scale: N, bias: N) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
|
|
|
|
Matrix4::new(
|
|
|
|
scale, _0, _0, bias,
|
|
|
|
_0, scale, _0, bias,
|
|
|
|
_0, _0, scale, bias,
|
|
|
|
_0, _0, _0, _1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Builds a scale-bias matrix, and right-multiply it to `m`.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn scale_bias<N: Number>(m: &Mat<N, U4, U4>, scale: N, bias: N) -> Mat<N, U4, U4> {
|
|
|
|
m * scale_bias_matrix(scale, bias)
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on X axis.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn shear2d_x<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
|
|
|
|
let shear = Matrix3::new(
|
|
|
|
_1, y, _0,
|
|
|
|
_0, _1, _0,
|
|
|
|
_0, _0, _1
|
|
|
|
);
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn shear_x<N: Number>(m: &Mat<N, U4, U4>, y: N, z: N) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
let shear = Matrix4::new(
|
|
|
|
_1, _0, _0, _0,
|
|
|
|
y, _1, _0, _0,
|
|
|
|
z, _0, _1, _0,
|
|
|
|
_0, _0, _0, _1,
|
|
|
|
);
|
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2018-09-23 02:16:43 +08:00
|
|
|
pub fn shear2d_y<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
|
|
|
|
let shear = Matrix3::new(
|
|
|
|
_1, _0, _0,
|
|
|
|
x, _1, _0,
|
|
|
|
_0, _0, _1
|
|
|
|
);
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn shear_y<N: Number>(m: &Mat<N, U4, U4>, x: N, z: N) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
let shear = Matrix4::new(
|
|
|
|
_1, x, _0, _0,
|
|
|
|
_0, _1, _0, _0,
|
|
|
|
_0, z, _1, _0,
|
|
|
|
_0, _0, _0, _1,
|
|
|
|
);
|
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Z axis.
|
2018-09-22 23:36:08 +08:00
|
|
|
pub fn shear_z<N: Number>(m: &Mat<N, U4, U4>, x: N, y: N) -> Mat<N, U4, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
let _0 = N::zero();
|
|
|
|
let _1 = N::one();
|
|
|
|
let shear = Matrix4::new(
|
|
|
|
_1, _0, x, _0,
|
|
|
|
_0, _1, y, _0,
|
|
|
|
_0, _0, _1, _0,
|
|
|
|
_0, _0, _0, _1,
|
|
|
|
);
|
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|