nalgebra_glm/gtx/transform.rs
1use na::{Rotation2, Rotation3, Unit};
2
3use crate::aliases::{TMat3, TMat4, TVec2, TVec3};
4use crate::traits::{Number, RealNumber};
5
6/// A rotation 4 * 4 matrix created from an axis of 3 scalars and an angle expressed in radians.
7///
8/// # See also:
9///
10/// * [`scaling()`]
11/// * [`translation()`]
12/// * [`rotation2d()`]
13/// * [`scaling2d()`]
14/// * [`translation2d()`]
15pub fn rotation<T: RealNumber>(angle: T, v: &TVec3<T>) -> TMat4<T> {
16 Rotation3::from_axis_angle(&Unit::new_normalize(*v), angle).to_homogeneous()
17}
18
19/// A 4 * 4 scale matrix created from a vector of 3 components.
20///
21/// # See also:
22///
23/// * [`rotation()`]
24/// * [`translation()`]
25/// * [`rotation2d()`]
26/// * [`scaling2d()`]
27/// * [`translation2d()`]
28pub fn scaling<T: Number>(v: &TVec3<T>) -> TMat4<T> {
29 TMat4::new_nonuniform_scaling(v)
30}
31
32/// A 4 * 4 translation matrix created from the scaling factor on each axis.
33///
34/// # See also:
35///
36/// * [`rotation()`]
37/// * [`scaling()`]
38/// * [`rotation2d()`]
39/// * [`scaling2d()`]
40/// * [`translation2d()`]
41pub fn translation<T: Number>(v: &TVec3<T>) -> TMat4<T> {
42 TMat4::new_translation(v)
43}
44
45/// A rotation 3 * 3 matrix created from an angle expressed in radians.
46///
47/// # See also:
48///
49/// * [`rotation()`]
50/// * [`scaling()`]
51/// * [`translation()`]
52/// * [`scaling2d()`]
53/// * [`translation2d()`]
54pub fn rotation2d<T: RealNumber>(angle: T) -> TMat3<T> {
55 Rotation2::new(angle).to_homogeneous()
56}
57
58/// A 3 * 3 scale matrix created from a vector of 2 components.
59///
60/// # See also:
61///
62/// * [`rotation()`]
63/// * [`scaling()`]
64/// * [`translation()`]
65/// * [`rotation2d()`]
66/// * [`translation2d()`]
67pub fn scaling2d<T: Number>(v: &TVec2<T>) -> TMat3<T> {
68 TMat3::new_nonuniform_scaling(v)
69}
70
71/// A 3 * 3 translation matrix created from the scaling factor on each axis.
72///
73/// # See also:
74///
75/// * [`rotation()`]
76/// * [`scaling()`]
77/// * [`translation()`]
78/// * [`rotation2d()`]
79/// * [`scaling2d()`]
80pub fn translation2d<T: Number>(v: &TVec2<T>) -> TMat3<T> {
81 TMat3::new_translation(v)
82}