matrix3.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*************************************************************************/
  2. /* matrix3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "matrix3.h"
  30. #include "math_funcs.h"
  31. #include "os/copymem.h"
  32. #define cofac(row1, col1, row2, col2) \
  33. (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
  34. void Basis::from_z(const Vector3 &p_z) {
  35. if (Math::abs(p_z.z) > Math_SQRT12) {
  36. // choose p in y-z plane
  37. real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
  38. real_t k = 1.0 / Math::sqrt(a);
  39. elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
  40. elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
  41. } else {
  42. // choose p in x-y plane
  43. real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
  44. real_t k = 1.0 / Math::sqrt(a);
  45. elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
  46. elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
  47. }
  48. elements[2] = p_z;
  49. }
  50. void Basis::invert() {
  51. real_t co[3] = {
  52. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  53. };
  54. real_t det = elements[0][0] * co[0] +
  55. elements[0][1] * co[1] +
  56. elements[0][2] * co[2];
  57. ERR_FAIL_COND(det == 0);
  58. real_t s = 1.0 / det;
  59. set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  60. co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  61. co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  62. }
  63. void Basis::orthonormalize() {
  64. ERR_FAIL_COND(determinant() == 0);
  65. // Gram-Schmidt Process
  66. Vector3 x = get_axis(0);
  67. Vector3 y = get_axis(1);
  68. Vector3 z = get_axis(2);
  69. x.normalize();
  70. y = (y - x * (x.dot(y)));
  71. y.normalize();
  72. z = (z - x * (x.dot(z)) - y * (y.dot(z)));
  73. z.normalize();
  74. set_axis(0, x);
  75. set_axis(1, y);
  76. set_axis(2, z);
  77. }
  78. Basis Basis::orthonormalized() const {
  79. Basis c = *this;
  80. c.orthonormalize();
  81. return c;
  82. }
  83. bool Basis::is_orthogonal() const {
  84. Basis id;
  85. Basis m = (*this) * transposed();
  86. return isequal_approx(id, m);
  87. }
  88. bool Basis::is_rotation() const {
  89. return Math::isequal_approx(determinant(), 1) && is_orthogonal();
  90. }
  91. bool Basis::is_symmetric() const {
  92. if (Math::abs(elements[0][1] - elements[1][0]) > CMP_EPSILON)
  93. return false;
  94. if (Math::abs(elements[0][2] - elements[2][0]) > CMP_EPSILON)
  95. return false;
  96. if (Math::abs(elements[1][2] - elements[2][1]) > CMP_EPSILON)
  97. return false;
  98. return true;
  99. }
  100. Basis Basis::diagonalize() {
  101. //NOTE: only implemented for symmetric matrices
  102. //with the Jacobi iterative method method
  103. ERR_FAIL_COND_V(!is_symmetric(), Basis());
  104. const int ite_max = 1024;
  105. real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
  106. int ite = 0;
  107. Basis acc_rot;
  108. while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
  109. real_t el01_2 = elements[0][1] * elements[0][1];
  110. real_t el02_2 = elements[0][2] * elements[0][2];
  111. real_t el12_2 = elements[1][2] * elements[1][2];
  112. // Find the pivot element
  113. int i, j;
  114. if (el01_2 > el02_2) {
  115. if (el12_2 > el01_2) {
  116. i = 1;
  117. j = 2;
  118. } else {
  119. i = 0;
  120. j = 1;
  121. }
  122. } else {
  123. if (el12_2 > el02_2) {
  124. i = 1;
  125. j = 2;
  126. } else {
  127. i = 0;
  128. j = 2;
  129. }
  130. }
  131. // Compute the rotation angle
  132. real_t angle;
  133. if (Math::abs(elements[j][j] - elements[i][i]) < CMP_EPSILON) {
  134. angle = Math_PI / 4;
  135. } else {
  136. angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
  137. }
  138. // Compute the rotation matrix
  139. Basis rot;
  140. rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle);
  141. rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle));
  142. // Update the off matrix norm
  143. off_matrix_norm_2 -= elements[i][j] * elements[i][j];
  144. // Apply the rotation
  145. *this = rot * *this * rot.transposed();
  146. acc_rot = rot * acc_rot;
  147. }
  148. return acc_rot;
  149. }
  150. Basis Basis::inverse() const {
  151. Basis inv = *this;
  152. inv.invert();
  153. return inv;
  154. }
  155. void Basis::transpose() {
  156. SWAP(elements[0][1], elements[1][0]);
  157. SWAP(elements[0][2], elements[2][0]);
  158. SWAP(elements[1][2], elements[2][1]);
  159. }
  160. Basis Basis::transposed() const {
  161. Basis tr = *this;
  162. tr.transpose();
  163. return tr;
  164. }
  165. // Multiplies the matrix from left by the scaling matrix: M -> S.M
  166. // See the comment for Basis::rotated for further explanation.
  167. void Basis::scale(const Vector3 &p_scale) {
  168. elements[0][0] *= p_scale.x;
  169. elements[0][1] *= p_scale.x;
  170. elements[0][2] *= p_scale.x;
  171. elements[1][0] *= p_scale.y;
  172. elements[1][1] *= p_scale.y;
  173. elements[1][2] *= p_scale.y;
  174. elements[2][0] *= p_scale.z;
  175. elements[2][1] *= p_scale.z;
  176. elements[2][2] *= p_scale.z;
  177. }
  178. Basis Basis::scaled(const Vector3 &p_scale) const {
  179. Basis m = *this;
  180. m.scale(p_scale);
  181. return m;
  182. }
  183. Vector3 Basis::get_scale() const {
  184. // We are assuming M = R.S, and performing a polar decomposition to extract R and S.
  185. // FIXME: We eventually need a proper polar decomposition.
  186. // As a cheap workaround until then, to ensure that R is a proper rotation matrix with determinant +1
  187. // (such that it can be represented by a Quat or Euler angles), we absorb the sign flip into the scaling matrix.
  188. // As such, it works in conjunction with get_rotation().
  189. real_t det_sign = determinant() > 0 ? 1 : -1;
  190. return det_sign * Vector3(
  191. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  192. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  193. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  194. }
  195. // Multiplies the matrix from left by the rotation matrix: M -> R.M
  196. // Note that this does *not* rotate the matrix itself.
  197. //
  198. // The main use of Basis is as Transform.basis, which is used a the transformation matrix
  199. // of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
  200. // not the matrix itself (which is R * (*this) * R.transposed()).
  201. Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const {
  202. return Basis(p_axis, p_phi) * (*this);
  203. }
  204. void Basis::rotate(const Vector3 &p_axis, real_t p_phi) {
  205. *this = rotated(p_axis, p_phi);
  206. }
  207. Basis Basis::rotated(const Vector3 &p_euler) const {
  208. return Basis(p_euler) * (*this);
  209. }
  210. void Basis::rotate(const Vector3 &p_euler) {
  211. *this = rotated(p_euler);
  212. }
  213. Vector3 Basis::get_rotation() const {
  214. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  215. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  216. // See the comment in get_scale() for further information.
  217. Basis m = orthonormalized();
  218. real_t det = m.determinant();
  219. if (det < 0) {
  220. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  221. m.scale(Vector3(-1, -1, -1));
  222. }
  223. return m.get_euler();
  224. }
  225. // get_euler returns a vector containing the Euler angles in the format
  226. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last
  227. // (following the convention they are commonly defined in the literature).
  228. //
  229. // The current implementation uses XYZ convention (Z is the first rotation),
  230. // so euler.z is the angle of the (first) rotation around Z axis and so on,
  231. //
  232. // And thus, assuming the matrix is a rotation matrix, this function returns
  233. // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates
  234. // around the z-axis by a and so on.
  235. Vector3 Basis::get_euler() const {
  236. // Euler angles in XYZ convention.
  237. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  238. //
  239. // rot = cy*cz -cy*sz sy
  240. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  241. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  242. Vector3 euler;
  243. ERR_FAIL_COND_V(is_rotation() == false, euler);
  244. euler.y = Math::asin(elements[0][2]);
  245. if (euler.y < Math_PI * 0.5) {
  246. if (euler.y > -Math_PI * 0.5) {
  247. euler.x = Math::atan2(-elements[1][2], elements[2][2]);
  248. euler.z = Math::atan2(-elements[0][1], elements[0][0]);
  249. } else {
  250. real_t r = Math::atan2(elements[1][0], elements[1][1]);
  251. euler.z = 0.0;
  252. euler.x = euler.z - r;
  253. }
  254. } else {
  255. real_t r = Math::atan2(elements[0][1], elements[1][1]);
  256. euler.z = 0;
  257. euler.x = r - euler.z;
  258. }
  259. return euler;
  260. }
  261. // set_euler expects a vector containing the Euler angles in the format
  262. // (c,b,a), where a is the angle of the first rotation, and c is the last.
  263. // The current implementation uses XYZ convention (Z is the first rotation).
  264. void Basis::set_euler(const Vector3 &p_euler) {
  265. real_t c, s;
  266. c = Math::cos(p_euler.x);
  267. s = Math::sin(p_euler.x);
  268. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  269. c = Math::cos(p_euler.y);
  270. s = Math::sin(p_euler.y);
  271. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  272. c = Math::cos(p_euler.z);
  273. s = Math::sin(p_euler.z);
  274. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  275. //optimizer will optimize away all this anyway
  276. *this = xmat * (ymat * zmat);
  277. }
  278. bool Basis::isequal_approx(const Basis &a, const Basis &b) const {
  279. for (int i = 0; i < 3; i++) {
  280. for (int j = 0; j < 3; j++) {
  281. if (Math::isequal_approx(a.elements[i][j], b.elements[i][j]) == false)
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. bool Basis::operator==(const Basis &p_matrix) const {
  288. for (int i = 0; i < 3; i++) {
  289. for (int j = 0; j < 3; j++) {
  290. if (elements[i][j] != p_matrix.elements[i][j])
  291. return false;
  292. }
  293. }
  294. return true;
  295. }
  296. bool Basis::operator!=(const Basis &p_matrix) const {
  297. return (!(*this == p_matrix));
  298. }
  299. Basis::operator String() const {
  300. String mtx;
  301. for (int i = 0; i < 3; i++) {
  302. for (int j = 0; j < 3; j++) {
  303. if (i != 0 || j != 0)
  304. mtx += ", ";
  305. mtx += rtos(elements[i][j]);
  306. }
  307. }
  308. return mtx;
  309. }
  310. Basis::operator Quat() const {
  311. ERR_FAIL_COND_V(is_rotation() == false, Quat());
  312. real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
  313. real_t temp[4];
  314. if (trace > 0.0) {
  315. real_t s = Math::sqrt(trace + 1.0);
  316. temp[3] = (s * 0.5);
  317. s = 0.5 / s;
  318. temp[0] = ((elements[2][1] - elements[1][2]) * s);
  319. temp[1] = ((elements[0][2] - elements[2][0]) * s);
  320. temp[2] = ((elements[1][0] - elements[0][1]) * s);
  321. } else {
  322. int i = elements[0][0] < elements[1][1] ?
  323. (elements[1][1] < elements[2][2] ? 2 : 1) :
  324. (elements[0][0] < elements[2][2] ? 2 : 0);
  325. int j = (i + 1) % 3;
  326. int k = (i + 2) % 3;
  327. real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0);
  328. temp[i] = s * 0.5;
  329. s = 0.5 / s;
  330. temp[3] = (elements[k][j] - elements[j][k]) * s;
  331. temp[j] = (elements[j][i] + elements[i][j]) * s;
  332. temp[k] = (elements[k][i] + elements[i][k]) * s;
  333. }
  334. return Quat(temp[0], temp[1], temp[2], temp[3]);
  335. }
  336. static const Basis _ortho_bases[24] = {
  337. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  338. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  339. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  340. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  341. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  342. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  343. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  344. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  345. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  346. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  347. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  348. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  349. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  350. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  351. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  352. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  353. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  354. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  355. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  356. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  357. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  358. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  359. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  360. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  361. };
  362. int Basis::get_orthogonal_index() const {
  363. //could be sped up if i come up with a way
  364. Basis orth = *this;
  365. for (int i = 0; i < 3; i++) {
  366. for (int j = 0; j < 3; j++) {
  367. real_t v = orth[i][j];
  368. if (v > 0.5)
  369. v = 1.0;
  370. else if (v < -0.5)
  371. v = -1.0;
  372. else
  373. v = 0;
  374. orth[i][j] = v;
  375. }
  376. }
  377. for (int i = 0; i < 24; i++) {
  378. if (_ortho_bases[i] == orth)
  379. return i;
  380. }
  381. return 0;
  382. }
  383. void Basis::set_orthogonal_index(int p_index) {
  384. //there only exist 24 orthogonal bases in r3
  385. ERR_FAIL_INDEX(p_index, 24);
  386. *this = _ortho_bases[p_index];
  387. }
  388. void Basis::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
  389. ERR_FAIL_COND(is_rotation() == false);
  390. real_t angle, x, y, z; // variables for result
  391. real_t epsilon = 0.01; // margin to allow for rounding errors
  392. real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  393. 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)) {
  394. // singularity found
  395. // first check for identity matrix which must have +1 for all terms
  396. // in leading diagonaland zero in other terms
  397. 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)) {
  398. // this singularity is identity matrix so angle = 0
  399. r_axis = Vector3(0, 1, 0);
  400. r_angle = 0;
  401. return;
  402. }
  403. // otherwise this singularity is angle = 180
  404. angle = Math_PI;
  405. real_t xx = (elements[0][0] + 1) / 2;
  406. real_t yy = (elements[1][1] + 1) / 2;
  407. real_t zz = (elements[2][2] + 1) / 2;
  408. real_t xy = (elements[1][0] + elements[0][1]) / 4;
  409. real_t xz = (elements[2][0] + elements[0][2]) / 4;
  410. real_t yz = (elements[2][1] + elements[1][2]) / 4;
  411. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  412. if (xx < epsilon) {
  413. x = 0;
  414. y = 0.7071;
  415. z = 0.7071;
  416. } else {
  417. x = Math::sqrt(xx);
  418. y = xy / x;
  419. z = xz / x;
  420. }
  421. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  422. if (yy < epsilon) {
  423. x = 0.7071;
  424. y = 0;
  425. z = 0.7071;
  426. } else {
  427. y = Math::sqrt(yy);
  428. x = xy / y;
  429. z = yz / y;
  430. }
  431. } else { // elements[2][2] is the largest diagonal term so base result on this
  432. if (zz < epsilon) {
  433. x = 0.7071;
  434. y = 0.7071;
  435. z = 0;
  436. } else {
  437. z = Math::sqrt(zz);
  438. x = xz / z;
  439. y = yz / z;
  440. }
  441. }
  442. r_axis = Vector3(x, y, z);
  443. r_angle = angle;
  444. return;
  445. }
  446. // as we have reached here there are no singularities so we can handle normally
  447. 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
  448. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  449. if (angle < 0) s = -s;
  450. x = (elements[2][1] - elements[1][2]) / s;
  451. y = (elements[0][2] - elements[2][0]) / s;
  452. z = (elements[1][0] - elements[0][1]) / s;
  453. r_axis = Vector3(x, y, z);
  454. r_angle = angle;
  455. }
  456. Basis::Basis(const Vector3 &p_euler) {
  457. set_euler(p_euler);
  458. }
  459. Basis::Basis(const Quat &p_quat) {
  460. real_t d = p_quat.length_squared();
  461. real_t s = 2.0 / d;
  462. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  463. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  464. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  465. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  466. set(1.0 - (yy + zz), xy - wz, xz + wy,
  467. xy + wz, 1.0 - (xx + zz), yz - wx,
  468. xz - wy, yz + wx, 1.0 - (xx + yy));
  469. }
  470. Basis::Basis(const Vector3 &p_axis, real_t p_phi) {
  471. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
  472. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  473. real_t cosine = Math::cos(p_phi);
  474. real_t sine = Math::sin(p_phi);
  475. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  476. elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine;
  477. elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine;
  478. elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine;
  479. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  480. elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine;
  481. elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine;
  482. elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine;
  483. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  484. }