Matrix3x3.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //**************************************************
  2. // Copyright©2018 何冠峰
  3. // Licensed under the MIT license
  4. //**************************************************
  5. namespace PF
  6. {
  7. public struct Matrix3x3
  8. {
  9. public float[] Data;
  10. public Matrix3x3(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8)
  11. {
  12. Data = new float[9];
  13. Data[0] = m0; Data[3] = m3; Data[6] = m6;
  14. Data[1] = m1; Data[4] = m4; Data[7] = m7;
  15. Data[2] = m2; Data[5] = m5; Data[8] = m8;
  16. }
  17. public static Matrix3x3 identity
  18. {
  19. get
  20. {
  21. return new Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  22. }
  23. }
  24. public void SetZero()
  25. {
  26. Data[0] = 0f; Data[3] = 0f; Data[6] = 0f;
  27. Data[1] = 0f; Data[4] = 0f; Data[7] = 0f;
  28. Data[2] = 0f; Data[5] = 0f; Data[8] = 0f;
  29. //The floats are laid out
  30. // m0 m3 m6
  31. // m1 m4 m7
  32. // m2 m5 m8
  33. //Data[0]=m00; Data[3]=m01; Data[6]=m02;
  34. //Data[1]=m10; Data[4]=m11; Data[7]=m12;
  35. //Data[2]=m20; Data[5]=m21; Data[8]=m22;
  36. //Get(0, 0) Get(0, 1) Get(0, 2)
  37. //Get(1, 0) Get(1, 1) Get(1, 2)
  38. //Get(2, 0) Get(2, 1) Get(2, 2)
  39. }
  40. public float Get(int row, int column)
  41. {
  42. return Data[row + (column * 3)];
  43. }
  44. public void Set(int row, int column, float value)
  45. {
  46. Data[row + (column * 3)] = value;
  47. }
  48. public void SetOrthoNormalBasis(Vector3 inX, Vector3 inY, Vector3 inZ)
  49. {
  50. this.Set(0, 0, inX.x); this.Set(0, 1, inY.x); this.Set(0, 2, inZ.x);
  51. this.Set(1, 0, inX.y); this.Set(1, 1, inY.y); this.Set(1, 2, inZ.y);
  52. this.Set(2, 0, inX.z); this.Set(2, 1, inY.z); this.Set(2, 2, inZ.z);
  53. }
  54. public float GetDeterminant()
  55. {
  56. float fCofactor0 = Get(0, 0) * Get(1, 1) * Get(2, 2);
  57. float fCofactor1 = Get(0, 1) * Get(1, 2) * Get(2, 0);
  58. float fCofactor2 = Get(0, 2) * Get(1, 0) * Get(2, 1);
  59. float fCofactor3 = Get(0, 2) * Get(1, 1) * Get(2, 0);
  60. float fCofactor4 = Get(0, 1) * Get(1, 0) * Get(2, 2);
  61. float fCofactor5 = Get(0, 0) * Get(1, 2) * Get(2, 1);
  62. return fCofactor0 + fCofactor1 + fCofactor2 - fCofactor3 - fCofactor4 - fCofactor5;
  63. }
  64. // Right handed
  65. public static bool LookRotationToMatrix(Vector3 viewVec, Vector3 upVec, out Matrix3x3 m)
  66. {
  67. m = Matrix3x3.identity;
  68. Vector3 z = viewVec;
  69. // compute u0
  70. float mag = z.Length();
  71. if (mag < Mathf.Epsilon)
  72. {
  73. return false;
  74. }
  75. z /= mag;
  76. Vector3 x = Vector3.Cross(upVec, z);
  77. mag = x.Length();
  78. if (mag < Mathf.Epsilon)
  79. {
  80. return false;
  81. }
  82. x /= mag;
  83. Vector3 y = Vector3.Cross(z, x);
  84. if (!Mathf.CompareApproximate(y.Length(), 1.0F))
  85. return false;
  86. m.SetOrthoNormalBasis(x, y, z);
  87. return true;
  88. }
  89. }
  90. }