basis.cpp 36 KB

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