Mat3.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "math/Mat3.hpp"
  21. #include <cmath>
  22. #include "math/Quaternion.h"
  23. #include "math/MathUtil.h"
  24. #include "base/ccMacros.h"
  25. NS_CC_MATH_BEGIN
  26. Mat3::Mat3()
  27. {
  28. *this = IDENTITY;
  29. }
  30. Mat3::Mat3(float m11, float m12, float m13, float m21, float m22, float m23,
  31. float m31, float m32, float m33)
  32. {
  33. set(m11, m12, m13, m21, m22, m23, m31, m32, m33);
  34. }
  35. Mat3::Mat3(const float* mat)
  36. {
  37. set(mat);
  38. }
  39. Mat3::Mat3(const Mat3& copy)
  40. {
  41. memcpy(m, copy.m, MATRIX3_SIZE);
  42. }
  43. Mat3::~Mat3()
  44. {
  45. }
  46. void Mat3::set(float m11, float m12, float m13, float m21, float m22, float m23, float m31, float m32, float m33)
  47. {
  48. m[0] = m11;
  49. m[1] = m21;
  50. m[2] = m31;
  51. m[3] = m12;
  52. m[4] = m22;
  53. m[5] = m32;
  54. m[6] = m13;
  55. m[7] = m23;
  56. m[8] = m33;
  57. }
  58. void Mat3::set(const float* mat)
  59. {
  60. GP_ASSERT(mat);
  61. memcpy(this->m, mat, MATRIX3_SIZE);
  62. }
  63. void Mat3::set(const Mat3& mat)
  64. {
  65. memcpy(this->m, mat.m, MATRIX3_SIZE);
  66. }
  67. void Mat3::identity(Mat3& mat)
  68. {
  69. mat.m[0] = 1;
  70. mat.m[1] = 0;
  71. mat.m[2] = 0;
  72. mat.m[3] = 0;
  73. mat.m[4] = 1;
  74. mat.m[5] = 0;
  75. mat.m[6] = 0;
  76. mat.m[7] = 0;
  77. mat.m[8] = 1;
  78. }
  79. void Mat3::transpose()
  80. {
  81. float a01 = m[1], a02 = m[2], a12 = m[5];
  82. m[1] = m[3];
  83. m[2] = m[6];
  84. m[3] = a01;
  85. m[5] = m[7];
  86. m[6] = a02;
  87. m[7] = a12;
  88. }
  89. void Mat3::transpose(Mat3 &out, const Mat3 &mat)
  90. {
  91. out.m[0] = mat.m[0];
  92. out.m[1] = mat.m[3];
  93. out.m[2] = mat.m[6];
  94. out.m[3] = mat.m[1];
  95. out.m[4] = mat.m[4];
  96. out.m[5] = mat.m[7];
  97. out.m[6] = mat.m[2];
  98. out.m[7] = mat.m[5];
  99. out.m[8] = mat.m[8];
  100. }
  101. void Mat3::inverse()
  102. {
  103. float a00 = m[0], a01 = m[1], a02 = m[2];
  104. float a10 = m[3], a11 = m[4], a12 = m[5];
  105. float a20 = m[6], a21 = m[7], a22 = m[8];
  106. float b01 = a22 * a11 - a12 * a21;
  107. float b11 = -a22 * a10 + a12 * a20;
  108. float b21 = a21 * a10 - a11 * a20;
  109. // Calculate the determinant
  110. float det = a00 * b01 + a01 * b11 + a02 * b21;
  111. det = 1.0 / det;
  112. m[0] = b01 * det;
  113. m[1] = (-a22 * a01 + a02 * a21) * det;
  114. m[2] = (a12 * a01 - a02 * a11) * det;
  115. m[3] = b11 * det;
  116. m[4] = (a22 * a00 - a02 * a20) * det;
  117. m[5] = (-a12 * a00 + a02 * a10) * det;
  118. m[6] = b21 * det;
  119. m[7] = (-a21 * a00 + a01 * a20) * det;
  120. m[8] = (a11 * a00 - a01 * a10) * det;
  121. }
  122. void Mat3::adjoint(Mat3 &out, const Mat3 &mat)
  123. {
  124. float a00 = mat.m[0], a01 = mat.m[1], a02 = mat.m[2];
  125. float a10 = mat.m[3], a11 = mat.m[4], a12 = mat.m[5];
  126. float a20 = mat.m[6], a21 = mat.m[7], a22 = mat.m[8];
  127. out.m[0] = (a11 * a22 - a12 * a21);
  128. out.m[1] = (a02 * a21 - a01 * a22);
  129. out.m[2] = (a01 * a12 - a02 * a11);
  130. out.m[3] = (a12 * a20 - a10 * a22);
  131. out.m[4] = (a00 * a22 - a02 * a20);
  132. out.m[5] = (a02 * a10 - a00 * a12);
  133. out.m[6] = (a10 * a21 - a11 * a20);
  134. out.m[7] = (a01 * a20 - a00 * a21);
  135. out.m[8] = (a00 * a11 - a01 * a10);
  136. }
  137. float Mat3::determinant()
  138. {
  139. return m[0] * (m[8] * m[4] - m[5] * m[7]) + m[1] * (-m[8] * m[3] + m[5] * m[6]) + m[2] * (m[7] * m[3] - m[4] * m[6]);
  140. }
  141. void Mat3::multiply(Mat3 &out, const Mat3 &a, const Mat3 &b)
  142. {
  143. float a00 = a.m[0], a01 = a.m[1], a02 = a.m[2];
  144. float a10 = a.m[3], a11 = a.m[4], a12 = a.m[5];
  145. float a20 = a.m[6], a21 = a.m[7], a22 = a.m[8];
  146. float b00 = b.m[0], b01 = b.m[1], b02 = b.m[2];
  147. float b10 = b.m[3], b11 = b.m[4], b12 = b.m[5];
  148. float b20 = b.m[6], b21 = b.m[7], b22 = b.m[8];
  149. out.m[0] = b00 * a00 + b01 * a10 + b02 * a20;
  150. out.m[1] = b00 * a01 + b01 * a11 + b02 * a21;
  151. out.m[2] = b00 * a02 + b01 * a12 + b02 * a22;
  152. out.m[3] = b10 * a00 + b11 * a10 + b12 * a20;
  153. out.m[4] = b10 * a01 + b11 * a11 + b12 * a21;
  154. out.m[5] = b10 * a02 + b11 * a12 + b12 * a22;
  155. out.m[6] = b20 * a00 + b21 * a10 + b22 * a20;
  156. out.m[7] = b20 * a01 + b21 * a11 + b22 * a21;
  157. out.m[8] = b20 * a02 + b21 * a12 + b22 * a22;
  158. }
  159. void Mat3::translate(Mat3 &out, const Mat3 &mat, const Vec2 &vec)
  160. {
  161. float a00 = mat.m[0], a01 = mat.m[1], a02 = mat.m[2];
  162. float a10 = mat.m[3], a11 = mat.m[4], a12 = mat.m[5];
  163. float a20 = mat.m[6], a21 = mat.m[7], a22 = mat.m[8];
  164. float x = vec.x, y = vec.y;
  165. out.m[0] = a00;
  166. out.m[1] = a01;
  167. out.m[2] = a02;
  168. out.m[3] = a10;
  169. out.m[4] = a11;
  170. out.m[5] = a12;
  171. out.m[6] = x * a00 + y * a10 + a20;
  172. out.m[7] = x * a01 + y * a11 + a21;
  173. out.m[8] = x * a02 + y * a12 + a22;
  174. }
  175. void Mat3::rotate(Mat3 &out, const Mat3 &mat, float rad)
  176. {
  177. float a00 = mat.m[0], a01 = mat.m[1], a02 = mat.m[2];
  178. float a10 = mat.m[3], a11 = mat.m[4], a12 = mat.m[5];
  179. float a20 = mat.m[6], a21 = mat.m[7], a22 = mat.m[8];
  180. float s = sin(rad);
  181. float c = cos(rad);
  182. out.m[0] = c * a00 + s * a10;
  183. out.m[1] = c * a01 + s * a11;
  184. out.m[2] = c * a02 + s * a12;
  185. out.m[3] = c * a10 - s * a00;
  186. out.m[4] = c * a11 - s * a01;
  187. out.m[5] = c * a12 - s * a02;
  188. out.m[6] = a20;
  189. out.m[7] = a21;
  190. out.m[8] = a22;
  191. }
  192. void Mat3::scale(Mat3 &out, const Mat3 &mat, const Vec2 &vec)
  193. {
  194. float x = vec.x, y = vec.y;
  195. out.m[0] = x * mat.m[0];
  196. out.m[1] = x * mat.m[1];
  197. out.m[2] = x * mat.m[2];
  198. out.m[3] = y * mat.m[3];
  199. out.m[4] = y * mat.m[4];
  200. out.m[5] = y * mat.m[5];
  201. out.m[6] = mat.m[6];
  202. out.m[7] = mat.m[7];
  203. out.m[8] = mat.m[8];
  204. }
  205. void Mat3::fromMat4(Mat3 &out, const Mat4 &mat)
  206. {
  207. out.m[0] = mat.m[0];
  208. out.m[1] = mat.m[1];
  209. out.m[2] = mat.m[2];
  210. out.m[3] = mat.m[4];
  211. out.m[4] = mat.m[5];
  212. out.m[5] = mat.m[6];
  213. out.m[6] = mat.m[8];
  214. out.m[7] = mat.m[9];
  215. out.m[8] = mat.m[10];
  216. }
  217. void Mat3::fromTranslation(Mat3 &out, const Vec2 &vec)
  218. {
  219. out.m[0] = 1;
  220. out.m[1] = 0;
  221. out.m[2] = 0;
  222. out.m[3] = 0;
  223. out.m[4] = 1;
  224. out.m[5] = 0;
  225. out.m[6] = vec.x;
  226. out.m[7] = vec.y;
  227. out.m[8] = 1;
  228. }
  229. void Mat3::fromRotation(Mat3 &out, float rad)
  230. {
  231. float s = sin(rad);
  232. float c = cos(rad);
  233. out.m[0] = c;
  234. out.m[1] = s;
  235. out.m[2] = 0;
  236. out.m[3] = -s;
  237. out.m[4] = c;
  238. out.m[5] = 0;
  239. out.m[6] = 0;
  240. out.m[7] = 0;
  241. out.m[8] = 1;
  242. }
  243. void Mat3::fromScaling(Mat3 &out, const Vec2 &vec)
  244. {
  245. out.m[0] = vec.x;
  246. out.m[1] = 0;
  247. out.m[2] = 0;
  248. out.m[3] = 0;
  249. out.m[4] = vec.y;
  250. out.m[5] = 0;
  251. out.m[6] = 0;
  252. out.m[7] = 0;
  253. out.m[8] = 1;
  254. }
  255. void Mat3::fromQuat(Mat3 &out, const Quaternion &quat)
  256. {
  257. float x = quat.x, y = quat.y, z = quat.z, w = quat.w;
  258. float x2 = x + x;
  259. float y2 = y + y;
  260. float z2 = z + z;
  261. float xx = x * x2;
  262. float yx = y * x2;
  263. float yy = y * y2;
  264. float zx = z * x2;
  265. float zy = z * y2;
  266. float zz = z * z2;
  267. float wx = w * x2;
  268. float wy = w * y2;
  269. float wz = w * z2;
  270. out.m[0] = 1 - yy - zz;
  271. out.m[3] = yx - wz;
  272. out.m[6] = zx + wy;
  273. out.m[1] = yx + wz;
  274. out.m[4] = 1 - xx - zz;
  275. out.m[7] = zy - wx;
  276. out.m[2] = zx - wy;
  277. out.m[5] = zy + wx;
  278. out.m[8] = 1 - xx - yy;
  279. }
  280. void Mat3::add(Mat3 &out, const Mat3 &a, const Mat3 &b)
  281. {
  282. out.m[0] = a.m[0] + b.m[0];
  283. out.m[1] = a.m[1] + b.m[1];
  284. out.m[2] = a.m[2] + b.m[2];
  285. out.m[3] = a.m[3] + b.m[3];
  286. out.m[4] = a.m[4] + b.m[4];
  287. out.m[5] = a.m[5] + b.m[5];
  288. out.m[6] = a.m[6] + b.m[6];
  289. out.m[7] = a.m[7] + b.m[7];
  290. out.m[8] = a.m[8] + b.m[8];
  291. }
  292. void Mat3::subtract(Mat3 &out, const Mat3 &a, const Mat3 &b)
  293. {
  294. out.m[0] = a.m[0] - b.m[0];
  295. out.m[1] = a.m[1] - b.m[1];
  296. out.m[2] = a.m[2] - b.m[2];
  297. out.m[3] = a.m[3] - b.m[3];
  298. out.m[4] = a.m[4] - b.m[4];
  299. out.m[5] = a.m[5] - b.m[5];
  300. out.m[6] = a.m[6] - b.m[6];
  301. out.m[7] = a.m[7] - b.m[7];
  302. out.m[8] = a.m[8] - b.m[8];
  303. }
  304. const Mat3 Mat3::IDENTITY = Mat3(
  305. 1.0f, 0.0f, 0.0f,
  306. 0.0f, 1.0f, 0.0f,
  307. 0.0f, 0.0f, 1.0f);
  308. const Mat3 Mat3::ZERO = Mat3(
  309. 0, 0, 0,
  310. 0, 0, 0,
  311. 0, 0, 0);
  312. NS_CC_MATH_END