Matrix.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #region License
  2. /*
  3. MIT License
  4. Copyright © 2006 The Mono.Xna Team
  5. All rights reserved.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. #endregion License
  23. using System;
  24. using System.ComponentModel;
  25. using System.Runtime.InteropServices;
  26. namespace MonoXnaCompactMaths
  27. {
  28. [Serializable]
  29. [StructLayout(LayoutKind.Sequential)]
  30. //[TypeConverter(typeof(MatrixConverter))]
  31. public struct Matrix : IEquatable<Matrix>
  32. {
  33. #region Public Constructors
  34. public Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31,
  35. float m32, float m33, float m34, float m41, float m42, float m43, float m44)
  36. {
  37. this.M11 = m11;
  38. this.M12 = m12;
  39. this.M13 = m13;
  40. this.M14 = m14;
  41. this.M21 = m21;
  42. this.M22 = m22;
  43. this.M23 = m23;
  44. this.M24 = m24;
  45. this.M31 = m31;
  46. this.M32 = m32;
  47. this.M33 = m33;
  48. this.M34 = m34;
  49. this.M41 = m41;
  50. this.M42 = m42;
  51. this.M43 = m43;
  52. this.M44 = m44;
  53. }
  54. #endregion Public Constructors
  55. #region Public Fields
  56. public float M11;
  57. public float M12;
  58. public float M13;
  59. public float M14;
  60. public float M21;
  61. public float M22;
  62. public float M23;
  63. public float M24;
  64. public float M31;
  65. public float M32;
  66. public float M33;
  67. public float M34;
  68. public float M41;
  69. public float M42;
  70. public float M43;
  71. public float M44;
  72. #endregion Public Fields
  73. #region Private Members
  74. private static Matrix identity = new Matrix(1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f);
  75. #endregion Private Members
  76. #region Public Properties
  77. public Vector3 Backward
  78. {
  79. get
  80. {
  81. return new Vector3(this.M31, this.M32, this.M33);
  82. }
  83. set
  84. {
  85. this.M31 = value.X;
  86. this.M32 = value.Y;
  87. this.M33 = value.Z;
  88. }
  89. }
  90. public Vector3 Down
  91. {
  92. get
  93. {
  94. return new Vector3(-this.M21, -this.M22, -this.M23);
  95. }
  96. set
  97. {
  98. this.M21 = -value.X;
  99. this.M22 = -value.Y;
  100. this.M23 = -value.Z;
  101. }
  102. }
  103. public Vector3 Forward
  104. {
  105. get
  106. {
  107. return new Vector3(-this.M31, -this.M32, -this.M33);
  108. }
  109. set
  110. {
  111. this.M31 = -value.X;
  112. this.M32 = -value.Y;
  113. this.M33 = -value.Z;
  114. }
  115. }
  116. public static Matrix Identity
  117. {
  118. get { return identity; }
  119. }
  120. public Vector3 Left
  121. {
  122. get
  123. {
  124. return new Vector3(-this.M11, -this.M12, -this.M13);
  125. }
  126. set
  127. {
  128. this.M11 = -value.X;
  129. this.M12 = -value.Y;
  130. this.M13 = -value.Z;
  131. }
  132. }
  133. public Vector3 Right
  134. {
  135. get
  136. {
  137. return new Vector3(this.M11, this.M12, this.M13);
  138. }
  139. set
  140. {
  141. this.M11 = value.X;
  142. this.M12 = value.Y;
  143. this.M13 = value.Z;
  144. }
  145. }
  146. public Vector3 Translation
  147. {
  148. get
  149. {
  150. return new Vector3(this.M41, this.M42, this.M43);
  151. }
  152. set
  153. {
  154. this.M41 = value.X;
  155. this.M42 = value.Y;
  156. this.M43 = value.Z;
  157. }
  158. }
  159. public Vector3 Up
  160. {
  161. get
  162. {
  163. return new Vector3(this.M21, this.M22, this.M23);
  164. }
  165. set
  166. {
  167. this.M21 = value.X;
  168. this.M22 = value.Y;
  169. this.M23 = value.Z;
  170. }
  171. }
  172. #endregion Public Properties
  173. #region Public Methods
  174. public static Matrix Add(Matrix matrix1, Matrix matrix2)
  175. {
  176. throw new NotImplementedException();
  177. }
  178. public static void Add(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
  179. {
  180. throw new NotImplementedException();
  181. }
  182. public static Matrix CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition,
  183. Vector3 cameraUpVector, Nullable<Vector3> cameraForwardVector)
  184. {
  185. throw new NotImplementedException();
  186. }
  187. public static void CreateBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
  188. ref Vector3 cameraUpVector, Vector3? cameraForwardVector, out Matrix result)
  189. {
  190. throw new NotImplementedException();
  191. }
  192. public static Matrix CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition,
  193. Vector3 rotateAxis, Nullable<Vector3> cameraForwardVector, Nullable<Vector3> objectForwardVector)
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public static void CreateConstrainedBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
  198. ref Vector3 rotateAxis, Vector3? cameraForwardVector, Vector3? objectForwardVector, out Matrix result)
  199. {
  200. throw new NotImplementedException();
  201. }
  202. public static Matrix CreateFromAxisAngle(Vector3 axis, float angle)
  203. {
  204. throw new NotImplementedException();
  205. }
  206. public static void CreateFromAxisAngle(ref Vector3 axis, float angle, out Matrix result)
  207. {
  208. throw new NotImplementedException();
  209. }
  210. public static Matrix CreateFromQuaternion(Quaternion quaternion)
  211. {
  212. //---
  213. //http://lists.ximian.com/pipermail/mono-patches/2006-December/084667.html
  214. float xx = quaternion.X * quaternion.X;
  215. float xy = quaternion.X * quaternion.Y;
  216. float xw = quaternion.X * quaternion.W;
  217. float yy = quaternion.Y * quaternion.Y;
  218. float yw = quaternion.Y * quaternion.W;
  219. float yz = quaternion.Y * quaternion.Z;
  220. float zx = quaternion.Z * quaternion.X;
  221. float zw = quaternion.Z * quaternion.W;
  222. float zz = quaternion.Z * quaternion.Z;
  223. return new Matrix(1f - (2f * (yy + zz)), 2f * (xy + zw), 2f * (zx - yw), 0f, 2f * (xy - zw),
  224. 1f - (2f * (zz + xx)), 2f * (yz + xw), 0f, 2f * (zx + yw), 2f * (yz - xw),
  225. 1f - (2f * (yy + xx)), 0f, 0f, 0f, 0f, 1f);
  226. //throw new NotImplementedException();
  227. }
  228. public static void CreateFromQuaternion(ref Quaternion quaternion, out Matrix result)
  229. {
  230. throw new NotImplementedException();
  231. }
  232. public static Matrix CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector)
  233. {
  234. throw new NotImplementedException();
  235. }
  236. public static void CreateLookAt(ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix result)
  237. {
  238. throw new NotImplementedException();
  239. }
  240. public static Matrix CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane)
  241. {
  242. throw new NotImplementedException();
  243. }
  244. public static void CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane, out Matrix result)
  245. {
  246. throw new NotImplementedException();
  247. }
  248. public static Matrix CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane)
  249. {
  250. throw new NotImplementedException();
  251. }
  252. public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top,
  253. float zNearPlane, float zFarPlane, out Matrix result)
  254. {
  255. throw new NotImplementedException();
  256. }
  257. public static Matrix CreatePerspective(float width, float height, float zNearPlane, float zFarPlane)
  258. {
  259. throw new NotImplementedException();
  260. }
  261. public static void CreatePerspective(float width, float height, float zNearPlane, float zFarPlane, out Matrix result)
  262. {
  263. throw new NotImplementedException();
  264. }
  265. public static Matrix CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float zNearPlane, float zFarPlane)
  266. {
  267. throw new NotImplementedException();
  268. }
  269. public static void CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
  270. {
  271. throw new NotImplementedException();
  272. }
  273. public static Matrix CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane)
  274. {
  275. throw new NotImplementedException();
  276. }
  277. public static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
  278. {
  279. throw new NotImplementedException();
  280. }
  281. public static Matrix CreateRotationX(float radians)
  282. {
  283. throw new NotImplementedException();
  284. }
  285. public static void CreateRotationX(float radians, out Matrix result)
  286. {
  287. throw new NotImplementedException();
  288. }
  289. public static Matrix CreateRotationY(float radians)
  290. {
  291. throw new NotImplementedException();
  292. }
  293. public static void CreateRotationY(float radians, out Matrix result)
  294. {
  295. throw new NotImplementedException();
  296. }
  297. public static Matrix CreateRotationZ(float radians)
  298. {
  299. throw new NotImplementedException();
  300. }
  301. public static void CreateRotationZ(float radians, out Matrix result)
  302. {
  303. throw new NotImplementedException();
  304. }
  305. public static Matrix CreateScale(float scale)
  306. {
  307. throw new NotImplementedException();
  308. }
  309. public static void CreateScale(float scale, out Matrix result)
  310. {
  311. throw new NotImplementedException();
  312. }
  313. public static Matrix CreateScale(float xScale, float yScale, float zScale)
  314. {
  315. throw new NotImplementedException();
  316. }
  317. public static void CreateScale(float xScale, float yScale, float zScale, out Matrix result)
  318. {
  319. throw new NotImplementedException();
  320. }
  321. public static Matrix CreateScale(Vector3 scales)
  322. {
  323. throw new NotImplementedException();
  324. }
  325. public static void CreateScale(ref Vector3 scales, out Matrix result)
  326. {
  327. throw new NotImplementedException();
  328. }
  329. public static Matrix CreateTranslation(float xPosition, float yPosition, float zPosition)
  330. {
  331. throw new NotImplementedException();
  332. }
  333. public static void CreateTranslation(ref Vector3 position, out Matrix result)
  334. {
  335. throw new NotImplementedException();
  336. }
  337. public static Matrix CreateTranslation(Vector3 position)
  338. {
  339. throw new NotImplementedException();
  340. }
  341. public static void CreateTranslation(float xPosition, float yPosition, float zPosition, out Matrix result)
  342. {
  343. throw new NotImplementedException();
  344. }
  345. public float Determinant()
  346. {
  347. throw new NotImplementedException();
  348. }
  349. public static Matrix Divide(Matrix matrix1, Matrix matrix2)
  350. {
  351. throw new NotImplementedException();
  352. }
  353. public static void Divide(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
  354. {
  355. throw new NotImplementedException();
  356. }
  357. public static Matrix Divide(Matrix matrix1, float divider)
  358. {
  359. throw new NotImplementedException();
  360. }
  361. public static void Divide(ref Matrix matrix1, float divider, out Matrix result)
  362. {
  363. throw new NotImplementedException();
  364. }
  365. public bool Equals(Matrix other)
  366. {
  367. throw new NotImplementedException();
  368. }
  369. public override bool Equals(object obj)
  370. {
  371. throw new NotImplementedException();
  372. }
  373. public override int GetHashCode()
  374. {
  375. throw new NotImplementedException();
  376. }
  377. public static Matrix Invert(Matrix matrix)
  378. {
  379. throw new NotImplementedException();
  380. }
  381. public static void Invert(ref Matrix matrix, out Matrix result)
  382. {
  383. throw new NotImplementedException();
  384. }
  385. public static Matrix Lerp(Matrix matrix1, Matrix matrix2, float amount)
  386. {
  387. throw new NotImplementedException();
  388. }
  389. public static void Lerp(ref Matrix matrix1, ref Matrix matrix2, float amount, out Matrix result)
  390. {
  391. throw new NotImplementedException();
  392. }
  393. public static Matrix Multiply(Matrix matrix1, Matrix matrix2)
  394. {
  395. throw new NotImplementedException();
  396. }
  397. public static void Multiply(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
  398. {
  399. throw new NotImplementedException();
  400. }
  401. public static Matrix Multiply(Matrix matrix1, float factor)
  402. {
  403. throw new NotImplementedException();
  404. }
  405. public static void Multiply(ref Matrix matrix1, float factor, out Matrix result)
  406. {
  407. throw new NotImplementedException();
  408. }
  409. public static Matrix Negate(Matrix matrix)
  410. {
  411. throw new NotImplementedException();
  412. }
  413. public static void Negate(ref Matrix matrix, out Matrix result)
  414. {
  415. throw new NotImplementedException();
  416. }
  417. public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  418. {
  419. throw new NotImplementedException();
  420. }
  421. public static Matrix operator /(Matrix matrix1, Matrix matrix2)
  422. {
  423. throw new NotImplementedException();
  424. }
  425. public static Matrix operator /(Matrix matrix1, float divider)
  426. {
  427. return new Matrix(
  428. matrix1.M11 / divider, matrix1.M12 / divider, matrix1.M13 / divider, matrix1.M14 / divider,
  429. matrix1.M21 / divider, matrix1.M22 / divider, matrix1.M23 / divider, matrix1.M24 / divider,
  430. matrix1.M31 / divider, matrix1.M32 / divider, matrix1.M33 / divider, matrix1.M34 / divider,
  431. matrix1.M41 / divider, matrix1.M42 / divider, matrix1.M43 / divider, matrix1.M44 / divider);
  432. }
  433. public static bool operator ==(Matrix matrix1, Matrix matrix2)
  434. {
  435. throw new NotImplementedException();
  436. }
  437. public static bool operator !=(Matrix matrix1, Matrix matrix2)
  438. {
  439. throw new NotImplementedException();
  440. }
  441. public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  442. {
  443. //---
  444. float[, ] arrayMatrix1 = new float[4, 4];
  445. float[, ] arrayMatrix2 = new float[4, 4];
  446. float[, ] arrayMatrixProduct = new float[4, 4];
  447. arrayMatrix1[0, 0] = matrix1.M11; arrayMatrix1[0, 1] = matrix1.M12; arrayMatrix1[0, 2] = matrix1.M13; arrayMatrix1[0, 3] = matrix1.M14;
  448. arrayMatrix1[1, 0] = matrix1.M21; arrayMatrix1[1, 1] = matrix1.M22; arrayMatrix1[1, 2] = matrix1.M23; arrayMatrix1[1, 3] = matrix1.M24;
  449. arrayMatrix1[2, 0] = matrix1.M31; arrayMatrix1[2, 1] = matrix1.M32; arrayMatrix1[2, 2] = matrix1.M33; arrayMatrix1[2, 3] = matrix1.M34;
  450. arrayMatrix1[3, 0] = matrix1.M41; arrayMatrix1[3, 1] = matrix1.M42; arrayMatrix1[3, 2] = matrix1.M43; arrayMatrix1[3, 3] = matrix1.M44;
  451. arrayMatrix2[0, 0] = matrix2.M11; arrayMatrix2[0, 1] = matrix2.M12; arrayMatrix2[0, 2] = matrix2.M13; arrayMatrix2[0, 3] = matrix2.M14;
  452. arrayMatrix2[1, 0] = matrix2.M21; arrayMatrix2[1, 1] = matrix2.M22; arrayMatrix2[1, 2] = matrix2.M23; arrayMatrix2[1, 3] = matrix2.M24;
  453. arrayMatrix2[2, 0] = matrix2.M31; arrayMatrix2[2, 1] = matrix2.M32; arrayMatrix2[2, 2] = matrix2.M33; arrayMatrix2[2, 3] = matrix2.M34;
  454. arrayMatrix2[3, 0] = matrix2.M41; arrayMatrix2[3, 1] = matrix2.M42; arrayMatrix2[3, 2] = matrix2.M43; arrayMatrix2[3, 3] = matrix2.M44;
  455. int n = 4;
  456. for (int i = 0; i < n; i++)
  457. {
  458. for (int j = 0; j < n; j++)
  459. {
  460. // (AB)[i,j] = Sum(k=0; k < 4; k++) { A[i,k] * B[k, j] }
  461. for (int k = 0; k < n; k++)
  462. {
  463. arrayMatrixProduct[i, j] += arrayMatrix1[i, k] * arrayMatrix2[k, j];
  464. }
  465. }
  466. }
  467. return new Matrix( arrayMatrixProduct[0, 0], arrayMatrixProduct[0, 1], arrayMatrixProduct[0, 2], arrayMatrixProduct[0, 3],
  468. arrayMatrixProduct[1, 0], arrayMatrixProduct[1, 1], arrayMatrixProduct[1, 2], arrayMatrixProduct[1, 3],
  469. arrayMatrixProduct[2, 0], arrayMatrixProduct[2, 1], arrayMatrixProduct[2, 2], arrayMatrixProduct[2, 3],
  470. arrayMatrixProduct[3, 1], arrayMatrixProduct[3, 1], arrayMatrixProduct[3, 2], arrayMatrixProduct[3, 3]);
  471. //---
  472. //throw new NotImplementedException();
  473. }
  474. public static Matrix operator *(Matrix matrix, float scaleFactor)
  475. {
  476. throw new NotImplementedException();
  477. }
  478. public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  479. {
  480. throw new NotImplementedException();
  481. }
  482. public static Matrix operator -(Matrix matrix1)
  483. {
  484. throw new NotImplementedException();
  485. }
  486. public static Matrix Subtract(Matrix matrix1, Matrix matrix2)
  487. {
  488. throw new NotImplementedException();
  489. }
  490. public static void Subtract(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
  491. {
  492. throw new NotImplementedException();
  493. }
  494. public override string ToString()
  495. {
  496. return "[(" + this.M11 + ", " + this.M12 + ", " + this.M13 + ", " + this.M14 + ")\n ("
  497. + this.M21 + ", " + this.M22 + ", " + this.M23 + ", " + this.M24 + ")\n ("
  498. + this.M31 + ", " + this.M32 + ", " + this.M33 + ", " + this.M34 + ")\n ("
  499. + this.M41 + ", " + this.M42 + ", " + this.M43 + ", " + this.M44 + ")]";
  500. }
  501. public static Matrix Transpose(Matrix matrix)
  502. {
  503. //---
  504. return new Matrix( matrix.M11, matrix.M21, matrix.M31, matrix.M41,
  505. matrix.M12, matrix.M22, matrix.M32, matrix.M42,
  506. matrix.M13, matrix.M23, matrix.M33, matrix.M43,
  507. matrix.M14, matrix.M24, matrix.M34, matrix.M44);
  508. //---
  509. //throw new NotImplementedException();
  510. }
  511. public static void Transpose(ref Matrix matrix, out Matrix result)
  512. {
  513. throw new NotImplementedException();
  514. }
  515. #endregion Public Methods
  516. }
  517. }