matrix3.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*************************************************************************/
  2. /* matrix3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "print_string.h"
  34. #define cofac(row1, col1, row2, col2) \
  35. (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
  36. void Basis::from_z(const Vector3 &p_z) {
  37. if (Math::abs(p_z.z) > Math_SQRT12) {
  38. // choose p in y-z plane
  39. real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
  40. real_t k = 1.0 / Math::sqrt(a);
  41. elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
  42. elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
  43. } else {
  44. // choose p in x-y plane
  45. real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
  46. real_t k = 1.0 / Math::sqrt(a);
  47. elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
  48. elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
  49. }
  50. elements[2] = p_z;
  51. }
  52. void Basis::invert() {
  53. real_t co[3] = {
  54. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  55. };
  56. real_t det = elements[0][0] * co[0] +
  57. elements[0][1] * co[1] +
  58. elements[0][2] * co[2];
  59. #ifdef MATH_CHECKS
  60. ERR_FAIL_COND(det == 0);
  61. #endif
  62. real_t s = 1.0 / det;
  63. set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  64. co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  65. co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  66. }
  67. void Basis::orthonormalize() {
  68. #ifdef MATH_CHECKS
  69. ERR_FAIL_COND(determinant() == 0);
  70. #endif
  71. // Gram-Schmidt Process
  72. Vector3 x = get_axis(0);
  73. Vector3 y = get_axis(1);
  74. Vector3 z = get_axis(2);
  75. x.normalize();
  76. y = (y - x * (x.dot(y)));
  77. y.normalize();
  78. z = (z - x * (x.dot(z)) - y * (y.dot(z)));
  79. z.normalize();
  80. set_axis(0, x);
  81. set_axis(1, y);
  82. set_axis(2, z);
  83. }
  84. Basis Basis::orthonormalized() const {
  85. Basis c = *this;
  86. c.orthonormalize();
  87. return c;
  88. }
  89. bool Basis::is_orthogonal() const {
  90. Basis id;
  91. Basis m = (*this) * transposed();
  92. return is_equal_approx(id, m);
  93. }
  94. bool Basis::is_rotation() const {
  95. return Math::is_equal_approx(determinant(), 1) && is_orthogonal();
  96. }
  97. bool Basis::is_symmetric() const {
  98. if (!Math::is_equal_approx(elements[0][1], elements[1][0]))
  99. return false;
  100. if (!Math::is_equal_approx(elements[0][2], elements[2][0]))
  101. return false;
  102. if (!Math::is_equal_approx(elements[1][2], elements[2][1]))
  103. return false;
  104. return true;
  105. }
  106. Basis Basis::diagonalize() {
  107. //NOTE: only implemented for symmetric matrices
  108. //with the Jacobi iterative method method
  109. #ifdef MATH_CHECKS
  110. ERR_FAIL_COND_V(!is_symmetric(), Basis());
  111. #endif
  112. const int ite_max = 1024;
  113. real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
  114. int ite = 0;
  115. Basis acc_rot;
  116. while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
  117. real_t el01_2 = elements[0][1] * elements[0][1];
  118. real_t el02_2 = elements[0][2] * elements[0][2];
  119. real_t el12_2 = elements[1][2] * elements[1][2];
  120. // Find the pivot element
  121. int i, j;
  122. if (el01_2 > el02_2) {
  123. if (el12_2 > el01_2) {
  124. i = 1;
  125. j = 2;
  126. } else {
  127. i = 0;
  128. j = 1;
  129. }
  130. } else {
  131. if (el12_2 > el02_2) {
  132. i = 1;
  133. j = 2;
  134. } else {
  135. i = 0;
  136. j = 2;
  137. }
  138. }
  139. // Compute the rotation angle
  140. real_t angle;
  141. if (Math::is_equal_approx(elements[j][j], elements[i][i])) {
  142. angle = Math_PI / 4;
  143. } else {
  144. angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
  145. }
  146. // Compute the rotation matrix
  147. Basis rot;
  148. rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle);
  149. rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle));
  150. // Update the off matrix norm
  151. off_matrix_norm_2 -= elements[i][j] * elements[i][j];
  152. // Apply the rotation
  153. *this = rot * *this * rot.transposed();
  154. acc_rot = rot * acc_rot;
  155. }
  156. return acc_rot;
  157. }
  158. Basis Basis::inverse() const {
  159. Basis inv = *this;
  160. inv.invert();
  161. return inv;
  162. }
  163. void Basis::transpose() {
  164. SWAP(elements[0][1], elements[1][0]);
  165. SWAP(elements[0][2], elements[2][0]);
  166. SWAP(elements[1][2], elements[2][1]);
  167. }
  168. Basis Basis::transposed() const {
  169. Basis tr = *this;
  170. tr.transpose();
  171. return tr;
  172. }
  173. // Multiplies the matrix from left by the scaling matrix: M -> S.M
  174. // See the comment for Basis::rotated for further explanation.
  175. void Basis::scale(const Vector3 &p_scale) {
  176. elements[0][0] *= p_scale.x;
  177. elements[0][1] *= p_scale.x;
  178. elements[0][2] *= p_scale.x;
  179. elements[1][0] *= p_scale.y;
  180. elements[1][1] *= p_scale.y;
  181. elements[1][2] *= p_scale.y;
  182. elements[2][0] *= p_scale.z;
  183. elements[2][1] *= p_scale.z;
  184. elements[2][2] *= p_scale.z;
  185. }
  186. Basis Basis::scaled(const Vector3 &p_scale) const {
  187. Basis m = *this;
  188. m.scale(p_scale);
  189. return m;
  190. }
  191. Vector3 Basis::get_scale() const {
  192. // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S.
  193. // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and
  194. // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal).
  195. //
  196. // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition
  197. // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where
  198. // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix,
  199. // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P,
  200. // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique.
  201. // Therefore, we are going to do this decomposition by sticking to a particular convention.
  202. // This may lead to confusion for some users though.
  203. //
  204. // The convention we use here is to absorb the sign flip into the scaling matrix.
  205. // The same convention is also used in other similar functions such as set_scale,
  206. // get_rotation_axis_angle, get_rotation, set_rotation_axis_angle, set_rotation_euler, ...
  207. //
  208. // A proper way to get rid of this issue would be to store the scaling values (or at least their signs)
  209. // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the
  210. // matrix elements.
  211. real_t det_sign = determinant() > 0 ? 1 : -1;
  212. return det_sign * Vector3(
  213. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  214. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  215. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  216. }
  217. // Multiplies the matrix from left by the rotation matrix: M -> R.M
  218. // Note that this does *not* rotate the matrix itself.
  219. //
  220. // The main use of Basis is as Transform.basis, which is used a the transformation matrix
  221. // of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
  222. // not the matrix itself (which is R * (*this) * R.transposed()).
  223. Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const {
  224. return Basis(p_axis, p_phi) * (*this);
  225. }
  226. void Basis::rotate(const Vector3 &p_axis, real_t p_phi) {
  227. *this = rotated(p_axis, p_phi);
  228. }
  229. Basis Basis::rotated(const Vector3 &p_euler) const {
  230. return Basis(p_euler) * (*this);
  231. }
  232. void Basis::rotate(const Vector3 &p_euler) {
  233. *this = rotated(p_euler);
  234. }
  235. // TODO: rename this to get_rotation_euler
  236. Vector3 Basis::get_rotation() const {
  237. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  238. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  239. // See the comment in get_scale() for further information.
  240. Basis m = orthonormalized();
  241. real_t det = m.determinant();
  242. if (det < 0) {
  243. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  244. m.scale(Vector3(-1, -1, -1));
  245. }
  246. return m.get_euler();
  247. }
  248. void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
  249. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  250. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  251. // See the comment in get_scale() for further information.
  252. Basis m = orthonormalized();
  253. real_t det = m.determinant();
  254. if (det < 0) {
  255. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  256. m.scale(Vector3(-1, -1, -1));
  257. }
  258. m.get_axis_angle(p_axis, p_angle);
  259. }
  260. // get_euler_xyz returns a vector containing the Euler angles in the format
  261. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last
  262. // (following the convention they are commonly defined in the literature).
  263. //
  264. // The current implementation uses XYZ convention (Z is the first rotation),
  265. // so euler.z is the angle of the (first) rotation around Z axis and so on,
  266. //
  267. // And thus, assuming the matrix is a rotation matrix, this function returns
  268. // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates
  269. // around the z-axis by a and so on.
  270. Vector3 Basis::get_euler_xyz() const {
  271. // Euler angles in XYZ convention.
  272. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  273. //
  274. // rot = cy*cz -cy*sz sy
  275. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  276. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  277. Vector3 euler;
  278. #ifdef MATH_CHECKS
  279. ERR_FAIL_COND_V(is_rotation() == false, euler);
  280. #endif
  281. euler.y = Math::asin(elements[0][2]);
  282. if (euler.y < Math_PI * 0.5) {
  283. if (euler.y > -Math_PI * 0.5) {
  284. //if rotation is Y-only, return a proper -pi,pi range like in x or z for the same case.
  285. if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
  286. euler.x = 0;
  287. euler.y = atan2(elements[0][2], elements[0][0]);
  288. euler.z = 0;
  289. } else {
  290. euler.x = Math::atan2(-elements[1][2], elements[2][2]);
  291. euler.z = Math::atan2(-elements[0][1], elements[0][0]);
  292. }
  293. } else {
  294. real_t r = Math::atan2(elements[1][0], elements[1][1]);
  295. euler.z = 0.0;
  296. euler.x = euler.z - r;
  297. }
  298. } else {
  299. real_t r = Math::atan2(elements[0][1], elements[1][1]);
  300. euler.z = 0;
  301. euler.x = r - euler.z;
  302. }
  303. return euler;
  304. }
  305. // set_euler_xyz expects a vector containing the Euler angles in the format
  306. // (ax,ay,az), where ax is the angle of rotation around x axis,
  307. // and similar for other axes.
  308. // The current implementation uses XYZ convention (Z is the first rotation).
  309. void Basis::set_euler_xyz(const Vector3 &p_euler) {
  310. real_t c, s;
  311. c = Math::cos(p_euler.x);
  312. s = Math::sin(p_euler.x);
  313. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  314. c = Math::cos(p_euler.y);
  315. s = Math::sin(p_euler.y);
  316. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  317. c = Math::cos(p_euler.z);
  318. s = Math::sin(p_euler.z);
  319. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  320. //optimizer will optimize away all this anyway
  321. *this = xmat * (ymat * zmat);
  322. }
  323. // get_euler_yxz returns a vector containing the Euler angles in the YXZ convention,
  324. // as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned
  325. // as the x, y, and z components of a Vector3 respectively.
  326. Vector3 Basis::get_euler_yxz() const {
  327. // Euler angles in YXZ convention.
  328. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  329. //
  330. // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy
  331. // cx*sz cx*cz -sx
  332. // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx
  333. Vector3 euler;
  334. #ifdef MATH_CHECKS
  335. ERR_FAIL_COND_V(is_rotation() == false, euler);
  336. #endif
  337. real_t m12 = elements[1][2];
  338. if (m12 < 1) {
  339. if (m12 > -1) {
  340. if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) { // use pure x rotation
  341. euler.x = atan2(-m12, elements[1][1]);
  342. euler.y = 0;
  343. euler.z = 0;
  344. } else {
  345. euler.x = asin(-m12);
  346. euler.y = atan2(elements[0][2], elements[2][2]);
  347. euler.z = atan2(elements[1][0], elements[1][1]);
  348. }
  349. } else { // m12 == -1
  350. euler.x = Math_PI * 0.5;
  351. euler.y = -atan2(-elements[0][1], elements[0][0]);
  352. euler.z = 0;
  353. }
  354. } else { // m12 == 1
  355. euler.x = -Math_PI * 0.5;
  356. euler.y = -atan2(-elements[0][1], elements[0][0]);
  357. euler.z = 0;
  358. }
  359. return euler;
  360. }
  361. // set_euler_yxz expects a vector containing the Euler angles in the format
  362. // (ax,ay,az), where ax is the angle of rotation around x axis,
  363. // and similar for other axes.
  364. // The current implementation uses YXZ convention (Z is the first rotation).
  365. void Basis::set_euler_yxz(const Vector3 &p_euler) {
  366. real_t c, s;
  367. c = Math::cos(p_euler.x);
  368. s = Math::sin(p_euler.x);
  369. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  370. c = Math::cos(p_euler.y);
  371. s = Math::sin(p_euler.y);
  372. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  373. c = Math::cos(p_euler.z);
  374. s = Math::sin(p_euler.z);
  375. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  376. //optimizer will optimize away all this anyway
  377. *this = ymat * xmat * zmat;
  378. }
  379. bool Basis::is_equal_approx(const Basis &a, const Basis &b) const {
  380. for (int i = 0; i < 3; i++) {
  381. for (int j = 0; j < 3; j++) {
  382. if (Math::is_equal_approx(a.elements[i][j], b.elements[i][j]) == false)
  383. return false;
  384. }
  385. }
  386. return true;
  387. }
  388. bool Basis::operator==(const Basis &p_matrix) const {
  389. for (int i = 0; i < 3; i++) {
  390. for (int j = 0; j < 3; j++) {
  391. if (elements[i][j] != p_matrix.elements[i][j])
  392. return false;
  393. }
  394. }
  395. return true;
  396. }
  397. bool Basis::operator!=(const Basis &p_matrix) const {
  398. return (!(*this == p_matrix));
  399. }
  400. Basis::operator String() const {
  401. String mtx;
  402. for (int i = 0; i < 3; i++) {
  403. for (int j = 0; j < 3; j++) {
  404. if (i != 0 || j != 0)
  405. mtx += ", ";
  406. mtx += rtos(elements[i][j]);
  407. }
  408. }
  409. return mtx;
  410. }
  411. Basis::operator Quat() const {
  412. //commenting this check because precision issues cause it to fail when it shouldn't
  413. //#ifdef MATH_CHECKS
  414. //ERR_FAIL_COND_V(is_rotation() == false, Quat());
  415. //#endif
  416. real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
  417. real_t temp[4];
  418. if (trace > 0.0) {
  419. real_t s = Math::sqrt(trace + 1.0);
  420. temp[3] = (s * 0.5);
  421. s = 0.5 / s;
  422. temp[0] = ((elements[2][1] - elements[1][2]) * s);
  423. temp[1] = ((elements[0][2] - elements[2][0]) * s);
  424. temp[2] = ((elements[1][0] - elements[0][1]) * s);
  425. } else {
  426. int i = elements[0][0] < elements[1][1] ?
  427. (elements[1][1] < elements[2][2] ? 2 : 1) :
  428. (elements[0][0] < elements[2][2] ? 2 : 0);
  429. int j = (i + 1) % 3;
  430. int k = (i + 2) % 3;
  431. real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0);
  432. temp[i] = s * 0.5;
  433. s = 0.5 / s;
  434. temp[3] = (elements[k][j] - elements[j][k]) * s;
  435. temp[j] = (elements[j][i] + elements[i][j]) * s;
  436. temp[k] = (elements[k][i] + elements[i][k]) * s;
  437. }
  438. return Quat(temp[0], temp[1], temp[2], temp[3]);
  439. }
  440. static const Basis _ortho_bases[24] = {
  441. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  442. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  443. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  444. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  445. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  446. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  447. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  448. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  449. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  450. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  451. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  452. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  453. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  454. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  455. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  456. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  457. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  458. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  459. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  460. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  461. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  462. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  463. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  464. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  465. };
  466. int Basis::get_orthogonal_index() const {
  467. //could be sped up if i come up with a way
  468. Basis orth = *this;
  469. for (int i = 0; i < 3; i++) {
  470. for (int j = 0; j < 3; j++) {
  471. real_t v = orth[i][j];
  472. if (v > 0.5)
  473. v = 1.0;
  474. else if (v < -0.5)
  475. v = -1.0;
  476. else
  477. v = 0;
  478. orth[i][j] = v;
  479. }
  480. }
  481. for (int i = 0; i < 24; i++) {
  482. if (_ortho_bases[i] == orth)
  483. return i;
  484. }
  485. return 0;
  486. }
  487. void Basis::set_orthogonal_index(int p_index) {
  488. //there only exist 24 orthogonal bases in r3
  489. ERR_FAIL_INDEX(p_index, 24);
  490. *this = _ortho_bases[p_index];
  491. }
  492. void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  493. #ifdef MATH_CHECKS
  494. ERR_FAIL_COND(is_rotation() == false);
  495. #endif
  496. real_t angle, x, y, z; // variables for result
  497. real_t epsilon = 0.01; // margin to allow for rounding errors
  498. real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  499. 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)) {
  500. // singularity found
  501. // first check for identity matrix which must have +1 for all terms
  502. // in leading diagonaland zero in other terms
  503. 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)) {
  504. // this singularity is identity matrix so angle = 0
  505. r_axis = Vector3(0, 1, 0);
  506. r_angle = 0;
  507. return;
  508. }
  509. // otherwise this singularity is angle = 180
  510. angle = Math_PI;
  511. real_t xx = (elements[0][0] + 1) / 2;
  512. real_t yy = (elements[1][1] + 1) / 2;
  513. real_t zz = (elements[2][2] + 1) / 2;
  514. real_t xy = (elements[1][0] + elements[0][1]) / 4;
  515. real_t xz = (elements[2][0] + elements[0][2]) / 4;
  516. real_t yz = (elements[2][1] + elements[1][2]) / 4;
  517. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  518. if (xx < epsilon) {
  519. x = 0;
  520. y = 0.7071;
  521. z = 0.7071;
  522. } else {
  523. x = Math::sqrt(xx);
  524. y = xy / x;
  525. z = xz / x;
  526. }
  527. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  528. if (yy < epsilon) {
  529. x = 0.7071;
  530. y = 0;
  531. z = 0.7071;
  532. } else {
  533. y = Math::sqrt(yy);
  534. x = xy / y;
  535. z = yz / y;
  536. }
  537. } else { // elements[2][2] is the largest diagonal term so base result on this
  538. if (zz < epsilon) {
  539. x = 0.7071;
  540. y = 0.7071;
  541. z = 0;
  542. } else {
  543. z = Math::sqrt(zz);
  544. x = xz / z;
  545. y = yz / z;
  546. }
  547. }
  548. r_axis = Vector3(x, y, z);
  549. r_angle = angle;
  550. return;
  551. }
  552. // as we have reached here there are no singularities so we can handle normally
  553. real_t 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])); // s=|axis||sin(angle)|, used to normalise
  554. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  555. if (angle < 0) s = -s;
  556. x = (elements[2][1] - elements[1][2]) / s;
  557. y = (elements[0][2] - elements[2][0]) / s;
  558. z = (elements[1][0] - elements[0][1]) / s;
  559. r_axis = Vector3(x, y, z);
  560. r_angle = angle;
  561. }
  562. Basis::Basis(const Vector3 &p_euler) {
  563. set_euler(p_euler);
  564. }
  565. Basis::Basis(const Quat &p_quat) {
  566. real_t d = p_quat.length_squared();
  567. real_t s = 2.0 / d;
  568. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  569. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  570. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  571. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  572. set(1.0 - (yy + zz), xy - wz, xz + wy,
  573. xy + wz, 1.0 - (xx + zz), yz - wx,
  574. xz - wy, yz + wx, 1.0 - (xx + yy));
  575. }
  576. void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
  577. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
  578. #ifdef MATH_CHECKS
  579. ERR_FAIL_COND(p_axis.is_normalized() == false);
  580. #endif
  581. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  582. real_t cosine = Math::cos(p_phi);
  583. real_t sine = Math::sin(p_phi);
  584. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  585. elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine;
  586. elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine;
  587. elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine;
  588. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  589. elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine;
  590. elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine;
  591. elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine;
  592. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  593. }
  594. Basis::Basis(const Vector3 &p_axis, real_t p_phi) {
  595. set_axis_angle(p_axis, p_phi);
  596. }