basis.cpp 32 KB

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