basis.cpp 32 KB

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