func_matrix.inl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "../geometric.hpp"
  2. #include <limits>
  3. namespace glm{
  4. namespace detail
  5. {
  6. template<length_t C, length_t R, typename T, qualifier Q, bool Aligned>
  7. struct compute_matrixCompMult
  8. {
  9. GLM_FUNC_QUALIFIER static mat<C, R, T, Q> call(mat<C, R, T, Q> const& x, mat<C, R, T, Q> const& y)
  10. {
  11. mat<C, R, T, Q> Result;
  12. for(length_t i = 0; i < Result.length(); ++i)
  13. Result[i] = x[i] * y[i];
  14. return Result;
  15. }
  16. };
  17. template<length_t C, length_t R, typename T, qualifier Q, bool Aligned>
  18. struct compute_transpose{};
  19. template<typename T, qualifier Q, bool Aligned>
  20. struct compute_transpose<2, 2, T, Q, Aligned>
  21. {
  22. GLM_FUNC_QUALIFIER static mat<2, 2, T, Q> call(mat<2, 2, T, Q> const& m)
  23. {
  24. mat<2, 2, T, Q> Result;
  25. Result[0][0] = m[0][0];
  26. Result[0][1] = m[1][0];
  27. Result[1][0] = m[0][1];
  28. Result[1][1] = m[1][1];
  29. return Result;
  30. }
  31. };
  32. template<typename T, qualifier Q, bool Aligned>
  33. struct compute_transpose<2, 3, T, Q, Aligned>
  34. {
  35. GLM_FUNC_QUALIFIER static mat<3, 2, T, Q> call(mat<2, 3, T, Q> const& m)
  36. {
  37. mat<3,2, T, Q> Result;
  38. Result[0][0] = m[0][0];
  39. Result[0][1] = m[1][0];
  40. Result[1][0] = m[0][1];
  41. Result[1][1] = m[1][1];
  42. Result[2][0] = m[0][2];
  43. Result[2][1] = m[1][2];
  44. return Result;
  45. }
  46. };
  47. template<typename T, qualifier Q, bool Aligned>
  48. struct compute_transpose<2, 4, T, Q, Aligned>
  49. {
  50. GLM_FUNC_QUALIFIER static mat<4, 2, T, Q> call(mat<2, 4, T, Q> const& m)
  51. {
  52. mat<4, 2, T, Q> Result;
  53. Result[0][0] = m[0][0];
  54. Result[0][1] = m[1][0];
  55. Result[1][0] = m[0][1];
  56. Result[1][1] = m[1][1];
  57. Result[2][0] = m[0][2];
  58. Result[2][1] = m[1][2];
  59. Result[3][0] = m[0][3];
  60. Result[3][1] = m[1][3];
  61. return Result;
  62. }
  63. };
  64. template<typename T, qualifier Q, bool Aligned>
  65. struct compute_transpose<3, 2, T, Q, Aligned>
  66. {
  67. GLM_FUNC_QUALIFIER static mat<2, 3, T, Q> call(mat<3, 2, T, Q> const& m)
  68. {
  69. mat<2, 3, T, Q> Result;
  70. Result[0][0] = m[0][0];
  71. Result[0][1] = m[1][0];
  72. Result[0][2] = m[2][0];
  73. Result[1][0] = m[0][1];
  74. Result[1][1] = m[1][1];
  75. Result[1][2] = m[2][1];
  76. return Result;
  77. }
  78. };
  79. template<typename T, qualifier Q, bool Aligned>
  80. struct compute_transpose<3, 3, T, Q, Aligned>
  81. {
  82. GLM_FUNC_QUALIFIER static mat<3, 3, T, Q> call(mat<3, 3, T, Q> const& m)
  83. {
  84. mat<3, 3, T, Q> Result;
  85. Result[0][0] = m[0][0];
  86. Result[0][1] = m[1][0];
  87. Result[0][2] = m[2][0];
  88. Result[1][0] = m[0][1];
  89. Result[1][1] = m[1][1];
  90. Result[1][2] = m[2][1];
  91. Result[2][0] = m[0][2];
  92. Result[2][1] = m[1][2];
  93. Result[2][2] = m[2][2];
  94. return Result;
  95. }
  96. };
  97. template<typename T, qualifier Q, bool Aligned>
  98. struct compute_transpose<3, 4, T, Q, Aligned>
  99. {
  100. GLM_FUNC_QUALIFIER static mat<4, 3, T, Q> call(mat<3, 4, T, Q> const& m)
  101. {
  102. mat<4, 3, T, Q> Result;
  103. Result[0][0] = m[0][0];
  104. Result[0][1] = m[1][0];
  105. Result[0][2] = m[2][0];
  106. Result[1][0] = m[0][1];
  107. Result[1][1] = m[1][1];
  108. Result[1][2] = m[2][1];
  109. Result[2][0] = m[0][2];
  110. Result[2][1] = m[1][2];
  111. Result[2][2] = m[2][2];
  112. Result[3][0] = m[0][3];
  113. Result[3][1] = m[1][3];
  114. Result[3][2] = m[2][3];
  115. return Result;
  116. }
  117. };
  118. template<typename T, qualifier Q, bool Aligned>
  119. struct compute_transpose<4, 2, T, Q, Aligned>
  120. {
  121. GLM_FUNC_QUALIFIER static mat<2, 4, T, Q> call(mat<4, 2, T, Q> const& m)
  122. {
  123. mat<2, 4, T, Q> Result;
  124. Result[0][0] = m[0][0];
  125. Result[0][1] = m[1][0];
  126. Result[0][2] = m[2][0];
  127. Result[0][3] = m[3][0];
  128. Result[1][0] = m[0][1];
  129. Result[1][1] = m[1][1];
  130. Result[1][2] = m[2][1];
  131. Result[1][3] = m[3][1];
  132. return Result;
  133. }
  134. };
  135. template<typename T, qualifier Q, bool Aligned>
  136. struct compute_transpose<4, 3, T, Q, Aligned>
  137. {
  138. GLM_FUNC_QUALIFIER static mat<3, 4, T, Q> call(mat<4, 3, T, Q> const& m)
  139. {
  140. mat<3, 4, T, Q> Result;
  141. Result[0][0] = m[0][0];
  142. Result[0][1] = m[1][0];
  143. Result[0][2] = m[2][0];
  144. Result[0][3] = m[3][0];
  145. Result[1][0] = m[0][1];
  146. Result[1][1] = m[1][1];
  147. Result[1][2] = m[2][1];
  148. Result[1][3] = m[3][1];
  149. Result[2][0] = m[0][2];
  150. Result[2][1] = m[1][2];
  151. Result[2][2] = m[2][2];
  152. Result[2][3] = m[3][2];
  153. return Result;
  154. }
  155. };
  156. template<typename T, qualifier Q, bool Aligned>
  157. struct compute_transpose<4, 4, T, Q, Aligned>
  158. {
  159. GLM_FUNC_QUALIFIER static mat<4, 4, T, Q> call(mat<4, 4, T, Q> const& m)
  160. {
  161. mat<4, 4, T, Q> Result;
  162. Result[0][0] = m[0][0];
  163. Result[0][1] = m[1][0];
  164. Result[0][2] = m[2][0];
  165. Result[0][3] = m[3][0];
  166. Result[1][0] = m[0][1];
  167. Result[1][1] = m[1][1];
  168. Result[1][2] = m[2][1];
  169. Result[1][3] = m[3][1];
  170. Result[2][0] = m[0][2];
  171. Result[2][1] = m[1][2];
  172. Result[2][2] = m[2][2];
  173. Result[2][3] = m[3][2];
  174. Result[3][0] = m[0][3];
  175. Result[3][1] = m[1][3];
  176. Result[3][2] = m[2][3];
  177. Result[3][3] = m[3][3];
  178. return Result;
  179. }
  180. };
  181. template<length_t C, length_t R, typename T, qualifier Q, bool Aligned>
  182. struct compute_determinant{};
  183. template<typename T, qualifier Q, bool Aligned>
  184. struct compute_determinant<2, 2, T, Q, Aligned>
  185. {
  186. GLM_FUNC_QUALIFIER static T call(mat<2, 2, T, Q> const& m)
  187. {
  188. return m[0][0] * m[1][1] - m[1][0] * m[0][1];
  189. }
  190. };
  191. template<typename T, qualifier Q, bool Aligned>
  192. struct compute_determinant<3, 3, T, Q, Aligned>
  193. {
  194. GLM_FUNC_QUALIFIER static T call(mat<3, 3, T, Q> const& m)
  195. {
  196. return
  197. + m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])
  198. - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2])
  199. + m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
  200. }
  201. };
  202. template<typename T, qualifier Q, bool Aligned>
  203. struct compute_determinant<4, 4, T, Q, Aligned>
  204. {
  205. GLM_FUNC_QUALIFIER static T call(mat<4, 4, T, Q> const& m)
  206. {
  207. T SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
  208. T SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
  209. T SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
  210. T SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
  211. T SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
  212. T SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
  213. vec<4, T, Q> DetCof(
  214. + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02),
  215. - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04),
  216. + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05),
  217. - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05));
  218. return
  219. m[0][0] * DetCof[0] + m[0][1] * DetCof[1] +
  220. m[0][2] * DetCof[2] + m[0][3] * DetCof[3];
  221. }
  222. };
  223. template<length_t C, length_t R, typename T, qualifier Q, bool Aligned>
  224. struct compute_inverse{};
  225. template<typename T, qualifier Q, bool Aligned>
  226. struct compute_inverse<2, 2, T, Q, Aligned>
  227. {
  228. GLM_FUNC_QUALIFIER static mat<2, 2, T, Q> call(mat<2, 2, T, Q> const& m)
  229. {
  230. T OneOverDeterminant = static_cast<T>(1) / (
  231. + m[0][0] * m[1][1]
  232. - m[1][0] * m[0][1]);
  233. mat<2, 2, T, Q> Inverse(
  234. + m[1][1] * OneOverDeterminant,
  235. - m[0][1] * OneOverDeterminant,
  236. - m[1][0] * OneOverDeterminant,
  237. + m[0][0] * OneOverDeterminant);
  238. return Inverse;
  239. }
  240. };
  241. template<typename T, qualifier Q, bool Aligned>
  242. struct compute_inverse<3, 3, T, Q, Aligned>
  243. {
  244. GLM_FUNC_QUALIFIER static mat<3, 3, T, Q> call(mat<3, 3, T, Q> const& m)
  245. {
  246. T OneOverDeterminant = static_cast<T>(1) / (
  247. + m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])
  248. - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2])
  249. + m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]));
  250. mat<3, 3, T, Q> Inverse;
  251. Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]) * OneOverDeterminant;
  252. Inverse[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]) * OneOverDeterminant;
  253. Inverse[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]) * OneOverDeterminant;
  254. Inverse[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]) * OneOverDeterminant;
  255. Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]) * OneOverDeterminant;
  256. Inverse[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]) * OneOverDeterminant;
  257. Inverse[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]) * OneOverDeterminant;
  258. Inverse[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]) * OneOverDeterminant;
  259. Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]) * OneOverDeterminant;
  260. return Inverse;
  261. }
  262. };
  263. template<typename T, qualifier Q, bool Aligned>
  264. struct compute_inverse<4, 4, T, Q, Aligned>
  265. {
  266. GLM_FUNC_QUALIFIER static mat<4, 4, T, Q> call(mat<4, 4, T, Q> const& m)
  267. {
  268. T Coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
  269. T Coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
  270. T Coef03 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
  271. T Coef04 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
  272. T Coef06 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
  273. T Coef07 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
  274. T Coef08 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
  275. T Coef10 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
  276. T Coef11 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
  277. T Coef12 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
  278. T Coef14 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
  279. T Coef15 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
  280. T Coef16 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
  281. T Coef18 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
  282. T Coef19 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
  283. T Coef20 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
  284. T Coef22 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
  285. T Coef23 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
  286. vec<4, T, Q> Fac0(Coef00, Coef00, Coef02, Coef03);
  287. vec<4, T, Q> Fac1(Coef04, Coef04, Coef06, Coef07);
  288. vec<4, T, Q> Fac2(Coef08, Coef08, Coef10, Coef11);
  289. vec<4, T, Q> Fac3(Coef12, Coef12, Coef14, Coef15);
  290. vec<4, T, Q> Fac4(Coef16, Coef16, Coef18, Coef19);
  291. vec<4, T, Q> Fac5(Coef20, Coef20, Coef22, Coef23);
  292. vec<4, T, Q> Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
  293. vec<4, T, Q> Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
  294. vec<4, T, Q> Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
  295. vec<4, T, Q> Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);
  296. vec<4, T, Q> Inv0(Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
  297. vec<4, T, Q> Inv1(Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
  298. vec<4, T, Q> Inv2(Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
  299. vec<4, T, Q> Inv3(Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);
  300. vec<4, T, Q> SignA(+1, -1, +1, -1);
  301. vec<4, T, Q> SignB(-1, +1, -1, +1);
  302. mat<4, 4, T, Q> Inverse(Inv0 * SignA, Inv1 * SignB, Inv2 * SignA, Inv3 * SignB);
  303. vec<4, T, Q> Row0(Inverse[0][0], Inverse[1][0], Inverse[2][0], Inverse[3][0]);
  304. vec<4, T, Q> Dot0(m[0] * Row0);
  305. T Dot1 = (Dot0.x + Dot0.y) + (Dot0.z + Dot0.w);
  306. T OneOverDeterminant = static_cast<T>(1) / Dot1;
  307. return Inverse * OneOverDeterminant;
  308. }
  309. };
  310. }//namespace detail
  311. template<length_t C, length_t R, typename T, qualifier Q>
  312. GLM_FUNC_QUALIFIER mat<C, R, T, Q> matrixCompMult(mat<C, R, T, Q> const& x, mat<C, R, T, Q> const& y)
  313. {
  314. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'matrixCompMult' only accept floating-point inputs");
  315. return detail::compute_matrixCompMult<C, R, T, Q, detail::is_aligned<Q>::value>::call(x, y);
  316. }
  317. template<length_t DA, length_t DB, typename T, qualifier Q>
  318. GLM_FUNC_QUALIFIER typename detail::outerProduct_trait<DA, DB, T, Q>::type outerProduct(vec<DA, T, Q> const& c, vec<DB, T, Q> const& r)
  319. {
  320. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'outerProduct' only accept floating-point inputs");
  321. typename detail::outerProduct_trait<DA, DB, T, Q>::type m;
  322. for(length_t i = 0; i < m.length(); ++i)
  323. m[i] = c * r[i];
  324. return m;
  325. }
  326. template<length_t C, length_t R, typename T, qualifier Q>
  327. GLM_FUNC_QUALIFIER typename mat<C, R, T, Q>::transpose_type transpose(mat<C, R, T, Q> const& m)
  328. {
  329. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'transpose' only accept floating-point inputs");
  330. return detail::compute_transpose<C, R, T, Q, detail::is_aligned<Q>::value>::call(m);
  331. }
  332. template<length_t C, length_t R, typename T, qualifier Q>
  333. GLM_FUNC_QUALIFIER T determinant(mat<C, R, T, Q> const& m)
  334. {
  335. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'determinant' only accept floating-point inputs");
  336. return detail::compute_determinant<C, R, T, Q, detail::is_aligned<Q>::value>::call(m);
  337. }
  338. template<length_t C, length_t R, typename T, qualifier Q>
  339. GLM_FUNC_QUALIFIER mat<C, R, T, Q> inverse(mat<C, R, T, Q> const& m)
  340. {
  341. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'inverse' only accept floating-point inputs");
  342. return detail::compute_inverse<C, R, T, Q, detail::is_aligned<Q>::value>::call(m);
  343. }
  344. }//namespace glm
  345. #if GLM_CONFIG_SIMD == GLM_ENABLE
  346. # include "func_matrix_simd.inl"
  347. #endif