1
0

mat3.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Macros:
  9. GLMS_MAT3_IDENTITY_INIT
  10. GLMS_MAT3_ZERO_INIT
  11. GLMS_MAT3_IDENTITY
  12. GLMS_MAT3_ZERO
  13. Functions:
  14. CGLM_INLINE mat3s glms_mat3_copy(mat3s mat);
  15. CGLM_INLINE mat3s glms_mat3_identity(void);
  16. CGLM_INLINE void glms_mat3_identity_array(mat3s * __restrict mat, size_t count);
  17. CGLM_INLINE mat3s glms_mat3_zero(void);
  18. CGLM_INLINE mat3s glms_mat3_mul(mat3s m1, mat3s m2);
  19. CGLM_INLINE ma3s glms_mat3_transpose(mat3s m);
  20. CGLM_INLINE vec3s glms_mat3_mulv(mat3s m, vec3s v);
  21. CGLM_INLINE float glms_mat3_trace(mat3s m);
  22. CGLM_INLINE versor glms_mat3_quat(mat3s m);
  23. CGLM_INLINE mat3s glms_mat3_scale(mat3s m, float s);
  24. CGLM_INLINE float glms_mat3_det(mat3s mat);
  25. CGLM_INLINE mat3s glms_mat3_inv(mat3s mat);
  26. CGLM_INLINE mat3s glms_mat3_swap_col(mat3s mat, int col1, int col2);
  27. CGLM_INLINE mat3s glms_mat3_swap_row(mat3s mat, int row1, int row2);
  28. CGLM_INLINE float glms_mat3_rmc(vec3s r, mat3s m, vec3s c);
  29. CGLM_INLINE mat3s glms_mat3_make(const float * __restrict src);
  30. */
  31. #ifndef cglms_mat3s_h
  32. #define cglms_mat3s_h
  33. #include "../common.h"
  34. #include "../types-struct.h"
  35. #include "../mat3.h"
  36. #include "vec3.h"
  37. /* api definition */
  38. #define glms_mat3_(NAME) CGLM_STRUCTAPI(mat3, NAME)
  39. #define GLMS_MAT3_IDENTITY_INIT {GLM_MAT3_IDENTITY_INIT}
  40. #define GLMS_MAT3_ZERO_INIT {GLM_MAT3_ZERO_INIT}
  41. /* for C only */
  42. #define GLMS_MAT3_IDENTITY ((mat3s)GLMS_MAT3_IDENTITY_INIT)
  43. #define GLMS_MAT3_ZERO ((mat3s)GLMS_MAT3_ZERO_INIT)
  44. /*!
  45. * @brief copy all members of [mat] to [dest]
  46. *
  47. * @param[in] mat source
  48. * @returns destination
  49. */
  50. CGLM_INLINE
  51. mat3s
  52. glms_mat3_(copy)(mat3s mat) {
  53. mat3s r;
  54. glm_mat3_copy(mat.raw, r.raw);
  55. return r;
  56. }
  57. /*!
  58. * @brief make given matrix identity. It is identical with below,
  59. * but it is more easy to do that with this func especially for members
  60. * e.g. glm_mat3_identity(aStruct->aMatrix);
  61. *
  62. * @code
  63. * glm_mat3_copy(GLM_MAT3_IDENTITY, mat); // C only
  64. *
  65. * // or
  66. * mat3 mat = GLM_MAT3_IDENTITY_INIT;
  67. * @endcode
  68. *
  69. * @returns destination
  70. */
  71. CGLM_INLINE
  72. mat3s
  73. glms_mat3_(identity)(void) {
  74. mat3s r;
  75. glm_mat3_identity(r.raw);
  76. return r;
  77. }
  78. /*!
  79. * @brief make given matrix array's each element identity matrix
  80. *
  81. * @param[in, out] mat matrix array (must be aligned (16/32)
  82. * if alignment is not disabled)
  83. *
  84. * @param[in] count count of matrices
  85. */
  86. CGLM_INLINE
  87. void
  88. glms_mat3_(identity_array)(mat3s * __restrict mat, size_t count) {
  89. CGLM_ALIGN_MAT mat3s t = GLMS_MAT3_IDENTITY_INIT;
  90. size_t i;
  91. for (i = 0; i < count; i++) {
  92. glm_mat3_copy(t.raw, mat[i].raw);
  93. }
  94. }
  95. /*!
  96. * @brief make given matrix zero.
  97. *
  98. * @returns matrix
  99. */
  100. CGLM_INLINE
  101. mat3s
  102. glms_mat3_(zero)(void) {
  103. mat3s r;
  104. glm_mat3_zero(r.raw);
  105. return r;
  106. }
  107. /*!
  108. * @brief multiply m1 and m2 to dest
  109. *
  110. * m1, m2 and dest matrices can be same matrix, it is possible to write this:
  111. *
  112. * @code
  113. * mat3 m = GLM_MAT3_IDENTITY_INIT;
  114. * r = glms_mat3_mul(m, m);
  115. * @endcode
  116. *
  117. * @param[in] m1 left matrix
  118. * @param[in] m2 right matrix
  119. * @returns destination matrix
  120. */
  121. CGLM_INLINE
  122. mat3s
  123. glms_mat3_(mul)(mat3s m1, mat3s m2) {
  124. mat3s r;
  125. glm_mat3_mul(m1.raw, m2.raw, r.raw);
  126. return r;
  127. }
  128. /*!
  129. * @brief transpose mat3 and store result in same matrix
  130. *
  131. * @param[in, out] m source and dest
  132. */
  133. CGLM_INLINE
  134. mat3s
  135. glms_mat3_(transpose)(mat3s m) {
  136. glm_mat3_transpose(m.raw);
  137. return m;
  138. }
  139. /*!
  140. * @brief multiply mat3 with vec3 (column vector) and store in dest vector
  141. *
  142. * @param[in] m mat3 (left)
  143. * @param[in] v vec3 (right, column vector)
  144. * @returns vec3 (result, column vector)
  145. */
  146. CGLM_INLINE
  147. vec3s
  148. glms_mat3_(mulv)(mat3s m, vec3s v) {
  149. vec3s r;
  150. glm_mat3_mulv(m.raw, v.raw, r.raw);
  151. return r;
  152. }
  153. /*!
  154. * @brief trace of matrix
  155. *
  156. * sum of the elements on the main diagonal from upper left to the lower right
  157. *
  158. * @param[in] m matrix
  159. */
  160. CGLM_INLINE
  161. float
  162. glms_mat3_(trace)(mat3s m) {
  163. return glm_mat3_trace(m.raw);
  164. }
  165. /*!
  166. * @brief convert mat3 to quaternion
  167. *
  168. * @param[in] m rotation matrix
  169. * @returns destination quaternion
  170. */
  171. CGLM_INLINE
  172. versors
  173. glms_mat3_(quat)(mat3s m) {
  174. versors r;
  175. glm_mat3_quat(m.raw, r.raw);
  176. return r;
  177. }
  178. /*!
  179. * @brief scale (multiply with scalar) matrix
  180. *
  181. * multiply matrix with scalar
  182. *
  183. * @param[in] m matrix
  184. * @param[in] s scalar
  185. * @returns scaled matrix
  186. */
  187. CGLM_INLINE
  188. mat3s
  189. glms_mat3_(scale)(mat3s m, float s) {
  190. glm_mat3_scale(m.raw, s);
  191. return m;
  192. }
  193. /*!
  194. * @brief mat3 determinant
  195. *
  196. * @param[in] mat matrix
  197. *
  198. * @return determinant
  199. */
  200. CGLM_INLINE
  201. float
  202. glms_mat3_(det)(mat3s mat) {
  203. return glm_mat3_det(mat.raw);
  204. }
  205. /*!
  206. * @brief inverse mat3 and store in dest
  207. *
  208. * @param[in] mat matrix
  209. * @returns inverse matrix
  210. */
  211. CGLM_INLINE
  212. mat3s
  213. glms_mat3_(inv)(mat3s mat) {
  214. mat3s r;
  215. glm_mat3_inv(mat.raw, r.raw);
  216. return r;
  217. }
  218. /*!
  219. * @brief swap two matrix columns
  220. *
  221. * @param[in] mat matrix
  222. * @param[in] col1 col1
  223. * @param[in] col2 col2
  224. * @returns matrix
  225. */
  226. CGLM_INLINE
  227. mat3s
  228. glms_mat3_(swap_col)(mat3s mat, int col1, int col2) {
  229. glm_mat3_swap_col(mat.raw, col1, col2);
  230. return mat;
  231. }
  232. /*!
  233. * @brief swap two matrix rows
  234. *
  235. * @param[in] mat matrix
  236. * @param[in] row1 row1
  237. * @param[in] row2 row2
  238. * @returns matrix
  239. */
  240. CGLM_INLINE
  241. mat3s
  242. glms_mat3_(swap_row)(mat3s mat, int row1, int row2) {
  243. glm_mat3_swap_row(mat.raw, row1, row2);
  244. return mat;
  245. }
  246. /*!
  247. * @brief helper for R (row vector) * M (matrix) * C (column vector)
  248. *
  249. * rmc stands for Row * Matrix * Column
  250. *
  251. * the result is scalar because R * M = Matrix1x3 (row vector),
  252. * then Matrix1x3 * Vec3 (column vector) = Matrix1x1 (Scalar)
  253. *
  254. * @param[in] r row vector or matrix1x3
  255. * @param[in] m matrix3x3
  256. * @param[in] c column vector or matrix3x1
  257. *
  258. * @return scalar value e.g. Matrix1x1
  259. */
  260. CGLM_INLINE
  261. float
  262. glms_mat3_(rmc)(vec3s r, mat3s m, vec3s c) {
  263. return glm_mat3_rmc(r.raw, m.raw, c.raw);
  264. }
  265. /*!
  266. * @brief Create mat3 matrix from pointer
  267. *
  268. * @param[in] src pointer to an array of floats
  269. * @return constructed matrix from raw pointer
  270. */
  271. CGLM_INLINE
  272. mat3s
  273. glms_mat3_(make)(const float * __restrict src) {
  274. mat3s r;
  275. glm_mat3_make(src, r.raw);
  276. return r;
  277. }
  278. #endif /* cglms_mat3s_h */