basis.cpp 36 KB

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