Skip to main content

mesh_graph/ops/
transform.rs

1use glam::{Mat4, Quat};
2
3use crate::MeshGraph;
4
5impl MeshGraph {
6    /// Apply a quaternion rotation to the mesh graph (positions and normals).
7    ///
8    /// After this you should probably call `rebuild_bvh` to update the bounding volume hierarchy.
9    pub fn apply_quat(&mut self, quat: Quat) {
10        for pos in self.positions.values_mut() {
11            *pos = quat * *pos;
12        }
13
14        if let Some(normals) = self.vertex_normals.as_mut() {
15            for normal in normals.values_mut() {
16                *normal = quat * *normal;
17            }
18        }
19    }
20
21    /// Apply a projection to the mesh graph (positions and normals).
22    ///
23    /// After this you should probably call `rebuild_bvh` to update the bounding volume hierarchy.
24    pub fn apply_projection(&mut self, projection: Mat4) {
25        for pos in self.positions.values_mut() {
26            *pos = projection.project_point3(*pos);
27        }
28
29        if self.vertex_normals.is_some() {
30            self.compute_vertex_normals();
31        }
32    }
33
34    /// Apply a transform matrix to the mesh graph (positions and normals).
35    ///
36    /// After this you should probably call `rebuild_bvh` to update the bounding volume hierarchy.
37    pub fn apply_transform(&mut self, transform: Mat4) {
38        for pos in self.positions.values_mut() {
39            *pos = transform.transform_point3(*pos);
40        }
41
42        if let Some(normals) = self.vertex_normals.as_mut() {
43            for normal in normals.values_mut() {
44                *normal = transform.transform_vector3(*normal);
45            }
46        }
47    }
48}