basis.cpp 36 KB

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