PageRenderTime 28ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/cocos2dx/kazmath/src/mat3.c

https://bitbucket.org/gontamoteam/gontamo-repo
C | 372 lines | 218 code | 73 blank | 81 comment | 13 complexity | 3b5716b982be16223687581d0bbf6dc0 MD5 | raw file
  1. /*
  2. Copyright (c) 2008, Luke Benstead.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <stdlib.h>
  23. #include <memory.h>
  24. #include <assert.h>
  25. #include "kazmath/utility.h"
  26. #include "kazmath/vec3.h"
  27. #include "kazmath/mat3.h"
  28. #include "kazmath/quaternion.h"
  29. kmMat3* const kmMat3Fill(kmMat3* pOut, const kmScalar* pMat)
  30. {
  31. memcpy(pOut->mat, pMat, sizeof(kmScalar) * 9);
  32. return pOut;
  33. }
  34. /** Sets pOut to an identity matrix returns pOut*/
  35. kmMat3* const kmMat3Identity(kmMat3* pOut)
  36. {
  37. memset(pOut->mat, 0, sizeof(float) * 9);
  38. pOut->mat[0] = pOut->mat[4] = pOut->mat[8] = 1.0f;
  39. return pOut;
  40. }
  41. const kmScalar kmMat3Determinant(const kmMat3* pIn)
  42. {
  43. kmScalar output;
  44. /*
  45. calculating the determinant following the rule of sarus,
  46. | 0 3 6 | 0 3 |
  47. m = | 1 4 7 | 1 4 |
  48. | 2 5 8 | 2 5 |
  49. now sum up the products of the diagonals going to the right (i.e. 0,4,8)
  50. and subtract the products of the other diagonals (i.e. 2,4,6)
  51. */
  52. output = pIn->mat[0] * pIn->mat[4] * pIn->mat[8] + pIn->mat[1] * pIn->mat[5] * pIn->mat[6] + pIn->mat[2] * pIn->mat[3] * pIn->mat[7];
  53. output -= pIn->mat[2] * pIn->mat[4] * pIn->mat[6] + pIn->mat[0] * pIn->mat[5] * pIn->mat[7] + pIn->mat[1] * pIn->mat[3] * pIn->mat[8];
  54. return output;
  55. }
  56. kmMat3* const kmMat3Adjugate(kmMat3* pOut, const kmMat3* pIn)
  57. {
  58. pOut->mat[0] = pIn->mat[4] * pIn->mat[8] - pIn->mat[5] * pIn->mat[7];
  59. pOut->mat[1] = pIn->mat[2] * pIn->mat[7] - pIn->mat[1] * pIn->mat[8];
  60. pOut->mat[2] = pIn->mat[1] * pIn->mat[5] - pIn->mat[2] * pIn->mat[4];
  61. pOut->mat[3] = pIn->mat[5] * pIn->mat[6] - pIn->mat[3] * pIn->mat[8];
  62. pOut->mat[4] = pIn->mat[0] * pIn->mat[8] - pIn->mat[2] * pIn->mat[6];
  63. pOut->mat[5] = pIn->mat[2] * pIn->mat[3] - pIn->mat[0] * pIn->mat[5];
  64. pOut->mat[6] = pIn->mat[3] * pIn->mat[7] - pIn->mat[4] * pIn->mat[6];
  65. // XXX: pIn->mat[9] is invalid!
  66. // pOut->mat[7] = pIn->mat[1] * pIn->mat[6] - pIn->mat[9] * pIn->mat[7];
  67. pOut->mat[8] = pIn->mat[0] * pIn->mat[4] - pIn->mat[1] * pIn->mat[3];
  68. return pOut;
  69. }
  70. kmMat3* const kmMat3Inverse(kmMat3* pOut, const kmScalar pDeterminate, const kmMat3* pM)
  71. {
  72. kmScalar detInv;
  73. kmMat3 adjugate;
  74. if(pDeterminate == 0.0)
  75. {
  76. return NULL;
  77. }
  78. detInv = 1.0f / pDeterminate;
  79. kmMat3Adjugate(&adjugate, pM);
  80. kmMat3ScalarMultiply(pOut, &adjugate, detInv);
  81. return pOut;
  82. }
  83. /** Returns true if pIn is an identity matrix */
  84. const int kmMat3IsIdentity(const kmMat3* pIn)
  85. {
  86. static const float identity [] = { 1.0f, 0.0f, 0.0f,
  87. 0.0f, 1.0f, 0.0f,
  88. 0.0f, 0.0f, 1.0f};
  89. return (memcmp(identity, pIn->mat, sizeof(float) * 9) == 0);
  90. }
  91. /** Sets pOut to the transpose of pIn, returns pOut */
  92. kmMat3* const kmMat3Transpose(kmMat3* pOut, const kmMat3* pIn)
  93. {
  94. int z, x;
  95. for (z = 0; z < 3; ++z) {
  96. for (x = 0; x < 3; ++x) {
  97. pOut->mat[(z * 3) + x] = pIn->mat[(x * 3) + z];
  98. }
  99. }
  100. return pOut;
  101. }
  102. /* Multiplies pM1 with pM2, stores the result in pOut, returns pOut */
  103. kmMat3* const kmMat3Multiply(kmMat3* pOut, const kmMat3* pM1, const kmMat3* pM2)
  104. {
  105. float mat[9];
  106. const float *m1 = pM1->mat, *m2 = pM2->mat;
  107. mat[0] = m1[0] * m2[0] + m1[3] * m2[1] + m1[6] * m2[2];
  108. mat[1] = m1[1] * m2[0] + m1[4] * m2[1] + m1[7] * m2[2];
  109. mat[2] = m1[2] * m2[0] + m1[5] * m2[1] + m1[8] * m2[2];
  110. mat[3] = m1[0] * m2[3] + m1[3] * m2[4] + m1[6] * m2[5];
  111. mat[4] = m1[1] * m2[3] + m1[4] * m2[4] + m1[7] * m2[5];
  112. mat[5] = m1[2] * m2[3] + m1[5] * m2[4] + m1[8] * m2[5];
  113. mat[6] = m1[0] * m2[6] + m1[3] * m2[7] + m1[6] * m2[8];
  114. mat[7] = m1[1] * m2[6] + m1[4] * m2[7] + m1[7] * m2[8];
  115. mat[8] = m1[2] * m2[6] + m1[5] * m2[7] + m1[8] * m2[8];
  116. memcpy(pOut->mat, mat, sizeof(float)*9);
  117. return pOut;
  118. }
  119. kmMat3* const kmMat3ScalarMultiply(kmMat3* pOut, const kmMat3* pM, const kmScalar pFactor)
  120. {
  121. float mat[9];
  122. int i;
  123. for(i = 0; i < 9; i++)
  124. {
  125. mat[i] = pM->mat[i] * pFactor;
  126. }
  127. memcpy(pOut->mat, mat, sizeof(float)*9);
  128. return pOut;
  129. }
  130. /** Assigns the value of pIn to pOut */
  131. kmMat3* const kmMat3Assign(kmMat3* pOut, const kmMat3* pIn)
  132. {
  133. assert(pOut != pIn); //You have tried to self-assign!!
  134. memcpy(pOut->mat, pIn->mat, sizeof(float)*9);
  135. return pOut;
  136. }
  137. /** Returns true if the 2 matrices are equal (approximately) */
  138. const int kmMat3AreEqual(const kmMat3* pMat1, const kmMat3* pMat2)
  139. {
  140. int i;
  141. if (pMat1 == pMat2) {
  142. return KM_TRUE;
  143. }
  144. for (i = 0; i < 9; ++i) {
  145. if (!(pMat1->mat[i] + kmEpsilon > pMat2->mat[i] &&
  146. pMat1->mat[i] - kmEpsilon < pMat2->mat[i])) {
  147. return KM_FALSE;
  148. }
  149. }
  150. return KM_TRUE;
  151. }
  152. /* Rotation around the z axis so everything stays planar in XY */
  153. kmMat3* const kmMat3Rotation(kmMat3* pOut, const float radians)
  154. {
  155. /*
  156. | cos(A) -sin(A) 0 |
  157. M = | sin(A) cos(A) 0 |
  158. | 0 0 1 |
  159. */
  160. pOut->mat[0] = cosf(radians);
  161. pOut->mat[1] = sinf(radians);
  162. pOut->mat[2] = 0.0f;
  163. pOut->mat[3] = -sinf(radians);;
  164. pOut->mat[4] = cosf(radians);
  165. pOut->mat[5] = 0.0f;
  166. pOut->mat[6] = 0.0f;
  167. pOut->mat[7] = 0.0f;
  168. pOut->mat[8] = 1.0f;
  169. return pOut;
  170. }
  171. /** Builds a scaling matrix */
  172. kmMat3* const kmMat3Scaling(kmMat3* pOut, const kmScalar x, const kmScalar y)
  173. {
  174. // memset(pOut->mat, 0, sizeof(float) * 9);
  175. kmMat3Identity(pOut);
  176. pOut->mat[0] = x;
  177. pOut->mat[4] = y;
  178. return pOut;
  179. }
  180. kmMat3* const kmMat3Translation(kmMat3* pOut, const kmScalar x, const kmScalar y)
  181. {
  182. // memset(pOut->mat, 0, sizeof(float) * 9);
  183. kmMat3Identity(pOut);
  184. pOut->mat[6] = x;
  185. pOut->mat[7] = y;
  186. // pOut->mat[8] = 1.0;
  187. return pOut;
  188. }
  189. kmMat3* const kmMat3RotationQuaternion(kmMat3* pOut, const kmQuaternion* pIn)
  190. {
  191. if (!pIn || !pOut) {
  192. return NULL;
  193. }
  194. // First row
  195. pOut->mat[0] = 1.0f - 2.0f * (pIn->y * pIn->y + pIn->z * pIn->z);
  196. pOut->mat[1] = 2.0f * (pIn->x * pIn->y - pIn->w * pIn->z);
  197. pOut->mat[2] = 2.0f * (pIn->x * pIn->z + pIn->w * pIn->y);
  198. // Second row
  199. pOut->mat[3] = 2.0f * (pIn->x * pIn->y + pIn->w * pIn->z);
  200. pOut->mat[4] = 1.0f - 2.0f * (pIn->x * pIn->x + pIn->z * pIn->z);
  201. pOut->mat[5] = 2.0f * (pIn->y * pIn->z - pIn->w * pIn->x);
  202. // Third row
  203. pOut->mat[6] = 2.0f * (pIn->x * pIn->z - pIn->w * pIn->y);
  204. pOut->mat[7] = 2.0f * (pIn->y * pIn->z + pIn->w * pIn->x);
  205. pOut->mat[8] = 1.0f - 2.0f * (pIn->x * pIn->x + pIn->y * pIn->y);
  206. return pOut;
  207. }
  208. kmMat3* const kmMat3RotationAxisAngle(kmMat3* pOut, const struct kmVec3* axis, kmScalar radians)
  209. {
  210. float rcos = cosf(radians);
  211. float rsin = sinf(radians);
  212. pOut->mat[0] = rcos + axis->x * axis->x * (1 - rcos);
  213. pOut->mat[1] = axis->z * rsin + axis->y * axis->x * (1 - rcos);
  214. pOut->mat[2] = -axis->y * rsin + axis->z * axis->x * (1 - rcos);
  215. pOut->mat[3] = -axis->z * rsin + axis->x * axis->y * (1 - rcos);
  216. pOut->mat[4] = rcos + axis->y * axis->y * (1 - rcos);
  217. pOut->mat[5] = axis->x * rsin + axis->z * axis->y * (1 - rcos);
  218. pOut->mat[6] = axis->y * rsin + axis->x * axis->z * (1 - rcos);
  219. pOut->mat[7] = -axis->x * rsin + axis->y * axis->z * (1 - rcos);
  220. pOut->mat[8] = rcos + axis->z * axis->z * (1 - rcos);
  221. return pOut;
  222. }
  223. kmVec3* const kmMat3RotationToAxisAngle(kmVec3* pAxis, kmScalar* radians, const kmMat3* pIn)
  224. {
  225. /*Surely not this easy?*/
  226. kmQuaternion temp;
  227. kmQuaternionRotationMatrix(&temp, pIn);
  228. kmQuaternionToAxisAngle(&temp, pAxis, radians);
  229. return pAxis;
  230. }
  231. /**
  232. * Builds an X-axis rotation matrix and stores it in pOut, returns pOut
  233. */
  234. kmMat3* const kmMat3RotationX(kmMat3* pOut, const float radians)
  235. {
  236. /*
  237. | 1 0 0 |
  238. M = | 0 cos(A) -sin(A) |
  239. | 0 sin(A) cos(A) |
  240. */
  241. pOut->mat[0] = 1.0f;
  242. pOut->mat[1] = 0.0f;
  243. pOut->mat[2] = 0.0f;
  244. pOut->mat[3] = 0.0f;
  245. pOut->mat[4] = cosf(radians);
  246. pOut->mat[5] = sinf(radians);
  247. pOut->mat[6] = 0.0f;
  248. pOut->mat[7] = -sinf(radians);
  249. pOut->mat[8] = cosf(radians);
  250. return pOut;
  251. }
  252. /**
  253. * Builds a rotation matrix using the rotation around the Y-axis
  254. * The result is stored in pOut, pOut is returned.
  255. */
  256. kmMat3* const kmMat3RotationY(kmMat3* pOut, const float radians)
  257. {
  258. /*
  259. | cos(A) 0 sin(A) |
  260. M = | 0 1 0 |
  261. | -sin(A) 0 cos(A) |
  262. */
  263. pOut->mat[0] = cosf(radians);
  264. pOut->mat[1] = 0.0f;
  265. pOut->mat[2] = -sinf(radians);
  266. pOut->mat[3] = 0.0f;
  267. pOut->mat[4] = 1.0f;
  268. pOut->mat[5] = 0.0f;
  269. pOut->mat[6] = sinf(radians);
  270. pOut->mat[7] = 0.0f;
  271. pOut->mat[8] = cosf(radians);
  272. return pOut;
  273. }
  274. /**
  275. * Builds a rotation matrix around the Z-axis. The resulting
  276. * matrix is stored in pOut. pOut is returned.
  277. */
  278. kmMat3* const kmMat3RotationZ(kmMat3* pOut, const float radians)
  279. {
  280. /*
  281. | cos(A) -sin(A) 0 |
  282. M = | sin(A) cos(A) 0 |
  283. | 0 0 1 |
  284. */
  285. pOut->mat[0] = cosf(radians);
  286. pOut->mat[1] =-sinf(radians);
  287. pOut->mat[2] = 0.0f;
  288. pOut->mat[3] = sinf(radians);;
  289. pOut->mat[4] = cosf(radians);
  290. pOut->mat[5] = 0.0f;
  291. pOut->mat[6] = 0.0f;
  292. pOut->mat[7] = 0.0f;
  293. pOut->mat[8] = 1.0f;
  294. return pOut;
  295. }