Matrix3.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// A 3x3 matrix. Can be used for non-homogenous transformations of three dimensional vectors and points.
  7. /// </summary>
  8. [StructLayout(LayoutKind.Sequential), SerializeObject]
  9. public struct Matrix3 // Note: Must match C++ class Matrix3
  10. {
  11. /// <summary>
  12. /// Contains constant data that is used when calculating euler angles in a certain order.
  13. /// </summary>
  14. private struct EulerAngleOrderData
  15. {
  16. public EulerAngleOrderData(int a, int b, int c, float sign)
  17. {
  18. this.a = a;
  19. this.b = b;
  20. this.c = c;
  21. this.sign = sign;
  22. }
  23. public int a, b, c;
  24. public float sign;
  25. };
  26. private static EulerAngleOrderData[] EA_LOOKUP =
  27. { new EulerAngleOrderData(0, 1, 2, 1.0f), new EulerAngleOrderData(0, 2, 1, -1.0f), new EulerAngleOrderData(1, 0, 2, -1.0f),
  28. new EulerAngleOrderData(1, 2, 0, 1.0f), new EulerAngleOrderData(2, 0, 1, 1.0f), new EulerAngleOrderData(2, 1, 0, -1.0f) };
  29. /// <summary>
  30. /// A matrix with all zero values.
  31. /// </summary>
  32. public static readonly Matrix3 Zero = new Matrix3();
  33. /// <summary>
  34. /// Identity matrix.
  35. /// </summary>
  36. public static readonly Matrix3 Identity = new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  37. public float m00;
  38. public float m01;
  39. public float m02;
  40. public float m10;
  41. public float m11;
  42. public float m12;
  43. public float m20;
  44. public float m21;
  45. public float m22;
  46. /// <summary>
  47. /// Creates a new matrix with the specified elements.
  48. /// </summary>
  49. public Matrix3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
  50. {
  51. this.m00 = m00;
  52. this.m01 = m01;
  53. this.m02 = m02;
  54. this.m10 = m10;
  55. this.m11 = m11;
  56. this.m12 = m12;
  57. this.m20 = m20;
  58. this.m21 = m21;
  59. this.m22 = m22;
  60. }
  61. /// <summary>
  62. /// Value of the specified element in the matrix.
  63. /// </summary>
  64. /// <param name="row">Row index of the element to retrieve.</param>
  65. /// <param name="column">Column index of the element to retrieve.</param>
  66. /// <returns>Value of the element.</returns>
  67. public float this[int row, int column]
  68. {
  69. get
  70. {
  71. return this[row * 3 + column];
  72. }
  73. set
  74. {
  75. this[row * 3 + column] = value;
  76. }
  77. }
  78. /// <summary>
  79. /// Value of the specified element in the matrix using a linear index.
  80. /// Linear index can be calculated using the following formula: idx = row * 3 + column.
  81. /// </summary>
  82. /// <param name="index">Linear index to get the value of.</param>
  83. /// <returns>Value of the element.</returns>
  84. public float this[int index]
  85. {
  86. get
  87. {
  88. switch (index)
  89. {
  90. case 0:
  91. return m00;
  92. case 1:
  93. return m01;
  94. case 2:
  95. return m02;
  96. case 3:
  97. return m10;
  98. case 4:
  99. return m11;
  100. case 5:
  101. return m12;
  102. case 6:
  103. return m20;
  104. case 7:
  105. return m21;
  106. case 8:
  107. return m22;
  108. default:
  109. throw new IndexOutOfRangeException("Invalid matrix index.");
  110. }
  111. }
  112. set
  113. {
  114. switch (index)
  115. {
  116. case 0:
  117. m00 = value;
  118. break;
  119. case 1:
  120. m01 = value;
  121. break;
  122. case 2:
  123. m02 = value;
  124. break;
  125. case 3:
  126. m10 = value;
  127. break;
  128. case 4:
  129. m11 = value;
  130. break;
  131. case 5:
  132. m12 = value;
  133. break;
  134. case 6:
  135. m20 = value;
  136. break;
  137. case 7:
  138. m21 = value;
  139. break;
  140. case 8:
  141. m22 = value;
  142. break;
  143. default:
  144. throw new IndexOutOfRangeException("Invalid matrix index.");
  145. }
  146. }
  147. }
  148. public static Matrix3 operator *(Matrix3 lhs, Matrix3 rhs)
  149. {
  150. return new Matrix3()
  151. {
  152. m00 = lhs.m00 * rhs.m00 + lhs.m01 * rhs.m10 + lhs.m02 * rhs.m20,
  153. m01 = lhs.m00 * rhs.m01 + lhs.m01 * rhs.m11 + lhs.m02 * rhs.m21,
  154. m02 = lhs.m00 * rhs.m02 + lhs.m01 * rhs.m12 + lhs.m02 * rhs.m22,
  155. m10 = lhs.m10 * rhs.m00 + lhs.m11 * rhs.m10 + lhs.m12 * rhs.m20,
  156. m11 = lhs.m10 * rhs.m01 + lhs.m11 * rhs.m11 + lhs.m12 * rhs.m21,
  157. m12 = lhs.m10 * rhs.m02 + lhs.m11 * rhs.m12 + lhs.m12 * rhs.m22,
  158. m20 = lhs.m20 * rhs.m00 + lhs.m21 * rhs.m10 + lhs.m22 * rhs.m20,
  159. m21 = lhs.m20 * rhs.m01 + lhs.m21 * rhs.m11 + lhs.m22 * rhs.m21,
  160. m22 = lhs.m20 * rhs.m02 + lhs.m21 * rhs.m12 + lhs.m22 * rhs.m22,
  161. };
  162. }
  163. public static bool operator== (Matrix3 lhs, Matrix3 rhs)
  164. {
  165. if (lhs.m00 == rhs.m00 && lhs.m01 == rhs.m01 && lhs.m02 == rhs.m02 &&
  166. lhs.m10 == rhs.m10 && lhs.m11 == rhs.m11 && lhs.m12 == rhs.m12 &&
  167. lhs.m20 == rhs.m20 && lhs.m21 == rhs.m21 && lhs.m22 == rhs.m22)
  168. return true;
  169. else
  170. return false;
  171. }
  172. public static bool operator !=(Matrix3 lhs, Matrix3 rhs)
  173. {
  174. return !(lhs == rhs);
  175. }
  176. /// <inheritdoc/>
  177. public override int GetHashCode()
  178. {
  179. float hash1 = m00.GetHashCode() ^ m10.GetHashCode() << 2 ^ m20.GetHashCode() >> 2;
  180. float hash2 = m01.GetHashCode() ^ m11.GetHashCode() << 2 ^ m21.GetHashCode() >> 2;
  181. float hash3 = m02.GetHashCode() ^ m12.GetHashCode() << 2 ^ m22.GetHashCode() >> 2;
  182. return hash1.GetHashCode() ^ hash2.GetHashCode() << 2 ^ hash3.GetHashCode() >> 2;
  183. }
  184. /// <inheritdoc/>
  185. public override bool Equals(object other)
  186. {
  187. if (!(other is Matrix3))
  188. return false;
  189. Matrix3 mat = (Matrix3)other;
  190. if (m00 == mat.m00 && m01 == mat.m01 && m02 == mat.m02 &&
  191. m10 == mat.m10 && m11 == mat.m11 && m12 == mat.m12 &&
  192. m20 == mat.m20 && m21 == mat.m21 && m22 == mat.m22)
  193. return true;
  194. else
  195. return false;
  196. }
  197. /// <summary>
  198. /// Calculates an inverse of the matrix if it exists.
  199. /// </summary>
  200. public void Invert()
  201. {
  202. float[,] invVals = new float[3,3];
  203. invVals[0, 0] = m11 * m22 - m12 * m21;
  204. invVals[1, 0] = m12 * m20 - m10 * m22;
  205. invVals[2, 0] = m10 * m21 - m11 * m20;
  206. float det = m00 * invVals[0, 0] + m01 * invVals[1, 0] + m02 * invVals[2, 0];
  207. if (MathEx.Abs(det) <= 1e-06f)
  208. throw new DivideByZeroException("Matrix determinant is zero. Cannot invert.");
  209. invVals[0, 1] = m02 * m21 - m01 * m22;
  210. invVals[0, 2] = m01 * m12 - m02 * m11;
  211. invVals[1, 1] = m00 * m22 - m02 * m20;
  212. invVals[1, 2] = m02 * m10 - m00 * m12;
  213. invVals[2, 1] = m01 * m20 - m00 * m21;
  214. invVals[2, 2] = m00 * m11 - m01 * m10;
  215. float invDet = 1.0f/det;
  216. for (int row = 0; row < 3; row++)
  217. {
  218. for (int col = 0; col < 3; col++)
  219. invVals[row, col] *= invDet;
  220. }
  221. }
  222. /// <summary>
  223. /// Returns a transpose of the matrix (switched columns and rows).
  224. /// </summary>
  225. public void Transpose()
  226. {
  227. float tmp = m10;
  228. m10 = m01;
  229. m01 = tmp;
  230. tmp = m20;
  231. m20 = m02;
  232. m02 = tmp;
  233. tmp = m12;
  234. m12 = m21;
  235. m21 = tmp;
  236. }
  237. /// <summary>
  238. /// Calculates the matrix determinant.
  239. /// </summary>
  240. /// <returns>Determinant of the matrix.</returns>
  241. public float Determinant()
  242. {
  243. float cofactor00 = m11 * m22 - m12 * m21;
  244. float cofactor10 = m12 * m20 - m10 * m22;
  245. float cofactor20 = m10 * m21 - m11 * m20;
  246. float det = m00 * cofactor00 + m01 * cofactor10 + m02 * cofactor20;
  247. return det;
  248. }
  249. /// <summary>
  250. /// Transforms the given vector by this matrix and returns the newly transformed vector.
  251. /// </summary>
  252. /// <param name="vec">Three dimensional vector to transform.</param>
  253. /// <returns>Vector transformed by the matrix.</returns>
  254. public Vector3 Transform(Vector3 vec)
  255. {
  256. Vector3 outVec;
  257. outVec.x = m00 * vec.x + m01 * vec.y + m02 * vec.z;
  258. outVec.y = m10 * vec.x + m11 * vec.y + m12 * vec.z;
  259. outVec.z = m20 * vec.x + m21 * vec.y + m22 * vec.z;
  260. return outVec;
  261. }
  262. /// <summary>
  263. /// Decomposes the matrix into a set of values.
  264. /// </summary>
  265. /// <param name="matQ">Columns form orthonormal bases. If your matrix is affine and doesn't use non-uniform scaling
  266. /// this matrix will be the rotation part of the matrix.
  267. /// </param>
  268. /// <param name="vecD">If the matrix is affine these will be scaling factors of the matrix.</param>
  269. /// <param name="vecU">If the matrix is affine these will be shear factors of the matrix.</param>
  270. public void QDUDecomposition(out Matrix3 matQ, out Vector3 vecD, out Vector3 vecU)
  271. {
  272. matQ = new Matrix3();
  273. vecD = new Vector3();
  274. vecU = new Vector3();
  275. // Build orthogonal matrix Q
  276. float invLength = MathEx.InvSqrt(m00*m00 + m10*m10 + m20*m20);
  277. matQ.m00 = m00*invLength;
  278. matQ.m10 = m10*invLength;
  279. matQ.m20 = m20*invLength;
  280. float dot = matQ.m00*m01 + matQ.m10*m11 + matQ.m20*m21;
  281. matQ.m01 = m01-dot*matQ.m00;
  282. matQ.m11 = m11-dot*matQ.m10;
  283. matQ.m21 = m21-dot*matQ.m20;
  284. invLength = MathEx.InvSqrt(matQ.m01*matQ.m01 + matQ.m11*matQ.m11 + matQ.m21*matQ.m21);
  285. matQ.m01 *= invLength;
  286. matQ.m11 *= invLength;
  287. matQ.m21 *= invLength;
  288. dot = matQ.m00*m02 + matQ.m10*m12 + matQ.m20*m22;
  289. matQ.m02 = m02-dot*matQ.m00;
  290. matQ.m12 = m12-dot*matQ.m10;
  291. matQ.m22 = m22-dot*matQ.m20;
  292. dot = matQ.m01*m02 + matQ.m11*m12 + matQ.m21*m22;
  293. matQ.m02 -= dot*matQ.m01;
  294. matQ.m12 -= dot*matQ.m11;
  295. matQ.m22 -= dot*matQ.m21;
  296. invLength = MathEx.InvSqrt(matQ.m02*matQ.m02 + matQ.m12*matQ.m12 + matQ.m22*matQ.m22);
  297. matQ.m02 *= invLength;
  298. matQ.m12 *= invLength;
  299. matQ.m22 *= invLength;
  300. // Guarantee that orthogonal matrix has determinant 1 (no reflections)
  301. float fDet = matQ.m00*matQ.m11*matQ.m22 + matQ.m01*matQ.m12*matQ.m20 +
  302. matQ.m02*matQ.m10*matQ.m21 - matQ.m02*matQ.m11*matQ.m20 -
  303. matQ.m01*matQ.m10*matQ.m22 - matQ.m00*matQ.m12*matQ.m21;
  304. if (fDet < 0.0f)
  305. {
  306. matQ.m00 = -matQ.m00;
  307. matQ.m01 = -matQ.m01;
  308. matQ.m02 = -matQ.m02;
  309. matQ.m10 = -matQ.m10;
  310. matQ.m11 = -matQ.m11;
  311. matQ.m12 = -matQ.m12;
  312. matQ.m20 = -matQ.m20;
  313. matQ.m21 = -matQ.m21;
  314. matQ.m21 = -matQ.m22;
  315. }
  316. // Build "right" matrix R
  317. Matrix3 matRight = new Matrix3();
  318. matRight.m00 = matQ.m00 * m00 + matQ.m10 * m10 + matQ.m20 * m20;
  319. matRight.m01 = matQ.m00 * m01 + matQ.m10 * m11 + matQ.m20 * m21;
  320. matRight.m11 = matQ.m01 * m01 + matQ.m11 * m11 + matQ.m21 * m21;
  321. matRight.m02 = matQ.m00 * m02 + matQ.m10 * m12 + matQ.m20 * m22;
  322. matRight.m12 = matQ.m01 * m02 + matQ.m11 * m12 + matQ.m21 * m22;
  323. matRight.m22 = matQ.m02 * m02 + matQ.m12 * m12 + matQ.m22 * m22;
  324. // The scaling component
  325. vecD[0] = matRight.m00;
  326. vecD[1] = matRight.m11;
  327. vecD[2] = matRight.m22;
  328. // The shear component
  329. float invD0 = 1.0f/vecD[0];
  330. vecU[0] = matRight.m01 * invD0;
  331. vecU[1] = matRight.m02 * invD0;
  332. vecU[2] = matRight.m12 / vecD[1];
  333. }
  334. /// <summary>
  335. /// Converts an orthonormal matrix to euler angle (pitch/yaw/roll) representation.
  336. /// </summary>
  337. /// <returns>Euler angles in degrees representing the rotation in this matrix.</returns>
  338. public Vector3 ToEulerAngles()
  339. {
  340. float xAngle = -MathEx.Asin(this[1, 2]);
  341. if (xAngle < MathEx.HalfPi)
  342. {
  343. if (xAngle > -MathEx.HalfPi)
  344. {
  345. float yAngle = MathEx.Atan2(this[0, 2], this[2, 2]);
  346. float zAngle = MathEx.Atan2(this[1, 0], this[1, 1]);
  347. return new Vector3(xAngle * MathEx.Rad2Deg, yAngle * MathEx.Rad2Deg, zAngle * MathEx.Rad2Deg);
  348. }
  349. else
  350. {
  351. // Note: Not an unique solution.
  352. xAngle = -MathEx.HalfPi;
  353. float yAngle = MathEx.Atan2(-this[0, 1], this[0, 0]);
  354. float zAngle = 0.0f;
  355. return new Vector3(xAngle * MathEx.Rad2Deg, yAngle * MathEx.Rad2Deg, zAngle * MathEx.Rad2Deg);
  356. }
  357. }
  358. else
  359. {
  360. // Note: Not an unique solution.
  361. xAngle = MathEx.HalfPi;
  362. float yAngle = MathEx.Atan2(this[0, 1], this[0, 0]);
  363. float zAngle = 0.0f;
  364. return new Vector3(xAngle * MathEx.Rad2Deg, yAngle * MathEx.Rad2Deg, zAngle * MathEx.Rad2Deg);
  365. }
  366. }
  367. /// <summary>
  368. /// Converts an orthonormal matrix to quaternion representation.
  369. /// </summary>
  370. /// <returns>Quaternion representing the rotation in this matrix.</returns>
  371. public Quaternion ToQuaternion()
  372. {
  373. return Quaternion.FromRotationMatrix(this);
  374. }
  375. /// <summary>
  376. /// Converts an orthonormal matrix to axis angle representation.
  377. /// </summary>
  378. /// <param name="axis">Axis around which the rotation is performed.</param>
  379. /// <param name="angle">Amount of rotation.</param>
  380. public void ToAxisAngle(out Vector3 axis, out Degree angle)
  381. {
  382. float trace = m00 + m11 + m22;
  383. float cos = 0.5f*(trace-1.0f);
  384. Radian radians = MathEx.Acos(cos); // In [0, PI]
  385. angle = radians.Degrees;
  386. if (radians > 0.0f)
  387. {
  388. if (radians < MathEx.Pi)
  389. {
  390. axis.x = m21 - m12;
  391. axis.y = m02 - m20;
  392. axis.z = m10 - m01;
  393. axis.Normalize();
  394. }
  395. else
  396. {
  397. // Angle is PI
  398. float halfInverse;
  399. if (m00 >= m11)
  400. {
  401. // r00 >= r11
  402. if (m00 >= m22)
  403. {
  404. // r00 is maximum diagonal term
  405. axis.x = 0.5f*MathEx.Sqrt(m00 - m11 - m22 + 1.0f);
  406. halfInverse = 0.5f/axis.x;
  407. axis.y = halfInverse*m01;
  408. axis.z = halfInverse*m02;
  409. }
  410. else
  411. {
  412. // r22 is maximum diagonal term
  413. axis.z = 0.5f*MathEx.Sqrt(m22 - m00 - m11 + 1.0f);
  414. halfInverse = 0.5f/axis.z;
  415. axis.x = halfInverse*m02;
  416. axis.y = halfInverse*m12;
  417. }
  418. }
  419. else
  420. {
  421. // r11 > r00
  422. if (m11 >= m22)
  423. {
  424. // r11 is maximum diagonal term
  425. axis.y = 0.5f*MathEx.Sqrt(m11 - m00 - m22 + 1.0f);
  426. halfInverse = 0.5f/axis.y;
  427. axis.x = halfInverse*m01;
  428. axis.z = halfInverse*m12;
  429. }
  430. else
  431. {
  432. // r22 is maximum diagonal term
  433. axis.z = 0.5f*MathEx.Sqrt(m22 - m00 - m11 + 1.0f);
  434. halfInverse = 0.5f/axis.z;
  435. axis.x = halfInverse*m02;
  436. axis.y = halfInverse*m12;
  437. }
  438. }
  439. }
  440. }
  441. else
  442. {
  443. // The angle is 0 and the matrix is the identity. Any axis will
  444. // work, so just use the x-axis.
  445. axis.x = 1.0f;
  446. axis.y = 0.0f;
  447. axis.z = 0.0f;
  448. }
  449. }
  450. /// <summary>
  451. /// Calculates the inverse of the matrix if it exists.
  452. /// </summary>
  453. /// <param name="mat">Matrix to calculate the inverse of.</param>
  454. /// <returns>Inverse of the matrix.</returns>
  455. public static Matrix3 Inverse(Matrix3 mat)
  456. {
  457. Matrix3 copy = mat;
  458. copy.Invert();
  459. return copy;
  460. }
  461. /// <summary>
  462. /// Calculates the transpose of the matrix.
  463. /// </summary>
  464. /// <param name="mat">Matrix to calculate the transpose of.</param>
  465. /// <returns>Transpose of the matrix.</returns>
  466. public static Matrix3 Transpose(Matrix3 mat)
  467. {
  468. Matrix3 copy = mat;
  469. copy.Transpose();
  470. return copy;
  471. }
  472. /// <summary>
  473. /// Creates a rotation matrix from the provided euler angle (pitch/yaw/roll) rotation.
  474. /// </summary>
  475. /// <param name="euler">Euler angles in degrees.</param>
  476. /// <param name="order">The order in which rotations will be applied. Different rotations can be created depending
  477. /// on the order.</param>
  478. /// <returns>Rotation matrix that can rotate an object to the specified angles.</returns>
  479. public static Matrix3 FromEuler(Vector3 euler, EulerAngleOrder order)
  480. {
  481. return FromEuler(new Degree(euler.x), new Degree(euler.y), new Degree(euler.y), order);
  482. }
  483. /// <summary>
  484. /// Creates a rotation matrix from the provided euler angle (pitch/yaw/roll) rotation.
  485. /// </summary>
  486. /// <param name="xAngle">Pitch angle of rotation.</param>
  487. /// <param name="yAngle">Yar angle of rotation.</param>
  488. /// <param name="zAngle">Roll angle of rotation.</param>
  489. /// <param name="order">The order in which rotations will be applied. Different rotations can be created depending
  490. /// on the order.</param>
  491. /// <returns>Rotation matrix that can rotate an object to the specified angles.</returns>
  492. public static Matrix3 FromEuler(Radian xAngle, Radian yAngle, Radian zAngle, EulerAngleOrder order)
  493. {
  494. EulerAngleOrderData l = EA_LOOKUP[(int)order];
  495. Matrix3[] mats = new Matrix3[3];
  496. float cx = MathEx.Cos(xAngle);
  497. float sx = MathEx.Sin(xAngle);
  498. mats[0] = new Matrix3(
  499. 1.0f, 0.0f, 0.0f,
  500. 0.0f, cx, -sx,
  501. 0.0f, sx, cx);
  502. float cy = MathEx.Cos(yAngle);
  503. float sy = MathEx.Sin(yAngle);
  504. mats[1] = new Matrix3(
  505. cy, 0.0f, sy,
  506. 0.0f, 1.0f, 0.0f,
  507. -sy, 0.0f, cy);
  508. float cz = MathEx.Cos(zAngle);
  509. float sz = MathEx.Sin(zAngle);
  510. mats[2] = new Matrix3(
  511. cz, -sz, 0.0f,
  512. sz, cz, 0.0f,
  513. 0.0f, 0.0f, 1.0f);
  514. return mats[l.a]*(mats[l.b]*mats[l.c]);
  515. }
  516. /// <summary>
  517. /// Creates a rotation matrix from the provided euler angle (pitch/yaw/roll) rotation. Angles are applied in YXZ
  518. /// order.
  519. /// </summary>
  520. /// <param name="euler">Euler angles in degrees.</param>
  521. /// <returns>Rotation matrix that can rotate an object to the specified angles.</returns>
  522. public static Matrix3 FromEuler(Vector3 euler)
  523. {
  524. return FromEuler(new Degree(euler.x), new Degree(euler.y), new Degree(euler.y));
  525. }
  526. /// <summary>
  527. /// Creates a rotation matrix from the provided euler angle (pitch/yaw/roll) rotation. Angles are applied in YXZ
  528. /// order.
  529. /// </summary>
  530. /// <param name="xAngle">Pitch angle of rotation.</param>
  531. /// <param name="yAngle">Yar angle of rotation.</param>
  532. /// <param name="zAngle">Roll angle of rotation.</param>
  533. /// <returns>Rotation matrix that can rotate an object to the specified angles.</returns>
  534. public static Matrix3 FromEuler(Radian xAngle, Radian yAngle, Radian zAngle)
  535. {
  536. Matrix3 m = new Matrix3();
  537. float cx = MathEx.Cos(xAngle);
  538. float sx = MathEx.Sin(xAngle);
  539. float cy = MathEx.Cos(yAngle);
  540. float sy = MathEx.Sin(yAngle);
  541. float cz = MathEx.Cos(zAngle);
  542. float sz = MathEx.Sin(zAngle);
  543. m[0, 0] = cy * cz + sx * sy * sz;
  544. m[0, 1] = cz * sx * sy - cy * sz;
  545. m[0, 2] = cx * sy;
  546. m[1, 0] = cx * sz;
  547. m[1, 1] = cx * cz;
  548. m[1, 2] = -sx;
  549. m[2, 0] = -cz * sy + cy * sx * sz;
  550. m[2, 1] = cy * cz * sx + sy * sz;
  551. m[2, 2] = cx * cy;
  552. return m;
  553. }
  554. /// <summary>
  555. /// Creates a rotation matrix from axis/angle rotation.
  556. /// </summary>
  557. /// <param name="axis">Axis around which the rotation is performed.</param>
  558. /// <param name="angle">Amount of rotation.</param>
  559. /// <returns>Rotation matrix that can rotate an object around the specified axis for the specified amount.</returns>
  560. public static Matrix3 FromAxisAngle(Vector3 axis, Degree angle)
  561. {
  562. Matrix3 mat;
  563. float cos = MathEx.Cos(angle);
  564. float sin = MathEx.Sin(angle);
  565. float oneMinusCos = 1.0f - cos;
  566. float x2 = axis.x * axis.x;
  567. float y2 = axis.y * axis.y;
  568. float z2 = axis.z * axis.z;
  569. float xym = axis.x * axis.y * oneMinusCos;
  570. float xzm = axis.x * axis.z * oneMinusCos;
  571. float yzm = axis.y * axis.z * oneMinusCos;
  572. float xSin = axis.x * sin;
  573. float ySin = axis.y * sin;
  574. float zSin = axis.z * sin;
  575. mat.m00 = x2 * oneMinusCos + cos;
  576. mat.m01 = xym - zSin;
  577. mat.m02 = xzm + ySin;
  578. mat.m10 = xym + zSin;
  579. mat.m11 = y2 * oneMinusCos + cos;
  580. mat.m12 = yzm - xSin;
  581. mat.m20 = xzm - ySin;
  582. mat.m21 = yzm + xSin;
  583. mat.m22 = z2 * oneMinusCos + cos;
  584. return mat;
  585. }
  586. /// <summary>
  587. /// Creates a rotation matrix from a quaternion rotation.
  588. /// </summary>
  589. /// <param name="quat">Quaternion to create the matrix from.</param>
  590. /// <returns>Rotation matrix containing the equivalent rotation of the provided quaternion.</returns>
  591. public static Matrix3 FromQuaternion(Quaternion quat)
  592. {
  593. return quat.ToRotationMatrix();
  594. }
  595. /// <inheritdoc/>
  596. public override string ToString()
  597. {
  598. return String.Format("({0}, {1}, {2},\n{3}, {4}, {5}\n{6}, {7}, {8})",
  599. m00, m01, m02, m10, m11, m12, m20, m21, m22);
  600. }
  601. }
  602. }