Matrix3.cs 25 KB

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