Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
matrix3_ptr.c
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Igal Alkon
2// SPDX-FileCopyrightText: 2026 ALKONTEK <git@alkontek.com>
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <vecmat.h>
6
13{
14 res->v[0] = 1.0f;
15 res->v[1] = 0.0f;
16 res->v[2] = 0.0f;
17 res->v[3] = 0.0f;
18 res->v[4] = 1.0f;
19 res->v[5] = 0.0f;
20 res->v[6] = 0.0f;
21 res->v[7] = 0.0f;
22 res->v[8] = 1.0f;
23}
24
34void mat3_mul_ptr(matrix3 *res, const matrix3 *a, const matrix3 *b)
35{
36 matrix3 temp;
37 temp.m11 = a->m11 * b->m11 + a->m12 * b->m21 + a->m13 * b->m31;
38 temp.m12 = a->m11 * b->m12 + a->m12 * b->m22 + a->m13 * b->m32;
39 temp.m13 = a->m11 * b->m13 + a->m12 * b->m23 + a->m13 * b->m33;
40
41 temp.m21 = a->m21 * b->m11 + a->m22 * b->m21 + a->m23 * b->m31;
42 temp.m22 = a->m21 * b->m12 + a->m22 * b->m22 + a->m23 * b->m32;
43 temp.m23 = a->m21 * b->m13 + a->m22 * b->m23 + a->m23 * b->m33;
44
45 temp.m31 = a->m31 * b->m11 + a->m32 * b->m21 + a->m33 * b->m31;
46 temp.m32 = a->m31 * b->m12 + a->m32 * b->m22 + a->m33 * b->m32;
47 temp.m33 = a->m31 * b->m13 + a->m32 * b->m23 + a->m33 * b->m33;
48
49 *res = temp;
50}
51
59{
60 matrix3 temp;
61 for (int i = 0; i < 3; i++) {
62 for (int j = 0; j < 3; j++) {
63 temp.v[i * 3 + j] = m->v[j * 3 + i];
64 }
65 }
66 *res = temp;
67}
68
78void mat3_inverse_ptr(matrix3 *res, const matrix3 *m)
79{
80 // Compute determinant
81 const vm_float_t det = m->v[0] * (m->v[4] * m->v[8] - m->v[5] * m->v[7]) -
82 m->v[1] * (m->v[3] * m->v[8] - m->v[5] * m->v[6]) +
83 m->v[2] * (m->v[3] * m->v[7] - m->v[4] * m->v[6]);
84
85 if (det == 0.0f) {
87 return;
88 }
89
90 const vm_float_t inv_det = 1.0f / det;
91
92 matrix3 inv;
93 inv.v[0] = (m->v[4] * m->v[8] - m->v[5] * m->v[7]) * inv_det;
94 inv.v[1] = (m->v[2] * m->v[7] - m->v[1] * m->v[8]) * inv_det;
95 inv.v[2] = (m->v[1] * m->v[5] - m->v[2] * m->v[4]) * inv_det;
96 inv.v[3] = (m->v[5] * m->v[6] - m->v[3] * m->v[8]) * inv_det;
97 inv.v[4] = (m->v[0] * m->v[8] - m->v[2] * m->v[6]) * inv_det;
98 inv.v[5] = (m->v[2] * m->v[3] - m->v[0] * m->v[5]) * inv_det;
99 inv.v[6] = (m->v[3] * m->v[7] - m->v[4] * m->v[6]) * inv_det;
100 inv.v[7] = (m->v[1] * m->v[6] - m->v[0] * m->v[7]) * inv_det;
101 inv.v[8] = (m->v[0] * m->v[4] - m->v[1] * m->v[3]) * inv_det;
102 *res = inv;
103}
104
112void mat3_rotation_z_ptr(matrix3 *res, const vm_float_t degrees)
113{
114 const vm_float_t rad = degrees * (vm_float_t)(M_PI / 180.0);
115 const vm_float_t c = VECMAT_COS(rad);
116 const vm_float_t s = VECMAT_SIN(rad);
117
118 res->m11 = c;
119 res->m12 = -s;
120 res->m13 = 0.0f;
121
122 res->m21 = s;
123 res->m22 = c;
124 res->m23 = 0.0f;
125
126 res->m31 = 0.0f;
127 res->m32 = 0.0f;
128 res->m33 = 1.0f;
129}
130
137void mat3_rotation_x_ptr(matrix3 *res, const vm_float_t degrees)
138{
139 const vm_float_t rad = deg_to_rad(degrees);
140 const vm_float_t c = VECMAT_COS(rad);
141 const vm_float_t s = VECMAT_SIN(rad);
142 *res = (matrix3){
143 .m11 = 1.0f, .m21 = 0.0f, .m31 = 0.0f,
144 .m12 = 0.0f, .m22 = c, .m32 = s,
145 .m13 = 0.0f, .m23 = -s, .m33 = c
146 };
147}
148
155void mat3_rotation_y_ptr(matrix3 *res, const vm_float_t degrees)
156{
157 const vm_float_t rad = deg_to_rad(degrees);
158 const vm_float_t c = VECMAT_COS(rad);
159 const vm_float_t s = VECMAT_SIN(rad);
160 *res = (matrix3){
161 .m11 = c, .m21 = 0.0f, .m31 = -s,
162 .m12 = 0.0f, .m22 = 1.0f, .m32 = 0.0f,
163 .m13 = s, .m23 = 0.0f, .m33 = c
164 };
165}
166
174{
176 res->m13 = t->x;
177 res->m23 = t->y;
178}
179
186void mat3_scale_ptr(matrix3 *res, const vector2 *s)
187{
189 res->m11 = s->x;
190 res->m22 = s->y;
191}
192
200{
201 res->m11 = m->m11; res->m21 = m->m21; res->m31 = m->m31;
202 res->m12 = m->m12; res->m22 = m->m22; res->m32 = m->m32;
203 res->m13 = m->m13; res->m23 = m->m23; res->m33 = m->m33;
204}
205
213void mat3_mul_vec3_ptr(vector3 *res, const matrix3 *m, const vector3 *v)
214{
215 const vm_float_t x = v->x;
216 const vm_float_t y = v->y;
217 const vm_float_t z = v->z;
218 res->x = m->m11 * x + m->m12 * y + m->m13 * z;
219 res->y = m->m21 * x + m->m22 * y + m->m23 * z;
220 res->z = m->m31 * x + m->m32 * y + m->m33 * z;
221}
222
230void mat3_mul_vec2_ptr(vector2 *res, const matrix3 *m, const vector2 *v)
231{
232 const vm_float_t x = v->x;
233 const vm_float_t y = v->y;
234 res->x = m->m11 * x + m->m12 * y + m->m13;
235 res->y = m->m21 * x + m->m22 * y + m->m23;
236}
void mat3_identity_ptr(matrix3 *res)
Initializes the 3x3 matrix to identity (diagonal 1.0, others 0.0).
Definition matrix3_ptr.c:12
void mat3_mul_vec2_ptr(vector2 *res, const matrix3 *m, const vector2 *v)
Applies a 3x3 affine transform to a vector2.
void mat3_translate_ptr(matrix3 *res, const vector2 *t)
Builds a 3x3 2D translation matrix.
void mat3_from_mat4_ptr(matrix3 *res, const matrix4 *m)
Copies the upper-left 3x3 of a matrix4.
void mat3_inverse_ptr(matrix3 *res, const matrix3 *m)
Computes the inverse of the input 3x3 matrix using the adjugate method and stores in res.
Definition matrix3_ptr.c:78
void mat3_mul_ptr(matrix3 *res, const matrix3 *a, const matrix3 *b)
Multiplies two 3x3 matrices (a * b) in column-major / column-vector convention.
Definition matrix3_ptr.c:34
void mat3_mul_vec3_ptr(vector3 *res, const matrix3 *m, const vector3 *v)
Multiplies a 3x3 matrix by a vector3.
void mat3_rotation_z_ptr(matrix3 *res, const vm_float_t degrees)
Sets the 3x3 matrix to a rotation around the Z-axis by the given angle in degrees.
void mat3_scale_ptr(matrix3 *res, const vector2 *s)
Builds a 3x3 2D scaling matrix.
void mat3_transpose_ptr(matrix3 *res, const matrix3 *m)
Computes the transpose of the input 3x3 matrix and stores in res.
Definition matrix3_ptr.c:58
void mat3_rotation_y_ptr(matrix3 *res, const vm_float_t degrees)
Builds a 3x3 rotation matrix around the Y axis (degrees).
void mat3_rotation_x_ptr(matrix3 *res, const vm_float_t degrees)
Builds a 3x3 rotation matrix around the X axis (degrees).
vm_float_t v[VECMAT_MAT3_SIZE]
Definition vecmat.h:238
vm_float_t m13
Definition vecmat.h:234
vm_float_t m21
Definition vecmat.h:229
vm_float_t m32
Definition vecmat.h:233
vm_float_t m23
Definition vecmat.h:235
vm_float_t m12
Definition vecmat.h:231
vm_float_t m33
Definition vecmat.h:236
vm_float_t m11
Definition vecmat.h:228
vm_float_t m31
Definition vecmat.h:230
vm_float_t m22
Definition vecmat.h:232
vm_float_t m12
Definition vecmat.h:258
vm_float_t m13
Definition vecmat.h:262
vm_float_t m33
Definition vecmat.h:264
vm_float_t m23
Definition vecmat.h:263
vm_float_t m21
Definition vecmat.h:255
vm_float_t m22
Definition vecmat.h:259
vm_float_t m32
Definition vecmat.h:260
vm_float_t m11
Definition vecmat.h:254
vm_float_t m31
Definition vecmat.h:256
vm_float_t y
Definition vecmat.h:128
vm_float_t x
Definition vecmat.h:127
vm_float_t z
Definition vecmat.h:139
vm_float_t y
Definition vecmat.h:138
vm_float_t x
Definition vecmat.h:137
#define VECMAT_SIN(x)
Definition vecmat.h:415
double vm_float_t
Definition vecmat.h:79
vm_float_t deg_to_rad(vm_float_t degrees)
Converts degrees to radians.
Definition vecmat.c:14
#define VECMAT_COS(x)
Definition vecmat.h:416
#define M_PI
Definition vecmat.h:43