summaryrefslogtreecommitdiff
path: root/libs/cglm/include/cglm/struct/mat4.h
blob: 28f80a36042e67a2a38fa9723407b71ce7c9e2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (c), Recep Aslantas.
 *
 * MIT License (MIT), http://opensource.org/licenses/MIT
 * Full license can be found in the LICENSE file
 */

/*!
 * Most of functions in this header are optimized manually with SIMD
 * if available. You dont need to call/incude SIMD headers manually
 */

/*
 Macros:
   GLMS_MAT4_IDENTITY_INIT
   GLMS_MAT4_ZERO_INIT
   GLMS_MAT4_IDENTITY
   GLMS_MAT4_ZERO

 Functions:
   CGLM_INLINE mat4s   glms_mat4_ucopy(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_copy(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_identity(void);
   CGLM_INLINE void    glms_mat4_identity_array(mat4s * __restrict mat, size_t count);
   CGLM_INLINE mat4s   glms_mat4_zero(void);
   CGLM_INLINE mat3s   glms_mat4_pick3(mat4s mat);
   CGLM_INLINE mat3s   glms_mat4_pick3t(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_ins3(mat3s mat);
   CGLM_INLINE mat4s   glms_mat4_mul(mat4s m1, mat4s m2);
   CGLM_INLINE mat4s   glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len);
   CGLM_INLINE vec4s   glms_mat4_mulv(mat4s m, vec4s v);
   CGLM_INLINE float   glms_mat4_trace(mat4s m);
   CGLM_INLINE float   glms_mat4_trace3(mat4s m);
   CGLM_INLINE versors glms_mat4_quat(mat4s m);
   CGLM_INLINE vec3s   glms_mat4_mulv3(mat4s m, vec3s v, float last);
   CGLM_INLINE mat4s   glms_mat4_transpose(mat4s m);
   CGLM_INLINE mat4s   glms_mat4_scale_p(mat4s m, float s);
   CGLM_INLINE mat4s   glms_mat4_scale(mat4s m, float s);
   CGLM_INLINE float   glms_mat4_det(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_inv(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_inv_fast(mat4s mat);
   CGLM_INLINE mat4s   glms_mat4_swap_col(mat4s mat, int col1, int col2);
   CGLM_INLINE mat4s   glms_mat4_swap_row(mat4s mat, int row1, int row2);
   CGLM_INLINE float   glms_mat4_rmc(vec4s r, mat4s m, vec4s c);
 */

#ifndef cglms_mat4s_h
#define cglms_mat4s_h

#include "../common.h"
#include "../types-struct.h"
#include "../mat4.h"
#include "vec4.h"
#include "vec3.h"

#define GLMS_MAT4_IDENTITY_INIT  {GLM_MAT4_IDENTITY_INIT}
#define GLMS_MAT4_ZERO_INIT      {GLM_MAT4_ZERO_INIT}

/* for C only */
#define GLMS_MAT4_IDENTITY ((mat4s)GLMS_MAT4_IDENTITY_INIT)
#define GLMS_MAT4_ZERO     ((mat4s)GLMS_MAT4_ZERO_INIT)

/*!
 * @brief copy all members of [mat] to [dest]
 *
 * matrix may not be aligned, u stands for unaligned, this may be useful when
 * copying a matrix from external source e.g. asset importer...
 *
 * @param[in]  mat  source
 * @returns         destination
 */
CGLM_INLINE
mat4s
glms_mat4_ucopy(mat4s mat) {
  mat4s r;
  glm_mat4_ucopy(mat.raw, r.raw);
  return r;
}

/*!
 * @brief copy all members of [mat] to [dest]
 *
 * @param[in]  mat  source
 * @returns         destination
 */
CGLM_INLINE
mat4s
glms_mat4_copy(mat4s mat) {
  mat4s r;
  glm_mat4_copy(mat.raw, r.raw);
  return r;
}

/*!
 * @brief make given matrix identity. It is identical with below, 
 *        but it is more easy to do that with this func especially for members
 *        e.g. glm_mat4_identity(aStruct->aMatrix);
 *
 * @code
 * glm_mat4_copy(GLM_MAT4_IDENTITY, mat); // C only
 *
 * // or
 * mat4 mat = GLM_MAT4_IDENTITY_INIT;
 * @endcode
 *
 * @retuns  destination
 */
CGLM_INLINE
mat4s
glms_mat4_identity(void) {
  mat4s r;
  glm_mat4_identity(r.raw);
  return r;
}

/*!
 * @brief make given matrix array's each element identity matrix
 *
 * @param[in, out]  mat   matrix array (must be aligned (16/32)
 *                        if alignment is not disabled)
 *
 * @param[in]       count count of matrices
 */
CGLM_INLINE
void
glms_mat4_identity_array(mat4s * __restrict mat, size_t count) {
  CGLM_ALIGN_MAT mat4s t = GLMS_MAT4_IDENTITY_INIT;
  size_t i;

  for (i = 0; i < count; i++) {
    glm_mat4_copy(t.raw, mat[i].raw);
  }
}

/*!
 * @brief make given matrix zero.
 *
 * @returns  matrix
 */
CGLM_INLINE
mat4s
glms_mat4_zero(void) {
  mat4s r;
  glm_mat4_zero(r.raw);
  return r;
}

/*!
 * @brief copy upper-left of mat4 to mat3
 *
 * @param[in]  mat  source
 * @returns         destination
 */
CGLM_INLINE
mat3s
glms_mat4_pick3(mat4s mat) {
  mat3s r;
  glm_mat4_pick3(mat.raw, r.raw);
  return r;
}

/*!
 * @brief copy upper-left of mat4 to mat3 (transposed)
 *
 * the postfix t stands for transpose
 *
 * @param[in]  mat  source
 * @returns         destination
 */
CGLM_INLINE
mat3s
glms_mat4_pick3t(mat4s mat) {
  mat3s r;
  glm_mat4_pick3t(mat.raw, r.raw);
  return r;
}

/*!
 * @brief copy mat3 to mat4's upper-left
 *
 * @param[in]  mat  source
 * @returns         destination
 */
CGLM_INLINE
mat4s
glms_mat4_ins3(mat3s mat) {
  mat4s r;
  glm_mat4_ins3(mat.raw, r.raw);
  return r;
}

/*!
 * @brief multiply m1 and m2 to dest
 *
 * m1, m2 and dest matrices can be same matrix, it is possible to write this:
 *
 * @code
 * mat4 m = GLM_MAT4_IDENTITY_INIT;
 * glm_mat4_mul(m, m, m);
 * @endcode
 *
 * @param[in]  m1   left matrix
 * @param[in]  m2   right matrix
 * @returns         destination matrix
 */
CGLM_INLINE
mat4s
glms_mat4_mul(mat4s m1, mat4s m2) {
  mat4s r;
  glm_mat4_mul(m1.raw, m2.raw, r.raw);
  return r;
}

/*!
 * @brief mupliply N mat4 matrices and store result in dest
 *
 * this function lets you multiply multiple (more than two or more...) matrices
 * <br><br>multiplication will be done in loop, this may reduce instructions
 * size but if <b>len</b> is too small then compiler may unroll whole loop,
 * usage:
 * @code
 * mat m1, m2, m3, m4, res;
 *
 * res = glm_mat4_mulN((mat4 *[]){&m1, &m2, &m3, &m4}, 4);
 * @endcode
 *
 * @warning matrices parameter is pointer array not mat4 array!
 *
 * @param[in]  matrices mat4 * array
 * @param[in]  len      matrices count
 * @returns             result matrix
 */
CGLM_INLINE
mat4s
glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len) {
  CGLM_ALIGN_MAT mat4s r = GLMS_MAT4_IDENTITY_INIT;
  size_t i;

  for (i = 0; i < len; i++) {
    r = glms_mat4_mul(r, *matrices[i]);
  }
  
  return r;
}

/*!
 * @brief multiply mat4 with vec4 (column vector) and store in dest vector
 *
 * @param[in]  m    mat4 (left)
 * @param[in]  v    vec4 (right, column vector)
 * @returns         vec4 (result, column vector)
 */
CGLM_INLINE
vec4s
glms_mat4_mulv(mat4s m, vec4s v) {
  vec4s r;
  glm_mat4_mulv(m.raw, v.raw, r.raw);
  return r;
}

/*!
 * @brief trace of matrix
 *
 * sum of the elements on the main diagonal from upper left to the lower right
 *
 * @param[in]  m matrix
 */
CGLM_INLINE
float
glms_mat4_trace(mat4s m) {
  return glm_mat4_trace(m.raw);
}

/*!
 * @brief trace of matrix (rotation part)
 *
 * sum of the elements on the main diagonal from upper left to the lower right
 *
 * @param[in]  m matrix
 */
CGLM_INLINE
float
glms_mat4_trace3(mat4s m) {
  return glm_mat4_trace3(m.raw);
}

/*!
 * @brief convert mat4's rotation part to quaternion
 *
 * @param[in]  m    affine matrix
 * @returns         destination quaternion
 */
CGLM_INLINE
versors
glms_mat4_quat(mat4s m) {
  versors r;
  glm_mat4_quat(m.raw, r.raw);
  return r;
}

/*!
 * @brief multiply vector with mat4
 *
 * @param[in]  m    mat4(affine transform)
 * @param[in]  v    vec3
 * @param[in]  last 4th item to make it vec4
 * @returns         result vector (vec3)
 */
CGLM_INLINE
vec3s
glms_mat4_mulv3(mat4s m, vec3s v, float last) {
  vec3s r;
  glm_mat4_mulv3(m.raw, v.raw, last, r.raw);
  return r;
}

/*!
 * @brief tranpose mat4 and store result in same matrix
 *
 * @param[in] m source
 * @returns     result
 */
CGLM_INLINE
mat4s
glms_mat4_transpose(mat4s m) {
  glm_mat4_transpose(m.raw);
  return m;
}

/*!
 * @brief scale (multiply with scalar) matrix without simd optimization
 *
 * multiply matrix with scalar
 *
 * @param[in] m matrix
 * @param[in] s scalar
 * @returns     matrix    
 */
CGLM_INLINE
mat4s
glms_mat4_scale_p(mat4s m, float s) {
  glm_mat4_scale_p(m.raw, s);
  return m;
}

/*!
 * @brief scale (multiply with scalar) matrix
 *
 * multiply matrix with scalar
 *
 * @param[in] m matrix
 * @param[in] s scalar
 * @returns     matrix
 */
CGLM_INLINE
mat4s
glms_mat4_scale(mat4s m, float s) {
  glm_mat4_scale(m.raw, s);
  return m;
}

/*!
 * @brief mat4 determinant
 *
 * @param[in] mat matrix
 *
 * @return determinant
 */
CGLM_INLINE
float
glms_mat4_det(mat4s mat) {
  return glm_mat4_det(mat.raw);
}

/*!
 * @brief inverse mat4 and store in dest
 *
 * @param[in]  mat  matrix
 * @returns         inverse matrix
 */
CGLM_INLINE
mat4s
glms_mat4_inv(mat4s mat) {
  mat4s r;
  glm_mat4_inv(mat.raw, r.raw);
  return r;
}

/*!
 * @brief inverse mat4 and store in dest
 *
 * this func uses reciprocal approximation without extra corrections
 * e.g Newton-Raphson. this should work faster than normal,
 * to get more precise use glm_mat4_inv version.
 *
 * NOTE: You will lose precision, glm_mat4_inv is more accurate
 *
 * @param[in]  mat  matrix
 * @returns         inverse matrix
 */
CGLM_INLINE
mat4s
glms_mat4_inv_fast(mat4s mat) {
  mat4s r;
  glm_mat4_inv_fast(mat.raw, r.raw);
  return r;
}

/*!
 * @brief swap two matrix columns
 *
 * @param[in] mat  matrix
 * @param[in] col1 col1
 * @param[in] col2 col2
 * @returns        matrix
 */
CGLM_INLINE
mat4s
glms_mat4_swap_col(mat4s mat, int col1, int col2) {
  glm_mat4_swap_col(mat.raw, col1, col2);
  return mat;
}

/*!
 * @brief swap two matrix rows
 *
 * @param[in] mat  matrix
 * @param[in] row1 row1
 * @param[in] row2 row2
 * @returns        matrix
 */
CGLM_INLINE
mat4s
glms_mat4_swap_row(mat4s mat, int row1, int row2) {
  glm_mat4_swap_row(mat.raw, row1, row2);
  return mat;
}

/*!
 * @brief helper for  R (row vector) * M (matrix) * C (column vector)
 *
 * rmc stands for Row * Matrix * Column
 *
 * the result is scalar because R * M = Matrix1x4 (row vector),
 * then Matrix1x4 * Vec4 (column vector) = Matrix1x1 (Scalar)
 *
 * @param[in]  r   row vector or matrix1x4
 * @param[in]  m   matrix4x4
 * @param[in]  c   column vector or matrix4x1
 *
 * @return scalar value e.g. B(s)
 */
CGLM_INLINE
float
glms_mat4_rmc(vec4s r, mat4s m, vec4s c) {
  return glm_mat4_rmc(r.raw, m.raw, c.raw);
}

#endif /* cglms_mat4s_h */