Skip to main content

token_value_map/math/
glam_impl.rs

1// AIDEV-NOTE: glam backend -- type aliases and utility functions.
2//
3// ZERO-COST: All utility functions have #[inline(always)] for complete inlining.
4
5pub type Vec2Impl = glam::Vec2;
6pub type Vec3Impl = glam::Vec3;
7pub type Mat3Impl = glam::Mat3;
8pub type Mat4Impl = glam::DMat4; // f64 version.
9
10// AIDEV-NOTE: glam has no separate Point3 type -- Vec3 serves as both.
11pub type Point3Impl = Vec3Impl;
12
13// --- Utility functions ---
14
15/// Create a zero `Vec2`.
16#[inline(always)]
17pub fn vec2_zeros() -> Vec2Impl {
18    Vec2Impl::ZERO
19}
20
21/// Create a zero `Vec3`.
22#[inline(always)]
23pub fn vec3_zeros() -> Vec3Impl {
24    Vec3Impl::ZERO
25}
26
27/// Get `Vec2` data as a `&[f32]` slice.
28#[inline(always)]
29pub fn vec2_as_slice(v: &Vec2Impl) -> &[f32] {
30    v.as_ref()
31}
32
33/// Get `Vec2` data as a `&[f32; 2]` ref.
34#[inline(always)]
35pub fn vec2_as_ref(v: &Vec2Impl) -> &[f32; 2] {
36    v.as_ref()
37}
38
39/// Get `Vec3` data as a `&[f32]` slice.
40#[inline(always)]
41pub fn vec3_as_slice(v: &Vec3Impl) -> &[f32] {
42    v.as_ref()
43}
44
45/// Get `Vec3` data as a `&[f32; 3]` ref.
46#[inline(always)]
47pub fn vec3_as_ref(v: &Vec3Impl) -> &[f32; 3] {
48    v.as_ref()
49}
50
51/// Return a normalized copy of a `Vec3`.
52#[inline(always)]
53pub fn vec3_normalized(v: &Vec3Impl) -> Vec3Impl {
54    v.normalize()
55}
56
57/// Create a `Mat3` from a row-major slice.
58#[inline(always)]
59pub fn mat3_from_row_slice(data: &[f32]) -> Mat3Impl {
60    assert_eq!(data.len(), 9, "Matrix3 requires exactly 9 elements");
61    // glam stores column-major, so transpose from row-major input.
62    Mat3Impl::from_cols_array(&[
63        data[0], data[3], data[6], // Column 0.
64        data[1], data[4], data[7], // Column 1.
65        data[2], data[5], data[8], // Column 2.
66    ])
67}
68
69/// Create a `Mat3` from a column-major slice.
70#[inline(always)]
71pub fn mat3_from_column_slice(data: &[f32]) -> Mat3Impl {
72    assert_eq!(data.len(), 9, "Matrix3 requires exactly 9 elements");
73    Mat3Impl::from_cols_array(data.try_into().unwrap())
74}
75
76/// Return a zero `Mat3`.
77#[inline(always)]
78pub fn mat3_zeros() -> Mat3Impl {
79    Mat3Impl::ZERO
80}
81
82/// Get `Mat3` data as a `&[f32]` slice (column-major order).
83#[inline(always)]
84pub fn mat3_as_slice(m: &Mat3Impl) -> &[f32] {
85    m.as_ref()
86}
87
88/// Iterate over `Mat3` elements (column-major order).
89#[inline(always)]
90pub fn mat3_iter(m: &Mat3Impl) -> impl Iterator<Item = &f32> {
91    m.as_ref().iter()
92}
93
94/// Extract a row from a `Mat3` as `[f32; 3]`.
95#[inline(always)]
96pub fn mat3_row(m: &Mat3Impl, i: usize) -> [f32; 3] {
97    // glam stores column-major: col[c][r].
98    let cols: &[f32; 9] = m.as_ref();
99    [cols[i], cols[i + 3], cols[i + 6]]
100}
101
102/// Create a `DMat4` from a row-major slice.
103#[inline(always)]
104pub fn mat4_from_row_slice(data: &[f64]) -> Mat4Impl {
105    assert_eq!(data.len(), 16, "Matrix4 requires exactly 16 elements");
106    Mat4Impl::from_cols_array(&[
107        data[0], data[4], data[8], data[12], // Column 0.
108        data[1], data[5], data[9], data[13], // Column 1.
109        data[2], data[6], data[10], data[14], // Column 2.
110        data[3], data[7], data[11], data[15], // Column 3.
111    ])
112}
113
114/// Subtract two `DMat4` matrices element-wise.
115#[inline(always)]
116pub fn mat4_sub(a: Mat4Impl, b: Mat4Impl) -> Mat4Impl {
117    a - b
118}
119
120/// Return a zero `DMat4`.
121#[inline(always)]
122pub fn mat4_zeros() -> Mat4Impl {
123    Mat4Impl::ZERO
124}
125
126/// Iterate over `DMat4` elements (column-major order).
127#[inline(always)]
128pub fn mat4_iter(m: &Mat4Impl) -> impl Iterator<Item = &f64> {
129    m.as_ref().iter()
130}
131
132/// Return `DMat4` data as a `&[f64]` slice.
133#[inline(always)]
134pub fn mat4_as_slice(m: &Mat4Impl) -> &[f64] {
135    m.as_ref()
136}
137
138/// Return the origin point (zero vector).
139#[inline(always)]
140pub fn point3_origin() -> Point3Impl {
141    Vec3Impl::ZERO
142}
143
144/// Get point coordinates as a slice.
145#[inline(always)]
146pub fn point3_as_slice(p: &Point3Impl) -> &[f32] {
147    p.as_ref()
148}
149
150/// Get point coordinates as a `&[f32; 3]` ref.
151#[inline(always)]
152pub fn point3_as_ref(p: &Point3Impl) -> &[f32; 3] {
153    p.as_ref()
154}
155
156/// Return the identity `Mat3`.
157#[inline(always)]
158pub fn mat3_identity() -> Mat3Impl {
159    Mat3Impl::IDENTITY
160}
161
162/// Return the identity `DMat4`.
163#[inline(always)]
164pub fn mat4_identity() -> Mat4Impl {
165    Mat4Impl::IDENTITY
166}
167
168/// Get element at `(row, col)` from a `Mat3`.
169#[inline(always)]
170pub fn mat3(m: &Mat3Impl, row: usize, col: usize) -> f32 {
171    // glam stores column-major.
172    m.col(col)[row]
173}
174
175/// Get element at `(row, col)` from a `DMat4`.
176#[inline(always)]
177pub fn mat4(m: &Mat4Impl, row: usize, col: usize) -> f64 {
178    // glam stores column-major.
179    m.col(col)[row]
180}
181
182/// Subtract two `Mat3` matrices element-wise.
183#[inline(always)]
184pub fn mat3_sub(a: Mat3Impl, b: Mat3Impl) -> Mat3Impl {
185    a - b
186}
187
188/// Create a diagonal `Mat3` with `v` on the diagonal.
189#[inline(always)]
190pub fn mat3_from_diagonal_element(v: f32) -> Mat3Impl {
191    Mat3Impl::from_diagonal(glam::Vec3::splat(v))
192}