Basis.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include "Basis.hpp"
  2. #include "Defs.hpp"
  3. #include "Vector3.hpp"
  4. #include "Quat.hpp"
  5. #include <algorithm>
  6. namespace godot {
  7. Basis::Basis(const Vector3& row0, const Vector3& row1, const Vector3& row2)
  8. {
  9. elements[0]=row0;
  10. elements[1]=row1;
  11. elements[2]=row2;
  12. }
  13. Basis::Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
  14. set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  15. }
  16. Basis::Basis() {
  17. elements[0][0]=1;
  18. elements[0][1]=0;
  19. elements[0][2]=0;
  20. elements[1][0]=0;
  21. elements[1][1]=1;
  22. elements[1][2]=0;
  23. elements[2][0]=0;
  24. elements[2][1]=0;
  25. elements[2][2]=1;
  26. }
  27. const Vector3& Basis::operator[](int axis) const {
  28. return elements[axis];
  29. }
  30. Vector3&Basis:: operator[](int axis) {
  31. return elements[axis];
  32. }
  33. #define cofac(row1,col1, row2, col2)\
  34. (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
  35. void Basis::invert()
  36. {
  37. real_t co[3]={
  38. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  39. };
  40. real_t det = elements[0][0] * co[0]+
  41. elements[0][1] * co[1]+
  42. elements[0][2] * co[2];
  43. if ( det != 0 ) {
  44. // WTF
  45. __builtin_trap(); // WTF WTF WTF
  46. // I shouldn't do this
  47. // @Todo @Fixme @Todo @Todo
  48. }
  49. real_t s = 1.0/det;
  50. set( co[0]*s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  51. co[1]*s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  52. co[2]*s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s );
  53. }
  54. #undef cofac
  55. bool Basis::isequal_approx(const Basis& a, const Basis& b) const {
  56. for (int i=0;i<3;i++) {
  57. for (int j=0;j<3;j++) {
  58. if ((::fabs(a.elements[i][j]-b.elements[i][j]) < CMP_EPSILON) == false)
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. bool Basis::is_orthogonal() const
  65. {
  66. Basis id;
  67. Basis m = (*this)*transposed();
  68. return isequal_approx(id,m);
  69. }
  70. bool Basis::is_rotation() const
  71. {
  72. return ::fabs(determinant()-1) < CMP_EPSILON && is_orthogonal();
  73. }
  74. void Basis::transpose()
  75. {
  76. std::swap(elements[0][1],elements[1][0]);
  77. std::swap(elements[0][2],elements[2][0]);
  78. std::swap(elements[1][2],elements[2][1]);
  79. }
  80. Basis Basis::inverse() const
  81. {
  82. Basis b = *this;
  83. b.invert();
  84. return b;
  85. }
  86. Basis Basis::transposed() const
  87. {
  88. Basis b = *this;
  89. b.transpose();
  90. return b;
  91. }
  92. real_t Basis::determinant() const
  93. {
  94. return elements[0][0]*(elements[1][1]*elements[2][2] - elements[2][1]*elements[1][2]) -
  95. elements[1][0]*(elements[0][1]*elements[2][2] - elements[2][1]*elements[0][2]) +
  96. elements[2][0]*(elements[0][1]*elements[1][2] - elements[1][1]*elements[0][2]);
  97. }
  98. Vector3 Basis::get_axis(int p_axis) const {
  99. // get actual basis axis (elements is transposed for performance)
  100. return Vector3( elements[0][p_axis], elements[1][p_axis], elements[2][p_axis] );
  101. }
  102. void Basis::set_axis(int p_axis, const Vector3& p_value) {
  103. // get actual basis axis (elements is transposed for performance)
  104. elements[0][p_axis]=p_value.x;
  105. elements[1][p_axis]=p_value.y;
  106. elements[2][p_axis]=p_value.z;
  107. }
  108. void Basis::rotate(const Vector3& p_axis, real_t p_phi)
  109. {
  110. *this = rotated(p_axis, p_phi);
  111. }
  112. Basis Basis::rotated(const Vector3& p_axis, real_t p_phi) const
  113. {
  114. return Basis(p_axis, p_phi) * (*this);
  115. }
  116. void Basis::scale( const Vector3& p_scale )
  117. {
  118. elements[0][0]*=p_scale.x;
  119. elements[0][1]*=p_scale.x;
  120. elements[0][2]*=p_scale.x;
  121. elements[1][0]*=p_scale.y;
  122. elements[1][1]*=p_scale.y;
  123. elements[1][2]*=p_scale.y;
  124. elements[2][0]*=p_scale.z;
  125. elements[2][1]*=p_scale.z;
  126. elements[2][2]*=p_scale.z;
  127. }
  128. Basis Basis::scaled( const Vector3& p_scale ) const
  129. {
  130. Basis b = *this;
  131. b.scale(p_scale);
  132. return b;
  133. }
  134. Vector3 Basis::get_scale() const
  135. {
  136. // We are assuming M = R.S, and performing a polar decomposition to extract R and S.
  137. // FIXME: We eventually need a proper polar decomposition.
  138. // As a cheap workaround until then, to ensure that R is a proper rotation matrix with determinant +1
  139. // (such that it can be represented by a Quat or Euler angles), we absorb the sign flip into the scaling matrix.
  140. // As such, it works in conjuction with get_rotation().
  141. real_t det_sign = determinant() > 0 ? 1 : -1;
  142. return det_sign*Vector3(
  143. Vector3(elements[0][0],elements[1][0],elements[2][0]).length(),
  144. Vector3(elements[0][1],elements[1][1],elements[2][1]).length(),
  145. Vector3(elements[0][2],elements[1][2],elements[2][2]).length()
  146. );
  147. }
  148. Vector3 Basis::get_euler() const
  149. {
  150. // Euler angles in XYZ convention.
  151. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  152. //
  153. // rot = cy*cz -cy*sz sy
  154. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  155. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  156. Vector3 euler;
  157. if (is_rotation() == false)
  158. return euler;
  159. euler.y = ::asin(elements[0][2]);
  160. if ( euler.y < Math_PI*0.5) {
  161. if ( euler.y > -Math_PI*0.5) {
  162. euler.x = ::atan2(-elements[1][2],elements[2][2]);
  163. euler.z = ::atan2(-elements[0][1],elements[0][0]);
  164. } else {
  165. real_t r = ::atan2(elements[1][0],elements[1][1]);
  166. euler.z = 0.0;
  167. euler.x = euler.z - r;
  168. }
  169. } else {
  170. real_t r = ::atan2(elements[0][1],elements[1][1]);
  171. euler.z = 0;
  172. euler.x = r - euler.z;
  173. }
  174. return euler;
  175. }
  176. void Basis::set_euler(const Vector3& p_euler)
  177. {
  178. real_t c, s;
  179. c = ::cos(p_euler.x);
  180. s = ::sin(p_euler.x);
  181. Basis xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
  182. c = ::cos(p_euler.y);
  183. s = ::sin(p_euler.y);
  184. Basis ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
  185. c = ::cos(p_euler.z);
  186. s = ::sin(p_euler.z);
  187. Basis zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
  188. //optimizer will optimize away all this anyway
  189. *this = xmat*(ymat*zmat);
  190. }
  191. // transposed dot products
  192. real_t Basis::tdotx(const Vector3& v) const {
  193. return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
  194. }
  195. real_t Basis::tdoty(const Vector3& v) const {
  196. return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
  197. }
  198. real_t Basis::tdotz(const Vector3& v) const {
  199. return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
  200. }
  201. bool Basis::operator==(const Basis& p_matrix) const
  202. {
  203. for (int i=0;i<3;i++) {
  204. for (int j=0;j<3;j++) {
  205. if (elements[i][j] != p_matrix.elements[i][j])
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. bool Basis::operator!=(const Basis& p_matrix) const
  212. {
  213. return (!(*this==p_matrix));
  214. }
  215. Vector3 Basis::xform(const Vector3& p_vector) const {
  216. return Vector3(
  217. elements[0].dot(p_vector),
  218. elements[1].dot(p_vector),
  219. elements[2].dot(p_vector)
  220. );
  221. }
  222. Vector3 Basis::xform_inv(const Vector3& p_vector) const {
  223. return Vector3(
  224. (elements[0][0]*p_vector.x ) + ( elements[1][0]*p_vector.y ) + ( elements[2][0]*p_vector.z ),
  225. (elements[0][1]*p_vector.x ) + ( elements[1][1]*p_vector.y ) + ( elements[2][1]*p_vector.z ),
  226. (elements[0][2]*p_vector.x ) + ( elements[1][2]*p_vector.y ) + ( elements[2][2]*p_vector.z )
  227. );
  228. }
  229. void Basis::operator*=(const Basis& p_matrix)
  230. {
  231. set(
  232. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  233. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  234. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  235. }
  236. Basis Basis::operator*(const Basis& p_matrix) const
  237. {
  238. return Basis(
  239. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  240. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  241. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]) );
  242. }
  243. void Basis::operator+=(const Basis& p_matrix) {
  244. elements[0] += p_matrix.elements[0];
  245. elements[1] += p_matrix.elements[1];
  246. elements[2] += p_matrix.elements[2];
  247. }
  248. Basis Basis::operator+(const Basis& p_matrix) const {
  249. Basis ret(*this);
  250. ret += p_matrix;
  251. return ret;
  252. }
  253. void Basis::operator-=(const Basis& p_matrix) {
  254. elements[0] -= p_matrix.elements[0];
  255. elements[1] -= p_matrix.elements[1];
  256. elements[2] -= p_matrix.elements[2];
  257. }
  258. Basis Basis::operator-(const Basis& p_matrix) const {
  259. Basis ret(*this);
  260. ret -= p_matrix;
  261. return ret;
  262. }
  263. void Basis::operator*=(real_t p_val) {
  264. elements[0]*=p_val;
  265. elements[1]*=p_val;
  266. elements[2]*=p_val;
  267. }
  268. Basis Basis::operator*(real_t p_val) const {
  269. Basis ret(*this);
  270. ret *= p_val;
  271. return ret;
  272. }
  273. Basis::operator String() const
  274. {
  275. String s;
  276. // @Todo
  277. return s;
  278. }
  279. /* create / set */
  280. void Basis::set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
  281. elements[0][0]=xx;
  282. elements[0][1]=xy;
  283. elements[0][2]=xz;
  284. elements[1][0]=yx;
  285. elements[1][1]=yy;
  286. elements[1][2]=yz;
  287. elements[2][0]=zx;
  288. elements[2][1]=zy;
  289. elements[2][2]=zz;
  290. }
  291. Vector3 Basis::get_column(int i) const {
  292. return Vector3(elements[0][i],elements[1][i],elements[2][i]);
  293. }
  294. Vector3 Basis::get_row(int i) const {
  295. return Vector3(elements[i][0],elements[i][1],elements[i][2]);
  296. }
  297. Vector3 Basis::get_main_diagonal() const {
  298. return Vector3(elements[0][0],elements[1][1],elements[2][2]);
  299. }
  300. void Basis::set_row(int i, const Vector3& p_row) {
  301. elements[i][0]=p_row.x;
  302. elements[i][1]=p_row.y;
  303. elements[i][2]=p_row.z;
  304. }
  305. Basis Basis::transpose_xform(const Basis& m) const
  306. {
  307. return Basis(
  308. elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
  309. elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
  310. elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
  311. elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
  312. elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
  313. elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
  314. elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
  315. elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
  316. elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
  317. }
  318. void Basis::orthonormalize()
  319. {
  320. if (determinant() != 0) {
  321. // not this crap again
  322. __builtin_trap(); // WTF WTF WTF
  323. // somebody please complain some day
  324. // so I can fix this
  325. // need propert error reporting here.
  326. }
  327. // Gram-Schmidt Process
  328. Vector3 x=get_axis(0);
  329. Vector3 y=get_axis(1);
  330. Vector3 z=get_axis(2);
  331. x.normalize();
  332. y = (y-x*(x.dot(y)));
  333. y.normalize();
  334. z = (z-x*(x.dot(z))-y*(y.dot(z)));
  335. z.normalize();
  336. set_axis(0,x);
  337. set_axis(1,y);
  338. set_axis(2,z);
  339. }
  340. Basis Basis::orthonormalized() const
  341. {
  342. Basis b = *this;
  343. b.orthonormalize();
  344. return b;
  345. }
  346. bool Basis::is_symmetric() const
  347. {
  348. if (::fabs(elements[0][1] - elements[1][0]) > CMP_EPSILON)
  349. return false;
  350. if (::fabs(elements[0][2] - elements[2][0]) > CMP_EPSILON)
  351. return false;
  352. if (::fabs(elements[1][2] - elements[2][1]) > CMP_EPSILON)
  353. return false;
  354. return true;
  355. }
  356. Basis Basis::diagonalize()
  357. {
  358. // I love copy paste
  359. if (!is_symmetric())
  360. return Basis();
  361. const int ite_max = 1024;
  362. real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
  363. int ite = 0;
  364. Basis acc_rot;
  365. while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max ) {
  366. real_t el01_2 = elements[0][1] * elements[0][1];
  367. real_t el02_2 = elements[0][2] * elements[0][2];
  368. real_t el12_2 = elements[1][2] * elements[1][2];
  369. // Find the pivot element
  370. int i, j;
  371. if (el01_2 > el02_2) {
  372. if (el12_2 > el01_2) {
  373. i = 1;
  374. j = 2;
  375. } else {
  376. i = 0;
  377. j = 1;
  378. }
  379. } else {
  380. if (el12_2 > el02_2) {
  381. i = 1;
  382. j = 2;
  383. } else {
  384. i = 0;
  385. j = 2;
  386. }
  387. }
  388. // Compute the rotation angle
  389. real_t angle;
  390. if (::fabs(elements[j][j] - elements[i][i]) < CMP_EPSILON) {
  391. angle = Math_PI / 4;
  392. } else {
  393. angle = 0.5 * ::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
  394. }
  395. // Compute the rotation matrix
  396. Basis rot;
  397. rot.elements[i][i] = rot.elements[j][j] = ::cos(angle);
  398. rot.elements[i][j] = - (rot.elements[j][i] = ::sin(angle));
  399. // Update the off matrix norm
  400. off_matrix_norm_2 -= elements[i][j] * elements[i][j];
  401. // Apply the rotation
  402. *this = rot * *this * rot.transposed();
  403. acc_rot = rot * acc_rot;
  404. }
  405. return acc_rot;
  406. }
  407. static const Basis _ortho_bases[24]={
  408. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  409. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  410. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  411. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  412. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  413. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  414. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  415. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  416. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  417. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  418. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  419. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  420. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  421. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  422. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  423. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  424. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  425. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  426. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  427. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  428. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  429. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  430. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  431. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  432. };
  433. int Basis::get_orthogonal_index() const
  434. {
  435. //could be sped up if i come up with a way
  436. Basis orth=*this;
  437. for(int i=0;i<3;i++) {
  438. for(int j=0;j<3;j++) {
  439. real_t v = orth[i][j];
  440. if (v>0.5)
  441. v=1.0;
  442. else if (v<-0.5)
  443. v=-1.0;
  444. else
  445. v=0;
  446. orth[i][j]=v;
  447. }
  448. }
  449. for(int i=0;i<24;i++) {
  450. if (_ortho_bases[i]==orth)
  451. return i;
  452. }
  453. return 0;
  454. }
  455. void Basis::set_orthogonal_index(int p_index){
  456. //there only exist 24 orthogonal bases in r3
  457. if (p_index >= 24) {
  458. __builtin_trap(); // kiiiiill me
  459. // I don't want to do shady stuff like that
  460. // @Todo WTF WTF
  461. }
  462. *this=_ortho_bases[p_index];
  463. }
  464. Basis::Basis(const Vector3& p_euler) {
  465. set_euler( p_euler );
  466. }
  467. }
  468. #include "Quat.hpp"
  469. namespace godot {
  470. Basis::Basis(const Quat& p_quat) {
  471. real_t d = p_quat.length_squared();
  472. real_t s = 2.0 / d;
  473. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  474. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  475. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  476. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  477. set( 1.0 - (yy + zz), xy - wz, xz + wy,
  478. xy + wz, 1.0 - (xx + zz), yz - wx,
  479. xz - wy, yz + wx, 1.0 - (xx + yy)) ;
  480. }
  481. Basis::Basis(const Vector3& p_axis, real_t p_phi) {
  482. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
  483. Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
  484. real_t cosine= ::cos(p_phi);
  485. real_t sine= ::sin(p_phi);
  486. elements[0][0] = axis_sq.x + cosine * ( 1.0 - axis_sq.x );
  487. elements[0][1] = p_axis.x * p_axis.y * ( 1.0 - cosine ) - p_axis.z * sine;
  488. elements[0][2] = p_axis.z * p_axis.x * ( 1.0 - cosine ) + p_axis.y * sine;
  489. elements[1][0] = p_axis.x * p_axis.y * ( 1.0 - cosine ) + p_axis.z * sine;
  490. elements[1][1] = axis_sq.y + cosine * ( 1.0 - axis_sq.y );
  491. elements[1][2] = p_axis.y * p_axis.z * ( 1.0 - cosine ) - p_axis.x * sine;
  492. elements[2][0] = p_axis.z * p_axis.x * ( 1.0 - cosine ) - p_axis.y * sine;
  493. elements[2][1] = p_axis.y * p_axis.z * ( 1.0 - cosine ) + p_axis.x * sine;
  494. elements[2][2] = axis_sq.z + cosine * ( 1.0 - axis_sq.z );
  495. }
  496. Basis::operator Quat() const {
  497. ERR_FAIL_COND_V(is_rotation() == false, Quat());
  498. real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
  499. real_t temp[4];
  500. if (trace > 0.0)
  501. {
  502. real_t s = ::sqrt(trace + 1.0);
  503. temp[3]=(s * 0.5);
  504. s = 0.5 / s;
  505. temp[0]=((elements[2][1] - elements[1][2]) * s);
  506. temp[1]=((elements[0][2] - elements[2][0]) * s);
  507. temp[2]=((elements[1][0] - elements[0][1]) * s);
  508. }
  509. else
  510. {
  511. int i = elements[0][0] < elements[1][1] ?
  512. (elements[1][1] < elements[2][2] ? 2 : 1) :
  513. (elements[0][0] < elements[2][2] ? 2 : 0);
  514. int j = (i + 1) % 3;
  515. int k = (i + 2) % 3;
  516. real_t s = ::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0);
  517. temp[i] = s * 0.5;
  518. s = 0.5 / s;
  519. temp[3] = (elements[k][j] - elements[j][k]) * s;
  520. temp[j] = (elements[j][i] + elements[i][j]) * s;
  521. temp[k] = (elements[k][i] + elements[i][k]) * s;
  522. }
  523. return Quat(temp[0],temp[1],temp[2],temp[3]);
  524. }
  525. }