💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

848 lines
24 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP
  20. #define ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP
  21. #include "math/matrix-type.hpp"
  22. #include "math/vector-type.hpp"
  23. #include "math/vector-functions.hpp"
  24. #include <type_traits>
  25. namespace math {
  26. /// @addtogroup matrix
  27. /// @{
  28. /**
  29. * Adds two matrices.
  30. *
  31. * @param x First matrix.
  32. * @param y Second matrix.
  33. * @return Sum of the two matrices.
  34. */
  35. template <class T>
  36. matrix<T, 2, 2> add(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y);
  37. /// @copydoc add(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  38. template <class T>
  39. matrix<T, 3, 3> add(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y);
  40. /// @copydoc add(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  41. template <class T>
  42. matrix<T, 4, 4> add(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y);
  43. /**
  44. * Reinterprets data as an `NxM` matrix of type `T`.
  45. *
  46. * @tparam N Number of columns.
  47. * @tparam M Number of rows.
  48. * @tparam T Element type.
  49. * @param data Data to reinterpret.
  50. */
  51. template <std::size_t N, std::size_t M, typename T>
  52. matrix<T, N, M>& as_matrix(T& data);
  53. /**
  54. * Calculates the determinant of a matrix.
  55. *
  56. * @param m Matrix of which to take the determinant.
  57. */
  58. template <class T>
  59. T determinant(const matrix<T, 2, 2>& m);
  60. /// @copydoc determinant(const matrix<T, 2, 2>&)
  61. template <class T>
  62. T determinant(const matrix<T, 3, 3>& m);
  63. /// @copydoc determinant(const matrix<T, 2, 2>&)
  64. template <class T>
  65. T determinant(const matrix<T, 4, 4>& m);
  66. /**
  67. * Calculates the inverse of a matrix.
  68. *
  69. * @param m Matrix of which to take the inverse.
  70. */
  71. template <class T>
  72. matrix<T, 2, 2> inverse(const matrix<T, 2, 2>& m);
  73. /// @copydoc inverse(const matrix<T, 2, 2>&)
  74. template <class T>
  75. matrix<T, 3, 3> inverse(const matrix<T, 3, 3>& m);
  76. /// @copydoc inverse(const matrix<T, 2, 2>&)
  77. template <class T>
  78. matrix<T, 4, 4> inverse(const matrix<T, 4, 4>& m);
  79. /**
  80. * Performs a component-wise multiplication of two matrices.
  81. *
  82. * @param x First matrix multiplicand.
  83. * @param y Second matrix multiplicand.
  84. */
  85. template <class T>
  86. matrix<T, 2, 2> componentwise_mul(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y);
  87. /// @copydoc componentwise_mul(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  88. template <class T>
  89. matrix<T, 3, 3> componentwise_mul(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y);
  90. /// @copydoc componentwise_mul(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  91. template <class T>
  92. matrix<T, 4, 4> componentwise_mul(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y);
  93. /**
  94. * Creates a viewing transformation matrix.
  95. *
  96. * @param position Position of the view point.
  97. * @param target Position of the target.
  98. * @param up Normalized direction of the up vector.
  99. * @return Viewing transformation matrix.
  100. */
  101. template <class T>
  102. matrix<T, 4, 4> look_at(const vector<T, 3>& position, const vector<T, 3>& target, vector<T, 3> up);
  103. /**
  104. * Multiplies two matrices.
  105. *
  106. * @param x First matrix.
  107. * @param y Second matrix.
  108. * @return Product of the two matrices.
  109. */
  110. template <class T>
  111. matrix<T, 2, 2> mul(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y);
  112. /// @copydoc mul(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&);
  113. template <class T>
  114. matrix<T, 3, 3> mul(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y);
  115. /// @copydoc mul(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&);
  116. template <class T>
  117. matrix<T, 4, 4> mul(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y);
  118. /**
  119. * Multiplies a matrix by a scalar.
  120. *
  121. * @param m Matrix.
  122. * @param s Scalar.
  123. * @return Product of the matrix and the scalar..
  124. */
  125. template <class T, std::size_t N, std::size_t M>
  126. matrix<T, N, M> mul(const matrix<T, N, M>& m, T s);
  127. /**
  128. * Transforms a vector by a matrix.
  129. *
  130. * @param m Matrix.
  131. * @param v Vector.
  132. * @return Transformed vector.
  133. */
  134. template <class T>
  135. vector<T, 2> mul(const matrix<T, 2, 2>& m, const vector<T, 2>& v);
  136. /// @copydoc mul(const matrix<T, 2, 2>&, const vector<T, 2>&)
  137. template <class T>
  138. vector<T, 3> mul(const matrix<T, 3, 3>& m, const vector<T, 3>& v);
  139. /// @copydoc mul(const matrix<T, 2, 2>&, const vector<T, 2>&)
  140. template <class T>
  141. vector<T, 4> mul(const matrix<T, 4, 4>& m, const vector<T, 4>& v);
  142. /**
  143. * Creates an orthographic projection matrix which will transform the near and far clipping planes to `[-1, 1]`, respectively.
  144. *
  145. * @param left Signed distance to the left clipping plane.
  146. * @param right Signed distance to the right clipping plane.
  147. * @param bottom Signed distance to the bottom clipping plane.
  148. * @param top Signed distance to the top clipping plane.
  149. * @param z_near Signed distance to the near clipping plane.
  150. * @param z_far Signed distance to the far clipping plane.
  151. * @return Orthographic projection matrix.
  152. */
  153. template <class T>
  154. matrix<T, 4, 4> ortho(T left, T right, T bottom, T top, T z_near, T z_far);
  155. /**
  156. * Creates an orthographic projection matrix which will transform the near and far clipping planes to `[0, 1]`, respectively.
  157. *
  158. * @param left Signed distance to the left clipping plane.
  159. * @param right Signed distance to the right clipping plane.
  160. * @param bottom Signed distance to the bottom clipping plane.
  161. * @param top Signed distance to the top clipping plane.
  162. * @param z_near Signed distance to the near clipping plane.
  163. * @param z_far Signed distance to the far clipping plane.
  164. * @return Orthographic projection matrix.
  165. */
  166. template <class T>
  167. matrix<T, 4, 4> ortho_half_z(T left, T right, T bottom, T top, T z_near, T z_far);
  168. /**
  169. * Calculates the outer product of a pair of vectors.
  170. *
  171. * @param c Parameter to be treated as a column vector.
  172. * @param r Parameter to be treated as a row vector.
  173. */
  174. template <class T>
  175. matrix<T, 2, 2> outer_product(const vector<T, 2>& c, const vector<T, 2>& r);
  176. /// @copydoc outer_product(const vector<T, 2>&, const vector<T, 2>&)
  177. template <class T>
  178. matrix<T, 3, 3> outer_product(const vector<T, 3>& c, const vector<T, 3>& r);
  179. /// @copydoc outer_product(const vector<T, 2>&, const vector<T, 2>&)
  180. template <class T>
  181. matrix<T, 4, 4> outer_product(const vector<T, 4>& c, const vector<T, 4> r);
  182. /**
  183. * Creates a perspective projection matrix which will transform the near and far clipping planes to `[-1, 1]`, respectively.
  184. *
  185. * @param vertical_fov Vertical field of view angle, in radians.
  186. * @param aspect_ratio Aspect ratio which determines the horizontal field of view.
  187. * @param z_near Distance to the near clipping plane.
  188. * @param z_far Distance to the far clipping plane.
  189. * @return Perspective projection matrix.
  190. */
  191. template <class T>
  192. matrix<T, 4, 4> perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far);
  193. /**
  194. * Creates a perspective projection matrix which will transform the near and far clipping planes to `[0, 1]`, respectively.
  195. *
  196. * @param vertical_fov Vertical field of view angle, in radians.
  197. * @param aspect_ratio Aspect ratio which determines the horizontal field of view.
  198. * @param z_near Distance to the near clipping plane.
  199. * @param z_far Distance to the far clipping plane.
  200. * @return Perspective projection matrix.
  201. */
  202. template <class T>
  203. matrix<T, 4, 4> perspective_half_z(T vertical_fov, T aspect_ratio, T z_near, T z_far);
  204. /**
  205. * Resizes a matrix. Any new elements will be set to `1` if in the diagonal, and `0` otherwise.
  206. *
  207. * @param m Matrix to resize.
  208. * @return Resized matrix.
  209. */
  210. template <std::size_t N1, std::size_t M1, class T, std::size_t N0, std::size_t M0>
  211. matrix<T, N1, M1> resize(const matrix<T, N0, M0>& m);
  212. /**
  213. * Rotates a matrix.
  214. *
  215. * @param m Matrix to rotate.
  216. * @param angle Angle of rotation (in radians).
  217. * @param axis Axis of rotation
  218. * @return Rotated matrix.
  219. */
  220. template <class T>
  221. matrix<T, 4, 4> rotate(const matrix<T, 4, 4>& m, T angle, const vector<T, 3>& axis);
  222. /**
  223. * Scales a matrix.
  224. *
  225. * @param m Matrix to scale.
  226. * @param v Scale vector.
  227. * @return Scaled matrix.
  228. */
  229. template <class T>
  230. matrix<T, 4, 4> scale(const matrix<T, 4, 4>& m, const vector<T, 3>& v);
  231. /**
  232. * Subtracts a matrix from another matrix.
  233. *
  234. * @param x First matrix.
  235. * @param y Second matrix.
  236. * @return Difference between the two matrices.
  237. */
  238. template <class T>
  239. matrix<T, 2, 2> sub(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y);
  240. /// @copydoc sub(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  241. template <class T>
  242. matrix<T, 3, 3> sub(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y);
  243. /// @copydoc sub(const matrix<T, 2, 2>&, const matrix<T, 2, 2>&)
  244. template <class T>
  245. matrix<T, 4, 4> sub(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y);
  246. /**
  247. * Translates a matrix.
  248. *
  249. * @param m Matrix to translate.
  250. * @param v Translation vector.
  251. * @return Translated matrix.
  252. */
  253. template <class T>
  254. matrix<T, 4, 4> translate(const matrix<T, 4, 4>& m, const vector<T, 3>& v);
  255. /**
  256. * Calculates the transpose of a matrix.
  257. *
  258. * @param m Matrix of which to take the transpose.
  259. */
  260. template <class T>
  261. matrix<T, 2, 2> transpose(const matrix<T, 2, 2>& m);
  262. /// @copydoc transpose(const matrix<T, 2, 2>&)
  263. template <class T>
  264. matrix<T, 3, 3> transpose(const matrix<T, 3, 3>& m);
  265. /// @copydoc transpose(const matrix<T, 2, 2>&)
  266. template <class T>
  267. matrix<T, 4, 4> transpose(const matrix<T, 4, 4>& m);
  268. template <class T>
  269. matrix<T, 2, 2> add(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y)
  270. {
  271. return
  272. {{
  273. x[0] + y[0],
  274. x[1] + y[1]
  275. }};
  276. }
  277. template <class T>
  278. matrix<T, 3, 3> add(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y)
  279. {
  280. return
  281. {{
  282. x[0] + y[0],
  283. x[1] + y[1],
  284. x[2] + y[2]
  285. }};
  286. }
  287. template <class T>
  288. matrix<T, 4, 4> add(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y)
  289. {
  290. return
  291. {{
  292. x[0] + y[0],
  293. x[1] + y[1],
  294. x[2] + y[2],
  295. x[3] + y[3]
  296. }};
  297. }
  298. template <std::size_t N, std::size_t M, typename T>
  299. inline matrix<T, N, M>& as_matrix(T& data)
  300. {
  301. static_assert(std::is_pod<matrix<T, N, M>>::value);
  302. return reinterpret_cast<matrix<T, N, M>&>(data);
  303. }
  304. template <class T>
  305. T determinant(const matrix<T, 2, 2>& m)
  306. {
  307. return m[0][0] * m[1][1] - m[0][1] * m[1][0];
  308. }
  309. template <class T>
  310. T determinant(const matrix<T, 3, 3>& m)
  311. {
  312. return m[0][0] * m [1][1] * m[2][2] +
  313. m[0][1] * m[1][2] * m[2][0] +
  314. m[0][2] * m[1][0] * m[2][1] -
  315. m[0][0] * m[1][2] * m[2][1] -
  316. m[0][1] * m[1][0] * m[2][2] -
  317. m[0][2] * m[1][1] * m[2][0];
  318. }
  319. template <class T>
  320. T determinant(const matrix<T, 4, 4>& m)
  321. {
  322. return m[0][3] * m[1][2] * m[2][1] * m[3][0] - m[0][2] * m[1][3] * m[2][1] * m[3][0] -
  323. m[0][3] * m[1][1] * m[2][2] * m[3][0] + m[0][1] * m[1][3] * m[2][2] * m[3][0] +
  324. m[0][2] * m[1][1] * m[2][3] * m[3][0] - m[0][1] * m[1][2] * m[2][3] * m[3][0] -
  325. m[0][3] * m[1][2] * m[2][0] * m[3][1] + m[0][2] * m[1][3] * m[2][0] * m[3][1] +
  326. m[0][3] * m[1][0] * m[2][2] * m[3][1] - m[0][0] * m[1][3] * m[2][2] * m[3][1] -
  327. m[0][2] * m[1][0] * m[2][3] * m[3][1] + m[0][0] * m[1][2] * m[2][3] * m[3][1] +
  328. m[0][3] * m[1][1] * m[2][0] * m[3][2] - m[0][1] * m[1][3] * m[2][0] * m[3][2] -
  329. m[0][3] * m[1][0] * m[2][1] * m[3][2] + m[0][0] * m[1][3] * m[2][1] * m[3][2] +
  330. m[0][1] * m[1][0] * m[2][3] * m[3][2] - m[0][0] * m[1][1] * m[2][3] * m[3][2] -
  331. m[0][2] * m[1][1] * m[2][0] * m[3][3] + m[0][1] * m[1][2] * m[2][0] * m[3][3] +
  332. m[0][2] * m[1][0] * m[2][1] * m[3][3] - m[0][0] * m[1][2] * m[2][1] * m[3][3] -
  333. m[0][1] * m[1][0] * m[2][2] * m[3][3] + m[0][0] * m[1][1] * m[2][2] * m[3][3];
  334. }
  335. template <class T>
  336. matrix<T, 2, 2> inverse(const matrix<T, 2, 2>& m)
  337. {
  338. static_assert(std::is_floating_point<T>::value);
  339. const T rd(T(1) / determinant(m));
  340. return
  341. {{
  342. { m[1][1] * rd, -m[0][1] * rd},
  343. {-m[1][0] * rd, m[0][0] * rd}
  344. }};
  345. }
  346. template <class T>
  347. matrix<T, 3, 3> inverse(const matrix<T, 3, 3>& m)
  348. {
  349. static_assert(std::is_floating_point<T>::value);
  350. const T rd(T(1) / determinant(m));
  351. return
  352. {{
  353. {
  354. (m[1][1] * m[2][2] - m[1][2] * m[2][1]) * rd,
  355. (m[0][2] * m[2][1] - m[0][1] * m[2][2]) * rd,
  356. (m[0][1] * m[1][2] - m[0][2] * m[1][1]) * rd
  357. },
  358. {
  359. (m[1][2] * m[2][0] - m[1][0] * m[2][2]) * rd,
  360. (m[0][0] * m[2][2] - m[0][2] * m[2][0]) * rd,
  361. (m[0][2] * m[1][0] - m[0][0] * m[1][2]) * rd
  362. },
  363. {
  364. (m[1][0] * m[2][1] - m[1][1] * m[2][0]) * rd,
  365. (m[0][1] * m[2][0] - m[0][0] * m[2][1]) * rd,
  366. (m[0][0] * m[1][1] - m[0][1] * m[1][0]) * rd
  367. }
  368. }};
  369. }
  370. template <class T>
  371. matrix<T, 4, 4> inverse(const matrix<T, 4, 4>& m)
  372. {
  373. static_assert(std::is_floating_point<T>::value);
  374. const T rd(T(1) / determinant(m));
  375. return
  376. {{
  377. {
  378. (m[1][2] * m[2][3] * m[3][1] - m[1][3] * m[2][2] * m[3][1] + m[1][3] * m[2][1] * m[3][2] - m[1][1] * m[2][3] * m[3][2] - m[1][2] * m[2][1] * m[3][3] + m[1][1] * m[2][2] * m[3][3]) * rd,
  379. (m[0][3] * m[2][2] * m[3][1] - m[0][2] * m[2][3] * m[3][1] - m[0][3] * m[2][1] * m[3][2] + m[0][1] * m[2][3] * m[3][2] + m[0][2] * m[2][1] * m[3][3] - m[0][1] * m[2][2] * m[3][3]) * rd,
  380. (m[0][2] * m[1][3] * m[3][1] - m[0][3] * m[1][2] * m[3][1] + m[0][3] * m[1][1] * m[3][2] - m[0][1] * m[1][3] * m[3][2] - m[0][2] * m[1][1] * m[3][3] + m[0][1] * m[1][2] * m[3][3]) * rd,
  381. (m[0][3] * m[1][2] * m[2][1] - m[0][2] * m[1][3] * m[2][1] - m[0][3] * m[1][1] * m[2][2] + m[0][1] * m[1][3] * m[2][2] + m[0][2] * m[1][1] * m[2][3] - m[0][1] * m[1][2] * m[2][3]) * rd
  382. },
  383. {
  384. (m[1][3] * m[2][2] * m[3][0] - m[1][2] * m[2][3] * m[3][0] - m[1][3] * m[2][0] * m[3][2] + m[1][0] * m[2][3] * m[3][2] + m[1][2] * m[2][0] * m[3][3] - m[1][0] * m[2][2] * m[3][3]) * rd,
  385. (m[0][2] * m[2][3] * m[3][0] - m[0][3] * m[2][2] * m[3][0] + m[0][3] * m[2][0] * m[3][2] - m[0][0] * m[2][3] * m[3][2] - m[0][2] * m[2][0] * m[3][3] + m[0][0] * m[2][2] * m[3][3]) * rd,
  386. (m[0][3] * m[1][2] * m[3][0] - m[0][2] * m[1][3] * m[3][0] - m[0][3] * m[1][0] * m[3][2] + m[0][0] * m[1][3] * m[3][2] + m[0][2] * m[1][0] * m[3][3] - m[0][0] * m[1][2] * m[3][3]) * rd,
  387. (m[0][2] * m[1][3] * m[2][0] - m[0][3] * m[1][2] * m[2][0] + m[0][3] * m[1][0] * m[2][2] - m[0][0] * m[1][3] * m[2][2] - m[0][2] * m[1][0] * m[2][3] + m[0][0] * m[1][2] * m[2][3]) * rd
  388. },
  389. {
  390. (m[1][1] * m[2][3] * m[3][0] - m[1][3] * m[2][1] * m[3][0] + m[1][3] * m[2][0] * m[3][1] - m[1][0] * m[2][3] * m[3][1] - m[1][1] * m[2][0] * m[3][3] + m[1][0] * m[2][1] * m[3][3]) * rd,
  391. (m[0][3] * m[2][1] * m[3][0] - m[0][1] * m[2][3] * m[3][0] - m[0][3] * m[2][0] * m[3][1] + m[0][0] * m[2][3] * m[3][1] + m[0][1] * m[2][0] * m[3][3] - m[0][0] * m[2][1] * m[3][3]) * rd,
  392. (m[0][1] * m[1][3] * m[3][0] - m[0][3] * m[1][1] * m[3][0] + m[0][3] * m[1][0] * m[3][1] - m[0][0] * m[1][3] * m[3][1] - m[0][1] * m[1][0] * m[3][3] + m[0][0] * m[1][1] * m[3][3]) * rd,
  393. (m[0][3] * m[1][1] * m[2][0] - m[0][1] * m[1][3] * m[2][0] - m[0][3] * m[1][0] * m[2][1] + m[0][0] * m[1][3] * m[2][1] + m[0][1] * m[1][0] * m[2][3] - m[0][0] * m[1][1] * m[2][3]) * rd
  394. },
  395. {
  396. (m[1][2] * m[2][1] * m[3][0] - m[1][1] * m[2][2] * m[3][0] - m[1][2] * m[2][0] * m[3][1] + m[1][0] * m[2][2] * m[3][1] + m[1][1] * m[2][0] * m[3][2] - m[1][0] * m[2][1] * m[3][2]) * rd,
  397. (m[0][1] * m[2][2] * m[3][0] - m[0][2] * m[2][1] * m[3][0] + m[0][2] * m[2][0] * m[3][1] - m[0][0] * m[2][2] * m[3][1] - m[0][1] * m[2][0] * m[3][2] + m[0][0] * m[2][1] * m[3][2]) * rd,
  398. (m[0][2] * m[1][1] * m[3][0] - m[0][1] * m[1][2] * m[3][0] - m[0][2] * m[1][0] * m[3][1] + m[0][0] * m[1][2] * m[3][1] + m[0][1] * m[1][0] * m[3][2] - m[0][0] * m[1][1] * m[3][2]) * rd,
  399. (m[0][1] * m[1][2] * m[2][0] - m[0][2] * m[1][1] * m[2][0] + m[0][2] * m[1][0] * m[2][1] - m[0][0] * m[1][2] * m[2][1] - m[0][1] * m[1][0] * m[2][2] + m[0][0] * m[1][1] * m[2][2]) * rd
  400. }
  401. }};
  402. }
  403. template <class T>
  404. matrix<T, 2, 2> componentwise_mul(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y)
  405. {
  406. return
  407. {{
  408. {x[0][0] * y[0][0], x[0][1] * y[0][1]},
  409. {x[1][0] * y[1][0], x[1][1] * y[1][1]}
  410. }};
  411. }
  412. template <class T>
  413. matrix<T, 3, 3> componentwise_mul(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y)
  414. {
  415. return
  416. {{
  417. {x[0][0] * y[0][0], x[0][1] * y[0][1], x[0][2] * y[0][2]},
  418. {x[1][0] * y[1][0], x[1][1] * y[1][1], x[1][2] * y[1][2]},
  419. {x[2][0] * y[2][0], x[2][1] * y[2][1], x[2][2] * y[2][2]}
  420. }};
  421. }
  422. template <class T>
  423. matrix<T, 4, 4> componentwise_mul(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y)
  424. {
  425. return
  426. {{
  427. {x[0][0] * y[0][0], x[0][1] * y[0][1], x[0][2] * y[0][2], x[0][3] * y[0][3]},
  428. {x[1][0] * y[1][0], x[1][1] * y[1][1], x[1][2] * y[1][2], x[1][3] * y[1][3]},
  429. {x[2][0] * y[2][0], x[2][1] * y[2][1], x[2][2] * y[2][2], x[2][3] * y[2][3]},
  430. {x[3][0] * y[3][0], x[3][1] * y[3][1], x[3][2] * y[3][2], x[3][3] * y[3][3]}
  431. }};
  432. }
  433. template <class T>
  434. matrix<T, 4, 4> look_at(const vector<T, 3>& position, const vector<T, 3>& target, vector<T, 3> up)
  435. {
  436. vector<T, 3> forward = normalize(sub(target, position));
  437. vector<T, 3> right = normalize(cross(forward, up));
  438. up = cross(right, forward);
  439. matrix<T, 4, 4> m =
  440. {{
  441. {right[0], up[0], -forward[0], T(0)},
  442. {right[1], up[1], -forward[1], T(0)},
  443. {right[2], up[2], -forward[2], T(0)},
  444. {T(0), T(0), T(0), T(1)}
  445. }};
  446. return translate(m, negate(position));
  447. }
  448. template <class T>
  449. matrix<T, 2, 2> mul(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y)
  450. {
  451. return
  452. {{
  453. x[0] * y[0][0] + x[1] * y[0][1],
  454. x[0] * y[1][0] + x[1] * y[1][1]
  455. }};
  456. }
  457. template <class T>
  458. matrix<T, 3, 3> mul(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y)
  459. {
  460. return
  461. {{
  462. x[0] * y[0][0] + x[1] * y[0][1] + x[2] * y[0][2],
  463. x[0] * y[1][0] + x[1] * y[1][1] + x[2] * y[1][2],
  464. x[0] * y[2][0] + x[1] * y[2][1] + x[2] * y[2][2]
  465. }};
  466. }
  467. template <class T>
  468. matrix<T, 4, 4> mul(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y)
  469. {
  470. return
  471. {{
  472. x[0] * y[0][0] + x[1] * y[0][1] + x[2] * y[0][2] + x[3] * y[0][3],
  473. x[0] * y[1][0] + x[1] * y[1][1] + x[2] * y[1][2] + x[3] * y[1][3],
  474. x[0] * y[2][0] + x[1] * y[2][1] + x[2] * y[2][2] + x[3] * y[2][3],
  475. x[0] * y[3][0] + x[1] * y[3][1] + x[2] * y[3][2] + x[3] * y[3][3]
  476. }};
  477. }
  478. /// @private
  479. template <class T, std::size_t N, std::size_t M, std::size_t... I>
  480. inline matrix<T, N, M> mul(const matrix<T, N, M>& m, T s, std::index_sequence<I...>)
  481. {
  482. return {{(m[I] * s)...}};
  483. }
  484. template <class T, std::size_t N, std::size_t M>
  485. inline matrix<T, N, M> mul(const matrix<T, N, M>& m, T s)
  486. {
  487. return mul(m, s, std::make_index_sequence<N>{});
  488. }
  489. template <class T>
  490. vector<T, 2> mul(const matrix<T, 2, 2>& m, const vector<T, 2>& v)
  491. {
  492. return
  493. {
  494. m[0][0] * v[0] + m[1][0] * v[1],
  495. m[0][1] * v[0] + m[1][1] * v[1]
  496. };
  497. }
  498. template <class T>
  499. vector<T, 3> mul(const matrix<T, 3, 3>& m, const vector<T, 3>& v)
  500. {
  501. return
  502. {
  503. m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2],
  504. m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2],
  505. m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2]
  506. };
  507. }
  508. template <class T>
  509. vector<T, 4> mul(const matrix<T, 4, 4>& m, const vector<T, 4>& v)
  510. {
  511. return
  512. {
  513. m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2] + m[3][0] * v[3],
  514. m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2] + m[3][1] * v[3],
  515. m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2] + m[3][2] * v[3],
  516. m[0][3] * v[0] + m[1][3] * v[1] + m[2][3] * v[2] + m[3][3] * v[3]
  517. };
  518. }
  519. template <class T>
  520. matrix<T, 4, 4> ortho(T left, T right, T bottom, T top, T z_near, T z_far)
  521. {
  522. return
  523. {{
  524. {T(2) / (right - left), T(0), T(0), T(0)},
  525. {T(0), T(2) / (top - bottom), T(0), T(0)},
  526. {T(0), T(0), T(-2) / (z_far - z_near), T(0)},
  527. {-((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((z_far + z_near) / (z_far - z_near)), T(1)}
  528. }};
  529. }
  530. template <class T>
  531. matrix<T, 4, 4> ortho_half_z(T left, T right, T bottom, T top, T z_near, T z_far)
  532. {
  533. return
  534. {{
  535. {T(2) / (right - left), T(0), T(0), T(0)},
  536. {T(0), T(2) / (top - bottom), T(0), T(0)},
  537. {T(0), T(0), T(-1) / (z_far - z_near), T(0)},
  538. {-((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -z_near / (z_far - z_near), T(1)}
  539. }};
  540. }
  541. template <class T>
  542. matrix<T, 2, 2> outer_product(const vector<T, 2>& c, const vector<T, 2>& r)
  543. {
  544. return
  545. {{
  546. {c[0] * r[0], c[1] * r[0]},
  547. {c[0] * r[1], c[1] * r[1]}
  548. }};
  549. }
  550. template <class T>
  551. matrix<T, 3, 3> outer_product(const vector<T, 3>& c, const vector<T, 3>& r)
  552. {
  553. return
  554. {{
  555. {c[0] * r[0], c[1] * r[0], c[2] * r[0]},
  556. {c[0] * r[1], c[1] * r[1], c[2] * r[1]},
  557. {c[0] * r[2], c[1] * r[2], c[2] * r[2]}
  558. }};
  559. }
  560. template <class T>
  561. matrix<T, 4, 4> outer_product(const vector<T, 4>& c, const vector<T, 4> r)
  562. {
  563. return
  564. {{
  565. {c[0] * r[0], c[1] * r[0], c[2] * r[0], c[3] * r[0]},
  566. {c[0] * r[1], c[1] * r[1], c[2] * r[1], c[3] * r[1]},
  567. {c[0] * r[2], c[1] * r[2], c[2] * r[2], c[3] * r[2]},
  568. {c[0] * r[3], c[1] * r[3], c[2] * r[3], c[3] * r[3]}
  569. }};
  570. }
  571. template <class T>
  572. matrix<T, 4, 4> perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far)
  573. {
  574. T half_fov = vertical_fov * T(0.5);
  575. T f = std::cos(half_fov) / std::sin(half_fov);
  576. return
  577. {{
  578. {f / aspect_ratio, T(0), T(0), T(0)},
  579. {T(0), f, T(0), T(0)},
  580. {T(0), T(0), (z_far + z_near) / (z_near - z_far), T(-1)},
  581. {T(0), T(0), (T(2) * z_far * z_near) / (z_near - z_far), T(0)}
  582. }};
  583. }
  584. template <class T>
  585. matrix<T, 4, 4> perspective_half_z(T vertical_fov, T aspect_ratio, T z_near, T z_far)
  586. {
  587. T half_fov = vertical_fov * T(0.5);
  588. T f = std::cos(half_fov) / std::sin(half_fov);
  589. return
  590. {{
  591. {f / aspect_ratio, T(0), T(0), T(0)},
  592. {T(0), f, T(0), T(0)},
  593. {T(0), T(0), z_far / (z_near - z_far), T(-1)},
  594. {T(0), T(0), -(z_far * z_near) / (z_far - z_near), T(0)}
  595. }};
  596. }
  597. template <std::size_t N1, std::size_t M1, class T, std::size_t N0, std::size_t M0>
  598. matrix<T, N1, M1> resize(const matrix<T, N0, M0>& m)
  599. {
  600. matrix<T, N1, M1> resized;
  601. for (std::size_t i = 0; i < N1; ++i)
  602. {
  603. for (std::size_t j = 0; j < M1; ++j)
  604. {
  605. resized[i][j] = (i < N0 && j < M0) ? m[i][j] : ((i == j) ? T(1) : T(0));
  606. }
  607. }
  608. return resized;
  609. }
  610. template <class T>
  611. matrix<T, 4, 4> rotate(const matrix<T, 4, 4>& m, T angle, const vector<T, 3>& axis)
  612. {
  613. const T c = std::cos(angle);
  614. const T s = std::sin(angle);
  615. const vector<T, 3> temp = mul(axis, T(1) - c);
  616. matrix<T, 4, 4> rotation;
  617. rotation[0][0] = axis[0] * temp[0] + c
  618. rotation[0][1] = axis[1] * temp[0] + axis[2] * s;
  619. rotation[0][2] = axis[2] * temp[0] - axis[1] * s;
  620. rotation[0][3] = T(0);
  621. rotation[1][0] = axis[0] * temp[1] - axis[2] * s
  622. rotation[1][1] = axis[1] * temp[1] + c;
  623. rotation[1][2] = axis[2] * temp[1] + axis[0] * s;
  624. rotation[1][3] = T(0);
  625. rotation[2][0] = axis[0] * temp[2] + axis[1] * s;
  626. rotation[2][1] = axis[1] * temp[2] - axis[0] * s;
  627. rotation[2][2] = axis[2] * temp[2] + c
  628. rotation[2][3] = T(0);
  629. rotation[3][0] = T(0);
  630. rotation[3][1] = T(0);
  631. rotation[3][2] = T(0);
  632. rotation[3][3] = T(1);
  633. return mul(m, rotation);
  634. }
  635. template <class T>
  636. matrix<T, 4, 4> scale(const matrix<T, 4, 4>& m, const vector<T, 3>& v)
  637. {
  638. return mul(m, matrix<T, 4, 4>
  639. {{
  640. {v[0], T(0), T(0), T(0)},
  641. {T(0), v[1], T(0), T(0)},
  642. {T(0), T(0), v[2], T(0)},
  643. {T(0), T(0), T(0), T(1)}
  644. }});
  645. }
  646. template <class T>
  647. matrix<T, 2, 2> sub(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y)
  648. {
  649. return
  650. {{
  651. x[0] - y[0],
  652. x[1] - y[1]
  653. }};
  654. }
  655. template <class T>
  656. matrix<T, 3, 3> sub(const matrix<T, 3, 3>& x, const matrix<T, 3, 3>& y)
  657. {
  658. return
  659. {{
  660. x[0] - y[0],
  661. x[1] - y[1],
  662. x[2] - y[2]
  663. }};
  664. }
  665. template <class T>
  666. matrix<T, 4, 4> sub(const matrix<T, 4, 4>& x, const matrix<T, 4, 4>& y)
  667. {
  668. return
  669. {{
  670. x[0] - y[0],
  671. x[1] - y[1],
  672. x[2] - y[2],
  673. x[3] - y[3]
  674. }};
  675. }
  676. template <class T>
  677. matrix<T, 4, 4> translate(const matrix<T, 4, 4>& m, const vector<T, 3>& v)
  678. {
  679. return mul(m, matrix<T, 4, 4>
  680. {{
  681. {T(1), T(0), T(0), T(0)},
  682. {T(0), T(1), T(0), T(0)},
  683. {T(0), T(0), T(1), T(0)},
  684. {v[0], v[1], v[2], T(1)}
  685. }});
  686. }
  687. template <class T>
  688. matrix<T, 2, 2> transpose(const matrix<T, 2, 2>& m)
  689. {
  690. return
  691. {{
  692. {
  693. m[0][0], m[1][0]
  694. },
  695. {
  696. m[0][1], m[1][1]
  697. }
  698. }};
  699. }
  700. template <class T>
  701. matrix<T, 3, 3> transpose(const matrix<T, 3, 3>& m)
  702. {
  703. return
  704. {{
  705. {
  706. m[0][0], m[1][0], m[2][0]
  707. },
  708. {
  709. m[0][1], m[1][1], m[2][1]
  710. },
  711. {
  712. m[0][2], m[1][2], m[2][2]
  713. }
  714. }};
  715. }
  716. template <class T>
  717. matrix<T, 4, 4> transpose(const matrix<T, 4, 4>& m)
  718. {
  719. return
  720. {{
  721. {
  722. m[0][0], m[1][0], m[2][0], m[3][0]
  723. },
  724. {
  725. m[0][1], m[1][1], m[2][1], m[3][1]
  726. },
  727. {
  728. m[0][2], m[1][2], m[2][2], m[3][2]
  729. },
  730. {
  731. m[0][3], m[1][3], m[2][3], m[3][3]
  732. }
  733. }};
  734. }
  735. /// @}
  736. } // namespace math
  737. #endif // ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP