2019-03-23 21:29:07 +08:00
|
|
|
use crate::aliases::{TMat3, TMat4, TVec2, TVec3};
|
|
|
|
use crate::traits::Number;
|
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`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn proj2d<T: Number>(m: &TMat3<T>, normal: &TVec2<T>) -> TMat3<T> {
|
2018-09-23 20:41:56 +08:00
|
|
|
let mut res = TMat3::identity();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut part = res.fixed_slice_mut::<2, 2>(0, 0);
|
2018-09-21 04:12:26 +08:00
|
|
|
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`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn proj<T: Number>(m: &TMat4<T>, normal: &TVec3<T>) -> TMat4<T> {
|
2018-09-23 20:41:56 +08:00
|
|
|
let mut res = TMat4::identity();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut part = res.fixed_slice_mut::<3, 3>(0, 0);
|
2018-09-21 04:12:26 +08:00
|
|
|
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`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn reflect2d<T: Number>(m: &TMat3<T>, normal: &TVec2<T>) -> TMat3<T> {
|
2018-09-23 20:41:56 +08:00
|
|
|
let mut res = TMat3::identity();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut part = res.fixed_slice_mut::<2, 2>(0, 0);
|
|
|
|
part -= (normal * T::from_f64(2.0).unwrap()) * normal.transpose();
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
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`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn reflect<T: Number>(m: &TMat4<T>, normal: &TVec3<T>) -> TMat4<T> {
|
2018-09-23 20:41:56 +08:00
|
|
|
let mut res = TMat4::identity();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut part = res.fixed_slice_mut::<3, 3>(0, 0);
|
|
|
|
part -= (normal * T::from_f64(2.0).unwrap()) * normal.transpose();
|
2018-09-21 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn scale_bias_matrix<T: Number>(scale: T, bias: T) -> TMat4<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
2018-09-23 20:41:56 +08:00
|
|
|
TMat4::new(
|
2018-10-22 04:11:27 +08:00
|
|
|
scale, _0, _0, bias, _0, scale, _0, bias, _0, _0, scale, bias, _0, _0, _0, _1,
|
2018-09-21 04:12:26 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Builds a scale-bias matrix, and right-multiply it to `m`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn scale_bias<T: Number>(m: &TMat4<T>, scale: T, bias: T) -> TMat4<T> {
|
2018-09-22 23:36:08 +08:00
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn shear2d_x<T: Number>(m: &TMat3<T>, y: T) -> TMat3<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
2018-10-22 04:11:27 +08:00
|
|
|
let shear = TMat3::new(_1, y, _0, _0, _1, _0, _0, _0, _1);
|
2018-09-21 04:12:26 +08:00
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn shear_x<T: Number>(m: &TMat4<T>, y: T, z: T) -> TMat4<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-10-22 04:11:27 +08:00
|
|
|
let shear = TMat4::new(_1, _0, _0, _0, y, _1, _0, _0, z, _0, _1, _0, _0, _0, _0, _1);
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn shear2d_y<T: Number>(m: &TMat3<T>, x: T) -> TMat3<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-09-21 04:12:26 +08:00
|
|
|
|
2018-10-22 04:11:27 +08:00
|
|
|
let shear = TMat3::new(_1, _0, _0, x, _1, _0, _0, _0, _1);
|
2018-09-21 04:12:26 +08:00
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Y axis.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn shear_y<T: Number>(m: &TMat4<T>, x: T, z: T) -> TMat4<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-10-22 04:11:27 +08:00
|
|
|
let shear = TMat4::new(_1, x, _0, _0, _0, _1, _0, _0, _0, z, _1, _0, _0, _0, _0, _1);
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Transforms a matrix with a shearing on Z axis.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn shear_z<T: Number>(m: &TMat4<T>, x: T, y: T) -> TMat4<T> {
|
|
|
|
let _0 = T::zero();
|
|
|
|
let _1 = T::one();
|
2018-10-22 04:11:27 +08:00
|
|
|
let shear = TMat4::new(_1, _0, x, _0, _0, _1, y, _0, _0, _0, _1, _0, _0, _0, _0, _1);
|
2018-09-21 04:12:26 +08:00
|
|
|
|
|
|
|
m * shear
|
|
|
|
}
|