matrix3.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 Matrix3::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 Matrix3::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 Matrix3::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. Matrix3 Matrix3::orthonormalized() const {
  79. Matrix3 c = *this;
  80. c.orthonormalize();
  81. return c;
  82. }
  83. bool Matrix3::is_orthogonal() const {
  84. Matrix3 id;
  85. Matrix3 m = (*this)*transposed();
  86. return isequal_approx(id,m);
  87. }
  88. bool Matrix3::is_rotation() const {
  89. return Math::isequal_approx(determinant(), 1) && is_orthogonal();
  90. }
  91. Matrix3 Matrix3::inverse() const {
  92. Matrix3 inv=*this;
  93. inv.invert();
  94. return inv;
  95. }
  96. void Matrix3::transpose() {
  97. SWAP(elements[0][1],elements[1][0]);
  98. SWAP(elements[0][2],elements[2][0]);
  99. SWAP(elements[1][2],elements[2][1]);
  100. }
  101. Matrix3 Matrix3::transposed() const {
  102. Matrix3 tr=*this;
  103. tr.transpose();
  104. return tr;
  105. }
  106. void Matrix3::scale(const Vector3& p_scale) {
  107. elements[0][0]*=p_scale.x;
  108. elements[1][0]*=p_scale.x;
  109. elements[2][0]*=p_scale.x;
  110. elements[0][1]*=p_scale.y;
  111. elements[1][1]*=p_scale.y;
  112. elements[2][1]*=p_scale.y;
  113. elements[0][2]*=p_scale.z;
  114. elements[1][2]*=p_scale.z;
  115. elements[2][2]*=p_scale.z;
  116. }
  117. Matrix3 Matrix3::scaled( const Vector3& p_scale ) const {
  118. Matrix3 m = *this;
  119. m.scale(p_scale);
  120. return m;
  121. }
  122. Vector3 Matrix3::get_scale() const {
  123. return Vector3(
  124. Vector3(elements[0][0],elements[1][0],elements[2][0]).length(),
  125. Vector3(elements[0][1],elements[1][1],elements[2][1]).length(),
  126. Vector3(elements[0][2],elements[1][2],elements[2][2]).length()
  127. );
  128. }
  129. // Matrix3::rotate and Matrix3::rotated return M * R(axis,phi), and is a convenience function. They do *not* perform proper matrix rotation.
  130. void Matrix3::rotate(const Vector3& p_axis, real_t p_phi) {
  131. // TODO: This function should also be renamed as the current name is misleading: rotate does *not* perform matrix rotation.
  132. // Same problem affects Matrix3::rotated.
  133. // A similar problem exists in 2D math, which will be handled separately.
  134. // After Matrix3 is renamed to Basis, this comments needs to be revised.
  135. *this = *this * Matrix3(p_axis, p_phi);
  136. }
  137. Matrix3 Matrix3::rotated(const Vector3& p_axis, real_t p_phi) const {
  138. return *this * Matrix3(p_axis, p_phi);
  139. }
  140. // get_euler returns a vector containing the Euler angles in the format
  141. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last
  142. // (following the convention they are commonly defined in the literature).
  143. //
  144. // The current implementation uses XYZ convention (Z is the first rotation),
  145. // so euler.z is the angle of the (first) rotation around Z axis and so on,
  146. //
  147. // And thus, assuming the matrix is a rotation matrix, this function returns
  148. // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates
  149. // around the z-axis by a and so on.
  150. Vector3 Matrix3::get_euler() const {
  151. // Euler angles in XYZ convention.
  152. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  153. //
  154. // rot = cy*cz -cy*sz sy
  155. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  156. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  157. Vector3 euler;
  158. ERR_FAIL_COND_V(is_rotation() == false, euler);
  159. euler.y = Math::asin(elements[0][2]);
  160. if ( euler.y < Math_PI*0.5) {
  161. if ( euler.y > -Math_PI*0.5) {
  162. euler.x = Math::atan2(-elements[1][2],elements[2][2]);
  163. euler.z = Math::atan2(-elements[0][1],elements[0][0]);
  164. } else {
  165. real_t r = Math::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 = Math::atan2(elements[0][1],elements[1][1]);
  171. euler.z = 0;
  172. euler.x = r - euler.z;
  173. }
  174. return euler;
  175. }
  176. // set_euler expects a vector containing the Euler angles in the format
  177. // (c,b,a), where a is the angle of the first rotation, and c is the last.
  178. // The current implementation uses XYZ convention (Z is the first rotation).
  179. void Matrix3::set_euler(const Vector3& p_euler) {
  180. real_t c, s;
  181. c = Math::cos(p_euler.x);
  182. s = Math::sin(p_euler.x);
  183. Matrix3 xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
  184. c = Math::cos(p_euler.y);
  185. s = Math::sin(p_euler.y);
  186. Matrix3 ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
  187. c = Math::cos(p_euler.z);
  188. s = Math::sin(p_euler.z);
  189. Matrix3 zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
  190. //optimizer will optimize away all this anyway
  191. *this = xmat*(ymat*zmat);
  192. }
  193. bool Matrix3::isequal_approx(const Matrix3& a, const Matrix3& b) const {
  194. for (int i=0;i<3;i++) {
  195. for (int j=0;j<3;j++) {
  196. if (Math::isequal_approx(a.elements[i][j],b.elements[i][j]) == false)
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. bool Matrix3::operator==(const Matrix3& p_matrix) const {
  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 Matrix3::operator!=(const Matrix3& p_matrix) const {
  212. return (!(*this==p_matrix));
  213. }
  214. Matrix3::operator String() const {
  215. String mtx;
  216. for (int i=0;i<3;i++) {
  217. for (int j=0;j<3;j++) {
  218. if (i!=0 || j!=0)
  219. mtx+=", ";
  220. mtx+=rtos( elements[i][j] );
  221. }
  222. }
  223. return mtx;
  224. }
  225. Matrix3::operator Quat() const {
  226. ERR_FAIL_COND_V(is_rotation() == false, Quat());
  227. real_t trace = elements[0][0] + elements[1][1] + elements[2][2];
  228. real_t temp[4];
  229. if (trace > 0.0)
  230. {
  231. real_t s = Math::sqrt(trace + 1.0);
  232. temp[3]=(s * 0.5);
  233. s = 0.5 / s;
  234. temp[0]=((elements[2][1] - elements[1][2]) * s);
  235. temp[1]=((elements[0][2] - elements[2][0]) * s);
  236. temp[2]=((elements[1][0] - elements[0][1]) * s);
  237. }
  238. else
  239. {
  240. int i = elements[0][0] < elements[1][1] ?
  241. (elements[1][1] < elements[2][2] ? 2 : 1) :
  242. (elements[0][0] < elements[2][2] ? 2 : 0);
  243. int j = (i + 1) % 3;
  244. int k = (i + 2) % 3;
  245. real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0);
  246. temp[i] = s * 0.5;
  247. s = 0.5 / s;
  248. temp[3] = (elements[k][j] - elements[j][k]) * s;
  249. temp[j] = (elements[j][i] + elements[i][j]) * s;
  250. temp[k] = (elements[k][i] + elements[i][k]) * s;
  251. }
  252. return Quat(temp[0],temp[1],temp[2],temp[3]);
  253. }
  254. static const Matrix3 _ortho_bases[24]={
  255. Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1),
  256. Matrix3(0, -1, 0, 1, 0, 0, 0, 0, 1),
  257. Matrix3(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  258. Matrix3(0, 1, 0, -1, 0, 0, 0, 0, 1),
  259. Matrix3(1, 0, 0, 0, 0, -1, 0, 1, 0),
  260. Matrix3(0, 0, 1, 1, 0, 0, 0, 1, 0),
  261. Matrix3(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  262. Matrix3(0, 0, -1, -1, 0, 0, 0, 1, 0),
  263. Matrix3(1, 0, 0, 0, -1, 0, 0, 0, -1),
  264. Matrix3(0, 1, 0, 1, 0, 0, 0, 0, -1),
  265. Matrix3(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  266. Matrix3(0, -1, 0, -1, 0, 0, 0, 0, -1),
  267. Matrix3(1, 0, 0, 0, 0, 1, 0, -1, 0),
  268. Matrix3(0, 0, -1, 1, 0, 0, 0, -1, 0),
  269. Matrix3(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  270. Matrix3(0, 0, 1, -1, 0, 0, 0, -1, 0),
  271. Matrix3(0, 0, 1, 0, 1, 0, -1, 0, 0),
  272. Matrix3(0, -1, 0, 0, 0, 1, -1, 0, 0),
  273. Matrix3(0, 0, -1, 0, -1, 0, -1, 0, 0),
  274. Matrix3(0, 1, 0, 0, 0, -1, -1, 0, 0),
  275. Matrix3(0, 0, 1, 0, -1, 0, 1, 0, 0),
  276. Matrix3(0, 1, 0, 0, 0, 1, 1, 0, 0),
  277. Matrix3(0, 0, -1, 0, 1, 0, 1, 0, 0),
  278. Matrix3(0, -1, 0, 0, 0, -1, 1, 0, 0)
  279. };
  280. int Matrix3::get_orthogonal_index() const {
  281. //could be sped up if i come up with a way
  282. Matrix3 orth=*this;
  283. for(int i=0;i<3;i++) {
  284. for(int j=0;j<3;j++) {
  285. float v = orth[i][j];
  286. if (v>0.5)
  287. v=1.0;
  288. else if (v<-0.5)
  289. v=-1.0;
  290. else
  291. v=0;
  292. orth[i][j]=v;
  293. }
  294. }
  295. for(int i=0;i<24;i++) {
  296. if (_ortho_bases[i]==orth)
  297. return i;
  298. }
  299. return 0;
  300. }
  301. void Matrix3::set_orthogonal_index(int p_index){
  302. //there only exist 24 orthogonal bases in r3
  303. ERR_FAIL_INDEX(p_index,24);
  304. *this=_ortho_bases[p_index];
  305. }
  306. void Matrix3::get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const {
  307. // TODO: We can handle improper matrices here too, in which case axis will also correspond to the axis of reflection.
  308. // See Eq. (52) in http://scipp.ucsc.edu/~haber/ph251/rotreflect_13.pdf for example
  309. // After that change, we should fail on is_orthogonal() == false.
  310. ERR_FAIL_COND(is_rotation() == false);
  311. double angle,x,y,z; // variables for result
  312. double epsilon = 0.01; // margin to allow for rounding errors
  313. double epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  314. if ( (Math::abs(elements[1][0]-elements[0][1])< epsilon)
  315. && (Math::abs(elements[2][0]-elements[0][2])< epsilon)
  316. && (Math::abs(elements[2][1]-elements[1][2])< epsilon)) {
  317. // singularity found
  318. // first check for identity matrix which must have +1 for all terms
  319. // in leading diagonaland zero in other terms
  320. if ((Math::abs(elements[1][0]+elements[0][1]) < epsilon2)
  321. && (Math::abs(elements[2][0]+elements[0][2]) < epsilon2)
  322. && (Math::abs(elements[2][1]+elements[1][2]) < epsilon2)
  323. && (Math::abs(elements[0][0]+elements[1][1]+elements[2][2]-3) < epsilon2)) {
  324. // this singularity is identity matrix so angle = 0
  325. r_axis=Vector3(0,1,0);
  326. r_angle=0;
  327. return;
  328. }
  329. // otherwise this singularity is angle = 180
  330. angle = Math_PI;
  331. double xx = (elements[0][0]+1)/2;
  332. double yy = (elements[1][1]+1)/2;
  333. double zz = (elements[2][2]+1)/2;
  334. double xy = (elements[1][0]+elements[0][1])/4;
  335. double xz = (elements[2][0]+elements[0][2])/4;
  336. double yz = (elements[2][1]+elements[1][2])/4;
  337. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  338. if (xx< epsilon) {
  339. x = 0;
  340. y = 0.7071;
  341. z = 0.7071;
  342. } else {
  343. x = Math::sqrt(xx);
  344. y = xy/x;
  345. z = xz/x;
  346. }
  347. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  348. if (yy< epsilon) {
  349. x = 0.7071;
  350. y = 0;
  351. z = 0.7071;
  352. } else {
  353. y = Math::sqrt(yy);
  354. x = xy/y;
  355. z = yz/y;
  356. }
  357. } else { // elements[2][2] is the largest diagonal term so base result on this
  358. if (zz< epsilon) {
  359. x = 0.7071;
  360. y = 0.7071;
  361. z = 0;
  362. } else {
  363. z = Math::sqrt(zz);
  364. x = xz/z;
  365. y = yz/z;
  366. }
  367. }
  368. r_axis=Vector3(x,y,z);
  369. r_angle=angle;
  370. return;
  371. }
  372. // as we have reached here there are no singularities so we can handle normally
  373. double s = Math::sqrt((elements[1][2] - elements[2][1])*(elements[1][2] - elements[2][1])
  374. +(elements[2][0] - elements[0][2])*(elements[2][0] - elements[0][2])
  375. +(elements[0][1] - elements[1][0])*(elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise
  376. angle = Math::acos(( elements[0][0] + elements[1][1] + elements[2][2] - 1)/2);
  377. if (angle < 0) s = -s;
  378. x = (elements[2][1] - elements[1][2])/s;
  379. y = (elements[0][2] - elements[2][0])/s;
  380. z = (elements[1][0] - elements[0][1])/s;
  381. r_axis=Vector3(x,y,z);
  382. r_angle=angle;
  383. }
  384. Matrix3::Matrix3(const Vector3& p_euler) {
  385. set_euler( p_euler );
  386. }
  387. Matrix3::Matrix3(const Quat& p_quat) {
  388. real_t d = p_quat.length_squared();
  389. real_t s = 2.0 / d;
  390. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  391. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  392. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  393. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  394. set( 1.0 - (yy + zz), xy - wz, xz + wy,
  395. xy + wz, 1.0 - (xx + zz), yz - wx,
  396. xz - wy, yz + wx, 1.0 - (xx + yy)) ;
  397. }
  398. Matrix3::Matrix3(const Vector3& p_axis, real_t p_phi) {
  399. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
  400. Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
  401. real_t cosine= Math::cos(p_phi);
  402. real_t sine= Math::sin(p_phi);
  403. elements[0][0] = axis_sq.x + cosine * ( 1.0 - axis_sq.x );
  404. elements[0][1] = p_axis.x * p_axis.y * ( 1.0 - cosine ) - p_axis.z * sine;
  405. elements[0][2] = p_axis.z * p_axis.x * ( 1.0 - cosine ) + p_axis.y * sine;
  406. elements[1][0] = p_axis.x * p_axis.y * ( 1.0 - cosine ) + p_axis.z * sine;
  407. elements[1][1] = axis_sq.y + cosine * ( 1.0 - axis_sq.y );
  408. elements[1][2] = p_axis.y * p_axis.z * ( 1.0 - cosine ) - p_axis.x * sine;
  409. elements[2][0] = p_axis.z * p_axis.x * ( 1.0 - cosine ) - p_axis.y * sine;
  410. elements[2][1] = p_axis.y * p_axis.z * ( 1.0 - cosine ) + p_axis.x * sine;
  411. elements[2][2] = axis_sq.z + cosine * ( 1.0 - axis_sq.z );
  412. }