2
0

PolyMatrix4.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include <string.h>
  21. #include "PolyGlobals.h"
  22. #include "PolyVector3.h"
  23. namespace Polycode {
  24. class Vector3;
  25. /**
  26. * 4x4 Matrix.
  27. */
  28. class _PolyExport Matrix4 : public PolyBase {
  29. public:
  30. Matrix4();
  31. /**
  32. * Construct with 16 matrix values.
  33. */
  34. inline Matrix4(
  35. Number m00, Number m01, Number m02, Number m03,
  36. Number m10, Number m11, Number m12, Number m13,
  37. Number m20, Number m21, Number m22, Number m23,
  38. Number m30, Number m31, Number m32, Number m33 )
  39. {
  40. m[0][0] = m00;
  41. m[0][1] = m01;
  42. m[0][2] = m02;
  43. m[0][3] = m03;
  44. m[1][0] = m10;
  45. m[1][1] = m11;
  46. m[1][2] = m12;
  47. m[1][3] = m13;
  48. m[2][0] = m20;
  49. m[2][1] = m21;
  50. m[2][2] = m22;
  51. m[2][3] = m23;
  52. m[3][0] = m30;
  53. m[3][1] = m31;
  54. m[3][2] = m32;
  55. m[3][3] = m33;
  56. }
  57. /**
  58. * Construct with pointer to 16 Number values.
  59. */
  60. Matrix4(const Number *m);
  61. ~Matrix4();
  62. union {
  63. Number m[4][4];
  64. Number ml[16];
  65. };
  66. /**
  67. * Resets the matrix to identity.
  68. */
  69. inline void identity() {
  70. memset(ml, 0, sizeof(Number)*16);
  71. ml[0] = 1;
  72. ml[5] = 1;
  73. ml[10] = 1;
  74. ml[15] = 1;
  75. }
  76. /**
  77. * Rotates a vector by the matrix values.
  78. * @param v2 Vector to rotate.
  79. */
  80. inline Vector3 rotateVector(const Vector3 &v2) const {
  81. return Vector3(v2.x*m[0][0] + v2.y*m[1][0] + v2.z*m[2][0],
  82. v2.x*m[0][1] + v2.y*m[1][1] + v2.z*m[2][1],
  83. v2.x*m[0][2] + v2.y*m[1][2] + v2.z*m[2][2]);
  84. }
  85. /**
  86. * Returns the position from the matrix.
  87. * @return Position.
  88. */
  89. inline Vector3 getPosition() const {
  90. Vector3 pos;
  91. pos.x = m[3][0];
  92. pos.y = m[3][1];
  93. pos.z = m[3][2];
  94. return pos;
  95. }
  96. // ----------------------------------------------------------------------------------------------------------------
  97. /** @name Operators
  98. * Available vector operators.
  99. */
  100. //@{
  101. inline Matrix4 operator * (Number n) const {
  102. return Matrix4(
  103. n*m[0][0], n*m[0][1], n*m[0][2], n*m[0][3],
  104. n*m[1][0], n*m[1][1], n*m[1][2], n*m[1][3],
  105. n*m[2][0], n*m[2][1], n*m[2][2], n*m[2][3],
  106. n*m[3][0], n*m[3][1], n*m[3][2], n*m[3][3]);
  107. }
  108. inline Vector3 operator * ( const Vector3 &v2 ) const
  109. {
  110. return Vector3(v2.x*m[0][0] + v2.y*m[1][0] + v2.z*m[2][0] + m[3][0],
  111. v2.x*m[0][1] + v2.y*m[1][1] + v2.z*m[2][1] + m[3][1],
  112. v2.x*m[0][2] + v2.y*m[1][2] + v2.z*m[2][2] + m[3][2]);
  113. }
  114. inline Number* operator [] ( int row ) { return m[row];}
  115. inline const Number* operator [] ( int row ) const { return m[row];}
  116. inline Matrix4 operator + ( const Matrix4 &m2 ) const {
  117. Matrix4 r;
  118. r.m[0][0] = m[0][0] + m2.m[0][0];
  119. r.m[0][1] = m[0][1] + m2.m[0][1];
  120. r.m[0][2] = m[0][2] + m2.m[0][2];
  121. r.m[0][3] = m[0][3] + m2.m[0][3];
  122. r.m[1][0] = m[1][0] + m2.m[1][0];
  123. r.m[1][1] = m[1][1] + m2.m[1][1];
  124. r.m[1][2] = m[1][2] + m2.m[1][2];
  125. r.m[1][3] = m[1][3] + m2.m[1][3];
  126. r.m[2][0] = m[2][0] + m2.m[2][0];
  127. r.m[2][1] = m[2][1] + m2.m[2][1];
  128. r.m[2][2] = m[2][2] + m2.m[2][2];
  129. r.m[2][3] = m[2][3] + m2.m[2][3];
  130. r.m[3][0] = m[3][0] + m2.m[3][0];
  131. r.m[3][1] = m[3][1] + m2.m[3][1];
  132. r.m[3][2] = m[3][2] + m2.m[3][2];
  133. r.m[3][3] = m[3][3] + m2.m[3][3];
  134. return r;
  135. }
  136. inline Matrix4 operator - ( const Matrix4 &m2 ) const {
  137. Matrix4 r;
  138. r.m[0][0] = m[0][0] - m2.m[0][0];
  139. r.m[0][1] = m[0][1] - m2.m[0][1];
  140. r.m[0][2] = m[0][2] - m2.m[0][2];
  141. r.m[0][3] = m[0][3] - m2.m[0][3];
  142. r.m[1][0] = m[1][0] - m2.m[1][0];
  143. r.m[1][1] = m[1][1] - m2.m[1][1];
  144. r.m[1][2] = m[1][2] - m2.m[1][2];
  145. r.m[1][3] = m[1][3] - m2.m[1][3];
  146. r.m[2][0] = m[2][0] - m2.m[2][0];
  147. r.m[2][1] = m[2][1] - m2.m[2][1];
  148. r.m[2][2] = m[2][2] - m2.m[2][2];
  149. r.m[2][3] = m[2][3] - m2.m[2][3];
  150. r.m[3][0] = m[3][0] - m2.m[3][0];
  151. r.m[3][1] = m[3][1] - m2.m[3][1];
  152. r.m[3][2] = m[3][2] - m2.m[3][2];
  153. r.m[3][3] = m[3][3] - m2.m[3][3];
  154. return r;
  155. }
  156. inline Matrix4 operator * (const Matrix4 &m2) const {
  157. Matrix4 r;
  158. r.m[0][0] = m[0][0] * m2.m[0][0] + m[0][1] * m2.m[1][0] + m[0][2] * m2.m[2][0] + m[0][3] * m2.m[3][0];
  159. r.m[0][1] = m[0][0] * m2.m[0][1] + m[0][1] * m2.m[1][1] + m[0][2] * m2.m[2][1] + m[0][3] * m2.m[3][1];
  160. r.m[0][2] = m[0][0] * m2.m[0][2] + m[0][1] * m2.m[1][2] + m[0][2] * m2.m[2][2] + m[0][3] * m2.m[3][2];
  161. r.m[0][3] = m[0][0] * m2.m[0][3] + m[0][1] * m2.m[1][3] + m[0][2] * m2.m[2][3] + m[0][3] * m2.m[3][3];
  162. r.m[1][0] = m[1][0] * m2.m[0][0] + m[1][1] * m2.m[1][0] + m[1][2] * m2.m[2][0] + m[1][3] * m2.m[3][0];
  163. r.m[1][1] = m[1][0] * m2.m[0][1] + m[1][1] * m2.m[1][1] + m[1][2] * m2.m[2][1] + m[1][3] * m2.m[3][1];
  164. r.m[1][2] = m[1][0] * m2.m[0][2] + m[1][1] * m2.m[1][2] + m[1][2] * m2.m[2][2] + m[1][3] * m2.m[3][2];
  165. r.m[1][3] = m[1][0] * m2.m[0][3] + m[1][1] * m2.m[1][3] + m[1][2] * m2.m[2][3] + m[1][3] * m2.m[3][3];
  166. r.m[2][0] = m[2][0] * m2.m[0][0] + m[2][1] * m2.m[1][0] + m[2][2] * m2.m[2][0] + m[2][3] * m2.m[3][0];
  167. r.m[2][1] = m[2][0] * m2.m[0][1] + m[2][1] * m2.m[1][1] + m[2][2] * m2.m[2][1] + m[2][3] * m2.m[3][1];
  168. r.m[2][2] = m[2][0] * m2.m[0][2] + m[2][1] * m2.m[1][2] + m[2][2] * m2.m[2][2] + m[2][3] * m2.m[3][2];
  169. r.m[2][3] = m[2][0] * m2.m[0][3] + m[2][1] * m2.m[1][3] + m[2][2] * m2.m[2][3] + m[2][3] * m2.m[3][3];
  170. r.m[3][0] = m[3][0] * m2.m[0][0] + m[3][1] * m2.m[1][0] + m[3][2] * m2.m[2][0] + m[3][3] * m2.m[3][0];
  171. r.m[3][1] = m[3][0] * m2.m[0][1] + m[3][1] * m2.m[1][1] + m[3][2] * m2.m[2][1] + m[3][3] * m2.m[3][1];
  172. r.m[3][2] = m[3][0] * m2.m[0][2] + m[3][1] * m2.m[1][2] + m[3][2] * m2.m[2][2] + m[3][3] * m2.m[3][2];
  173. r.m[3][3] = m[3][0] * m2.m[0][3] + m[3][1] * m2.m[1][3] + m[3][2] * m2.m[2][3] + m[3][3] * m2.m[3][3];
  174. return r;
  175. }
  176. //@}
  177. // ----------------------------------------------------------------------------------------------------------------
  178. /**
  179. * Sets the position in the matrix.
  180. * @param x X coordinate.
  181. * @param y Y coordinate.
  182. * @param z Z coordinate.
  183. */
  184. inline void setPosition(Number x, Number y, Number z) {
  185. m[3][0] = x;
  186. m[3][1] = y;
  187. m[3][2] = z;
  188. }
  189. /**
  190. * Sets the scale in the matrix.
  191. * @param x X scale.
  192. * @param y Y scale.
  193. * @param z Z scale.
  194. */
  195. inline void setScale(Vector3 scale) {
  196. m[0][0] = scale.x;
  197. m[1][1] = scale.y;
  198. m[2][2] = scale.z;
  199. }
  200. /**
  201. * Returns the matrix rotation as euler angles. (This might be kind of buggy).
  202. * @param ax Pointer to roll angle to set.
  203. * @param ay Pointer to pitch angle to set.
  204. * @param az Pointer to yaw angle to set.
  205. */
  206. inline void getEulerAngles(Number *ax, Number *ay, Number *az) const {
  207. Number angle_x, angle_y, angle_z,tr_x,tr_y,C;
  208. angle_y = asin(m[0][2]);
  209. C = cosf(angle_y);
  210. angle_y *= TODEGREES;
  211. if(fabsf(C) > 0.005) {
  212. tr_x = m[2][2] / C; // 10
  213. tr_y = -m[1][2] / C; // 6
  214. angle_x = atan2f( tr_y, tr_x ) * TODEGREES;
  215. tr_x = m[0][0] / C; // 0
  216. tr_y = -m[0][1] / C; // 1
  217. angle_z = atan2f( tr_y, tr_x ) * TODEGREES;
  218. } else {
  219. angle_x = 0;
  220. tr_x = m[1][1];
  221. tr_y = m[1][0];
  222. angle_z = atan2f( tr_y, tr_x ) * TODEGREES;
  223. }
  224. if (angle_x < 0) angle_x += 360;
  225. if (angle_y < 0) angle_y += 360;
  226. if (angle_z < 0) angle_z += 360;
  227. *ax = -fabs(angle_x);
  228. *ay = fabs(angle_y);
  229. *az = -fabs(angle_z);
  230. }
  231. /**
  232. * Returns the transpose of the matrix.
  233. */
  234. inline Matrix4 transpose() const {
  235. return Matrix4(m[0][0], m[1][0], m[2][0], m[3][0],
  236. m[0][1], m[1][1], m[2][1], m[3][1],
  237. m[0][2], m[1][2], m[2][2], m[3][2],
  238. m[0][3], m[1][3], m[2][3], m[3][3]);
  239. }
  240. /**
  241. * Returns the inverse of the matrix.
  242. */
  243. Matrix4 Inverse() const;
  244. /**
  245. * Returns the affine inverse of the matrix.
  246. */
  247. Matrix4 inverseAffine() const;
  248. /**
  249. * Returns the determinant of the matrix.
  250. */
  251. Number determinant() const;
  252. /**
  253. * Returns the determinant of any general (square) matrix.
  254. * @param a A square matrix as an array of pointers to rows (or columns).
  255. * @param n The number of dimensions in matrix A.
  256. */
  257. static Number generalDeterminant(Number const* const*a, int n);
  258. protected:
  259. };
  260. }