basis.cpp 35 KB

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