SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_matrix.h
Go to the documentation of this file.
1/*! \file
2 \brief API for operating on MxN matrices within memory.
3 \ingroup matrix
4
5 This file provides a collection of routines for manipulating
6 MxN matrices which are stored within memory, rather than in
7 the XMTRX FP register back-bank.
8
9 Some of these routines are simply loading into the back-bank
10 and are performing work there, temporarily. Some of them have
11 implementations which have specific optimizations for when the
12 matrices are NOT within such registers, which are faster than
13 having to go through XMTRX and clobbering the back-bank.
14
15 \todo
16 - shz_mat4x4_add_symmetric_skew()
17 - shz_mat4x4_add_diagonal()
18 - shz_mat4x4_apply_transpose()
19 - shz_mat4x4_apply_unaligned()
20 - shz_mat4x4_mult_transpose()
21 - shz_mat4x4_mult_unaligned()
22
23 \author 2025, 2026 Falco Girgis
24 \author 2025 Daniel Fairchild
25
26 \copyright MIT License
27*/
28
29#ifndef SHZ_MATRIX_H
30#define SHZ_MATRIX_H
31
32#include "shz_vector.h"
33#include "shz_quat.h"
34#include "shz_xmtrx.h"
35
36/*! \defgroup matrix Matrices
37 \brief In-Memory Matrix Manipulation
38
39 These types and their corresponding functions are for working
40 with matrices stored within memory, as opposed to being preloaded
41 within the XMTRX back-bank of FP registers. Typically this is
42 desirable for one-off operations where there is no batching of
43 matrix operations, especially when clobbering XMTRX is undesirable.
44
45 For most transform types, this API offers 4 different "versions" of
46 the operation. Using translation as an example:
47 - shz_mat4x4_init_translation(): **Initializes** the the matrix
48 to a given transform, setting the other components to identity.
49 - shz_mat4x4_set_translation(): **Sets** only the values
50 corresponding to the given transform, leaving the others alone.
51 - shz_mat4x4_apply_translation(): **Apply** transform operation,
52 updates only the values corresponding to the given transform based
53 on their current values (additively in this case, multiplicatively
54 for scaling and rotation).
55 - shz_mat4x4_translate(): **GL-based** transform operation,
56 multiplying and accumulating the given matrix by a matrix which
57 has been initialized to the given transform.
58
59 \warning
60 Beware that some of these routines clobber the matrix currently loaded
61 as the active 4x4 matrix, XMTRX.
62
63 \sa xmtrx
64*/
65
66SHZ_DECLS_BEGIN
67
68/*! Structure representing a 4x4 column-major matrix.
69
70 \warning
71 This structure MUST be aligned on 8-byte boundaries!
72*/
73typedef SHZ_ALIGNAS(8) struct shz_mat4x4 {
74 union { //!< Inner convenience union.
75 float elem[16]; //!< Access the matrix as a 1D array of 16 single-precision floats.
76 float elem2D[4][4]; //!< Access the matrix as a 2D array of 4x4 single-precision floats.
77 shz_vec4_t col[4]; //!< Access the matrix as an array of 4 1x4 column vectors.
78 struct { //!< Named column vectors.
79 shz_vec4_t left; //!< Access the first column of the matrix as a 1x4 vector.
80 shz_vec4_t up; //!< Access the second column of the matrix as a 1x4 vector.
81 shz_vec4_t forward; //!< Access the third column of the matrix as a 1x4 vector.
82 shz_vec4_t pos; //!< Access the last column of the matrix as a 1x4 vector.
83 };
84 };
85} shz_mat4x4_t;
86
87//! Alternate shz_mat4x4_t C typedef for those who hate POSIX style.
88typedef shz_mat4x4_t shz_mat4x4;
89
90/*! \name Initialization
91 \brief Routines for fully initializing a matrix.
92 @{
93*/
94
95/*! Initializes the given matrix to the identity matrix as fast as possible.
96
97 \warning This routine will NOT zero out NaNs properly!
98 \warning This routine clobbers XMTRX.
99
100 \sa shz_mat4x4_init_identity_safe()
101*/
102SHZ_INLINE void shz_mat4x4_init_identity(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
103
104/*! Initializes the given matrix to the identity matrix, safely zeroing out NaN values.
105
106 \warning This routine clobbers XMTRX.
107
108 \sa shz_mat4x4_init_identity()
109*/
110SHZ_INLINE void shz_mat4x4_init_identity_safe(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
111
112/*! Initializes the given matrix with all 0s for its element values.
113
114 \warning This routine clobbers XMTRX.
115*/
116SHZ_INLINE void shz_mat4x4_init_zero(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
117
118/*! Initializes the given matrix with all 1s for its element values.
119
120 \warning This routine clobbers XMTRX.
121*/
122SHZ_INLINE void shz_mat4x4_init_one(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
123
124/*! Initializes the given matrix with all elements assigned to the given value.
125
126 \warning This routine clobbers XMTRX.
127*/
128SHZ_INLINE void shz_mat4x4_init_fill(shz_mat4x4_t* mat, float value) SHZ_NOEXCEPT;
129
130/*! Initializes the given matrix to a 3D translation matrix with the given coordinates.
131
132 \warning This routine clobbers XMTRX.
133*/
134SHZ_INLINE void shz_mat4x4_init_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
135
136/*! Initializes the given matrix to a 3D scaling matrix with the given dimensions.
137
138 \warning This routine clobbers XMTRX.
139*/
140SHZ_INLINE void shz_mat4x4_init_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
141
142/*! Initializes the given matrix to a 3D rotation matrix by \p xAngle radians over the X-axis.
143
144 \warning This routine clobbers XMTRX.
145*/
146SHZ_INLINE void shz_mat4x4_init_rotation_x(shz_mat4x4_t* mat, float xAngle) SHZ_NOEXCEPT;
147
148/*! Initializes the given matrix to a 3D rotation matrix by \p yAngle radians over the Y-axis.
149
150 \warning This routine clobbers XMTRX.
151*/
152SHZ_INLINE void shz_mat4x4_init_rotation_y(shz_mat4x4_t* mat, float yAngle) SHZ_NOEXCEPT;
153
154/*! Initializes the given matrix to a 3D rotation matrix by \p zAngle radians over the Z-axis.
155
156 \warning This routine clobber XMTRX.
157*/
158SHZ_INLINE void shz_mat4x4_init_rotation_z(shz_mat4x4_t* mat, float zAngle) SHZ_NOEXCEPT;
159
160/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan X-Y-Z angles.
161
162 \warning This routine clobbers XMTRX.
163*/
164SHZ_INLINE void shz_mat4x4_init_rotation_xyz(shz_mat4x4_t* mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT;
165
166/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
167
168 \warning This routine clobbers XMTRX.
169*/
170SHZ_INLINE void shz_mat4x4_init_rotation_zyx(shz_mat4x4_t* mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT;
171
172/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-X-Y angles.
173
174 \warning This routine clobbers XMTRX.
175*/
176SHZ_INLINE void shz_mat4x4_init_rotation_zxy(shz_mat4x4_t* mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT;
177
178/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Y-X-Z angles.
179
180 \warning This routine clobbers XMTRX.
181*/
182SHZ_INLINE void shz_mat4x4_init_rotation_yxz(shz_mat4x4_t* mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT;
183
184/*! Initializes the given matrix to a 3D rotation matrix about the given \p axis rotated by \p angle radians.
185
186 \warning This routine clobbers XMTRX.
187*/
188SHZ_INLINE void shz_mat4x4_init_rotation(shz_mat4x4_t* mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
189
190/*! Initializes the given matrix to a 3D rotation matrix with its orientation given by a quaternion.
191
192 \warning This routine clobbers XMTRX.
193*/
194SHZ_INLINE void shz_mat4x4_init_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
195
196/*! Initializes the given matrix to a diagonal matrix with the given 4 values.
197
198 \warning This routine clobbers XMTRX.
199*/
200SHZ_INLINE void shz_mat4x4_init_diagonal(shz_mat4x4_t* mat, float x, float y, float z, float w) SHZ_NOEXCEPT;
201
202/*! Initializes the given matrix to an upper triangular matrix whose nonzero entries have the given value.
203
204 \warning This routine clobbers XMTRX.
205*/
206SHZ_INLINE void shz_mat4x4_init_upper_triangular(shz_mat4x4_t* mat, float col1, shz_vec2_t col2, shz_vec3_t col3, shz_vec4_t col4) SHZ_NOEXCEPT;
207
208/*! Initializes the given matrix to a lower triangular matrix whose nonzero entries have the given value.
209
210 \warning This routine clobbers XMTRX.
211*/
212SHZ_INLINE void shz_mat4x4_init_lower_triangular(shz_mat4x4_t* mat, shz_vec4_t col1, shz_vec3_t col2, shz_vec2_t col3, float col4) SHZ_NOEXCEPT;
213
214/*! Initializes the given matrix to be the symmetric skew of the given 3D vector components.
215
216 \note
217 This can be useful for batching the cross-product operation against a constant vector.
218 For one-offs, prefer shz_vec3_cross().
219
220 \warning This routine clobbers XMTRX.
221*/
222SHZ_INLINE void shz_mat4x4_init_symmetric_skew(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
223
224/*! Initializes the given matrix to be the result from taking the outer product of the two given 4D vectors.
225
226 \warning This routine clobbers XMTRX.
227*/
228SHZ_INLINE void shz_mat4x4_init_outer_product(shz_mat4x4_t* mat, shz_vec4_t v1, shz_vec4_t v2) SHZ_NOEXCEPT;
229
230/*! Initializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
231
232 \warning This routine clobbers XMTRX.
233*/
234SHZ_INLINE void shz_mat4x4_init_permutation_wxyz(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
235
236/*! Initializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
237
238 \warning This routine clobbers XMTRX.
239*/
240SHZ_INLINE void shz_mat4x4_init_permutation_yzwx(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
241
242/*! Initializes the given matrix to the viewport matrix with the given dimenions.
243
244 \sa This routine clobbers XMTRX.
245*/
246SHZ_INLINE void shz_mat4x4_init_screen(shz_mat4x4_t* mat, float width, float height) SHZ_NOEXCEPT;
247
248/*! Initializes the given matrix to a "lookAt" view matrix.
249
250 \warning This routine clobbers XMTRX.
251*/
252SHZ_INLINE void shz_mat4x4_init_lookat(shz_mat4x4_t* mat, shz_vec3_t eye, shz_vec3_t center, shz_vec3_t up) SHZ_NOEXCEPT;
253
254/*! Initializes the given matrix to an orthographic projection matrix.
255
256 \warning This routine clobbers XMTRX.
257*/
258SHZ_INLINE void shz_mat4x4_init_ortho(shz_mat4x4_t* mat, float left, float right, float bottom, float top, float near, float far) SHZ_NOEXCEPT;
259
260/*! Initializes the given matrix to a frustum projection matrix.
261
262 \warning This routine clobbers XMTRX.
263*/
264SHZ_INLINE void shz_mat4x4_init_frustum(shz_mat4x4_t* mat, float left, float right, float bottom, float top, float near, float far) SHZ_NOEXCEPT;
265
266/*! Initializes the given matrix to a perspective projection matrix.
267
268 \warning This routine clobbers XMTRX.
269*/
270SHZ_INLINE void shz_mat4x4_init_perspective(shz_mat4x4_t* mat, float fov, float aspect, float near_z) SHZ_NOEXCEPT;
271
272//! @}
273
274
275/*! \name Decomposition
276 \brief Routines for decomposing a matrix into its constituent transforms.
277 @{
278*/
279
280/*! Decomposes a 4x4 transform matrix into translation, rotation, and scale.
281
282 \note Any output pointer may be NULL to skip that component.
283 \warning This routine clobbers XMTRX.
284*/
285void shz_mat4x4_decompose(const shz_mat4x4_t* mat,
286 shz_vec3_t* translation,
287 shz_quat_t* rotation,
288 shz_vec3_t* scale) SHZ_NOEXCEPT;
289
290//! @}
291
292/*! \name Getting
293 \brief Routines for getting specific values within a matrix.
294 @{
295*/
296
297//! Extracts the \p row index as a 4D row vector from the given matrix.
298SHZ_FORCE_INLINE shz_vec4_t shz_mat4x4_row(const shz_mat4x4_t* mat, size_t row) SHZ_NOEXCEPT;
299
300//! Extracts the \p col index as a 4D column vector from the given matrix.
301SHZ_FORCE_INLINE shz_vec4_t shz_mat4x4_col(const shz_mat4x4_t* mat, size_t col) SHZ_NOEXCEPT;
302
303//! Returns the determinant of the given 4x4 matrix.
304SHZ_INLINE float shz_mat4x4_determinant(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
305
306//! Returns the trace of the given 4x4 matrix.
307SHZ_FORCE_INLINE float shz_mat4x4_trace(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
308
309//! Extracts the top-left 3x3 of the given 4D matrix.
310SHZ_INLINE void shz_mat4x4_3x3(const shz_mat4x4_t* mat4, shz_mat3x3_t* mat3) SHZ_NOEXCEPT;
311
312//! Extracts and inverts the top-level, unscaled 3x3 of the given 4x4 matrix.
313SHZ_INLINE void shz_mat4x4_3x3_inverse_unscaled(const shz_mat4x4_t* mat4, shz_mat3x3_t* invmat3) SHZ_NOEXCEPT;
314
315//! Extracts and inverts the top-level 3x3 (which may be scaled) of the given 4x4 matrix.
316SHZ_INLINE void shz_mat4x4_3x3_inverse(const shz_mat4x4_t* mat4, shz_mat3x3_t* invmat3) SHZ_NOEXCEPT;
317
318//! Returns the determinant of the given 4x4 matrix's internal top-level 3x3 matrix.
319SHZ_INLINE float shz_mat4x4_3x3_determinant(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
320
321//! @}
322
323/*! \name Setting
324 \brief Routines for setting specific values within a matrix
325 @{
326*/
327
328//! Sets the values of \p mat at the given \p row to those of the 4D vector, \p vec.
329SHZ_FORCE_INLINE void shz_mat4x4_set_row(shz_mat4x4_t* mat, size_t row, shz_vec4_t vec) SHZ_NOEXCEPT;
330
331//! Sets the values of \p mat at the given \p col to those of the 4D vector, \p vec.
332SHZ_FORCE_INLINE void shz_mat4x4_set_col(shz_mat4x4_t* mat, size_t col, shz_vec4_t vec) SHZ_NOEXCEPT;
333
334//! Swaps the 4D row vectors located at \p row1 and \p row2 within \p mat.
335SHZ_INLINE void shz_mat4x4_swap_rows(shz_mat4x4_t* mat, size_t row1, size_t row2) SHZ_NOEXCEPT;
336
337//! Swaps the 4D column vectors located at \p col1 and \p col2 within \p mat.
338SHZ_INLINE void shz_mat4x4_swap_cols(shz_mat4x4_t* mat, size_t col1, size_t col2) SHZ_NOEXCEPT;
339
340//! Assigns only the 3D translation-related elements of the given matrix to the given values.
341SHZ_INLINE void shz_mat4x4_set_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
342
343//! Assigns only the 3D scale-related elements of the given matrix to the given values.
344SHZ_INLINE void shz_mat4x4_set_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
345
346//! Sets just the rotational component of the matrix to the orientation given by a quaternion, keeping the other elements in-tact.
347SHZ_INLINE void shz_mat4x4_set_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
348
349//! Assigns only the 4 elements along the diagonal of the given matrix to the given values.
350SHZ_INLINE void shz_mat4x4_set_diagonal(shz_mat4x4_t* mat, float x, float y, float z, float w) SHZ_NOEXCEPT;
351
352//! @}
353
354/*! \name Applying
355 \brief Routines for multiplying and accumulating onto the given matrix.
356 @{
357*/
358
359/*! Multiplies and accumulates the \p src 4x4 matrix onto the \p dst 4x4 matrix.
360
361 \warning This routine clobbers XMTRX.
362*/
363SHZ_INLINE void shz_mat4x4_apply(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
364
365/*! Multiplies and accumulates the unaligned \p src 4x4 matrix onto the \p dst 4x4 matrix.
366
367 \warning This routine clobbers XMTRX.
368*/
369SHZ_INLINE void shz_mat4x4_apply_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
370
371/*! Multiplies and accumulates the transposed \p src 4x4 matrix onto the \p dst 4x4 matrix.
372
373 \warning This routine clobbers XMTRX.
374*/
375SHZ_INLINE void shz_mat4x4_apply_transpose(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
376
377/*! Multiplies and accumulates the transposed unaligned \p src 4x4 matrix onto the \p dst 4x4 matrix.
378
379 \warning This routine clobbers XMTRX.
380*/
381SHZ_INLINE void shz_mat4x4_apply_transpose_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
382
383/*! Adds the given 3D vector components to the translational values of the given matrix.
384
385 \note This routine does not use XMTRX.
386*/
387SHZ_INLINE void shz_mat4x4_apply_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
388
389/*! Multiplies and accumulates the scale-related elements of the given matrix by the given 3D components.
390
391 \warning This routine clobbers XMTRX.
392*/
393SHZ_INLINE void shz_mat4x4_apply_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
394
395/*! Multiplies and accumulates a rotation matrix by \p xAngle radians about the X-axis onto the given matrix.
396
397 \warning This routine clobbers XMTRX.
398*/
399SHZ_INLINE void shz_mat4x4_apply_rotation_x(shz_mat4x4_t* mat, float xAngle) SHZ_NOEXCEPT;
400
401/*! Multiplies and accumulates a rotation matrix by \p yAngle radians about the Y-axis onto the given matrix.
402
403 \warning This routine clobbers XMTRX.
404*/
405SHZ_INLINE void shz_mat4x4_apply_rotation_y(shz_mat4x4_t* mat, float yAngle) SHZ_NOEXCEPT;
406
407/*! Multiplies and accumulates a rotation matrix by \p zAngle radians about the Z-axis onto the given matrix.
408
409 \warning This routine clobbers XMTRX.
410*/
411SHZ_INLINE void shz_mat4x4_apply_rotation_z(shz_mat4x4_t* mat, float zAngle) SHZ_NOEXCEPT;
412
413/*! Rotates the given transform matrix about the X axis, then the Y axis, then the Z axis by the given angles in radians.
414
415 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan X-Y-Z angles.
416
417 \warning This routine clobbers XMTRX.
418*/
419SHZ_INLINE void shz_mat4x4_apply_rotation_xyz(shz_mat4x4_t* mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT;
420
421/*! Rotates the given transform matrix about the Z axis, then the Y axis, then the X axis by the given angles in radians.
422
423 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
424
425 \warning This routine clobbers XMTRX.
426*/
427SHZ_INLINE void shz_mat4x4_apply_rotation_zyx(shz_mat4x4_t* mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT;
428
429/*! Rotates the given transform matrix about the Z axis, then the X axis, then the Y axis by the given angles in radians.
430
431 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
432
433 \warning This routine clobbers XMTRX.
434*/
435SHZ_INLINE void shz_mat4x4_apply_rotation_zxy(shz_mat4x4_t* mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT;
436
437/*! Rotates the given transform matrix about the Y axis, then the X axis, then the Z axis by the given angles in radians.
438
439 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Y-X-Z angles.
440
441 \warning This routine clobbers XMTRX.
442*/
443SHZ_INLINE void shz_mat4x4_apply_rotation_yxz(shz_mat4x4_t* mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT;
444
445/*! Rotates the given transform matrix about the arbitrary axis given by a 3D direction vector and angle of rotation in radians.
446
447 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
448
449 \warning This routine clobbers XMTRX.
450*/
451SHZ_INLINE void shz_mat4x4_apply_rotation(shz_mat4x4_t* mat, float angle, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
452
453/*! Multiplies and accumulates the given matrix with a rotation matrix whose orientation is given by a quaternion.
454
455 \note This routine does not use XMTRX.
456*/
457SHZ_INLINE void shz_mat4x4_apply_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
458
459/*! Applies the 3D "lookAt" matrix constructed with the given vector components onto the given matrix.
460
461 \warning This routine clobbers XMTRX.
462*/
463SHZ_INLINE void shz_mat4x4_apply_lookat(shz_mat4x4_t* m, shz_vec3_t pos, shz_vec3_t target, shz_vec3_t up) SHZ_NOEXCEPT;
464
465/*! Multiplies and accumulates the perspective matrix constructed from the given values onto the given matrix.
466
467 \warning This routine clobbers XMTRX.
468*/
469SHZ_INLINE void shz_mat4x4_apply_perspective(shz_mat4x4_t* m, float fov, float aspect, float near_z) SHZ_NOEXCEPT;
470
471/*! Multiplies and accumulates the viewport matrix created with the given components ont othe given matrix.
472
473 \warning This routine clobbers XMTRX.
474*/
475SHZ_INLINE void shz_mat4x4_apply_screen(shz_mat4x4_t* m, float width, float height) SHZ_NOEXCEPT;
476
477/*! Multiplies and accumulates the given matrix with a symmetric skew matrix formed from the given 3D vector components.
478
479 \warning This routine clobbers XMTRX.
480*/
481SHZ_INLINE void shz_mat4x4_apply_symmetric_skew(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
482
483/*! Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
484
485 \warning This routine clobbers XMTRX.
486*/
487SHZ_INLINE void shz_mat4x4_apply_permutation_wxyz(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
488
489/*! Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
490
491 \warning This routine clobbers XMTRX.
492*/
493SHZ_INLINE void shz_mat4x4_apply_permutation_yzwx(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
494
495/*! Multiplies and accumulates the given matrix onto itself.
496
497 \warning This routine clobbers XMTRX.
498*/
499SHZ_INLINE void shz_mat4x4_apply_self(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
500
501//! @}
502
503/*! \name GL Transformations
504 \brief OpenGL-style 4x4 matrix transforms.
505 @{
506*/
507
508/*! Multiplies and accumulates \p mat by a 3D translation matrix with the given components.
509
510 \note glTranslatef() equivalent.
511 \warning This routine clobbers XMTRX.
512*/
513SHZ_INLINE void shz_mat4x4_translate(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
514
515/*! Multiplies and accumulates \p mat by a 3D scaling matrix with the given components.
516
517 \note glScalef() equivalent.
518 \warning This routine clobbers XMTRX.
519*/
520SHZ_INLINE void shz_mat4x4_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
521
522/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the X axis.
523
524 \warning This routine clobbers XMTRX.
525*/
526SHZ_INLINE void shz_mat4x4_rotate_x(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
527
528/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the Y axis.
529
530 \warning This routine clobbers XMTRX.
531*/
532SHZ_INLINE void shz_mat4x4_rotate_y(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
533
534/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the Z axis.
535
536 \warning This routine clobbers XMTRX.
537*/
538SHZ_INLINE void shz_mat4x4_rotate_z(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
539
540/*! Multiplies and accumulates \p mat by 3D rotation matrices about the X then Y then Z axes.
541
542 \warning This routine clobbers XMTRX.
543*/
544SHZ_FORCE_INLINE void shz_mat4x4_rotate_xyz(shz_mat4x4_t* mat, float xRadians, float yRadians, float zRadians) SHZ_NOEXCEPT;
545
546/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Z then Y then X axes.
547
548 \warning This routine clobbers XMTRX.
549*/
550SHZ_FORCE_INLINE void shz_mat4x4_rotate_zyx(shz_mat4x4_t* mat, float zRadians, float yRadians, float xRadians) SHZ_NOEXCEPT;
551
552/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Z then X then Y axes.
553
554 \warning This routine clobbers XMTRX.
555*/
556SHZ_FORCE_INLINE void shz_mat4x4_rotate_zxy(shz_mat4x4_t* mat, float zRadians, float xRadians, float yRadians) SHZ_NOEXCEPT;
557
558/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Y then X then Z axes.
559
560 \warning This routine clobbers XMTRX.
561*/
562SHZ_FORCE_INLINE void shz_mat4x4_rotate_yxz(shz_mat4x4_t* mat, float yRadians, float xRadians, float zRadians) SHZ_NOEXCEPT;
563
564/*! Multiplies and accumulates \p mat by the 3D rotation matrix formed by the given axis and angle.
565
566 \note Equivalent to glRotatef().
567 \warning This routine clobbers XMTRX.
568*/
569SHZ_INLINE void shz_mat4x4_rotate(shz_mat4x4_t* mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
570
571//!@}
572
573/*! \name Transforming
574 \brief Routines for transforming vectors and points by a matrix.
575 @{
576*/
577
578/*! Multiplies two 4x4 matrices together, storing the result into a third.
579
580 \warning This routine clobbers XMTRX.
581*/
582SHZ_INLINE void shz_mat4x4_mult(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const shz_mat4x4_t* rhs) SHZ_NOEXCEPT;
583
584/*! Multiplies two 4x4 matrices together, with the right handed matrix being unaligned, storing the result into a third.
585
586 \warning This routine clobbers XMTRX.
587*/
588SHZ_INLINE void shz_mat4x4_mult_unaligned(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const float rhs[16]) SHZ_NOEXCEPT;
589
590/*! Transforms a 2D vector by a 4x4 matrix.
591
592 This is a routine specializing in one-off transforms of a **single**
593 2D vector by a **single** 4x4 matrix. It should be faster than going
594 through XMTRX.
595
596 \note
597 For batch transforming multiple 2D vectors against the same 4x4 matrix,
598 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec2().
599
600 \sa shz_mat4x4_transform_vec2(), shz_xmtrx_transform_vec3()
601*/
602SHZ_INLINE shz_vec2_t shz_mat4x4_transform_vec2(const shz_mat4x4_t* m, shz_vec2_t v) SHZ_NOEXCEPT;
603
604/*! Transforms a 3D vector by a 4x4 matrix.
605
606 This is a routine specializing in one-off transforms of a **single**
607 3D vector (such as a normal, without a W component) by a **single**
608 4x4 matrix. It should be faster than going through XMTRX.
609
610 \note
611 For batch transforming multiple 3D vectors against the same 4x4 matrix,
612 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec3().
613
614 \sa shz_mat4x4_transform_vec4(), shz_xmtrx_transform_vec3()
615*/
616SHZ_INLINE shz_vec3_t shz_mat4x4_transform_vec3(const shz_mat4x4_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
617
618/*! Transforms a 4D vector by a 4x4 matrix.
619
620 This is a routine specializing in one-off transforms of a *single*
621 4D vector by a *single* 4x4 matrix. It should be faster than going
622 through XMTRX.
623
624 \note
625 For batch transforming multiple 4D vectors against the same 4x4 matrix,
626 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec4().
627
628 \sa shz_mat4x4_transform_vec3(), shz_xmtrx_transform_vec4()
629*/
630SHZ_INLINE shz_vec4_t shz_mat4x4_transform_vec4(const shz_mat4x4_t* mat, shz_vec4_t in) SHZ_NOEXCEPT;
631
632/*! Transforms a 2D vector the the transpose of a 4x4 matrix.
633
634 This is a routine specializing in one-off transforms of a **single**
635 2D vector by the transpose of a **single** 4x4 matrix. It should be
636 faster than going through XMTRX.
637
638 \note
639 For batch transforming multiple 2D vectors against the transpose of
640 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
641 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec2().
642
643 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec2().
644*/
645SHZ_INLINE shz_vec2_t shz_mat4x4_transform_vec2_transpose(const shz_mat4x4_t* m, shz_vec2_t v) SHZ_NOEXCEPT;
646
647/*! Transforms a 3D vector the the transpose of a 4x4 matrix.
648
649 This is a routine specializing in one-off transforms of a **single**
650 3D vector by the transpose of a **single** 4x4 matrix. It should be
651 faster than going through XMTRX.
652
653 \note
654 For batch transforming multiple 3D vectors against the transpose of
655 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
656 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec3().
657
658 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec3().
659*/
660SHZ_INLINE shz_vec3_t shz_mat4x4_transform_vec3_transpose(const shz_mat4x4_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
661
662/*! Transforms a 4D vector the the transpose of a 4x4 matrix.
663
664 This is a routine specializing in one-off transforms of a **single**
665 4D vector by the transpose of a **single** 4x4 matrix. It should be
666 faster than going through XMTRX.
667
668 \note
669 For batch transforming multiple 4D vectors against the transpose of
670 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
671 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec4().
672
673 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec4().
674*/
675SHZ_INLINE shz_vec4_t shz_mat4x4_transform_vec4_transpose(const shz_mat4x4_t* m, shz_vec4_t v) SHZ_NOEXCEPT;
676
677/*! Transforms a 2D point by a 4x4 matrix.
678
679 This is a routine specializing in one-off transforms of a *single*
680 2D point by a *single* 4x4 matrix. It should be faster than going
681 through XMTRX.
682
683 \note
684 For batch transforming multiple 2D points against the same 4x4 matrix,
685 preload the matrix into XMTRX, then use shz_xmtrx_transform_point2().
686
687 \sa shz_xmtrx_transform_point2()
688*/
689SHZ_FORCE_INLINE shz_vec2_t shz_mat4x4_transform_point2(const shz_mat4x4_t* mat, shz_vec2_t pt) SHZ_NOEXCEPT;
690
691/*! Transforms a 3D point by a 4x4 matrix.
692
693 This is a routine specializing in one-off transforms of a *single*
694 3D point by a *single* 4x4 matrix. It should be faster than going
695 through XMTRX.
696
697 \note
698 For batch transforming multiple 3D points against the same 4x4 matrix,
699 preload the matrix into XMTRX, then use shz_xmtrx_transform_point3().
700
701 \sa shz_xmtrx_transform_point3()
702*/
703SHZ_FORCE_INLINE shz_vec3_t shz_mat4x4_transform_point3(const shz_mat4x4_t* mat, shz_vec3_t pt) SHZ_NOEXCEPT;
704
705/*! Transforms a 2D point by the transpose of a 4x4 matrix.
706
707 This is a routine specializing in one-off transforms of a *single*
708 2D point by the transpose of a a *single* 4x4 matrix. It should be
709 faster than going through XMTRX.
710
711 \note
712 For batch transforming multiple 2D points against the same 4x4 matrix,
713 preload the matrix into XMTRX, use shz_xmtrx_transpose(), then use
714 shz_xmtrx_transform_point2().
715
716 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_point2()
717*/
718SHZ_FORCE_INLINE shz_vec2_t shz_mat4x4_transform_point2_transpose(const shz_mat4x4_t* mat, shz_vec2_t pt) SHZ_NOEXCEPT;
719
720/*! Transforms a 3D point by the transpose of a 4x4 matrix.
721
722 This is a routine specializing in one-off transforms of a *single*
723 3D point by the transpose of a a *single* 4x4 matrix. It should be
724 faster than going through XMTRX.
725
726 \note
727 For batch transforming multiple 3D points against the same 4x4 matrix,
728 preload the matrix into XMTRX, use shz_xmtrx_transpose(), then use
729 shz_xmtrx_transform_point2().
730
731 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_point3()
732*/
733SHZ_FORCE_INLINE shz_vec3_t shz_mat4x4_transform_point3_transpose(const shz_mat4x4_t* mat, shz_vec3_t pt) SHZ_NOEXCEPT;
734
735//! @}
736
737/*! \name Miscellaneous
738 \brief Other matrix-related operations and routines
739 @{
740*/
741
742//! Converts the given 4x4 orientation matrix into a quaternion.
743SHZ_INLINE shz_quat_t shz_mat4x4_to_quat(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
744
745/*! Stores the transpose of \p mat within \p out.
746
747 \warning This routine clobbers XMTRX.
748*/
749SHZ_INLINE void shz_mat4x4_transpose(const shz_mat4x4_t* mat, shz_mat4x4_t* out) SHZ_NOEXCEPT;
750
751/*! Computes the inverse of a 4x4 matrix.
752
753 \note
754 In-place inversion (mtrx == out) is not supported.
755
756 \warning This routine clobbers XMTRX.
757
758 \param mtrx Pointer to the 4x4 matrix to invert.
759 \param out Pointer to the resulting inverted matrix.
760*/
761void shz_mat4x4_inverse(const shz_mat4x4_t* SHZ_RESTRICT mtrx, shz_mat4x4_t* SHZ_RESTRICT out) SHZ_NOEXCEPT;
762
763/*! Computes the inverse of a 4x4 matrix in block-triangular form.
764
765 This is a special-case faster optimization for 4x4 matrices which take the form:
766
767 A = [ M b ]
768 [ 0 w ]
769
770 Where A is 4x4, M is 3x3, b is 3x1, and the bottom row is (0, 0, 0, w) with
771 w != 0. For this block-triangular form, det(A) = det(M) * w. Then
772
773 inv(A) = [ inv(M) -inv(M) * b / w ]
774 [ 0 1/w ]
775 \note
776 A regular 3D transform matrix is already in this form.
777
778 \note
779 shz_mat4x4_inverse() will dynamically check whether to use this optimization.
780*/
781void shz_mat4x4_inverse_block_triangular(const shz_mat4x4_t* mtx, shz_mat4x4_t* out) SHZ_NOEXCEPT;
782
783//! Returns true if the two matrices are equal, based on either absolute or relative tolerance.
784SHZ_INLINE bool shz_mat4x4_equal(const shz_mat4x4_t* SHZ_RESTRICT mat1, const shz_mat4x4_t* mat2) SHZ_NOEXCEPT;
785
786//! Returns true if the given matrix is in block-triangular form: having a bottom row in the form of `<0.0f, 0.0f, 0.0f, w>`.
787SHZ_INLINE bool shz_mat4x4_is_block_triangular(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
788
789/*! Copies the given \p src 4x4 matrix into the given \p dst 4x4 matrix.
790
791 \warning This routine clobbers XMTRX!
792*/
793SHZ_INLINE void shz_mat4x4_copy(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
794
795/*! Copies the given unaligned \p src 4x4 matrix into the given \p dst 4x4 matrix.
796
797 \warning This routine clobbers XMtRX.
798*/
799SHZ_INLINE void shz_mat4x4_copy_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
800
801//! @}
802
803//! \cond UNDOCUMENTED
804// Until API is complete.
805typedef SHZ_ALIGNAS(8) struct shz_mat2x2 {
806 union {
807 float elem[4];
808 float elem2D[2][2];
809 shz_vec2_t col[2];
810 };
811} shz_mat2x2_t;
812
813typedef struct shz_mat3x3 {
814 union {
815 float elem[9];
816 float elem2D[3][3];
817 shz_vec3_t col[3];
818 struct {
819 shz_vec3_t left;
820 shz_vec3_t up;
821 shz_vec3_t forward;
822 };
823 };
824} shz_mat3x3_t;
825
826typedef struct shz_mat3x4 {
827 union {
828 float elem[12];
829 float elem2D[4][3];
830 shz_vec3_t col[4];
831 struct {
832 shz_vec3_t left;
833 shz_vec3_t up;
834 shz_vec3_t forward;
835 shz_vec3_t pos;
836 };
837 };
838} shz_mat3x4_t;
839
840typedef struct shz_mat4x3 {
841 union {
842 float elem[12];
843 float elem2D[3][4];
844 shz_vec4_t col[3];
845 struct {
846 shz_vec4_t left;
847 shz_vec4_t up;
848 shz_vec4_t forward;
849 };
850 };
851} shz_mat4x3_t;
852
853/*!
854 Stores the transpose of 3x3 matrix \p mat within \p out.
855
856 mtrx: Pointer to the 3x3 matrix to transpose.
857 out: Pointer to the resulting transposed matrix.
858
859 \warning This routine clobbers XMTRX.
860
861 */
862SHZ_INLINE void shz_mat3x3_transpose(const shz_mat3x3_t* mat, shz_mat3x3_t* out) SHZ_NOEXCEPT;
863
864/*!
865 Computes the inverse of a 3x3 matrix, saves cycles by not scaling by the
866 determinant, which makes sense when used for normals and lighting that are
867 usually normalized later.
868
869 mtrx: Pointer to the 3x3 matrix to invert.
870 out: Pointer to the resulting inverted matrix.
871
872 \note
873 Only valid if the matrix is known to be orthonormal.
874
875 \warning This routine clobbers XMTRX.
876 */
877SHZ_INLINE void shz_mat3x3_inverse_unscaled(const shz_mat3x3_t* mtrx, shz_mat3x3_t* out) SHZ_NOEXCEPT;
878
879/*!
880 Computes the inverse of a 3x3 matrix.
881
882 mtrx: Pointer to the 3x3 matrix to invert.
883 out: Pointer to the resulting inverted matrix.
884
885 \note
886 Only valid for non-singular matrices.
887
888 \warning This routine clobbers XMTRX.
889 */
890SHZ_INLINE void shz_mat3x3_inverse(const shz_mat3x3_t* mtrx, shz_mat3x3_t* out) SHZ_NOEXCEPT;
891
892SHZ_INLINE void shz_mat3x3_scale(shz_mat3x3_t* dst, const shz_mat3x3_t* src, float value) SHZ_NOEXCEPT;
893SHZ_INLINE shz_vec3_t shz_mat3x3_transform_vec3(const shz_mat3x3_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
894SHZ_INLINE shz_vec3_t shz_mat3x3_transform_vec3_transpose(const shz_mat3x3_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
895
896//! \endcond
897
898#include "inline/shz_matrix.inl.h"
899
900SHZ_DECLS_END
901
902#endif // SHZ_MATRIX_H
bool shz_mat4x4_is_block_triangular(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns true if the given matrix is in block-triangular form: having a bottom row in the form of <0....
void shz_mat4x4_apply_rotation_y(shz_mat4x4_t *mat, float yAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by yAngle radians about the Y-axis onto the given matrix...
void shz_mat4x4_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D scaling matrix with the given components.
void shz_mat4x4_init_lower_triangular(shz_mat4x4_t *mat, shz_vec4_t col1, shz_vec3_t col2, shz_vec2_t col3, float col4) SHZ_NOEXCEPT
Initializes the given matrix to a lower triangular matrix whose nonzero entries have the given value.
void shz_mat4x4_init_rotation_z(shz_mat4x4_t *mat, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by zAngle radians over the Z-axis.
void shz_mat4x4_set_diagonal(shz_mat4x4_t *mat, float x, float y, float z, float w) SHZ_NOEXCEPT
Assigns only the 4 elements along the diagonal of the given matrix to the given values.
void shz_mat4x4_transpose(const shz_mat4x4_t *mat, shz_mat4x4_t *out) SHZ_NOEXCEPT
Stores the transpose of mat within out.
void shz_mat4x4_init_rotation_zxy(shz_mat4x4_t *mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_apply_permutation_yzwx(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors...
void shz_mat4x4_decompose(const shz_mat4x4_t *mat, shz_vec3_t *translation, shz_quat_t *rotation, shz_vec3_t *scale) SHZ_NOEXCEPT
Decomposes a 4x4 transform matrix into translation, rotation, and scale.
shz_vec3_t shz_mat4x4_transform_point3(const shz_mat4x4_t *mat, shz_vec3_t pt) SHZ_NOEXCEPT
Transforms a 3D point by a 4x4 matrix.
void shz_mat4x4_copy_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Copies the given unaligned src 4x4 matrix into the given dst 4x4 matrix.
void shz_mat4x4_rotate_y(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the Y axis.
void shz_mat4x4_mult(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const shz_mat4x4_t *rhs) SHZ_NOEXCEPT
Multiplies two 4x4 matrices together, storing the result into a third.
void shz_mat4x4_apply_transpose(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Multiplies and accumulates the transposed src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_apply_transpose_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Multiplies and accumulates the transposed unaligned src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_init_identity(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix to the identity matrix as fast as possible.
void shz_mat4x4_init_rotation(shz_mat4x4_t *mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix about the given axis rotated by angle radians.
void shz_mat4x4_3x3_inverse_unscaled(const shz_mat4x4_t *mat4, shz_mat3x3_t *invmat3) SHZ_NOEXCEPT
Extracts and inverts the top-level, unscaled 3x3 of the given 4x4 matrix.
float shz_mat4x4_3x3_determinant(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the determinant of the given 4x4 matrix's internal top-level 3x3 matrix.
void shz_mat4x4_init_permutation_wxyz(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the matrix to to a permutation matrix, which reorders the components of transformed vecto...
void shz_mat4x4_init_rotation_xyz(shz_mat4x4_t *mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_apply_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix with a rotation matrix whose orientation is given by a qu...
void shz_mat4x4_init_rotation_y(shz_mat4x4_t *mat, float yAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by yAngle radians over the Y-axis.
void shz_mat4x4_init_zero(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix with all 0s for its element values.
shz_vec2_t shz_mat4x4_transform_point2(const shz_mat4x4_t *mat, shz_vec2_t pt) SHZ_NOEXCEPT
Transforms a 2D point by a 4x4 matrix.
void shz_mat4x4_swap_rows(shz_mat4x4_t *mat, size_t row1, size_t row2) SHZ_NOEXCEPT
Swaps the 4D row vectors located at row1 and row2 within mat.
float shz_mat4x4_determinant(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the determinant of the given 4x4 matrix.
shz_vec3_t shz_mat4x4_transform_point3_transpose(const shz_mat4x4_t *mat, shz_vec3_t pt) SHZ_NOEXCEPT
Transforms a 3D point by the transpose of a 4x4 matrix.
void shz_mat4x4_init_outer_product(shz_mat4x4_t *mat, shz_vec4_t v1, shz_vec4_t v2) SHZ_NOEXCEPT
Initializes the given matrix to be the result from taking the outer product of the two given 4D vecto...
void shz_mat4x4_apply_screen(shz_mat4x4_t *m, float width, float height) SHZ_NOEXCEPT
Multiplies and accumulates the viewport matrix created with the given components ont othe given matri...
shz_vec2_t shz_mat4x4_transform_vec2(const shz_mat4x4_t *m, shz_vec2_t v) SHZ_NOEXCEPT
Transforms a 2D vector by a 4x4 matrix.
void shz_mat4x4_3x3_inverse(const shz_mat4x4_t *mat4, shz_mat3x3_t *invmat3) SHZ_NOEXCEPT
Extracts and inverts the top-level 3x3 (which may be scaled) of the given 4x4 matrix.
shz_vec2_t shz_mat4x4_transform_point2_transpose(const shz_mat4x4_t *mat, shz_vec2_t pt) SHZ_NOEXCEPT
Transforms a 2D point by the transpose of a 4x4 matrix.
void shz_mat4x4_init_rotation_x(shz_mat4x4_t *mat, float xAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by xAngle radians over the X-axis.
float shz_mat4x4_trace(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the trace of the given 4x4 matrix.
void shz_mat4x4_apply_rotation_zxy(shz_mat4x4_t *mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Z axis, then the X axis, then the Y axis by the given an...
void shz_mat4x4_apply_symmetric_skew(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix with a symmetric skew matrix formed from the given 3D vec...
void shz_mat4x4_apply_self(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix onto itself.
void shz_mat4x4_init_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to a 3D scaling matrix with the given dimensions.
void shz_mat4x4_init_identity_safe(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix to the identity matrix, safely zeroing out NaN values.
void shz_mat4x4_apply_rotation_xyz(shz_mat4x4_t *mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the X axis, then the Y axis, then the Z axis by the given an...
void shz_mat4x4_translate(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D translation matrix with the given components.
shz_vec4_t shz_mat4x4_col(const shz_mat4x4_t *mat, size_t col) SHZ_NOEXCEPT
Extracts the col index as a 4D column vector from the given matrix.
void shz_mat4x4_apply_permutation_wxyz(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors...
void shz_mat4x4_rotate(shz_mat4x4_t *mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Multiplies and accumulates mat by the 3D rotation matrix formed by the given axis and angle.
void shz_mat4x4_init_perspective(shz_mat4x4_t *mat, float fov, float aspect, float near_z) SHZ_NOEXCEPT
Initializes the given matrix to a perspective projection matrix.
void shz_mat4x4_apply_rotation_yxz(shz_mat4x4_t *mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Y axis, then the X axis, then the Z axis by the given an...
void shz_mat4x4_set_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Assigns only the 3D translation-related elements of the given matrix to the given values.
void shz_mat4x4_rotate_xyz(shz_mat4x4_t *mat, float xRadians, float yRadians, float zRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the X then Y then Z axes.
void shz_mat4x4_apply_perspective(shz_mat4x4_t *m, float fov, float aspect, float near_z) SHZ_NOEXCEPT
Multiplies and accumulates the perspective matrix constructed from the given values onto the given ma...
void shz_mat4x4_init_upper_triangular(shz_mat4x4_t *mat, float col1, shz_vec2_t col2, shz_vec3_t col3, shz_vec4_t col4) SHZ_NOEXCEPT
Initializes the given matrix to an upper triangular matrix whose nonzero entries have the given value...
void shz_mat4x4_rotate_yxz(shz_mat4x4_t *mat, float yRadians, float xRadians, float zRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Y then X then Z axes.
shz_quat_t shz_mat4x4_to_quat(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Converts the given 4x4 orientation matrix into a quaternion.
void shz_mat4x4_apply(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Multiplies and accumulates the src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_rotate_zxy(shz_mat4x4_t *mat, float zRadians, float xRadians, float yRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Z then X then Y axes.
shz_vec3_t shz_mat4x4_transform_vec3_transpose(const shz_mat4x4_t *m, shz_vec3_t v) SHZ_NOEXCEPT
Transforms a 3D vector the the transpose of a 4x4 matrix.
shz_vec4_t shz_mat4x4_transform_vec4(const shz_mat4x4_t *mat, shz_vec4_t in) SHZ_NOEXCEPT
Transforms a 4D vector by a 4x4 matrix.
void shz_mat4x4_swap_cols(shz_mat4x4_t *mat, size_t col1, size_t col2) SHZ_NOEXCEPT
Swaps the 4D column vectors located at col1 and col2 within mat.
void shz_mat4x4_init_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to a 3D translation matrix with the given coordinates.
void shz_mat4x4_apply_rotation_zyx(shz_mat4x4_t *mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Z axis, then the Y axis, then the X axis by the given an...
void shz_mat4x4_copy(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Copies the given src 4x4 matrix into the given dst 4x4 matrix.
void shz_mat4x4_rotate_z(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the Z axis.
void shz_mat4x4_apply_rotation_z(shz_mat4x4_t *mat, float zAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by zAngle radians about the Z-axis onto the given matrix...
void shz_mat4x4_set_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Assigns only the 3D scale-related elements of the given matrix to the given values.
void shz_mat4x4_apply_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates the scale-related elements of the given matrix by the given 3D components.
void shz_mat4x4_set_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Sets just the rotational component of the matrix to the orientation given by a quaternion,...
shz_vec2_t shz_mat4x4_transform_vec2_transpose(const shz_mat4x4_t *m, shz_vec2_t v) SHZ_NOEXCEPT
Transforms a 2D vector the the transpose of a 4x4 matrix.
void shz_mat4x4_apply_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Adds the given 3D vector components to the translational values of the given matrix.
void shz_mat4x4_rotate_zyx(shz_mat4x4_t *mat, float zRadians, float yRadians, float xRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Z then Y then X axes.
void shz_mat4x4_set_row(shz_mat4x4_t *mat, size_t row, shz_vec4_t vec) SHZ_NOEXCEPT
Sets the values of mat at the given row to those of the 4D vector, vec.
void shz_mat4x4_mult_unaligned(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const float rhs[16]) SHZ_NOEXCEPT
Multiplies two 4x4 matrices together, with the right handed matrix being unaligned,...
void shz_mat4x4_rotate_x(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the X axis.
void shz_mat4x4_init_symmetric_skew(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to be the symmetric skew of the given 3D vector components.
void shz_mat4x4_init_lookat(shz_mat4x4_t *mat, shz_vec3_t eye, shz_vec3_t center, shz_vec3_t up) SHZ_NOEXCEPT
Initializes the given matrix to a "lookAt" view matrix.
void shz_mat4x4_init_rotation_yxz(shz_mat4x4_t *mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_init_diagonal(shz_mat4x4_t *mat, float x, float y, float z, float w) SHZ_NOEXCEPT
Initializes the given matrix to a diagonal matrix with the given 4 values.
shz_mat4x4_t shz_mat4x4
Alternate shz_mat4x4_t C typedef for those who hate POSIX style.
Definition shz_matrix.h:88
void shz_mat4x4_init_rotation_zyx(shz_mat4x4_t *mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_init_permutation_yzwx(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the matrix to to a permutation matrix, which reorders the components of transformed vecto...
shz_vec4_t shz_mat4x4_transform_vec4_transpose(const shz_mat4x4_t *m, shz_vec4_t v) SHZ_NOEXCEPT
Transforms a 4D vector the the transpose of a 4x4 matrix.
void shz_mat4x4_init_frustum(shz_mat4x4_t *mat, float left, float right, float bottom, float top, float near, float far) SHZ_NOEXCEPT
Initializes the given matrix to a frustum projection matrix.
void shz_mat4x4_init_ortho(shz_mat4x4_t *mat, float left, float right, float bottom, float top, float near, float far) SHZ_NOEXCEPT
Initializes the given matrix to an orthographic projection matrix.
void shz_mat4x4_apply_rotation_x(shz_mat4x4_t *mat, float xAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by xAngle radians about the X-axis onto the given matrix...
void shz_mat4x4_init_fill(shz_mat4x4_t *mat, float value) SHZ_NOEXCEPT
Initializes the given matrix with all elements assigned to the given value.
void shz_mat4x4_apply_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Multiplies and accumulates the unaligned src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_apply_lookat(shz_mat4x4_t *m, shz_vec3_t pos, shz_vec3_t target, shz_vec3_t up) SHZ_NOEXCEPT
Applies the 3D "lookAt" matrix constructed with the given vector components onto the given matrix.
shz_vec4_t shz_mat4x4_row(const shz_mat4x4_t *mat, size_t row) SHZ_NOEXCEPT
Extracts the row index as a 4D row vector from the given matrix.
bool shz_mat4x4_equal(const shz_mat4x4_t *SHZ_RESTRICT mat1, const shz_mat4x4_t *mat2) SHZ_NOEXCEPT
Returns true if the two matrices are equal, based on either absolute or relative tolerance.
shz_vec3_t shz_mat4x4_transform_vec3(const shz_mat4x4_t *m, shz_vec3_t v) SHZ_NOEXCEPT
Transforms a 3D vector by a 4x4 matrix.
void shz_mat4x4_apply_rotation(shz_mat4x4_t *mat, float angle, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Rotates the given transform matrix about the arbitrary axis given by a 3D direction vector and angle ...
void shz_mat4x4_inverse(const shz_mat4x4_t *SHZ_RESTRICT mtrx, shz_mat4x4_t *SHZ_RESTRICT out) SHZ_NOEXCEPT
Computes the inverse of a 4x4 matrix.
void shz_mat4x4_init_one(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix with all 1s for its element values.
void shz_mat4x4_init_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix with its orientation given by a quaternion.
void shz_mat4x4_3x3(const shz_mat4x4_t *mat4, shz_mat3x3_t *mat3) SHZ_NOEXCEPT
Extracts the top-left 3x3 of the given 4D matrix.
void shz_mat4x4_inverse_block_triangular(const shz_mat4x4_t *mtx, shz_mat4x4_t *out) SHZ_NOEXCEPT
Computes the inverse of a 4x4 matrix in block-triangular form.
void shz_mat4x4_init_screen(shz_mat4x4_t *mat, float width, float height) SHZ_NOEXCEPT
Initializes the given matrix to the viewport matrix with the given dimenions.
void shz_mat4x4_set_col(shz_mat4x4_t *mat, size_t col, shz_vec4_t vec) SHZ_NOEXCEPT
Sets the values of mat at the given col to those of the 4D vector, vec.
Structure representing a 4x4 column-major matrix.
Definition shz_matrix.h:73
float elem[16]
< Inner convenience union.
Definition shz_matrix.h:75
shz_vec4_t pos
Access the last column of the matrix as a 1x4 vector.
Definition shz_matrix.h:82
shz_vec4_t col[4]
Access the matrix as an array of 4 1x4 column vectors.
Definition shz_matrix.h:77
shz_vec4_t forward
Access the third column of the matrix as a 1x4 vector.
Definition shz_matrix.h:81
shz_vec4_t left
< Named column vectors.
Definition shz_matrix.h:79
shz_vec4_t up
Access the second column of the matrix as a 1x4 vector.
Definition shz_matrix.h:80
float elem2D[4][4]
Access the matrix as a 2D array of 4x4 single-precision floats.
Definition shz_matrix.h:76