matrix3.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*************************************************************************/
  2. /* matrix3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "matrix3.h"
  31. #include "math_funcs.h"
  32. #include "os/copymem.h"
  33. #define cofac(row1, col1, row2, col2) \
  34. (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
  35. void Matrix3::from_z(const Vector3 &p_z) {
  36. if (Math::abs(p_z.z) > Math_SQRT12) {
  37. // choose p in y-z plane
  38. real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
  39. real_t k = 1.0 / Math::sqrt(a);
  40. elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
  41. elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
  42. } else {
  43. // choose p in x-y plane
  44. real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
  45. real_t k = 1.0 / Math::sqrt(a);
  46. elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
  47. elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
  48. }
  49. elements[2] = p_z;
  50. }
  51. void Matrix3::invert() {
  52. real_t co[3] = {
  53. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  54. };
  55. real_t det = elements[0][0] * co[0] +
  56. elements[0][1] * co[1] +
  57. elements[0][2] * co[2];
  58. ERR_FAIL_COND(det == 0);
  59. real_t s = 1.0 / det;
  60. set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  61. co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  62. co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  63. }
  64. void Matrix3::orthonormalize() {
  65. // Gram-Schmidt Process
  66. Vector3 x = get_axis(0);
  67. Vector3 y = get_axis(1);
  68. Vector3 z = get_axis(2);
  69. x.normalize();
  70. y = (y - x * (x.dot(y)));
  71. y.normalize();
  72. z = (z - x * (x.dot(z)) - y * (y.dot(z)));
  73. z.normalize();
  74. set_axis(0, x);
  75. set_axis(1, y);
  76. set_axis(2, z);
  77. }
  78. Matrix3 Matrix3::orthonormalized() const {
  79. Matrix3 c = *this;
  80. c.orthonormalize();
  81. return c;
  82. }
  83. Matrix3 Matrix3::inverse() const {
  84. Matrix3 inv = *this;
  85. inv.invert();
  86. return inv;
  87. }
  88. void Matrix3::transpose() {
  89. SWAP(elements[0][1], elements[1][0]);
  90. SWAP(elements[0][2], elements[2][0]);
  91. SWAP(elements[1][2], elements[2][1]);
  92. }
  93. Matrix3 Matrix3::transposed() const {
  94. Matrix3 tr = *this;
  95. tr.transpose();
  96. return tr;
  97. }
  98. void Matrix3::scale(const Vector3 &p_scale) {
  99. elements[0][0] *= p_scale.x;
  100. elements[1][0] *= p_scale.x;
  101. elements[2][0] *= p_scale.x;
  102. elements[0][1] *= p_scale.y;
  103. elements[1][1] *= p_scale.y;
  104. elements[2][1] *= p_scale.y;
  105. elements[0][2] *= p_scale.z;
  106. elements[1][2] *= p_scale.z;
  107. elements[2][2] *= p_scale.z;
  108. }
  109. Matrix3 Matrix3::scaled(const Vector3 &p_scale) const {
  110. Matrix3 m = *this;
  111. m.scale(p_scale);
  112. return m;
  113. }
  114. Vector3 Matrix3::get_scale() const {
  115. return Vector3(
  116. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  117. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  118. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  119. }
  120. void Matrix3::rotate(const Vector3 &p_axis, real_t p_phi) {
  121. *this = *this * Matrix3(p_axis, p_phi);
  122. }
  123. Matrix3 Matrix3::rotated(const Vector3 &p_axis, real_t p_phi) const {
  124. return *this * Matrix3(p_axis, p_phi);
  125. }
  126. Vector3 Matrix3::get_euler() const {
  127. // rot = cy*cz -cy*sz sy
  128. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  129. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  130. Matrix3 m = *this;
  131. m.orthonormalize();
  132. Vector3 euler;
  133. euler.y = Math::asin(m[0][2]);
  134. if (euler.y < Math_PI * 0.5) {
  135. if (euler.y > -Math_PI * 0.5) {
  136. euler.x = Math::atan2(-m[1][2], m[2][2]);
  137. euler.z = Math::atan2(-m[0][1], m[0][0]);
  138. } else {
  139. real_t r = Math::atan2(m[1][0], m[1][1]);
  140. euler.z = 0.0;
  141. euler.x = euler.z - r;
  142. }
  143. } else {
  144. real_t r = Math::atan2(m[0][1], m[1][1]);
  145. euler.z = 0;
  146. euler.x = r - euler.z;
  147. }
  148. return euler;
  149. }
  150. void Matrix3::set_euler(const Vector3 &p_euler) {
  151. real_t c, s;
  152. c = Math::cos(p_euler.x);
  153. s = Math::sin(p_euler.x);
  154. Matrix3 xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  155. c = Math::cos(p_euler.y);
  156. s = Math::sin(p_euler.y);
  157. Matrix3 ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  158. c = Math::cos(p_euler.z);
  159. s = Math::sin(p_euler.z);
  160. Matrix3 zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  161. //optimizer will optimize away all this anyway
  162. *this = xmat * (ymat * zmat);
  163. }
  164. bool Matrix3::operator==(const Matrix3 &p_matrix) const {
  165. for (int i = 0; i < 3; i++) {
  166. for (int j = 0; j < 3; j++) {
  167. if (elements[i][j] != p_matrix.elements[i][j])
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. bool Matrix3::operator!=(const Matrix3 &p_matrix) const {
  174. return (!(*this == p_matrix));
  175. }
  176. Matrix3::operator String() const {
  177. String mtx;
  178. for (int i = 0; i < 3; i++) {
  179. for (int j = 0; j < 3; j++) {
  180. if (i != 0 || j != 0)
  181. mtx += ", ";
  182. mtx += rtos(elements[i][j]);
  183. }
  184. }
  185. return mtx;
  186. }
  187. Matrix3::operator Quat() const {
  188. Matrix3 m = *this;
  189. m.orthonormalize();
  190. real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
  191. real_t temp[4];
  192. if (trace > 0.0) {
  193. real_t s = Math::sqrt(trace + 1.0);
  194. temp[3] = (s * 0.5);
  195. s = 0.5 / s;
  196. temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s);
  197. temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s);
  198. temp[2] = ((m.elements[1][0] - m.elements[0][1]) * s);
  199. } else {
  200. int i = m.elements[0][0] < m.elements[1][1] ?
  201. (m.elements[1][1] < m.elements[2][2] ? 2 : 1) :
  202. (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
  203. int j = (i + 1) % 3;
  204. int k = (i + 2) % 3;
  205. real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0);
  206. temp[i] = s * 0.5;
  207. s = 0.5 / s;
  208. temp[3] = (m.elements[k][j] - m.elements[j][k]) * s;
  209. temp[j] = (m.elements[j][i] + m.elements[i][j]) * s;
  210. temp[k] = (m.elements[k][i] + m.elements[i][k]) * s;
  211. }
  212. return Quat(temp[0], temp[1], temp[2], temp[3]);
  213. }
  214. static const Matrix3 _ortho_bases[24] = {
  215. Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1),
  216. Matrix3(0, -1, 0, 1, 0, 0, 0, 0, 1),
  217. Matrix3(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  218. Matrix3(0, 1, 0, -1, 0, 0, 0, 0, 1),
  219. Matrix3(1, 0, 0, 0, 0, -1, 0, 1, 0),
  220. Matrix3(0, 0, 1, 1, 0, 0, 0, 1, 0),
  221. Matrix3(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  222. Matrix3(0, 0, -1, -1, 0, 0, 0, 1, 0),
  223. Matrix3(1, 0, 0, 0, -1, 0, 0, 0, -1),
  224. Matrix3(0, 1, 0, 1, 0, 0, 0, 0, -1),
  225. Matrix3(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  226. Matrix3(0, -1, 0, -1, 0, 0, 0, 0, -1),
  227. Matrix3(1, 0, 0, 0, 0, 1, 0, -1, 0),
  228. Matrix3(0, 0, -1, 1, 0, 0, 0, -1, 0),
  229. Matrix3(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  230. Matrix3(0, 0, 1, -1, 0, 0, 0, -1, 0),
  231. Matrix3(0, 0, 1, 0, 1, 0, -1, 0, 0),
  232. Matrix3(0, -1, 0, 0, 0, 1, -1, 0, 0),
  233. Matrix3(0, 0, -1, 0, -1, 0, -1, 0, 0),
  234. Matrix3(0, 1, 0, 0, 0, -1, -1, 0, 0),
  235. Matrix3(0, 0, 1, 0, -1, 0, 1, 0, 0),
  236. Matrix3(0, 1, 0, 0, 0, 1, 1, 0, 0),
  237. Matrix3(0, 0, -1, 0, 1, 0, 1, 0, 0),
  238. Matrix3(0, -1, 0, 0, 0, -1, 1, 0, 0)
  239. };
  240. int Matrix3::get_orthogonal_index() const {
  241. //could be sped up if i come up with a way
  242. Matrix3 orth = *this;
  243. for (int i = 0; i < 3; i++) {
  244. for (int j = 0; j < 3; j++) {
  245. float v = orth[i][j];
  246. if (v > 0.5)
  247. v = 1.0;
  248. else if (v < -0.5)
  249. v = -1.0;
  250. else
  251. v = 0;
  252. orth[i][j] = v;
  253. }
  254. }
  255. for (int i = 0; i < 24; i++) {
  256. if (_ortho_bases[i] == orth)
  257. return i;
  258. }
  259. return 0;
  260. }
  261. void Matrix3::set_orthogonal_index(int p_index) {
  262. //there only exist 24 orthogonal bases in r3
  263. ERR_FAIL_INDEX(p_index, 24);
  264. *this = _ortho_bases[p_index];
  265. }
  266. void Matrix3::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
  267. double angle, x, y, z; // variables for result
  268. double epsilon = 0.01; // margin to allow for rounding errors
  269. double epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  270. if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) {
  271. // singularity found
  272. // first check for identity matrix which must have +1 for all terms
  273. // in leading diagonaland zero in other terms
  274. if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) {
  275. // this singularity is identity matrix so angle = 0
  276. r_axis = Vector3(0, 1, 0);
  277. r_angle = 0;
  278. return;
  279. }
  280. // otherwise this singularity is angle = 180
  281. angle = Math_PI;
  282. double xx = (elements[0][0] + 1) / 2;
  283. double yy = (elements[1][1] + 1) / 2;
  284. double zz = (elements[2][2] + 1) / 2;
  285. double xy = (elements[1][0] + elements[0][1]) / 4;
  286. double xz = (elements[2][0] + elements[0][2]) / 4;
  287. double yz = (elements[2][1] + elements[1][2]) / 4;
  288. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  289. if (xx < epsilon) {
  290. x = 0;
  291. y = 0.7071;
  292. z = 0.7071;
  293. } else {
  294. x = Math::sqrt(xx);
  295. y = xy / x;
  296. z = xz / x;
  297. }
  298. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  299. if (yy < epsilon) {
  300. x = 0.7071;
  301. y = 0;
  302. z = 0.7071;
  303. } else {
  304. y = Math::sqrt(yy);
  305. x = xy / y;
  306. z = yz / y;
  307. }
  308. } else { // elements[2][2] is the largest diagonal term so base result on this
  309. if (zz < epsilon) {
  310. x = 0.7071;
  311. y = 0.7071;
  312. z = 0;
  313. } else {
  314. z = Math::sqrt(zz);
  315. x = xz / z;
  316. y = yz / z;
  317. }
  318. }
  319. r_axis = Vector3(x, y, z);
  320. r_angle = angle;
  321. return;
  322. }
  323. // as we have reached here there are no singularities so we can handle normally
  324. double s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // used to normalise
  325. if (Math::abs(s) < 0.001) s = 1;
  326. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  327. // caught by singularity test above, but I've left it in just in case
  328. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  329. x = (elements[1][2] - elements[2][1]) / s;
  330. y = (elements[2][0] - elements[0][2]) / s;
  331. z = (elements[0][1] - elements[1][0]) / s;
  332. r_axis = Vector3(x, y, z);
  333. r_angle = angle;
  334. }
  335. Matrix3::Matrix3(const Vector3 &p_euler) {
  336. set_euler(p_euler);
  337. }
  338. Matrix3::Matrix3(const Quat &p_quat) {
  339. real_t d = p_quat.length_squared();
  340. real_t s = 2.0 / d;
  341. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  342. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  343. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  344. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  345. set(1.0 - (yy + zz), xy - wz, xz + wy,
  346. xy + wz, 1.0 - (xx + zz), yz - wx,
  347. xz - wy, yz + wx, 1.0 - (xx + yy));
  348. }
  349. Matrix3::Matrix3(const Vector3 &p_axis, real_t p_phi) {
  350. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  351. real_t cosine = Math::cos(p_phi);
  352. real_t sine = Math::sin(p_phi);
  353. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  354. elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine;
  355. elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine;
  356. elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine;
  357. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  358. elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine;
  359. elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine;
  360. elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine;
  361. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  362. }