fork download
  1. #include <math.h>
  2. #include "Matrix3D.h"
  3. class Matrix3D
  4. {
  5. public:
  6. float x [3][3];
  7.  
  8. Matrix3D () {}
  9. Matrix3D ( float );
  10. Matrix3D ( const Matrix3D& );
  11. Matrix3D ( float d1, float d2, float d3 );
  12. Matrix3D ( const Vector3D& c1, const Vector3D& c2, const Vector3D& c3 );
  13.  
  14. Matrix3D& operator = ( const Matrix3D& );
  15. Matrix3D& operator = ( float );
  16. Matrix3D& operator += ( const Matrix3D& );
  17. Matrix3D& operator -= ( const Matrix3D& );
  18. Matrix3D& operator *= ( const Matrix3D& );
  19. Matrix3D& operator *= ( float );
  20. Matrix3D& operator /= ( float );
  21.  
  22. const float * operator [] ( int i ) const
  23. {
  24. return & x[i][0];
  25. }
  26.  
  27. float * operator [] ( int i )
  28. {
  29. return & x[i][0];
  30. }
  31.  
  32. Matrix3D& invert ();
  33. Matrix3D& transpose ();
  34. float det () const;
  35.  
  36. Matrix3D inverse () const
  37. {
  38. return Matrix3D ( *this ).invert ();
  39. }
  40.  
  41. void getHomMatrix ( float * matrix ) const;
  42. void getHomMatrix ( float * matrix, const Vector3D& offs ) const;
  43.  
  44. static Matrix3D identity ();
  45. static Matrix3D scale ( const Vector3D& );
  46. static Matrix3D rotateX ( float );
  47. static Matrix3D rotateY ( float );
  48. static Matrix3D rotateZ ( float );
  49. static Matrix3D rotate ( const Vector3D&, float );
  50. static Matrix3D rotate ( float yaw, float pitch, float roll );
  51. static Matrix3D mirrorX ();
  52. static Matrix3D mirrorY ();
  53. static Matrix3D mirrorZ ();
  54.  
  55. friend Matrix3D operator + ( const Matrix3D&, const Matrix3D& );
  56. friend Matrix3D operator - ( const Matrix3D&, const Matrix3D& );
  57. friend Matrix3D operator * ( const Matrix3D&, const Matrix3D& );
  58. friend Matrix3D operator * ( const Matrix3D&, float );
  59. friend Matrix3D operator * ( float, const Matrix3D& );
  60. };
  61.  
  62. Matrix3D :: Matrix3D ( float a )
  63. {
  64. x [0][1] = x [0][2] = x [1][0] =
  65. x [1][2] = x [2][0] = x [2][1] = 0;
  66. x [0][0] = x [1][1] = x [2][2] = a;
  67. }
  68. Matrix3D :: Matrix3D ( const Matrix3D& a )
  69. {
  70. x [0][0] = a.x [0][0];
  71. x [0][1] = a.x [0][1];
  72. x [0][2] = a.x [0][2];
  73. x [1][0] = a.x [1][0];
  74. x [1][1] = a.x [1][1];
  75. x [1][2] = a.x [1][2];
  76. x [2][0] = a.x [2][0];
  77. x [2][1] = a.x [2][1];
  78. x [2][2] = a.x [2][2];
  79. }
  80. Matrix3D :: Matrix3D ( const Vector3D& c1, const Vector3D& c2, const Vector3D& c3 )
  81. {
  82. x [0][0] = c1.x;
  83. x [0][1] = c2.x;
  84. x [0][2] = c3.x;
  85.  
  86. x [1][0] = c1.y;
  87. x [1][1] = c2.y;
  88. x [1][2] = c3.y;
  89.  
  90. x [2][0] = c1.z;
  91. x [2][1] = c2.z;
  92. x [2][2] = c3.z;
  93. }
  94.  
  95. Matrix3D& Matrix3D :: operator = ( const Matrix3D& a )
  96. {
  97. x [0][0] = a.x [0][0];
  98. x [0][1] = a.x [0][1];
  99. x [0][2] = a.x [0][2];
  100. x [1][0] = a.x [1][0];
  101. x [1][1] = a.x [1][1];
  102. x [1][2] = a.x [1][2];
  103. x [2][0] = a.x [2][0];
  104. x [2][1] = a.x [2][1];
  105. x [2][2] = a.x [2][2];
  106.  
  107. return *this;
  108. }
  109.  
  110. Matrix3D& Matrix3D :: operator = ( float a )
  111. {
  112. x [0][1] = x [0][2] = x [1][0] =
  113. x [1][2] = x [2][0] = x [2][1] = 0.0;
  114. x [0][0] = x [1][1] = x [2][2] = a;
  115.  
  116. return *this;
  117. }
  118.  
  119. Matrix3D& Matrix3D :: operator += ( const Matrix3D& a )
  120. {
  121. x [0][0] += a.x [0][0];
  122. x [0][1] += a.x [0][1];
  123. x [0][2] += a.x [0][2];
  124. x [1][0] += a.x [1][0];
  125. x [1][1] += a.x [1][1];
  126. x [1][2] += a.x [1][2];
  127. x [2][0] += a.x [2][0];
  128. x [2][1] += a.x [2][1];
  129. x [2][2] += a.x [2][2];
  130.  
  131. return *this;
  132. }
  133.  
  134. Matrix3D& Matrix3D :: operator -= ( const Matrix3D& a )
  135. {
  136. x [0][0] -=a.x [0][0];
  137. x [0][1] -=a.x [0][1];
  138. x [0][2] -=a.x [0][2];
  139. x [1][0] -=a.x [1][0];
  140. x [1][1] -=a.x [1][1];
  141. x [1][2] -=a.x [1][2];
  142. x [2][0] -=a.x [2][0];
  143. x [2][1] -=a.x [2][1];
  144. x [2][2] -=a.x [2][2];
  145.  
  146. return *this;
  147. }
  148.  
  149. Matrix3D& Matrix3D :: operator *= ( const Matrix3D& a )
  150. {
  151. Matrix3D c ( *this );
  152.  
  153. x[0][0]=c.x[0][0]*a.x[0][0]+c.x[0][1]*a.x[1][0]+c.x[0][2]*a.x[2][0];
  154. x[0][1]=c.x[0][0]*a.x[0][1]+c.x[0][1]*a.x[1][1]+c.x[0][2]*a.x[2][1];
  155. x[0][2]=c.x[0][0]*a.x[0][2]+c.x[0][1]*a.x[1][2]+c.x[0][2]*a.x[2][2];
  156. x[1][0]=c.x[1][0]*a.x[0][0]+c.x[1][1]*a.x[1][0]+c.x[1][2]*a.x[2][0];
  157. x[1][1]=c.x[1][0]*a.x[0][1]+c.x[1][1]*a.x[1][1]+c.x[1][2]*a.x[2][1];
  158. x[1][2]=c.x[1][0]*a.x[0][2]+c.x[1][1]*a.x[1][2]+c.x[1][2]*a.x[2][2];
  159. x[2][0]=c.x[2][0]*a.x[0][0]+c.x[2][1]*a.x[1][0]+c.x[2][2]*a.x[2][0];
  160. x[2][1]=c.x[2][0]*a.x[0][1]+c.x[2][1]*a.x[1][1]+c.x[2][2]*a.x[2][1];
  161. x[2][2]=c.x[2][0]*a.x[0][2]+c.x[2][1]*a.x[1][2]+c.x[2][2]*a.x[2][2];
  162.  
  163. return *this;
  164. }
  165.  
  166. Matrix3D& Matrix3D :: operator *= ( float a )
  167. {
  168. x [0][0] *= a;
  169. x [0][1] *= a;
  170. x [0][2] *= a;
  171. x [1][0] *= a;
  172. x [1][1] *= a;
  173. x [1][2] *= a;
  174. x [2][0] *= a;
  175. x [2][1] *= a;
  176. x [2][2] *= a;
  177.  
  178. return *this;
  179. }
  180.  
  181. Matrix3D& Matrix3D :: operator /= ( float a )
  182. {
  183. x [0][0] /= a;
  184. x [0][1] /= a;
  185. x [0][2] /= a;
  186. x [1][0] /= a;
  187. x [1][1] /= a;
  188. x [1][2] /= a;
  189. x [2][0] /= a;
  190. x [2][1] /= a;
  191. x [2][2] /= a;
  192.  
  193. return *this;
  194. };
  195.  
  196. float Matrix3D :: det () const
  197. {
  198. return x [0][0]*(x [1][1]*x [2][2]-x [1][2]*x [2][1]) -
  199. x [0][1]*(x [1][0]*x [2][2]-x [1][2]*x [2][0]) +
  200. x [0][2]*(x [1][0]*x [2][1]-x [1][1]*x [2][0]);
  201. }
  202.  
  203. Matrix3D& Matrix3D :: invert ()
  204. {
  205.  
  206. float det;
  207. Matrix3D a;
  208. // compute a determinant
  209. det = x [0][0]*(x [1][1]*x [2][2]-x [1][2]*x [2][1]) -
  210. x [0][1]*(x [1][0]*x [2][2]-x [1][2]*x [2][0]) +
  211. x [0][2]*(x [1][0]*x [2][1]-x [1][1]*x [2][0]);
  212.  
  213. a.x [0][0] = (x [1][1]*x [2][2]-x [1][2]*x [2][1]) / det;
  214. a.x [0][1] = (x [0][2]*x [2][1]-x [0][1]*x [2][2]) / det;
  215. a.x [0][2] = (x [0][1]*x [1][2]-x [0][2]*x [1][1]) / det;
  216. a.x [1][0] = (x [1][2]*x [2][0]-x [1][0]*x [2][2]) / det;
  217. a.x [1][1] = (x [0][0]*x [2][2]-x [0][2]*x [2][0]) / det;
  218. a.x [1][2] = (x [0][2]*x [1][0]-x [0][0]*x [1][2]) / det;
  219. a.x [2][0] = (x [1][0]*x [2][1]-x [1][1]*x [2][0]) / det;
  220. a.x [2][1] = (x [0][1]*x [2][0]-x [0][0]*x [2][1]) / det;
  221. a.x [2][2] = (x [0][0]*x [1][1]-x [0][1]*x [1][0]) / det;
  222.  
  223. return *this = a;
  224. }
  225.  
  226. Matrix3D& Matrix3D :: transpose ()
  227. {
  228. Matrix3D a;
  229.  
  230. a.x [0][0] = x [0][0];
  231. a.x [0][1] = x [1][0];
  232. a.x [0][2] = x [2][0];
  233. a.x [1][0] = x [0][1];
  234. a.x [1][1] = x [1][1];
  235. a.x [1][2] = x [2][1];
  236. a.x [2][0] = x [0][2];
  237. a.x [2][1] = x [1][2];
  238. a.x [2][2] = x [2][2];
  239.  
  240. return *this = a;
  241. }
  242.  
  243. Matrix3D operator + ( const Matrix3D& a, const Matrix3D& b )
  244. {
  245. Matrix3D c;
  246.  
  247. c.x [0][0] = a.x [0][0] + b.x [0][0];
  248. c.x [0][1] = a.x [0][1] + b.x [0][1];
  249. c.x [0][2] = a.x [0][2] + b.x [0][2];
  250. c.x [1][0] = a.x [1][0] + b.x [1][0];
  251. c.x [1][1] = a.x [1][1] + b.x [1][1];
  252. c.x [1][2] = a.x [1][2] + b.x [1][2];
  253. c.x [2][0] = a.x [2][0] + b.x [2][0];
  254. c.x [2][1] = a.x [2][1] + b.x [2][1];
  255. c.x [2][2] = a.x [2][2] + b.x [2][2];
  256.  
  257. return c;
  258. }
  259.  
  260. Matrix3D operator - ( const Matrix3D& a, const Matrix3D& b )
  261. {
  262. Matrix3D c;
  263.  
  264. c.x [0][0] = a.x [0][0] - b.x [0][0];
  265. c.x [0][1] = a.x [0][1] - b.x [0][1];
  266. c.x [0][2] = a.x [0][2] - b.x [0][2];
  267. c.x [1][0] = a.x [1][0] - b.x [1][0];
  268. c.x [1][1] = a.x [1][1] - b.x [1][1];
  269. c.x [1][2] = a.x [1][2] - b.x [1][2];
  270. c.x [2][0] = a.x [2][0] - b.x [2][0];
  271. c.x [2][1] = a.x [2][1] - b.x [2][1];
  272. c.x [2][2] = a.x [2][2] - b.x [2][2];
  273.  
  274. return c;
  275. }
  276.  
  277. Matrix3D operator * ( const Matrix3D& a, const Matrix3D& b )
  278. {
  279. Matrix3D c ( a );
  280.  
  281. c.x[0][0]=a.x[0][0]*b.x[0][0]+a.x[0][1]*b.x[1][0]+a.x[0][2]*b.x[2][0];
  282. c.x[0][1]=a.x[0][0]*b.x[0][1]+a.x[0][1]*b.x[1][1]+a.x[0][2]*b.x[2][1];
  283. c.x[0][2]=a.x[0][0]*b.x[0][2]+a.x[0][1]*b.x[1][2]+a.x[0][2]*b.x[2][2];
  284. c.x[1][0]=a.x[1][0]*b.x[0][0]+a.x[1][1]*b.x[1][0]+a.x[1][2]*b.x[2][0];
  285. c.x[1][1]=a.x[1][0]*b.x[0][1]+a.x[1][1]*b.x[1][1]+a.x[1][2]*b.x[2][1];
  286. c.x[1][2]=a.x[1][0]*b.x[0][2]+a.x[1][1]*b.x[1][2]+a.x[1][2]*b.x[2][2];
  287. c.x[2][0]=a.x[2][0]*b.x[0][0]+a.x[2][1]*b.x[1][0]+a.x[2][2]*b.x[2][0];
  288. c.x[2][1]=a.x[2][0]*b.x[0][1]+a.x[2][1]*b.x[1][1]+a.x[2][2]*b.x[2][1];
  289. c.x[2][2]=a.x[2][0]*b.x[0][2]+a.x[2][1]*b.x[1][2]+a.x[2][2]*b.x[2][2];
  290.  
  291. return c;
  292. }
  293.  
  294. Matrix3D operator * ( const Matrix3D& a, float b )
  295. {
  296. Matrix3D c ( a );
  297.  
  298. return (c *= b);
  299. }
  300.  
  301. Matrix3D operator * ( float b, const Matrix3D& a )
  302. {
  303. Matrix3D c ( a );
  304.  
  305. return (c *= b);
  306. }
  307.  
  308. Matrix3D Matrix3D :: identity ()
  309. {
  310. return Matrix3D ( 1 );
  311. }
  312.  
  313. Matrix3D Matrix3D :: scale ( const Vector3D& v )
  314. {
  315. Matrix3D a ( 1 );
  316.  
  317. a.x [0][0] = v.x;
  318. a.x [1][1] = v.y;
  319. a.x [2][2] = v.z;
  320.  
  321. return a;
  322. }
  323.  
  324. Matrix3D Matrix3D :: rotateX ( float angle )
  325. {
  326. Matrix3D a ( 1 );
  327. float cosine = (float)cos ( angle );
  328. float sine = (float)sin ( angle );
  329.  
  330. a.x [1][1] = cosine;
  331. a.x [1][2] = sine;
  332. a.x [2][1] = -sine;
  333. a.x [2][2] = cosine;
  334.  
  335. return a;
  336. }
  337.  
  338. Matrix3D Matrix3D :: rotateY ( float angle )
  339. {
  340. Matrix3D a ( 1 );
  341. float cosine = (float)cos ( angle );
  342. float sine = (float)sin ( angle );
  343.  
  344. a.x [0][0] = cosine;
  345. a.x [0][2] = -sine;
  346. a.x [2][0] = sine;
  347. a.x [2][2] = cosine;
  348.  
  349. return a;
  350. }
  351.  
  352. Matrix3D Matrix3D :: rotateZ ( float angle )
  353. {
  354. Matrix3D a ( 1 );
  355. float cosine = (float)cos ( angle );
  356. float sine = (float)sin ( angle );
  357.  
  358. a.x [0][0] = cosine;
  359. a.x [0][1] = sine;
  360. a.x [1][0] = -sine;
  361. a.x [1][1] = cosine;
  362.  
  363. return a;
  364. }
  365.  
  366. Matrix3D Matrix3D :: rotate ( const Vector3D& v, float angle )
  367. {
  368. Matrix3D a;
  369. float cosine = (float)cos ( angle );
  370. float sine = (float)sin ( angle );
  371.  
  372. a.x [0][0] = v.x *v.x + (1-v.x*v.x) * cosine;
  373. a.x [0][1] = v.x *v.y * (1-cosine) + v.z * sine;
  374. a.x [0][2] = v.x *v.z * (1-cosine) - v.y * sine;
  375. a.x [1][0] = v.x *v.y * (1-cosine) - v.z * sine;
  376. a.x [1][1] = v.y *v.y + (1-v.y*v.y) * cosine;
  377. a.x [1][2] = v.y *v.z * (1-cosine) + v.x * sine;
  378. a.x [2][0] = v.x *v.z * (1-cosine) + v.y * sine;
  379. a.x [2][1] = v.y *v.z * (1-cosine) - v.x * sine;
  380. a.x [2][2] = v.z *v.z + (1-v.z*v.z) * cosine;
  381.  
  382. return a;
  383. }
  384.  
  385. Matrix3D Matrix3D :: rotate ( float yaw, float pitch, float roll )
  386. {
  387. return rotateZ ( roll ) * rotateY ( yaw ) * rotateX ( pitch );
  388. }
  389.  
  390. // build a homogenous matrix
  391. void Matrix3D :: getHomMatrix ( float * matrix ) const
  392. {
  393. if ( matrix == (float *) 0l )
  394. return;
  395.  
  396. // 1st row
  397. matrix [ 0] = x [0][0];
  398. matrix [ 1] = x [0][1];
  399. matrix [ 2] = x [0][2];
  400. matrix [ 3] = 0;
  401.  
  402. // 2nd row
  403. matrix [ 4] = x [1][0];
  404. matrix [ 5] = x [1][1];
  405. matrix [ 6] = x [1][2];
  406. matrix [ 7] = 0;
  407.  
  408. // 3rd row
  409. matrix [ 8] = x [2][0];
  410. matrix [ 9] = x [2][1];
  411. matrix [10] = x [2][2];
  412. matrix [11] = 0;
  413.  
  414. // 4th row
  415. matrix [12] = 0;
  416. matrix [13] = 0;
  417. matrix [14] = 0;
  418. matrix [15] = 1;
  419. }
  420.  
  421. void Matrix3D :: getHomMatrix ( float * matrix, const Vector3D& offs ) const
  422. {
  423. if ( matrix == (float *) 0l )
  424. return;
  425.  
  426. // 1st row
  427. matrix [ 0] = x [0][0];
  428. matrix [ 1] = x [0][1];
  429. matrix [ 2] = x [0][2];
  430. matrix [ 3] = 0;
  431.  
  432. // 2nd row
  433. matrix [ 4] = x [1][0];
  434. matrix [ 5] = x [1][1];
  435. matrix [ 6] = x [1][2];
  436. matrix [ 7] = 0;
  437.  
  438. // 3rd row
  439. matrix [ 8] = x [2][0];
  440. matrix [ 9] = x [2][1];
  441. matrix [10] = x [2][2];
  442. matrix [11] = 0;
  443.  
  444. // 4th row
  445. matrix [12] = offs.x;
  446. matrix [13] = offs.y;
  447. matrix [14] = offs.z;
  448. matrix [15] = 1;
  449. }
  450.  
  451. Matrix3D Matrix3D :: mirrorX ()
  452. {
  453. Matrix3D a ( 1 );
  454.  
  455. a.x [0][0] = -1;
  456.  
  457. return a;
  458. }
  459.  
  460. Matrix3D Matrix3D :: mirrorY ()
  461. {
  462. Matrix3D a ( 1 );
  463.  
  464. a.x [1][1] = -1;
  465.  
  466. return a;
  467. }
  468.  
  469. Matrix3D Matrix3D :: mirrorZ ()
  470. {
  471. Matrix3D a ( 1 );
  472.  
  473. a.x [2][2] = -1;
  474.  
  475. return a;
  476. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:22: fatal error: Matrix3D.h: No such file or directory
 #include "Matrix3D.h"
                      ^
compilation terminated.
stdout
Standard output is empty