bevy_math/mat3.rs
1//! Extra utilities for 3×3 matrices.
2
3use glam::{Mat3A, Vec3, Vec3A};
4
5/// Creates a 3×3 matrix that reflects points across the plane at the origin
6/// with the given normal.
7///
8/// This is also known as a [Householder matrix]. It has the general form I -
9/// 2NNᵀ, where N is the normal of the plane and I is the identity matrix.
10///
11/// If the plane across which points are to be reflected isn't at the origin,
12/// you can create a translation matrix that translates the points to the
13/// origin, then apply the matrix that this function returns on top of that, and
14/// finally translate back to the original position.
15///
16/// See the `mirror` example for a demonstration of how you might use this
17/// function.
18///
19/// [Householder matrix]: https://en.wikipedia.org/wiki/Householder_transformation
20#[doc(alias = "householder")]
21pub fn reflection_matrix(plane_normal: Vec3) -> Mat3A {
22 // N times Nᵀ.
23 let n_nt = Mat3A::from_cols(
24 Vec3A::from(plane_normal) * plane_normal.x,
25 Vec3A::from(plane_normal) * plane_normal.y,
26 Vec3A::from(plane_normal) * plane_normal.z,
27 );
28
29 Mat3A::IDENTITY - n_nt * 2.0
30}