matrix3.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*************************************************************************/
  2. /* matrix3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. // Gram-Schmidt Process
  65. Vector3 x=get_axis(0);
  66. Vector3 y=get_axis(1);
  67. Vector3 z=get_axis(2);
  68. x.normalize();
  69. y = (y-x*(x.dot(y)));
  70. y.normalize();
  71. z = (z-x*(x.dot(z))-y*(y.dot(z)));
  72. z.normalize();
  73. set_axis(0,x);
  74. set_axis(1,y);
  75. set_axis(2,z);
  76. }
  77. Matrix3 Matrix3::orthonormalized() const {
  78. Matrix3 c = *this;
  79. c.orthonormalize();
  80. return c;
  81. }
  82. Matrix3 Matrix3::inverse() const {
  83. Matrix3 inv=*this;
  84. inv.invert();
  85. return inv;
  86. }
  87. void Matrix3::transpose() {
  88. SWAP(elements[0][1],elements[1][0]);
  89. SWAP(elements[0][2],elements[2][0]);
  90. SWAP(elements[1][2],elements[2][1]);
  91. }
  92. Matrix3 Matrix3::transposed() const {
  93. Matrix3 tr=*this;
  94. tr.transpose();
  95. return tr;
  96. }
  97. void Matrix3::scale(const Vector3& p_scale) {
  98. elements[0][0]*=p_scale.x;
  99. elements[1][0]*=p_scale.x;
  100. elements[2][0]*=p_scale.x;
  101. elements[0][1]*=p_scale.y;
  102. elements[1][1]*=p_scale.y;
  103. elements[2][1]*=p_scale.y;
  104. elements[0][2]*=p_scale.z;
  105. elements[1][2]*=p_scale.z;
  106. elements[2][2]*=p_scale.z;
  107. }
  108. Matrix3 Matrix3::scaled( const Vector3& p_scale ) const {
  109. Matrix3 m = *this;
  110. m.scale(p_scale);
  111. return m;
  112. }
  113. Vector3 Matrix3::get_scale() const {
  114. return Vector3(
  115. Vector3(elements[0][0],elements[1][0],elements[2][0]).length(),
  116. Vector3(elements[0][1],elements[1][1],elements[2][1]).length(),
  117. Vector3(elements[0][2],elements[1][2],elements[2][2]).length()
  118. );
  119. }
  120. void Matrix3::rotate(const Vector3& p_axis, real_t p_phi) {
  121. *this = *this * Matrix3(p_axis, p_phi);
  122. }
  123. Matrix3 Matrix3::rotated(const Vector3& p_axis, real_t p_phi) const {
  124. return *this * Matrix3(p_axis, p_phi);
  125. }
  126. Vector3 Matrix3::get_euler() const {
  127. // rot = cy*cz -cy*sz sy
  128. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  129. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  130. Matrix3 m = *this;
  131. m.orthonormalize();
  132. Vector3 euler;
  133. euler.y = Math::asin(m[0][2]);
  134. if ( euler.y < Math_PI*0.5) {
  135. if ( euler.y > -Math_PI*0.5) {
  136. euler.x = Math::atan2(-m[1][2],m[2][2]);
  137. euler.z = Math::atan2(-m[0][1],m[0][0]);
  138. } else {
  139. real_t r = Math::atan2(m[1][0],m[1][1]);
  140. euler.z = 0.0;
  141. euler.x = euler.z - r;
  142. }
  143. } else {
  144. real_t r = Math::atan2(m[0][1],m[1][1]);
  145. euler.z = 0;
  146. euler.x = r - euler.z;
  147. }
  148. return euler;
  149. }
  150. void Matrix3::set_euler(const Vector3& p_euler) {
  151. real_t c, s;
  152. c = Math::cos(p_euler.x);
  153. s = Math::sin(p_euler.x);
  154. Matrix3 xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
  155. c = Math::cos(p_euler.y);
  156. s = Math::sin(p_euler.y);
  157. Matrix3 ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
  158. c = Math::cos(p_euler.z);
  159. s = Math::sin(p_euler.z);
  160. Matrix3 zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
  161. //optimizer will optimize away all this anyway
  162. *this = xmat*(ymat*zmat);
  163. }
  164. bool Matrix3::operator==(const Matrix3& p_matrix) const {
  165. for (int i=0;i<3;i++) {
  166. for (int j=0;j<3;j++) {
  167. if (elements[i][j]!=p_matrix.elements[i][j])
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. bool Matrix3::operator!=(const Matrix3& p_matrix) const {
  174. return (!(*this==p_matrix));
  175. }
  176. Matrix3::operator String() const {
  177. String mtx;
  178. for (int i=0;i<3;i++) {
  179. for (int j=0;j<3;j++) {
  180. if (i!=0 || j!=0)
  181. mtx+=", ";
  182. mtx+=rtos( elements[i][j] );
  183. }
  184. }
  185. return mtx;
  186. }
  187. Matrix3::operator Quat() const {
  188. Matrix3 m=*this;
  189. m.orthonormalize();
  190. real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
  191. real_t temp[4];
  192. if (trace > 0.0)
  193. {
  194. real_t s = Math::sqrt(trace + 1.0);
  195. temp[3]=(s * 0.5);
  196. s = 0.5 / s;
  197. temp[0]=((m.elements[2][1] - m.elements[1][2]) * s);
  198. temp[1]=((m.elements[0][2] - m.elements[2][0]) * s);
  199. temp[2]=((m.elements[1][0] - m.elements[0][1]) * s);
  200. }
  201. else
  202. {
  203. int i = m.elements[0][0] < m.elements[1][1] ?
  204. (m.elements[1][1] < m.elements[2][2] ? 2 : 1) :
  205. (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
  206. int j = (i + 1) % 3;
  207. int k = (i + 2) % 3;
  208. real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0);
  209. temp[i] = s * 0.5;
  210. s = 0.5 / s;
  211. temp[3] = (m.elements[k][j] - m.elements[j][k]) * s;
  212. temp[j] = (m.elements[j][i] + m.elements[i][j]) * s;
  213. temp[k] = (m.elements[k][i] + m.elements[i][k]) * s;
  214. }
  215. return Quat(temp[0],temp[1],temp[2],temp[3]);
  216. }
  217. static const Matrix3 _ortho_bases[24]={
  218. Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1),
  219. Matrix3(0, -1, 0, 1, 0, 0, 0, 0, 1),
  220. Matrix3(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  221. Matrix3(0, 1, 0, -1, 0, 0, 0, 0, 1),
  222. Matrix3(1, 0, 0, 0, 0, -1, 0, 1, 0),
  223. Matrix3(0, 0, 1, 1, 0, 0, 0, 1, 0),
  224. Matrix3(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  225. Matrix3(0, 0, -1, -1, 0, 0, 0, 1, 0),
  226. Matrix3(1, 0, 0, 0, -1, 0, 0, 0, -1),
  227. Matrix3(0, 1, 0, 1, 0, 0, 0, 0, -1),
  228. Matrix3(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  229. Matrix3(0, -1, 0, -1, 0, 0, 0, 0, -1),
  230. Matrix3(1, 0, 0, 0, 0, 1, 0, -1, 0),
  231. Matrix3(0, 0, -1, 1, 0, 0, 0, -1, 0),
  232. Matrix3(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  233. Matrix3(0, 0, 1, -1, 0, 0, 0, -1, 0),
  234. Matrix3(0, 0, 1, 0, 1, 0, -1, 0, 0),
  235. Matrix3(0, -1, 0, 0, 0, 1, -1, 0, 0),
  236. Matrix3(0, 0, -1, 0, -1, 0, -1, 0, 0),
  237. Matrix3(0, 1, 0, 0, 0, -1, -1, 0, 0),
  238. Matrix3(0, 0, 1, 0, -1, 0, 1, 0, 0),
  239. Matrix3(0, 1, 0, 0, 0, 1, 1, 0, 0),
  240. Matrix3(0, 0, -1, 0, 1, 0, 1, 0, 0),
  241. Matrix3(0, -1, 0, 0, 0, -1, 1, 0, 0)
  242. };
  243. int Matrix3::get_orthogonal_index() const {
  244. //could be sped up if i come up with a way
  245. Matrix3 orth=*this;
  246. for(int i=0;i<3;i++) {
  247. for(int j=0;j<3;j++) {
  248. float v = orth[i][j];
  249. if (v>0.5)
  250. v=1.0;
  251. else if (v<-0.5)
  252. v=-1.0;
  253. else
  254. v=0;
  255. orth[i][j]=v;
  256. }
  257. }
  258. for(int i=0;i<24;i++) {
  259. if (_ortho_bases[i]==orth)
  260. return i;
  261. }
  262. return 0;
  263. }
  264. void Matrix3::set_orthogonal_index(int p_index){
  265. //there only exist 24 orthogonal bases in r3
  266. ERR_FAIL_INDEX(p_index,24);
  267. *this=_ortho_bases[p_index];
  268. }
  269. void Matrix3::get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const {
  270. double angle,x,y,z; // variables for result
  271. double epsilon = 0.01; // margin to allow for rounding errors
  272. double epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  273. if ( (Math::abs(elements[1][0]-elements[0][1])< epsilon)
  274. && (Math::abs(elements[2][0]-elements[0][2])< epsilon)
  275. && (Math::abs(elements[2][1]-elements[1][2])< epsilon)) {
  276. // singularity found
  277. // first check for identity matrix which must have +1 for all terms
  278. // in leading diagonaland zero in other terms
  279. if ((Math::abs(elements[1][0]+elements[0][1]) < epsilon2)
  280. && (Math::abs(elements[2][0]+elements[0][2]) < epsilon2)
  281. && (Math::abs(elements[2][1]+elements[1][2]) < epsilon2)
  282. && (Math::abs(elements[0][0]+elements[1][1]+elements[2][2]-3) < epsilon2)) {
  283. // this singularity is identity matrix so angle = 0
  284. r_axis=Vector3(0,1,0);
  285. r_angle=0;
  286. return;
  287. }
  288. // otherwise this singularity is angle = 180
  289. angle = Math_PI;
  290. double xx = (elements[0][0]+1)/2;
  291. double yy = (elements[1][1]+1)/2;
  292. double zz = (elements[2][2]+1)/2;
  293. double xy = (elements[1][0]+elements[0][1])/4;
  294. double xz = (elements[2][0]+elements[0][2])/4;
  295. double yz = (elements[2][1]+elements[1][2])/4;
  296. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  297. if (xx< epsilon) {
  298. x = 0;
  299. y = 0.7071;
  300. z = 0.7071;
  301. } else {
  302. x = Math::sqrt(xx);
  303. y = xy/x;
  304. z = xz/x;
  305. }
  306. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  307. if (yy< epsilon) {
  308. x = 0.7071;
  309. y = 0;
  310. z = 0.7071;
  311. } else {
  312. y = Math::sqrt(yy);
  313. x = xy/y;
  314. z = yz/y;
  315. }
  316. } else { // elements[2][2] is the largest diagonal term so base result on this
  317. if (zz< epsilon) {
  318. x = 0.7071;
  319. y = 0.7071;
  320. z = 0;
  321. } else {
  322. z = Math::sqrt(zz);
  323. x = xz/z;
  324. y = yz/z;
  325. }
  326. }
  327. r_axis=Vector3(x,y,z);
  328. r_angle=angle;
  329. return;
  330. }
  331. // as we have reached here there are no singularities so we can handle normally
  332. double s = Math::sqrt((elements[1][2] - elements[2][1])*(elements[1][2] - elements[2][1])
  333. +(elements[2][0] - elements[0][2])*(elements[2][0] - elements[0][2])
  334. +(elements[0][1] - elements[1][0])*(elements[0][1] - elements[1][0])); // used to normalise
  335. if (Math::abs(s) < 0.001) s=1;
  336. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  337. // caught by singularity test above, but I've left it in just in case
  338. angle = Math::acos(( elements[0][0] + elements[1][1] + elements[2][2] - 1)/2);
  339. x = (elements[1][2] - elements[2][1])/s;
  340. y = (elements[2][0] - elements[0][2])/s;
  341. z = (elements[0][1] - elements[1][0])/s;
  342. r_axis=Vector3(x,y,z);
  343. r_angle=angle;
  344. }
  345. Matrix3::Matrix3(const Vector3& p_euler) {
  346. set_euler( p_euler );
  347. }
  348. Matrix3::Matrix3(const Quat& p_quat) {
  349. real_t d = p_quat.length_squared();
  350. real_t s = 2.0 / d;
  351. real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
  352. real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
  353. real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
  354. real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
  355. set( 1.0 - (yy + zz), xy - wz, xz + wy,
  356. xy + wz, 1.0 - (xx + zz), yz - wx,
  357. xz - wy, yz + wx, 1.0 - (xx + yy)) ;
  358. }
  359. Matrix3::Matrix3(const Vector3& p_axis, real_t p_phi) {
  360. Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
  361. real_t cosine= Math::cos(p_phi);
  362. real_t sine= Math::sin(p_phi);
  363. elements[0][0] = axis_sq.x + cosine * ( 1.0 - axis_sq.x );
  364. elements[0][1] = p_axis.x * p_axis.y * ( 1.0 - cosine ) + p_axis.z * sine;
  365. elements[0][2] = p_axis.z * p_axis.x * ( 1.0 - cosine ) - p_axis.y * sine;
  366. elements[1][0] = p_axis.x * p_axis.y * ( 1.0 - cosine ) - p_axis.z * sine;
  367. elements[1][1] = axis_sq.y + cosine * ( 1.0 - axis_sq.y );
  368. elements[1][2] = p_axis.y * p_axis.z * ( 1.0 - cosine ) + p_axis.x * sine;
  369. elements[2][0] = p_axis.z * p_axis.x * ( 1.0 - cosine ) + p_axis.y * sine;
  370. elements[2][1] = p_axis.y * p_axis.z * ( 1.0 - cosine ) - p_axis.x * sine;
  371. elements[2][2] = axis_sq.z + cosine * ( 1.0 - axis_sq.z );
  372. }