Skip to main content

mln_util/
mat4.rs

1// This is an incomplete port of http://glmatrix.net/
2//
3// Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV
4//
5// This software is provided 'as-is', without any express or implied warranty.
6// In no event will the authors be held liable for any damages arising from the
7// use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it
11// freely, subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented; you must not claim
14//    that you wrote the original software. If you use this software in a
15//    product, an acknowledgment in the product documentation would be
16//    appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such, and must not be
19//    misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22
23pub type Mat4 = [f64; 16];
24
25/// How a graphics API's clip space holds depth.
26#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub enum ClipDepthRange {
28    /// Clip z from 0 to w: Metal, Vulkan, D3D12 and WebGPU.
29    ZeroToOne,
30    /// Clip z from -w to w: OpenGL.
31    NegativeOneToOne,
32}
33#[cfg(test)]
34pub(crate) type Matf4 = [f32; 16];
35
36/// Matrix operations on `Mat4`.
37///
38/// C++ lets `out` alias `a` (and `b`); in Rust the caller passes a copy
39/// (`let a = m; matrix::translate(&mut m, &a, x, y, z)`). The C++ aliased
40/// branches of `translate` and `rotate_*` compute the same values as the
41/// non-aliased ones (they only skip copying the unchanged entries), and
42/// `multiply`/`transform_mat4` read every input before the output entry that
43/// overwrites it, so passing a copy gives bit-identical results.
44///
45/// The C++ literals are floats (`1.0f / (left - right)`) promoted to double;
46/// all of them are exactly representable, so the arithmetic here is f64.
47pub mod matrix {
48    use super::Mat4;
49    #[cfg(test)]
50    use super::Matf4;
51    use crate::vectors::Vec4;
52
53    pub fn identity(out: &mut Mat4) {
54        out[0] = 1.0;
55        out[1] = 0.0;
56        out[2] = 0.0;
57        out[3] = 0.0;
58        out[4] = 0.0;
59        out[5] = 1.0;
60        out[6] = 0.0;
61        out[7] = 0.0;
62        out[8] = 0.0;
63        out[9] = 0.0;
64        out[10] = 1.0;
65        out[11] = 0.0;
66        out[12] = 0.0;
67        out[13] = 0.0;
68        out[14] = 0.0;
69        out[15] = 1.0;
70    }
71
72    #[cfg(test)]
73    #[must_use]
74    pub(crate) const fn identity4() -> Mat4 {
75        [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
76    }
77
78    #[cfg(test)]
79    #[must_use]
80    pub(crate) const fn identity4f() -> Matf4 {
81        [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
82    }
83
84    /// Returns `true` on FAILURE (singular matrix), as in C++; `out` is then
85    /// left untouched.
86    pub fn invert(out: &mut Mat4, a: &Mat4) -> bool {
87        let a00 = a[0];
88        let a01 = a[1];
89        let a02 = a[2];
90        let a03 = a[3];
91        let a10 = a[4];
92        let a11 = a[5];
93        let a12 = a[6];
94        let a13 = a[7];
95        let a20 = a[8];
96        let a21 = a[9];
97        let a22 = a[10];
98        let a23 = a[11];
99        let a30 = a[12];
100        let a31 = a[13];
101        let a32 = a[14];
102        let a33 = a[15];
103        let b00 = a00 * a11 - a01 * a10;
104        let b01 = a00 * a12 - a02 * a10;
105        let b02 = a00 * a13 - a03 * a10;
106        let b03 = a01 * a12 - a02 * a11;
107        let b04 = a01 * a13 - a03 * a11;
108        let b05 = a02 * a13 - a03 * a12;
109        let b06 = a20 * a31 - a21 * a30;
110        let b07 = a20 * a32 - a22 * a30;
111        let b08 = a20 * a33 - a23 * a30;
112        let b09 = a21 * a32 - a22 * a31;
113        let b10 = a21 * a33 - a23 * a31;
114        let b11 = a22 * a33 - a23 * a32;
115        // Calculate the determinant
116        let mut det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
117
118        // C++ `if (!det)`: true for ±0 only; a NaN determinant goes on.
119        if det == 0.0 {
120            return true;
121        }
122        det = 1.0 / det;
123
124        out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
125        out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
126        out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
127        out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
128        out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
129        out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
130        out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
131        out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
132        out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
133        out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
134        out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
135        out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
136        out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
137        out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
138        out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
139        out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
140
141        false
142    }
143
144    pub fn ortho(out: &mut Mat4, left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) {
145        let lr = 1.0 / (left - right);
146        let bt = 1.0 / (bottom - top);
147        let nf = 1.0 / (near - far);
148        out[0] = -2.0 * lr;
149        out[1] = 0.0;
150        out[2] = 0.0;
151        out[3] = 0.0;
152        out[4] = 0.0;
153        out[5] = -2.0 * bt;
154        out[6] = 0.0;
155        out[7] = 0.0;
156        out[8] = 0.0;
157        out[9] = 0.0;
158        out[10] = 2.0 * nf;
159        out[11] = 0.0;
160        out[12] = (left + right) * lr;
161        out[13] = (top + bottom) * bt;
162        out[14] = (far + near) * nf;
163        out[15] = 1.0;
164    }
165
166    pub fn perspective(out: &mut Mat4, fovy: f64, aspect: f64, near: f64, far: f64) {
167        let f = 1.0 / (fovy / 2.0).tan();
168        let nf = 1.0 / (near - far);
169        out[0] = f / aspect;
170        out[1] = 0.0;
171        out[2] = 0.0;
172        out[3] = 0.0;
173        out[4] = 0.0;
174        out[5] = f;
175        out[6] = 0.0;
176        out[7] = 0.0;
177        out[8] = 0.0;
178        out[9] = 0.0;
179        out[10] = (far + near) * nf;
180        out[11] = -1.0;
181        out[12] = 0.0;
182        out[13] = 0.0;
183        out[14] = (2.0 * far * near) * nf;
184        out[15] = 0.0;
185    }
186
187    pub fn translate(out: &mut Mat4, a: &Mat4, x: f64, y: f64, z: f64) {
188        // C++ has a separate `&a == &out` branch that only writes out[12..16];
189        // it computes the same values as this one.
190        let a00 = a[0];
191        let a01 = a[1];
192        let a02 = a[2];
193        let a03 = a[3];
194        let a10 = a[4];
195        let a11 = a[5];
196        let a12 = a[6];
197        let a13 = a[7];
198        let a20 = a[8];
199        let a21 = a[9];
200        let a22 = a[10];
201        let a23 = a[11];
202
203        out[0] = a00;
204        out[1] = a01;
205        out[2] = a02;
206        out[3] = a03;
207        out[4] = a10;
208        out[5] = a11;
209        out[6] = a12;
210        out[7] = a13;
211        out[8] = a20;
212        out[9] = a21;
213        out[10] = a22;
214        out[11] = a23;
215
216        out[12] = a00 * x + a10 * y + a20 * z + a[12];
217        out[13] = a01 * x + a11 * y + a21 * z + a[13];
218        out[14] = a02 * x + a12 * y + a22 * z + a[14];
219        out[15] = a03 * x + a13 * y + a23 * z + a[15];
220    }
221
222    pub fn rotate_x(out: &mut Mat4, a: &Mat4, rad: f64) {
223        let s = rad.sin();
224        let c = rad.cos();
225        let a10 = a[4];
226        let a11 = a[5];
227        let a12 = a[6];
228        let a13 = a[7];
229        let a20 = a[8];
230        let a21 = a[9];
231        let a22 = a[10];
232        let a23 = a[11];
233
234        // Source and destination always differ here: copy the unchanged rows
235        out[0] = a[0];
236        out[1] = a[1];
237        out[2] = a[2];
238        out[3] = a[3];
239        out[12] = a[12];
240        out[13] = a[13];
241        out[14] = a[14];
242        out[15] = a[15];
243
244        // Perform axis-specific matrix multiplication
245        out[4] = a10 * c + a20 * s;
246        out[5] = a11 * c + a21 * s;
247        out[6] = a12 * c + a22 * s;
248        out[7] = a13 * c + a23 * s;
249        out[8] = a20 * c - a10 * s;
250        out[9] = a21 * c - a11 * s;
251        out[10] = a22 * c - a12 * s;
252        out[11] = a23 * c - a13 * s;
253    }
254
255    pub fn rotate_y(out: &mut Mat4, a: &Mat4, rad: f64) {
256        let s = rad.sin();
257        let c = rad.cos();
258        let a00 = a[0];
259        let a01 = a[1];
260        let a02 = a[2];
261        let a03 = a[3];
262        let a20 = a[8];
263        let a21 = a[9];
264        let a22 = a[10];
265        let a23 = a[11];
266
267        // Source and destination always differ here: copy the unchanged rows
268        out[4] = a[4];
269        out[5] = a[5];
270        out[6] = a[6];
271        out[7] = a[7];
272        out[12] = a[12];
273        out[13] = a[13];
274        out[14] = a[14];
275        out[15] = a[15];
276
277        // Perform axis-specific matrix multiplication
278        out[0] = a00 * c - a20 * s;
279        out[1] = a01 * c - a21 * s;
280        out[2] = a02 * c - a22 * s;
281        out[3] = a03 * c - a23 * s;
282        out[8] = a00 * s + a20 * c;
283        out[9] = a01 * s + a21 * c;
284        out[10] = a02 * s + a22 * c;
285        out[11] = a03 * s + a23 * c;
286    }
287
288    pub fn rotate_z(out: &mut Mat4, a: &Mat4, rad: f64) {
289        let s = rad.sin();
290        let c = rad.cos();
291        let a00 = a[0];
292        let a01 = a[1];
293        let a02 = a[2];
294        let a03 = a[3];
295        let a10 = a[4];
296        let a11 = a[5];
297        let a12 = a[6];
298        let a13 = a[7];
299
300        // Source and destination always differ here: copy the unchanged last row
301        out[8] = a[8];
302        out[9] = a[9];
303        out[10] = a[10];
304        out[11] = a[11];
305        out[12] = a[12];
306        out[13] = a[13];
307        out[14] = a[14];
308        out[15] = a[15];
309
310        // Perform axis-specific matrix multiplication
311        out[0] = a00 * c + a10 * s;
312        out[1] = a01 * c + a11 * s;
313        out[2] = a02 * c + a12 * s;
314        out[3] = a03 * c + a13 * s;
315        out[4] = a10 * c - a00 * s;
316        out[5] = a11 * c - a01 * s;
317        out[6] = a12 * c - a02 * s;
318        out[7] = a13 * c - a03 * s;
319    }
320
321    pub fn scale(out: &mut Mat4, a: &Mat4, x: f64, y: f64, z: f64) {
322        out[0] = a[0] * x;
323        out[1] = a[1] * x;
324        out[2] = a[2] * x;
325        out[3] = a[3] * x;
326        out[4] = a[4] * y;
327        out[5] = a[5] * y;
328        out[6] = a[6] * y;
329        out[7] = a[7] * y;
330        out[8] = a[8] * z;
331        out[9] = a[9] * z;
332        out[10] = a[10] * z;
333        out[11] = a[11] * z;
334        out[12] = a[12];
335        out[13] = a[13];
336        out[14] = a[14];
337        out[15] = a[15];
338    }
339
340    pub fn multiply(out: &mut Mat4, a: &Mat4, b: &Mat4) {
341        let a00 = a[0];
342        let a01 = a[1];
343        let a02 = a[2];
344        let a03 = a[3];
345        let a10 = a[4];
346        let a11 = a[5];
347        let a12 = a[6];
348        let a13 = a[7];
349        let a20 = a[8];
350        let a21 = a[9];
351        let a22 = a[10];
352        let a23 = a[11];
353        let a30 = a[12];
354        let a31 = a[13];
355        let a32 = a[14];
356        let a33 = a[15];
357
358        // Cache only the current line of the second matrix
359        let mut b0 = b[0];
360        let mut b1 = b[1];
361        let mut b2 = b[2];
362        let mut b3 = b[3];
363        out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
364        out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
365        out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
366        out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
367
368        b0 = b[4];
369        b1 = b[5];
370        b2 = b[6];
371        b3 = b[7];
372        out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
373        out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
374        out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
375        out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
376
377        b0 = b[8];
378        b1 = b[9];
379        b2 = b[10];
380        b3 = b[11];
381        out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
382        out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
383        out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
384        out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
385
386        b0 = b[12];
387        b1 = b[13];
388        b2 = b[14];
389        b3 = b[15];
390        out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
391        out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
392        out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
393        out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
394    }
395
396    /// Transform a 4D vector by a 4x4 matrix (v * m).
397    pub fn transform_mat4(out: &mut Vec4, a: &Vec4, m: &Mat4) {
398        let x = a[0];
399        let y = a[1];
400        let z = a[2];
401        let w = a[3];
402        out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
403        out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
404        out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
405        out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
406    }
407
408    /// The value-returning `transformMat4(a, m)` overload; also the C++
409    /// `operator*(mat4, vec4)`. a * m if m is row-major, m * a if m is
410    /// column-major (OpenGL style).
411    #[must_use]
412    pub fn transform_mat4_value(a: &Vec4, m: &Mat4) -> Vec4 {
413        let mut result = [0.0; 4];
414        transform_mat4(&mut result, a, m);
415        result
416    }
417
418    #[cfg(test)]
419    pub(crate) fn diffsplit(out_value: &mut Matf4, out_diff: &mut Matf4, source: &Mat4) {
420        for i in 0..16 {
421            out_value[i] = source[i] as f32;
422            // double - float in double, then narrowed to float
423            out_diff[i] = (source[i] - f64::from(out_value[i])) as f32;
424        }
425    }
426}
427
428#[cfg(test)]
429#[path = "mat4_test.rs"]
430mod mat4_test;