basis.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction) {
  331. // Takes two vectors and rotates the basis from the first vector to the second vector.
  332. // Adopted from: https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
  333. const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
  334. if (axis.length_squared() != 0) {
  335. real_t dot = p_start_direction.dot(p_end_direction);
  336. dot = CLAMP(dot, -1.0, 1.0);
  337. const real_t angle_rads = Math::acos(dot);
  338. set_axis_angle(axis, angle_rads);
  339. }
  340. }
  341. void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
  342. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  343. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  344. // See the comment in get_scale() for further information.
  345. Basis m = orthonormalized();
  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. }
  353. void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const {
  354. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  355. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  356. // See the comment in get_scale() for further information.
  357. Basis m = transposed();
  358. m.orthonormalize();
  359. real_t det = m.determinant();
  360. if (det < 0) {
  361. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  362. m.scale(Vector3(-1, -1, -1));
  363. }
  364. m.get_axis_angle(p_axis, p_angle);
  365. p_angle = -p_angle;
  366. }
  367. // get_euler_xyz returns a vector containing the Euler angles in the format
  368. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last
  369. // (following the convention they are commonly defined in the literature).
  370. //
  371. // The current implementation uses XYZ convention (Z is the first rotation),
  372. // so euler.z is the angle of the (first) rotation around Z axis and so on,
  373. //
  374. // And thus, assuming the matrix is a rotation matrix, this function returns
  375. // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates
  376. // around the z-axis by a and so on.
  377. Vector3 Basis::get_euler_xyz() const {
  378. // Euler angles in XYZ convention.
  379. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  380. //
  381. // rot = cy*cz -cy*sz sy
  382. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  383. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  384. Vector3 euler;
  385. real_t sy = elements[0][2];
  386. if (sy < (1.0 - CMP_EPSILON)) {
  387. if (sy > -(1.0 - CMP_EPSILON)) {
  388. // is this a pure Y rotation?
  389. if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
  390. // return the simplest form (human friendlier in editor and scripts)
  391. euler.x = 0;
  392. euler.y = atan2(elements[0][2], elements[0][0]);
  393. euler.z = 0;
  394. } else {
  395. euler.x = Math::atan2(-elements[1][2], elements[2][2]);
  396. euler.y = Math::asin(sy);
  397. euler.z = Math::atan2(-elements[0][1], elements[0][0]);
  398. }
  399. } else {
  400. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  401. euler.y = -Math_PI / 2.0;
  402. euler.z = 0.0;
  403. }
  404. } else {
  405. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  406. euler.y = Math_PI / 2.0;
  407. euler.z = 0.0;
  408. }
  409. return euler;
  410. }
  411. // set_euler_xyz expects a vector containing the Euler angles in the format
  412. // (ax,ay,az), where ax is the angle of rotation around x axis,
  413. // and similar for other axes.
  414. // The current implementation uses XYZ convention (Z is the first rotation).
  415. void Basis::set_euler_xyz(const Vector3 &p_euler) {
  416. real_t c, s;
  417. c = Math::cos(p_euler.x);
  418. s = Math::sin(p_euler.x);
  419. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  420. c = Math::cos(p_euler.y);
  421. s = Math::sin(p_euler.y);
  422. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  423. c = Math::cos(p_euler.z);
  424. s = Math::sin(p_euler.z);
  425. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  426. //optimizer will optimize away all this anyway
  427. *this = xmat * (ymat * zmat);
  428. }
  429. Vector3 Basis::get_euler_xzy() const {
  430. // Euler angles in XZY convention.
  431. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  432. //
  433. // rot = cz*cy -sz cz*sy
  434. // sx*sy+cx*cy*sz cx*cz cx*sz*sy-cy*sx
  435. // cy*sx*sz cz*sx cx*cy+sx*sz*sy
  436. Vector3 euler;
  437. real_t sz = elements[0][1];
  438. if (sz < (1.0 - CMP_EPSILON)) {
  439. if (sz > -(1.0 - CMP_EPSILON)) {
  440. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  441. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  442. euler.z = Math::asin(-sz);
  443. } else {
  444. // It's -1
  445. euler.x = -Math::atan2(elements[1][2], elements[2][2]);
  446. euler.y = 0.0;
  447. euler.z = Math_PI / 2.0;
  448. }
  449. } else {
  450. // It's 1
  451. euler.x = -Math::atan2(elements[1][2], elements[2][2]);
  452. euler.y = 0.0;
  453. euler.z = -Math_PI / 2.0;
  454. }
  455. return euler;
  456. }
  457. void Basis::set_euler_xzy(const Vector3 &p_euler) {
  458. real_t c, s;
  459. c = Math::cos(p_euler.x);
  460. s = Math::sin(p_euler.x);
  461. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  462. c = Math::cos(p_euler.y);
  463. s = Math::sin(p_euler.y);
  464. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  465. c = Math::cos(p_euler.z);
  466. s = Math::sin(p_euler.z);
  467. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  468. *this = xmat * zmat * ymat;
  469. }
  470. Vector3 Basis::get_euler_yzx() const {
  471. // Euler angles in YZX convention.
  472. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  473. //
  474. // rot = cy*cz sy*sx-cy*cx*sz cx*sy+cy*sz*sx
  475. // sz cz*cx -cz*sx
  476. // -cz*sy cy*sx+cx*sy*sz cy*cx-sy*sz*sx
  477. Vector3 euler;
  478. real_t sz = elements[1][0];
  479. if (sz < (1.0 - CMP_EPSILON)) {
  480. if (sz > -(1.0 - CMP_EPSILON)) {
  481. euler.x = Math::atan2(-elements[1][2], elements[1][1]);
  482. euler.y = Math::atan2(-elements[2][0], elements[0][0]);
  483. euler.z = Math::asin(sz);
  484. } else {
  485. // It's -1
  486. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  487. euler.y = 0.0;
  488. euler.z = -Math_PI / 2.0;
  489. }
  490. } else {
  491. // It's 1
  492. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  493. euler.y = 0.0;
  494. euler.z = Math_PI / 2.0;
  495. }
  496. return euler;
  497. }
  498. void Basis::set_euler_yzx(const Vector3 &p_euler) {
  499. real_t c, s;
  500. c = Math::cos(p_euler.x);
  501. s = Math::sin(p_euler.x);
  502. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  503. c = Math::cos(p_euler.y);
  504. s = Math::sin(p_euler.y);
  505. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  506. c = Math::cos(p_euler.z);
  507. s = Math::sin(p_euler.z);
  508. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  509. *this = ymat * zmat * xmat;
  510. }
  511. // get_euler_yxz returns a vector containing the Euler angles in the YXZ convention,
  512. // as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned
  513. // as the x, y, and z components of a Vector3 respectively.
  514. Vector3 Basis::get_euler_yxz() const {
  515. // Euler angles in YXZ convention.
  516. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  517. //
  518. // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy
  519. // cx*sz cx*cz -sx
  520. // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx
  521. Vector3 euler;
  522. real_t m12 = elements[1][2];
  523. if (m12 < (1 - CMP_EPSILON)) {
  524. if (m12 > -(1 - CMP_EPSILON)) {
  525. // is this a pure X rotation?
  526. if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) {
  527. // return the simplest form (human friendlier in editor and scripts)
  528. euler.x = atan2(-m12, elements[1][1]);
  529. euler.y = 0;
  530. euler.z = 0;
  531. } else {
  532. euler.x = asin(-m12);
  533. euler.y = atan2(elements[0][2], elements[2][2]);
  534. euler.z = atan2(elements[1][0], elements[1][1]);
  535. }
  536. } else { // m12 == -1
  537. euler.x = Math_PI * 0.5;
  538. euler.y = atan2(elements[0][1], elements[0][0]);
  539. euler.z = 0;
  540. }
  541. } else { // m12 == 1
  542. euler.x = -Math_PI * 0.5;
  543. euler.y = -atan2(elements[0][1], elements[0][0]);
  544. euler.z = 0;
  545. }
  546. return euler;
  547. }
  548. // set_euler_yxz expects a vector containing the Euler angles in the format
  549. // (ax,ay,az), where ax is the angle of rotation around x axis,
  550. // and similar for other axes.
  551. // The current implementation uses YXZ convention (Z is the first rotation).
  552. void Basis::set_euler_yxz(const Vector3 &p_euler) {
  553. real_t c, s;
  554. c = Math::cos(p_euler.x);
  555. s = Math::sin(p_euler.x);
  556. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  557. c = Math::cos(p_euler.y);
  558. s = Math::sin(p_euler.y);
  559. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  560. c = Math::cos(p_euler.z);
  561. s = Math::sin(p_euler.z);
  562. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  563. //optimizer will optimize away all this anyway
  564. *this = ymat * xmat * zmat;
  565. }
  566. Vector3 Basis::get_euler_zxy() const {
  567. // Euler angles in ZXY convention.
  568. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  569. //
  570. // rot = cz*cy-sz*sx*sy -cx*sz cz*sy+cy*sz*sx
  571. // cy*sz+cz*sx*sy cz*cx sz*sy-cz*cy*sx
  572. // -cx*sy sx cx*cy
  573. Vector3 euler;
  574. real_t sx = elements[2][1];
  575. if (sx < (1.0 - CMP_EPSILON)) {
  576. if (sx > -(1.0 - CMP_EPSILON)) {
  577. euler.x = Math::asin(sx);
  578. euler.y = Math::atan2(-elements[2][0], elements[2][2]);
  579. euler.z = Math::atan2(-elements[0][1], elements[1][1]);
  580. } else {
  581. // It's -1
  582. euler.x = -Math_PI / 2.0;
  583. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  584. euler.z = 0;
  585. }
  586. } else {
  587. // It's 1
  588. euler.x = Math_PI / 2.0;
  589. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  590. euler.z = 0;
  591. }
  592. return euler;
  593. }
  594. void Basis::set_euler_zxy(const Vector3 &p_euler) {
  595. real_t c, s;
  596. c = Math::cos(p_euler.x);
  597. s = Math::sin(p_euler.x);
  598. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  599. c = Math::cos(p_euler.y);
  600. s = Math::sin(p_euler.y);
  601. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  602. c = Math::cos(p_euler.z);
  603. s = Math::sin(p_euler.z);
  604. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  605. *this = zmat * xmat * ymat;
  606. }
  607. Vector3 Basis::get_euler_zyx() const {
  608. // Euler angles in ZYX convention.
  609. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  610. //
  611. // rot = cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*cy
  612. // cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx
  613. // -sy cy*sx cy*cx
  614. Vector3 euler;
  615. real_t sy = elements[2][0];
  616. if (sy < (1.0 - CMP_EPSILON)) {
  617. if (sy > -(1.0 - CMP_EPSILON)) {
  618. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  619. euler.y = Math::asin(-sy);
  620. euler.z = Math::atan2(elements[1][0], elements[0][0]);
  621. } else {
  622. // It's -1
  623. euler.x = 0;
  624. euler.y = Math_PI / 2.0;
  625. euler.z = -Math::atan2(elements[0][1], elements[1][1]);
  626. }
  627. } else {
  628. // It's 1
  629. euler.x = 0;
  630. euler.y = -Math_PI / 2.0;
  631. euler.z = -Math::atan2(elements[0][1], elements[1][1]);
  632. }
  633. return euler;
  634. }
  635. void Basis::set_euler_zyx(const Vector3 &p_euler) {
  636. real_t c, s;
  637. c = Math::cos(p_euler.x);
  638. s = Math::sin(p_euler.x);
  639. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  640. c = Math::cos(p_euler.y);
  641. s = Math::sin(p_euler.y);
  642. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  643. c = Math::cos(p_euler.z);
  644. s = Math::sin(p_euler.z);
  645. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  646. *this = zmat * ymat * xmat;
  647. }
  648. bool Basis::is_equal_approx(const Basis &p_basis) const {
  649. 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]);
  650. }
  651. bool Basis::operator==(const Basis &p_matrix) const {
  652. for (int i = 0; i < 3; i++) {
  653. for (int j = 0; j < 3; j++) {
  654. if (elements[i][j] != p_matrix.elements[i][j]) {
  655. return false;
  656. }
  657. }
  658. }
  659. return true;
  660. }
  661. bool Basis::operator!=(const Basis &p_matrix) const {
  662. return (!(*this == p_matrix));
  663. }
  664. Basis::operator String() const {
  665. return "[X: " + get_axis(0).operator String() +
  666. ", Y: " + get_axis(1).operator String() +
  667. ", Z: " + get_axis(2).operator String() + "]";
  668. }
  669. Quaternion Basis::get_quaternion() const {
  670. #ifdef MATH_CHECKS
  671. 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() instead.");
  672. #endif
  673. /* Allow getting a quaternion from an unnormalized transform */
  674. Basis m = *this;
  675. real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
  676. real_t temp[4];
  677. if (trace > 0.0) {
  678. real_t s = Math::sqrt(trace + 1.0);
  679. temp[3] = (s * 0.5);
  680. s = 0.5 / s;
  681. temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s);
  682. temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s);
  683. temp[2] = ((m.elements[1][0] - m.elements[0][1]) * s);
  684. } else {
  685. int i = m.elements[0][0] < m.elements[1][1] ?
  686. (m.elements[1][1] < m.elements[2][2] ? 2 : 1) :
  687. (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
  688. int j = (i + 1) % 3;
  689. int k = (i + 2) % 3;
  690. real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0);
  691. temp[i] = s * 0.5;
  692. s = 0.5 / s;
  693. temp[3] = (m.elements[k][j] - m.elements[j][k]) * s;
  694. temp[j] = (m.elements[j][i] + m.elements[i][j]) * s;
  695. temp[k] = (m.elements[k][i] + m.elements[i][k]) * s;
  696. }
  697. return Quaternion(temp[0], temp[1], temp[2], temp[3]);
  698. }
  699. static const Basis _ortho_bases[24] = {
  700. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  701. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  702. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  703. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  704. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  705. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  706. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  707. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  708. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  709. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  710. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  711. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  712. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  713. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  714. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  715. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  716. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  717. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  718. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  719. Basis(0, 1, 0, 0, 0, -1, -1, 0, 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. };
  725. int Basis::get_orthogonal_index() const {
  726. //could be sped up if i come up with a way
  727. Basis orth = *this;
  728. for (int i = 0; i < 3; i++) {
  729. for (int j = 0; j < 3; j++) {
  730. real_t v = orth[i][j];
  731. if (v > 0.5) {
  732. v = 1.0;
  733. } else if (v < -0.5) {
  734. v = -1.0;
  735. } else {
  736. v = 0;
  737. }
  738. orth[i][j] = v;
  739. }
  740. }
  741. for (int i = 0; i < 24; i++) {
  742. if (_ortho_bases[i] == orth) {
  743. return i;
  744. }
  745. }
  746. return 0;
  747. }
  748. void Basis::set_orthogonal_index(int p_index) {
  749. //there only exist 24 orthogonal bases in r3
  750. ERR_FAIL_INDEX(p_index, 24);
  751. *this = _ortho_bases[p_index];
  752. }
  753. void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  754. /* checking this is a bad idea, because obtaining from scaled transform is a valid use case
  755. #ifdef MATH_CHECKS
  756. ERR_FAIL_COND(!is_rotation());
  757. #endif
  758. */
  759. real_t angle, x, y, z; // variables for result
  760. real_t epsilon = 0.01; // margin to allow for rounding errors
  761. real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  762. 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)) {
  763. // singularity found
  764. // first check for identity matrix which must have +1 for all terms
  765. // in leading diagonal and zero in other terms
  766. 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)) {
  767. // this singularity is identity matrix so angle = 0
  768. r_axis = Vector3(0, 1, 0);
  769. r_angle = 0;
  770. return;
  771. }
  772. // otherwise this singularity is angle = 180
  773. angle = Math_PI;
  774. real_t xx = (elements[0][0] + 1) / 2;
  775. real_t yy = (elements[1][1] + 1) / 2;
  776. real_t zz = (elements[2][2] + 1) / 2;
  777. real_t xy = (elements[1][0] + elements[0][1]) / 4;
  778. real_t xz = (elements[2][0] + elements[0][2]) / 4;
  779. real_t yz = (elements[2][1] + elements[1][2]) / 4;
  780. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  781. if (xx < epsilon) {
  782. x = 0;
  783. y = Math_SQRT12;
  784. z = Math_SQRT12;
  785. } else {
  786. x = Math::sqrt(xx);
  787. y = xy / x;
  788. z = xz / x;
  789. }
  790. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  791. if (yy < epsilon) {
  792. x = Math_SQRT12;
  793. y = 0;
  794. z = Math_SQRT12;
  795. } else {
  796. y = Math::sqrt(yy);
  797. x = xy / y;
  798. z = yz / y;
  799. }
  800. } else { // elements[2][2] is the largest diagonal term so base result on this
  801. if (zz < epsilon) {
  802. x = Math_SQRT12;
  803. y = Math_SQRT12;
  804. z = 0;
  805. } else {
  806. z = Math::sqrt(zz);
  807. x = xz / z;
  808. y = yz / z;
  809. }
  810. }
  811. r_axis = Vector3(x, y, z);
  812. r_angle = angle;
  813. return;
  814. }
  815. // as we have reached here there are no singularities so we can handle normally
  816. 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
  817. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  818. if (angle < 0) {
  819. s = -s;
  820. }
  821. x = (elements[2][1] - elements[1][2]) / s;
  822. y = (elements[0][2] - elements[2][0]) / s;
  823. z = (elements[1][0] - elements[0][1]) / s;
  824. r_axis = Vector3(x, y, z);
  825. r_angle = angle;
  826. }
  827. void Basis::set_quaternion(const Quaternion &p_quaternion) {
  828. real_t d = p_quaternion.length_squared();
  829. real_t s = 2.0 / d;
  830. real_t xs = p_quaternion.x * s, ys = p_quaternion.y * s, zs = p_quaternion.z * s;
  831. real_t wx = p_quaternion.w * xs, wy = p_quaternion.w * ys, wz = p_quaternion.w * zs;
  832. real_t xx = p_quaternion.x * xs, xy = p_quaternion.x * ys, xz = p_quaternion.x * zs;
  833. real_t yy = p_quaternion.y * ys, yz = p_quaternion.y * zs, zz = p_quaternion.z * zs;
  834. set(1.0 - (yy + zz), xy - wz, xz + wy,
  835. xy + wz, 1.0 - (xx + zz), yz - wx,
  836. xz - wy, yz + wx, 1.0 - (xx + yy));
  837. }
  838. void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
  839. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
  840. #ifdef MATH_CHECKS
  841. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  842. #endif
  843. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  844. real_t cosine = Math::cos(p_phi);
  845. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  846. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  847. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  848. real_t sine = Math::sin(p_phi);
  849. real_t t = 1 - cosine;
  850. real_t xyzt = p_axis.x * p_axis.y * t;
  851. real_t zyxs = p_axis.z * sine;
  852. elements[0][1] = xyzt - zyxs;
  853. elements[1][0] = xyzt + zyxs;
  854. xyzt = p_axis.x * p_axis.z * t;
  855. zyxs = p_axis.y * sine;
  856. elements[0][2] = xyzt + zyxs;
  857. elements[2][0] = xyzt - zyxs;
  858. xyzt = p_axis.y * p_axis.z * t;
  859. zyxs = p_axis.x * sine;
  860. elements[1][2] = xyzt - zyxs;
  861. elements[2][1] = xyzt + zyxs;
  862. }
  863. void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) {
  864. set_diagonal(p_scale);
  865. rotate(p_axis, p_phi);
  866. }
  867. void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) {
  868. set_diagonal(p_scale);
  869. rotate(p_euler);
  870. }
  871. void Basis::set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale) {
  872. set_diagonal(p_scale);
  873. rotate(p_quaternion);
  874. }
  875. void Basis::set_diagonal(const Vector3 &p_diag) {
  876. elements[0][0] = p_diag.x;
  877. elements[0][1] = 0;
  878. elements[0][2] = 0;
  879. elements[1][0] = 0;
  880. elements[1][1] = p_diag.y;
  881. elements[1][2] = 0;
  882. elements[2][0] = 0;
  883. elements[2][1] = 0;
  884. elements[2][2] = p_diag.z;
  885. }
  886. Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
  887. //consider scale
  888. Quaternion from(*this);
  889. Quaternion to(p_to);
  890. Basis b(from.slerp(to, p_weight));
  891. b.elements[0] *= Math::lerp(elements[0].length(), p_to.elements[0].length(), p_weight);
  892. b.elements[1] *= Math::lerp(elements[1].length(), p_to.elements[1].length(), p_weight);
  893. b.elements[2] *= Math::lerp(elements[2].length(), p_to.elements[2].length(), p_weight);
  894. return b;
  895. }
  896. void Basis::rotate_sh(real_t *p_values) {
  897. // code by John Hable
  898. // http://filmicworlds.com/blog/simple-and-fast-spherical-harmonic-rotation/
  899. // this code is Public Domain
  900. const static real_t s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi))
  901. const static real_t s_c4 = -0.31539156525; // (-sqrt(5))/(4*sqrt(pi))
  902. const static real_t s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi))
  903. const static real_t s_c_scale = 1.0 / 0.91529123286551084;
  904. const static real_t s_c_scale_inv = 0.91529123286551084;
  905. const static real_t s_rc2 = 1.5853309190550713 * s_c_scale;
  906. const static real_t s_c4_div_c3 = s_c4 / s_c3;
  907. const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
  908. const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv;
  909. const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv;
  910. 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] };
  911. real_t m00 = elements[0][0];
  912. real_t m01 = elements[0][1];
  913. real_t m02 = elements[0][2];
  914. real_t m10 = elements[1][0];
  915. real_t m11 = elements[1][1];
  916. real_t m12 = elements[1][2];
  917. real_t m20 = elements[2][0];
  918. real_t m21 = elements[2][1];
  919. real_t m22 = elements[2][2];
  920. p_values[0] = src[0];
  921. p_values[1] = m11 * src[1] - m12 * src[2] + m10 * src[3];
  922. p_values[2] = -m21 * src[1] + m22 * src[2] - m20 * src[3];
  923. p_values[3] = m01 * src[1] - m02 * src[2] + m00 * src[3];
  924. real_t sh0 = src[7] + src[8] + src[8] - src[5];
  925. real_t sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8];
  926. real_t sh2 = src[4];
  927. real_t sh3 = -src[7];
  928. real_t sh4 = -src[5];
  929. // Rotations. R0 and R1 just use the raw matrix columns
  930. real_t r2x = m00 + m01;
  931. real_t r2y = m10 + m11;
  932. real_t r2z = m20 + m21;
  933. real_t r3x = m00 + m02;
  934. real_t r3y = m10 + m12;
  935. real_t r3z = m20 + m22;
  936. real_t r4x = m01 + m02;
  937. real_t r4y = m11 + m12;
  938. real_t r4z = m21 + m22;
  939. // dense matrix multiplication one column at a time
  940. // column 0
  941. real_t sh0_x = sh0 * m00;
  942. real_t sh0_y = sh0 * m10;
  943. real_t d0 = sh0_x * m10;
  944. real_t d1 = sh0_y * m20;
  945. real_t d2 = sh0 * (m20 * m20 + s_c4_div_c3);
  946. real_t d3 = sh0_x * m20;
  947. real_t d4 = sh0_x * m00 - sh0_y * m10;
  948. // column 1
  949. real_t sh1_x = sh1 * m02;
  950. real_t sh1_y = sh1 * m12;
  951. d0 += sh1_x * m12;
  952. d1 += sh1_y * m22;
  953. d2 += sh1 * (m22 * m22 + s_c4_div_c3);
  954. d3 += sh1_x * m22;
  955. d4 += sh1_x * m02 - sh1_y * m12;
  956. // column 2
  957. real_t sh2_x = sh2 * r2x;
  958. real_t sh2_y = sh2 * r2y;
  959. d0 += sh2_x * r2y;
  960. d1 += sh2_y * r2z;
  961. d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
  962. d3 += sh2_x * r2z;
  963. d4 += sh2_x * r2x - sh2_y * r2y;
  964. // column 3
  965. real_t sh3_x = sh3 * r3x;
  966. real_t sh3_y = sh3 * r3y;
  967. d0 += sh3_x * r3y;
  968. d1 += sh3_y * r3z;
  969. d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
  970. d3 += sh3_x * r3z;
  971. d4 += sh3_x * r3x - sh3_y * r3y;
  972. // column 4
  973. real_t sh4_x = sh4 * r4x;
  974. real_t sh4_y = sh4 * r4y;
  975. d0 += sh4_x * r4y;
  976. d1 += sh4_y * r4z;
  977. d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
  978. d3 += sh4_x * r4z;
  979. d4 += sh4_x * r4x - sh4_y * r4y;
  980. // extra multipliers
  981. p_values[4] = d0;
  982. p_values[5] = -d1;
  983. p_values[6] = d2 * s_scale_dst2;
  984. p_values[7] = -d3;
  985. p_values[8] = d4 * s_scale_dst4;
  986. }
  987. Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) {
  988. #ifdef MATH_CHECKS
  989. ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero.");
  990. ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero.");
  991. #endif
  992. Vector3 v_z = -p_target.normalized();
  993. Vector3 v_x = p_up.cross(v_z);
  994. #ifdef MATH_CHECKS
  995. 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.");
  996. #endif
  997. v_x.normalize();
  998. Vector3 v_y = v_z.cross(v_x);
  999. Basis basis;
  1000. basis.set(v_x, v_y, v_z);
  1001. return basis;
  1002. }