b3Mat3x3.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef B3_MAT3x3_H
  2. #define B3_MAT3x3_H
  3. #include "Bullet3Common/shared/b3Quat.h"
  4. #ifdef __cplusplus
  5. #include "Bullet3Common/b3Matrix3x3.h"
  6. #define b3Mat3x3 b3Matrix3x3
  7. #define b3Mat3x3ConstArg const b3Matrix3x3&
  8. inline b3Mat3x3 b3QuatGetRotationMatrix(b3QuatConstArg quat)
  9. {
  10. return b3Mat3x3(quat);
  11. }
  12. inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg mat)
  13. {
  14. return mat.absolute();
  15. }
  16. #define b3GetRow(m, row) m.getRow(row)
  17. __inline b3Float4 mtMul3(b3Float4ConstArg a, b3Mat3x3ConstArg b)
  18. {
  19. return b * a;
  20. }
  21. #else
  22. typedef struct
  23. {
  24. b3Float4 m_row[3];
  25. } b3Mat3x3;
  26. #define b3Mat3x3ConstArg const b3Mat3x3
  27. #define b3GetRow(m, row) (m.m_row[row])
  28. inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)
  29. {
  30. b3Float4 quat2 = (b3Float4)(quat.x * quat.x, quat.y * quat.y, quat.z * quat.z, 0.f);
  31. b3Mat3x3 out;
  32. out.m_row[0].x = 1 - 2 * quat2.y - 2 * quat2.z;
  33. out.m_row[0].y = 2 * quat.x * quat.y - 2 * quat.w * quat.z;
  34. out.m_row[0].z = 2 * quat.x * quat.z + 2 * quat.w * quat.y;
  35. out.m_row[0].w = 0.f;
  36. out.m_row[1].x = 2 * quat.x * quat.y + 2 * quat.w * quat.z;
  37. out.m_row[1].y = 1 - 2 * quat2.x - 2 * quat2.z;
  38. out.m_row[1].z = 2 * quat.y * quat.z - 2 * quat.w * quat.x;
  39. out.m_row[1].w = 0.f;
  40. out.m_row[2].x = 2 * quat.x * quat.z - 2 * quat.w * quat.y;
  41. out.m_row[2].y = 2 * quat.y * quat.z + 2 * quat.w * quat.x;
  42. out.m_row[2].z = 1 - 2 * quat2.x - 2 * quat2.y;
  43. out.m_row[2].w = 0.f;
  44. return out;
  45. }
  46. inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)
  47. {
  48. b3Mat3x3 out;
  49. out.m_row[0] = fabs(matIn.m_row[0]);
  50. out.m_row[1] = fabs(matIn.m_row[1]);
  51. out.m_row[2] = fabs(matIn.m_row[2]);
  52. return out;
  53. }
  54. __inline b3Mat3x3 mtZero();
  55. __inline b3Mat3x3 mtIdentity();
  56. __inline b3Mat3x3 mtTranspose(b3Mat3x3 m);
  57. __inline b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);
  58. __inline b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);
  59. __inline b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);
  60. __inline b3Mat3x3 mtZero()
  61. {
  62. b3Mat3x3 m;
  63. m.m_row[0] = (b3Float4)(0.f);
  64. m.m_row[1] = (b3Float4)(0.f);
  65. m.m_row[2] = (b3Float4)(0.f);
  66. return m;
  67. }
  68. __inline b3Mat3x3 mtIdentity()
  69. {
  70. b3Mat3x3 m;
  71. m.m_row[0] = (b3Float4)(1, 0, 0, 0);
  72. m.m_row[1] = (b3Float4)(0, 1, 0, 0);
  73. m.m_row[2] = (b3Float4)(0, 0, 1, 0);
  74. return m;
  75. }
  76. __inline b3Mat3x3 mtTranspose(b3Mat3x3 m)
  77. {
  78. b3Mat3x3 out;
  79. out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
  80. out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
  81. out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
  82. return out;
  83. }
  84. __inline b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)
  85. {
  86. b3Mat3x3 transB;
  87. transB = mtTranspose(b);
  88. b3Mat3x3 ans;
  89. // why this doesn't run when 0ing in the for{}
  90. a.m_row[0].w = 0.f;
  91. a.m_row[1].w = 0.f;
  92. a.m_row[2].w = 0.f;
  93. for (int i = 0; i < 3; i++)
  94. {
  95. // a.m_row[i].w = 0.f;
  96. ans.m_row[i].x = b3Dot3F4(a.m_row[i], transB.m_row[0]);
  97. ans.m_row[i].y = b3Dot3F4(a.m_row[i], transB.m_row[1]);
  98. ans.m_row[i].z = b3Dot3F4(a.m_row[i], transB.m_row[2]);
  99. ans.m_row[i].w = 0.f;
  100. }
  101. return ans;
  102. }
  103. __inline b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)
  104. {
  105. b3Float4 ans;
  106. ans.x = b3Dot3F4(a.m_row[0], b);
  107. ans.y = b3Dot3F4(a.m_row[1], b);
  108. ans.z = b3Dot3F4(a.m_row[2], b);
  109. ans.w = 0.f;
  110. return ans;
  111. }
  112. __inline b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)
  113. {
  114. b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  115. b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  116. b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  117. b3Float4 ans;
  118. ans.x = b3Dot3F4(a, colx);
  119. ans.y = b3Dot3F4(a, coly);
  120. ans.z = b3Dot3F4(a, colz);
  121. return ans;
  122. }
  123. #endif
  124. #endif //B3_MAT3x3_H