Basis.cpp 15 KB

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