basis.cpp 32 KB

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