nalgebra_glm/gtx/
transform2.rs

1use crate::aliases::{TMat3, TMat4, TVec2, TVec3};
2use crate::traits::Number;
3use crate::RealNumber;
4
5/// Build planar projection matrix along normal axis and right-multiply it to `m`.
6pub fn proj2d<T: Number>(m: &TMat3<T>, normal: &TVec2<T>) -> TMat3<T> {
7    let mut res = TMat3::identity();
8
9    {
10        let mut part = res.fixed_view_mut::<2, 2>(0, 0);
11        part -= normal * normal.transpose();
12    }
13
14    m * res
15}
16
17/// Build planar projection matrix along normal axis, and right-multiply it to `m`.
18pub fn proj<T: Number>(m: &TMat4<T>, normal: &TVec3<T>) -> TMat4<T> {
19    let mut res = TMat4::identity();
20
21    {
22        let mut part = res.fixed_view_mut::<3, 3>(0, 0);
23        part -= normal * normal.transpose();
24    }
25
26    m * res
27}
28
29/// Builds a reflection matrix and right-multiply it to `m`.
30pub fn reflect2d<T: RealNumber>(m: &TMat3<T>, normal: &TVec2<T>) -> TMat3<T> {
31    let mut res = TMat3::identity();
32
33    {
34        let mut part = res.fixed_view_mut::<2, 2>(0, 0);
35        part -= (normal * T::from_subset(&2.0)) * normal.transpose();
36    }
37
38    m * res
39}
40
41/// Builds a reflection matrix, and right-multiply it to `m`.
42pub fn reflect<T: RealNumber>(m: &TMat4<T>, normal: &TVec3<T>) -> TMat4<T> {
43    let mut res = TMat4::identity();
44
45    {
46        let mut part = res.fixed_view_mut::<3, 3>(0, 0);
47        part -= (normal * T::from_subset(&2.0)) * normal.transpose();
48    }
49
50    m * res
51}
52
53/// Builds a scale-bias matrix.
54pub fn scale_bias_matrix<T: Number>(scale: T, bias: T) -> TMat4<T> {
55    let _0 = T::zero();
56    let _1 = T::one();
57
58    TMat4::new(
59        scale, _0, _0, bias, _0, scale, _0, bias, _0, _0, scale, bias, _0, _0, _0, _1,
60    )
61}
62
63/// Builds a scale-bias matrix, and right-multiply it to `m`.
64pub fn scale_bias<T: Number>(m: &TMat4<T>, scale: T, bias: T) -> TMat4<T> {
65    m * scale_bias_matrix(scale, bias)
66}
67
68/// Transforms a matrix with a shearing on X axis.
69pub fn shear2d_x<T: Number>(m: &TMat3<T>, y: T) -> TMat3<T> {
70    let _0 = T::zero();
71    let _1 = T::one();
72
73    let shear = TMat3::new(_1, y, _0, _0, _1, _0, _0, _0, _1);
74    m * shear
75}
76
77/// Transforms a matrix with a shearing on Y axis.
78pub fn shear_x<T: Number>(m: &TMat4<T>, y: T, z: T) -> TMat4<T> {
79    let _0 = T::zero();
80    let _1 = T::one();
81    let shear = TMat4::new(_1, _0, _0, _0, y, _1, _0, _0, z, _0, _1, _0, _0, _0, _0, _1);
82
83    m * shear
84}
85
86/// Transforms a matrix with a shearing on Y axis.
87pub fn shear2d_y<T: Number>(m: &TMat3<T>, x: T) -> TMat3<T> {
88    let _0 = T::zero();
89    let _1 = T::one();
90
91    let shear = TMat3::new(_1, _0, _0, x, _1, _0, _0, _0, _1);
92    m * shear
93}
94
95/// Transforms a matrix with a shearing on Y axis.
96pub fn shear_y<T: Number>(m: &TMat4<T>, x: T, z: T) -> TMat4<T> {
97    let _0 = T::zero();
98    let _1 = T::one();
99    let shear = TMat4::new(_1, x, _0, _0, _0, _1, _0, _0, _0, z, _1, _0, _0, _0, _0, _1);
100
101    m * shear
102}
103
104/// Transforms a matrix with a shearing on Z axis.
105pub fn shear_z<T: Number>(m: &TMat4<T>, x: T, y: T) -> TMat4<T> {
106    let _0 = T::zero();
107    let _1 = T::one();
108    let shear = TMat4::new(_1, _0, x, _0, _0, _1, y, _0, _0, _0, _1, _0, _0, _0, _0, _1);
109
110    m * shear
111}