matrix3.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*************************************************************************/
  2. /* matrix3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/math/math_funcs.h"
  32. #include "core/os/copymem.h"
  33. #include "core/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_diagonal() const {
  95. return (
  96. Math::is_equal_approx(elements[0][1], 0) && Math::is_equal_approx(elements[0][2], 0) &&
  97. Math::is_equal_approx(elements[1][0], 0) && Math::is_equal_approx(elements[1][2], 0) &&
  98. Math::is_equal_approx(elements[2][0], 0) && Math::is_equal_approx(elements[2][1], 0));
  99. }
  100. bool Basis::is_rotation() const {
  101. return Math::is_equal_approx(determinant(), 1) && is_orthogonal();
  102. }
  103. bool Basis::is_symmetric() const {
  104. if (!Math::is_equal_approx(elements[0][1], elements[1][0]))
  105. return false;
  106. if (!Math::is_equal_approx(elements[0][2], elements[2][0]))
  107. return false;
  108. if (!Math::is_equal_approx(elements[1][2], elements[2][1]))
  109. return false;
  110. return true;
  111. }
  112. Basis Basis::diagonalize() {
  113. //NOTE: only implemented for symmetric matrices
  114. //with the Jacobi iterative method method
  115. #ifdef MATH_CHECKS
  116. ERR_FAIL_COND_V(!is_symmetric(), Basis());
  117. #endif
  118. const int ite_max = 1024;
  119. real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
  120. int ite = 0;
  121. Basis acc_rot;
  122. while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
  123. real_t el01_2 = elements[0][1] * elements[0][1];
  124. real_t el02_2 = elements[0][2] * elements[0][2];
  125. real_t el12_2 = elements[1][2] * elements[1][2];
  126. // Find the pivot element
  127. int i, j;
  128. if (el01_2 > el02_2) {
  129. if (el12_2 > el01_2) {
  130. i = 1;
  131. j = 2;
  132. } else {
  133. i = 0;
  134. j = 1;
  135. }
  136. } else {
  137. if (el12_2 > el02_2) {
  138. i = 1;
  139. j = 2;
  140. } else {
  141. i = 0;
  142. j = 2;
  143. }
  144. }
  145. // Compute the rotation angle
  146. real_t angle;
  147. if (Math::is_equal_approx(elements[j][j], elements[i][i])) {
  148. angle = Math_PI / 4;
  149. } else {
  150. angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
  151. }
  152. // Compute the rotation matrix
  153. Basis rot;
  154. rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle);
  155. rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle));
  156. // Update the off matrix norm
  157. off_matrix_norm_2 -= elements[i][j] * elements[i][j];
  158. // Apply the rotation
  159. *this = rot * *this * rot.transposed();
  160. acc_rot = rot * acc_rot;
  161. }
  162. return acc_rot;
  163. }
  164. Basis Basis::inverse() const {
  165. Basis inv = *this;
  166. inv.invert();
  167. return inv;
  168. }
  169. void Basis::transpose() {
  170. SWAP(elements[0][1], elements[1][0]);
  171. SWAP(elements[0][2], elements[2][0]);
  172. SWAP(elements[1][2], elements[2][1]);
  173. }
  174. Basis Basis::transposed() const {
  175. Basis tr = *this;
  176. tr.transpose();
  177. return tr;
  178. }
  179. // Multiplies the matrix from left by the scaling matrix: M -> S.M
  180. // See the comment for Basis::rotated for further explanation.
  181. void Basis::scale(const Vector3 &p_scale) {
  182. elements[0][0] *= p_scale.x;
  183. elements[0][1] *= p_scale.x;
  184. elements[0][2] *= p_scale.x;
  185. elements[1][0] *= p_scale.y;
  186. elements[1][1] *= p_scale.y;
  187. elements[1][2] *= p_scale.y;
  188. elements[2][0] *= p_scale.z;
  189. elements[2][1] *= p_scale.z;
  190. elements[2][2] *= p_scale.z;
  191. }
  192. Basis Basis::scaled(const Vector3 &p_scale) const {
  193. Basis m = *this;
  194. m.scale(p_scale);
  195. return m;
  196. }
  197. void Basis::scale_local(const Vector3 &p_scale) {
  198. // performs a scaling in object-local coordinate system:
  199. // M -> (M.S.Minv).M = M.S.
  200. *this = scaled_local(p_scale);
  201. }
  202. Basis Basis::scaled_local(const Vector3 &p_scale) const {
  203. Basis b;
  204. b.set_diagonal(p_scale);
  205. return (*this) * b;
  206. }
  207. Vector3 Basis::get_scale_abs() const {
  208. return Vector3(
  209. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  210. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  211. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  212. }
  213. Vector3 Basis::get_scale_local() const {
  214. real_t det_sign = determinant() > 0 ? 1 : -1;
  215. return det_sign * Vector3(elements[0].length(), elements[1].length(), elements[2].length());
  216. }
  217. // get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature.
  218. Vector3 Basis::get_scale() const {
  219. // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S.
  220. // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and
  221. // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal).
  222. //
  223. // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition
  224. // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where
  225. // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix,
  226. // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P,
  227. // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique.
  228. // Therefore, we are going to do this decomposition by sticking to a particular convention.
  229. // This may lead to confusion for some users though.
  230. //
  231. // The convention we use here is to absorb the sign flip into the scaling matrix.
  232. // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ...
  233. //
  234. // A proper way to get rid of this issue would be to store the scaling values (or at least their signs)
  235. // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the
  236. // matrix elements.
  237. //
  238. // The rotation part of this decomposition is returned by get_rotation* functions.
  239. real_t det_sign = determinant() > 0 ? 1 : -1;
  240. return det_sign * Vector3(
  241. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  242. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  243. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  244. }
  245. // Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S.
  246. // Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3.
  247. // This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so.
  248. Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const {
  249. #ifdef MATH_CHECKS
  250. ERR_FAIL_COND_V(determinant() == 0, Vector3());
  251. Basis m = transposed() * (*this);
  252. ERR_FAIL_COND_V(!m.is_diagonal(), Vector3());
  253. #endif
  254. Vector3 scale = get_scale();
  255. Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale
  256. rotref = (*this) * inv_scale;
  257. #ifdef MATH_CHECKS
  258. ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3());
  259. #endif
  260. return scale.abs();
  261. }
  262. // Multiplies the matrix from left by the rotation matrix: M -> R.M
  263. // Note that this does *not* rotate the matrix itself.
  264. //
  265. // The main use of Basis is as Transform.basis, which is used a the transformation matrix
  266. // of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
  267. // not the matrix itself (which is R * (*this) * R.transposed()).
  268. Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const {
  269. return Basis(p_axis, p_phi) * (*this);
  270. }
  271. void Basis::rotate(const Vector3 &p_axis, real_t p_phi) {
  272. *this = rotated(p_axis, p_phi);
  273. }
  274. void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) {
  275. // performs a rotation in object-local coordinate system:
  276. // M -> (M.R.Minv).M = M.R.
  277. *this = rotated_local(p_axis, p_phi);
  278. }
  279. Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const {
  280. return (*this) * Basis(p_axis, p_phi);
  281. }
  282. Basis Basis::rotated(const Vector3 &p_euler) const {
  283. return Basis(p_euler) * (*this);
  284. }
  285. void Basis::rotate(const Vector3 &p_euler) {
  286. *this = rotated(p_euler);
  287. }
  288. Basis Basis::rotated(const Quat &p_quat) const {
  289. return Basis(p_quat) * (*this);
  290. }
  291. void Basis::rotate(const Quat &p_quat) {
  292. *this = rotated(p_quat);
  293. }
  294. Vector3 Basis::get_rotation_euler() const {
  295. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  296. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  297. // See the comment in get_scale() for further information.
  298. Basis m = orthonormalized();
  299. real_t det = m.determinant();
  300. if (det < 0) {
  301. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  302. m.scale(Vector3(-1, -1, -1));
  303. }
  304. return m.get_euler();
  305. }
  306. Quat Basis::get_rotation_quat() const {
  307. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  308. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  309. // See the comment in get_scale() for further information.
  310. Basis m = orthonormalized();
  311. real_t det = m.determinant();
  312. if (det < 0) {
  313. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  314. m.scale(Vector3(-1, -1, -1));
  315. }
  316. return m.get_quat();
  317. }
  318. void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
  319. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  320. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  321. // See the comment in get_scale() for further information.
  322. Basis m = orthonormalized();
  323. real_t det = m.determinant();
  324. if (det < 0) {
  325. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  326. m.scale(Vector3(-1, -1, -1));
  327. }
  328. m.get_axis_angle(p_axis, p_angle);
  329. }
  330. void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const {
  331. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  332. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  333. // See the comment in get_scale() for further information.
  334. Basis m = transposed();
  335. m.orthonormalize();
  336. real_t det = m.determinant();
  337. if (det < 0) {
  338. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  339. m.scale(Vector3(-1, -1, -1));
  340. }
  341. m.get_axis_angle(p_axis, p_angle);
  342. p_angle = -p_angle;
  343. }
  344. // get_euler_xyz returns a vector containing the Euler angles in the format
  345. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last
  346. // (following the convention they are commonly defined in the literature).
  347. //
  348. // The current implementation uses XYZ convention (Z is the first rotation),
  349. // so euler.z is the angle of the (first) rotation around Z axis and so on,
  350. //
  351. // And thus, assuming the matrix is a rotation matrix, this function returns
  352. // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates
  353. // around the z-axis by a and so on.
  354. Vector3 Basis::get_euler_xyz() const {
  355. // Euler angles in XYZ convention.
  356. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  357. //
  358. // rot = cy*cz -cy*sz sy
  359. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  360. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  361. Vector3 euler;
  362. #ifdef MATH_CHECKS
  363. ERR_FAIL_COND_V(!is_rotation(), euler);
  364. #endif
  365. real_t sy = elements[0][2];
  366. if (sy < 1.0) {
  367. if (sy > -1.0) {
  368. // is this a pure Y rotation?
  369. if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
  370. // return the simplest form (human friendlier in editor and scripts)
  371. euler.x = 0;
  372. euler.y = atan2(elements[0][2], elements[0][0]);
  373. euler.z = 0;
  374. } else {
  375. euler.x = Math::atan2(-elements[1][2], elements[2][2]);
  376. euler.y = Math::asin(sy);
  377. euler.z = Math::atan2(-elements[0][1], elements[0][0]);
  378. }
  379. } else {
  380. euler.x = -Math::atan2(elements[0][1], elements[1][1]);
  381. euler.y = -Math_PI / 2.0;
  382. euler.z = 0.0;
  383. }
  384. } else {
  385. euler.x = Math::atan2(elements[0][1], elements[1][1]);
  386. euler.y = Math_PI / 2.0;
  387. euler.z = 0.0;
  388. }
  389. return euler;
  390. }
  391. // set_euler_xyz expects a vector containing the Euler angles in the format
  392. // (ax,ay,az), where ax is the angle of rotation around x axis,
  393. // and similar for other axes.
  394. // The current implementation uses XYZ convention (Z is the first rotation).
  395. void Basis::set_euler_xyz(const Vector3 &p_euler) {
  396. real_t c, s;
  397. c = Math::cos(p_euler.x);
  398. s = Math::sin(p_euler.x);
  399. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  400. c = Math::cos(p_euler.y);
  401. s = Math::sin(p_euler.y);
  402. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  403. c = Math::cos(p_euler.z);
  404. s = Math::sin(p_euler.z);
  405. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  406. //optimizer will optimize away all this anyway
  407. *this = xmat * (ymat * zmat);
  408. }
  409. // get_euler_yxz returns a vector containing the Euler angles in the YXZ convention,
  410. // as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned
  411. // as the x, y, and z components of a Vector3 respectively.
  412. Vector3 Basis::get_euler_yxz() const {
  413. // Euler angles in YXZ convention.
  414. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  415. //
  416. // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy
  417. // cx*sz cx*cz -sx
  418. // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx
  419. Vector3 euler;
  420. #ifdef MATH_CHECKS
  421. ERR_FAIL_COND_V(!is_rotation(), euler);
  422. #endif
  423. real_t m12 = elements[1][2];
  424. if (m12 < 1) {
  425. if (m12 > -1) {
  426. // is this a pure X rotation?
  427. if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) {
  428. // return the simplest form (human friendlier in editor and scripts)
  429. euler.x = atan2(-m12, elements[1][1]);
  430. euler.y = 0;
  431. euler.z = 0;
  432. } else {
  433. euler.x = asin(-m12);
  434. euler.y = atan2(elements[0][2], elements[2][2]);
  435. euler.z = atan2(elements[1][0], elements[1][1]);
  436. }
  437. } else { // m12 == -1
  438. euler.x = Math_PI * 0.5;
  439. euler.y = -atan2(-elements[0][1], elements[0][0]);
  440. euler.z = 0;
  441. }
  442. } else { // m12 == 1
  443. euler.x = -Math_PI * 0.5;
  444. euler.y = -atan2(-elements[0][1], elements[0][0]);
  445. euler.z = 0;
  446. }
  447. return euler;
  448. }
  449. // set_euler_yxz expects a vector containing the Euler angles in the format
  450. // (ax,ay,az), where ax is the angle of rotation around x axis,
  451. // and similar for other axes.
  452. // The current implementation uses YXZ convention (Z is the first rotation).
  453. void Basis::set_euler_yxz(const Vector3 &p_euler) {
  454. real_t c, s;
  455. c = Math::cos(p_euler.x);
  456. s = Math::sin(p_euler.x);
  457. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  458. c = Math::cos(p_euler.y);
  459. s = Math::sin(p_euler.y);
  460. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  461. c = Math::cos(p_euler.z);
  462. s = Math::sin(p_euler.z);
  463. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  464. //optimizer will optimize away all this anyway
  465. *this = ymat * xmat * zmat;
  466. }
  467. bool Basis::is_equal_approx(const Basis &a, const Basis &b) const {
  468. for (int i = 0; i < 3; i++) {
  469. for (int j = 0; j < 3; j++) {
  470. if (!Math::is_equal_approx(a.elements[i][j], b.elements[i][j]))
  471. return false;
  472. }
  473. }
  474. return true;
  475. }
  476. bool Basis::operator==(const Basis &p_matrix) const {
  477. for (int i = 0; i < 3; i++) {
  478. for (int j = 0; j < 3; j++) {
  479. if (elements[i][j] != p_matrix.elements[i][j])
  480. return false;
  481. }
  482. }
  483. return true;
  484. }
  485. bool Basis::operator!=(const Basis &p_matrix) const {
  486. return (!(*this == p_matrix));
  487. }
  488. Basis::operator String() const {
  489. String mtx;
  490. for (int i = 0; i < 3; i++) {
  491. for (int j = 0; j < 3; j++) {
  492. if (i != 0 || j != 0)
  493. mtx += ", ";
  494. mtx += rtos(elements[i][j]);
  495. }
  496. }
  497. return mtx;
  498. }
  499. Quat Basis::get_quat() const {
  500. #ifdef MATH_CHECKS
  501. ERR_FAIL_COND_V(!is_rotation(), Quat());
  502. #endif
  503. real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
  504. real_t temp[4];
  505. if (trace > 0.0) {
  506. real_t s = Math::sqrt(trace + 1.0);
  507. temp[3] = (s * 0.5);
  508. s = 0.5 / s;
  509. temp[0] = ((elements[2][1] - elements[1][2]) * s);
  510. temp[1] = ((elements[0][2] - elements[2][0]) * s);
  511. temp[2] = ((elements[1][0] - elements[0][1]) * s);
  512. } else {
  513. int i = elements[0][0] < elements[1][1] ?
  514. (elements[1][1] < elements[2][2] ? 2 : 1) :
  515. (elements[0][0] < elements[2][2] ? 2 : 0);
  516. int j = (i + 1) % 3;
  517. int k = (i + 2) % 3;
  518. real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0);
  519. temp[i] = s * 0.5;
  520. s = 0.5 / s;
  521. temp[3] = (elements[k][j] - elements[j][k]) * s;
  522. temp[j] = (elements[j][i] + elements[i][j]) * s;
  523. temp[k] = (elements[k][i] + elements[i][k]) * s;
  524. }
  525. return Quat(temp[0], temp[1], temp[2], temp[3]);
  526. }
  527. static const Basis _ortho_bases[24] = {
  528. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  529. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  530. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  531. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  532. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  533. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  534. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  535. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  536. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  537. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  538. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  539. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  540. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  541. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  542. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  543. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  544. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  545. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  546. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  547. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  548. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  549. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  550. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  551. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  552. };
  553. int Basis::get_orthogonal_index() const {
  554. //could be sped up if i come up with a way
  555. Basis orth = *this;
  556. for (int i = 0; i < 3; i++) {
  557. for (int j = 0; j < 3; j++) {
  558. real_t v = orth[i][j];
  559. if (v > 0.5)
  560. v = 1.0;
  561. else if (v < -0.5)
  562. v = -1.0;
  563. else
  564. v = 0;
  565. orth[i][j] = v;
  566. }
  567. }
  568. for (int i = 0; i < 24; i++) {
  569. if (_ortho_bases[i] == orth)
  570. return i;
  571. }
  572. return 0;
  573. }
  574. void Basis::set_orthogonal_index(int p_index) {
  575. //there only exist 24 orthogonal bases in r3
  576. ERR_FAIL_INDEX(p_index, 24);
  577. *this = _ortho_bases[p_index];
  578. }
  579. void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  580. #ifdef MATH_CHECKS
  581. ERR_FAIL_COND(!is_rotation());
  582. #endif
  583. real_t angle, x, y, z; // variables for result
  584. real_t epsilon = 0.01; // margin to allow for rounding errors
  585. real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  586. 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)) {
  587. // singularity found
  588. // first check for identity matrix which must have +1 for all terms
  589. // in leading diagonaland zero in other terms
  590. 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)) {
  591. // this singularity is identity matrix so angle = 0
  592. r_axis = Vector3(0, 1, 0);
  593. r_angle = 0;
  594. return;
  595. }
  596. // otherwise this singularity is angle = 180
  597. angle = Math_PI;
  598. real_t xx = (elements[0][0] + 1) / 2;
  599. real_t yy = (elements[1][1] + 1) / 2;
  600. real_t zz = (elements[2][2] + 1) / 2;
  601. real_t xy = (elements[1][0] + elements[0][1]) / 4;
  602. real_t xz = (elements[2][0] + elements[0][2]) / 4;
  603. real_t yz = (elements[2][1] + elements[1][2]) / 4;
  604. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  605. if (xx < epsilon) {
  606. x = 0;
  607. y = 0.7071;
  608. z = 0.7071;
  609. } else {
  610. x = Math::sqrt(xx);
  611. y = xy / x;
  612. z = xz / x;
  613. }
  614. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  615. if (yy < epsilon) {
  616. x = 0.7071;
  617. y = 0;
  618. z = 0.7071;
  619. } else {
  620. y = Math::sqrt(yy);
  621. x = xy / y;
  622. z = yz / y;
  623. }
  624. } else { // elements[2][2] is the largest diagonal term so base result on this
  625. if (zz < epsilon) {
  626. x = 0.7071;
  627. y = 0.7071;
  628. z = 0;
  629. } else {
  630. z = Math::sqrt(zz);
  631. x = xz / z;
  632. y = yz / z;
  633. }
  634. }
  635. r_axis = Vector3(x, y, z);
  636. r_angle = angle;
  637. return;
  638. }
  639. // as we have reached here there are no singularities so we can handle normally
  640. 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
  641. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  642. if (angle < 0) s = -s;
  643. x = (elements[2][1] - elements[1][2]) / s;
  644. y = (elements[0][2] - elements[2][0]) / s;
  645. z = (elements[1][0] - elements[0][1]) / s;
  646. r_axis = Vector3(x, y, z);
  647. r_angle = angle;
  648. }
  649. void Basis::set_quat(const Quat &p_quat) {
  650. real_t d = p_quat.length_squared();
  651. real_t s = 2.0 / d;
  652. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  653. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  654. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  655. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  656. set(1.0 - (yy + zz), xy - wz, xz + wy,
  657. xy + wz, 1.0 - (xx + zz), yz - wx,
  658. xz - wy, yz + wx, 1.0 - (xx + yy));
  659. }
  660. void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
  661. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
  662. #ifdef MATH_CHECKS
  663. ERR_FAIL_COND(!p_axis.is_normalized());
  664. #endif
  665. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  666. real_t cosine = Math::cos(p_phi);
  667. real_t sine = Math::sin(p_phi);
  668. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  669. elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine;
  670. elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine;
  671. elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine;
  672. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  673. elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine;
  674. elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine;
  675. elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine;
  676. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  677. }
  678. void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) {
  679. set_diagonal(p_scale);
  680. rotate(p_axis, p_phi);
  681. }
  682. void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) {
  683. set_diagonal(p_scale);
  684. rotate(p_euler);
  685. }
  686. void Basis::set_quat_scale(const Quat &p_quat, const Vector3 &p_scale) {
  687. set_diagonal(p_scale);
  688. rotate(p_quat);
  689. }
  690. void Basis::set_diagonal(const Vector3 p_diag) {
  691. elements[0][0] = p_diag.x;
  692. elements[0][1] = 0;
  693. elements[0][2] = 0;
  694. elements[1][0] = 0;
  695. elements[1][1] = p_diag.y;
  696. elements[1][2] = 0;
  697. elements[2][0] = 0;
  698. elements[2][1] = 0;
  699. elements[2][2] = p_diag.z;
  700. }
  701. Basis Basis::slerp(const Basis &target, const real_t &t) const {
  702. // TODO: implement this directly without using quaternions to make it more efficient
  703. #ifdef MATH_CHECKS
  704. ERR_FAIL_COND_V(!is_rotation(), Basis());
  705. ERR_FAIL_COND_V(!target.is_rotation(), Basis());
  706. #endif
  707. Quat from(*this);
  708. Quat to(target);
  709. return Basis(from.slerp(to, t));
  710. }