mat2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. GLM_MAT2_IDENTITY_INIT
  10. GLM_MAT2_ZERO_INIT
  11. GLM_MAT2_IDENTITY
  12. GLM_MAT2_ZERO
  13. Functions:
  14. CGLM_INLINE void glms_mat2_identity(mat2 mat)
  15. CGLM_INLINE void glms_mat2_identity_array(mat2 * restrict mat, size_t count)
  16. CGLM_INLINE void glms_mat2_zero(mat2 mat)
  17. CGLM_INLINE void glms_mat2_mul(mat2 m1, mat2 m2, mat2 dest)
  18. CGLM_INLINE void glms_mat2_transpose_to(mat2 m, mat2 dest)
  19. CGLM_INLINE void glms_mat2_transpose(mat2 m)
  20. CGLM_INLINE void glms_mat2_mulv(mat2 m, vec2 v, vec2 dest)
  21. CGLM_INLINE float glms_mat2_trace(mat2 m)
  22. CGLM_INLINE void glms_mat2_scale(mat2 m, float s)
  23. CGLM_INLINE float glms_mat2_det(mat2 mat)
  24. CGLM_INLINE void glms_mat2_inv(mat2 mat, mat2 dest)
  25. CGLM_INLINE void glms_mat2_swap_col(mat2 mat, int col1, int col2)
  26. CGLM_INLINE void glms_mat2_swap_row(mat2 mat, int row1, int row2)
  27. CGLM_INLINE float glms_mat2_rmc(vec2 r, mat2 m, vec2 c)
  28. CGLM_INLINE mat2s glms_mat2_make(const float * __restrict src);
  29. */
  30. #ifndef cglms_mat2_h
  31. #define cglms_mat2_h
  32. #include "../common.h"
  33. #include "../types-struct.h"
  34. #include "../mat2.h"
  35. /* api definition */
  36. #define glms_mat2_(NAME) CGLM_STRUCTAPI(mat2, NAME)
  37. #define GLMS_MAT2_IDENTITY_INIT {GLM_MAT2_IDENTITY_INIT}
  38. #define GLMS_MAT2_ZERO_INIT {GLM_MAT2_ZERO_INIT}
  39. /* for C only */
  40. #define GLMS_MAT2_IDENTITY ((mat2s)GLMS_MAT2_IDENTITY_INIT)
  41. #define GLMS_MAT2_ZERO ((mat2s)GLMS_MAT2_ZERO_INIT)
  42. /*!
  43. * @brief make given matrix identity. It is identical with below,
  44. * but it is more easy to do that with this func especially for members
  45. * e.g. glm_mat2_identity(aStruct->aMatrix);
  46. *
  47. * @code
  48. * glm_mat2_copy(GLM_MAT2_IDENTITY, mat); // C only
  49. *
  50. * // or
  51. * mat2 mat = GLM_MAT2_IDENTITY_INIT;
  52. * @endcode
  53. *
  54. * @returns identity matrix
  55. */
  56. CGLM_INLINE
  57. mat2s
  58. glms_mat2_(identity)(void) {
  59. mat2s r;
  60. glm_mat2_identity(r.raw);
  61. return r;
  62. }
  63. /*!
  64. * @brief make given matrix array's each element identity matrix
  65. *
  66. * @param[in, out] mat matrix array (must be aligned (16)
  67. * if alignment is not disabled)
  68. *
  69. * @param[in] count count of matrices
  70. */
  71. CGLM_INLINE
  72. void
  73. glms_mat2_(identity_array)(mat2s * __restrict mat, size_t count) {
  74. CGLM_ALIGN_MAT mat2s t = GLMS_MAT2_IDENTITY_INIT;
  75. size_t i;
  76. for (i = 0; i < count; i++) {
  77. glm_mat2_copy(t.raw, mat[i].raw);
  78. }
  79. }
  80. /*!
  81. * @brief make given matrix zero.
  82. *
  83. * @returns matrix
  84. */
  85. CGLM_INLINE
  86. mat2s
  87. glms_mat2_(zero)(void) {
  88. mat2s r;
  89. glm_mat2_zero(r.raw);
  90. return r;
  91. }
  92. /*!
  93. * @brief multiply m1 and m2 to dest
  94. *
  95. * m1, m2 and dest matrices can be same matrix, it is possible to write this:
  96. *
  97. * @code
  98. * mat2 m = GLM_MAT2_IDENTITY_INIT;
  99. * r = glms_mat2_mul(m, m);
  100. * @endcode
  101. *
  102. * @param[in] m1 left matrix
  103. * @param[in] m2 right matrix
  104. *
  105. * @returns destination matrix
  106. */
  107. CGLM_INLINE
  108. mat2s
  109. glms_mat2_(mul)(mat2s m1, mat2s m2) {
  110. mat2s r;
  111. glm_mat2_mul(m1.raw, m2.raw, r.raw);
  112. return r;
  113. }
  114. /*!
  115. * @brief transpose mat2
  116. *
  117. * @param[in] m matrix to transpose
  118. *
  119. * @returns transposed matrix
  120. */
  121. CGLM_INLINE
  122. mat2s
  123. glms_mat2_(transpose)(mat2s m) {
  124. glm_mat2_transpose(m.raw);
  125. return m;
  126. }
  127. /*!
  128. * @brief multiply mat2 with vec2 (column vector) and store in dest vector
  129. *
  130. * @param[in] m mat2 (left)
  131. * @param[in] v vec2 (right, column vector)
  132. * @returns vec2 (result, column vector)
  133. */
  134. CGLM_INLINE
  135. vec2s
  136. glms_mat2_(mulv)(mat2s m, vec2s v) {
  137. vec2s r;
  138. glm_mat2_mulv(m.raw, v.raw, r.raw);
  139. return r;
  140. }
  141. /*!
  142. * @brief trace of matrix
  143. *
  144. * sum of the elements on the main diagonal from upper left to the lower right
  145. *
  146. * @param[in] m matrix
  147. */
  148. CGLM_INLINE
  149. float
  150. glms_mat2_(trace)(mat2s m) {
  151. return glm_mat2_trace(m.raw);
  152. }
  153. /*!
  154. * @brief scale (multiply with scalar) matrix
  155. *
  156. * multiply matrix with scalar
  157. *
  158. * @param[in, out] m matrix
  159. * @param[in] s scalar
  160. * @returns matrix
  161. */
  162. CGLM_INLINE
  163. mat2s
  164. glms_mat2_(scale)(mat2s m, float s) {
  165. glm_mat2_scale(m.raw, s);
  166. return m;
  167. }
  168. /*!
  169. * @brief mat2 determinant
  170. *
  171. * @param[in] mat matrix
  172. *
  173. * @return determinant
  174. */
  175. CGLM_INLINE
  176. float
  177. glms_mat2_(det)(mat2s mat) {
  178. return glm_mat2_det(mat.raw);
  179. }
  180. /*!
  181. * @brief inverse mat2 and store in dest
  182. *
  183. * @param[in] mat matrix
  184. * @returns matrix
  185. */
  186. CGLM_INLINE
  187. mat2s
  188. glms_mat2_(inv)(mat2s mat) {
  189. mat2s r;
  190. glm_mat2_inv(mat.raw, r.raw);
  191. return r;
  192. }
  193. /*!
  194. * @brief swap two matrix columns
  195. *
  196. * @param[in] mat matrix
  197. * @param[in] col1 col1
  198. * @param[in] col2 col2
  199. * @returns matrix
  200. */
  201. CGLM_INLINE
  202. mat2s
  203. glms_mat2_(swap_col)(mat2s mat, int col1, int col2) {
  204. glm_mat2_swap_col(mat.raw, col1, col2);
  205. return mat;
  206. }
  207. /*!
  208. * @brief swap two matrix rows
  209. *
  210. * @param[in] mat matrix
  211. * @param[in] row1 row1
  212. * @param[in] row2 row2
  213. * @returns matrix
  214. */
  215. CGLM_INLINE
  216. mat2s
  217. glms_mat2_(swap_row)(mat2s mat, int row1, int row2) {
  218. glm_mat2_swap_row(mat.raw, row1, row2);
  219. return mat;
  220. }
  221. /*!
  222. * @brief helper for R (row vector) * M (matrix) * C (column vector)
  223. *
  224. * rmc stands for Row * Matrix * Column
  225. *
  226. * the result is scalar because R * M = Matrix1x2 (row vector),
  227. * then Matrix1x2 * Vec2 (column vector) = Matrix1x1 (Scalar)
  228. *
  229. * @param[in] r row vector or matrix1x2
  230. * @param[in] m matrix2x2
  231. * @param[in] c column vector or matrix2x1
  232. *
  233. * @return scalar value e.g. Matrix1x1
  234. */
  235. CGLM_INLINE
  236. float
  237. glms_mat2_(rmc)(vec2s r, mat2s m, vec2s c) {
  238. return glm_mat2_rmc(r.raw, m.raw, c.raw);
  239. }
  240. /*!
  241. * @brief Create mat2 matrix from pointer
  242. *
  243. * @param[in] src pointer to an array of floats
  244. * @return constructed matrix from raw pointer
  245. */
  246. CGLM_INLINE
  247. mat2s
  248. glms_mat2_(make)(const float * __restrict src) {
  249. mat2s r;
  250. glm_mat2_make(src, r.raw);
  251. return r;
  252. }
  253. #endif /* cglms_mat2_h */