nalgebra/nalgebra-glm/src/gtx/transform2.rs

113 lines
3.1 KiB
Rust
Raw Normal View History

2018-09-23 20:41:56 +08:00
use na::{U2, U3};
2018-09-21 04:12:26 +08:00
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
/// Build planar projection matrix along normal axis and right-multiply it to `m`.
2018-09-23 20:41:56 +08:00
pub fn proj2d<N: Number>(m: &TMat3<N>, normal: &TVec2<N>) -> TMat3<N> {
let mut res = TMat3::identity();
2018-09-21 04:12:26 +08:00
{
let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
part -= normal * normal.transpose();
}
m * res
2018-09-21 04:12:26 +08:00
}
/// Build planar projection matrix along normal axis, and right-multiply it to `m`.
2018-09-23 20:41:56 +08:00
pub fn proj<N: Number>(m: &TMat4<N>, normal: &TVec3<N>) -> TMat4<N> {
let mut res = TMat4::identity();
2018-09-21 04:12:26 +08:00
{
let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
part -= normal * normal.transpose();
}
m * res
2018-09-21 04:12:26 +08:00
}
/// Builds a reflection matrix and right-multiply it to `m`.
2018-09-23 20:41:56 +08:00
pub fn reflect2d<N: Number>(m: &TMat3<N>, normal: &TVec2<N>) -> TMat3<N> {
let mut res = TMat3::identity();
2018-09-21 04:12:26 +08:00
{
let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
}
m * res
2018-09-21 04:12:26 +08:00
}
/// Builds a reflection matrix, and right-multiply it to `m`.
2018-09-23 20:41:56 +08:00
pub fn reflect<N: Number>(m: &TMat4<N>, normal: &TVec3<N>) -> TMat4<N> {
let mut res = TMat4::identity();
2018-09-21 04:12:26 +08:00
{
let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
}
m * res
2018-09-21 04:12:26 +08:00
}
/// Builds a scale-bias matrix.
2018-09-23 20:41:56 +08:00
pub fn scale_bias_matrix<N: Number>(scale: N, bias: N) -> TMat4<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::one();
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
)
}
/// Builds a scale-bias matrix, and right-multiply it to `m`.
2018-09-23 20:41:56 +08:00
pub fn scale_bias<N: Number>(m: &TMat4<N>, scale: N, bias: N) -> TMat4<N> {
2018-09-22 23:36:08 +08:00
m * scale_bias_matrix(scale, bias)
2018-09-21 04:12:26 +08:00
}
/// Transforms a matrix with a shearing on X axis.
2018-09-23 20:41:56 +08:00
pub fn shear2d_x<N: Number>(m: &TMat3<N>, y: N) -> TMat3<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::one();
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
}
/// Transforms a matrix with a shearing on Y axis.
2018-09-23 20:41:56 +08:00
pub fn shear_x<N: Number>(m: &TMat4<N>, y: N, z: N) -> TMat4<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::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
}
/// Transforms a matrix with a shearing on Y axis.
2018-09-23 20:41:56 +08:00
pub fn shear2d_y<N: Number>(m: &TMat3<N>, x: N) -> TMat3<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::one();
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
}
/// Transforms a matrix with a shearing on Y axis.
2018-09-23 20:41:56 +08:00
pub fn shear_y<N: Number>(m: &TMat4<N>, x: N, z: N) -> TMat4<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::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
}
/// Transforms a matrix with a shearing on Z axis.
2018-09-23 20:41:56 +08:00
pub fn shear_z<N: Number>(m: &TMat4<N>, x: N, y: N) -> TMat4<N> {
2018-09-21 04:12:26 +08:00
let _0 = N::zero();
let _1 = N::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
}