2
0

b3Mat3x3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  18. b3Float4 mtMul3(b3Float4ConstArg a, b3Mat3x3ConstArg b)
  19. {
  20. return b*a;
  21. }
  22. #else
  23. typedef struct
  24. {
  25. b3Float4 m_row[3];
  26. }b3Mat3x3;
  27. #define b3Mat3x3ConstArg const b3Mat3x3
  28. #define b3GetRow(m,row) (m.m_row[row])
  29. inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)
  30. {
  31. b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);
  32. b3Mat3x3 out;
  33. out.m_row[0].x=1-2*quat2.y-2*quat2.z;
  34. out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;
  35. out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;
  36. out.m_row[0].w = 0.f;
  37. out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;
  38. out.m_row[1].y=1-2*quat2.x-2*quat2.z;
  39. out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;
  40. out.m_row[1].w = 0.f;
  41. out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;
  42. out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;
  43. out.m_row[2].z=1-2*quat2.x-2*quat2.y;
  44. out.m_row[2].w = 0.f;
  45. return out;
  46. }
  47. inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)
  48. {
  49. b3Mat3x3 out;
  50. out.m_row[0] = fabs(matIn.m_row[0]);
  51. out.m_row[1] = fabs(matIn.m_row[1]);
  52. out.m_row[2] = fabs(matIn.m_row[2]);
  53. return out;
  54. }
  55. __inline
  56. b3Mat3x3 mtZero();
  57. __inline
  58. b3Mat3x3 mtIdentity();
  59. __inline
  60. b3Mat3x3 mtTranspose(b3Mat3x3 m);
  61. __inline
  62. b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);
  63. __inline
  64. b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);
  65. __inline
  66. b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);
  67. __inline
  68. b3Mat3x3 mtZero()
  69. {
  70. b3Mat3x3 m;
  71. m.m_row[0] = (b3Float4)(0.f);
  72. m.m_row[1] = (b3Float4)(0.f);
  73. m.m_row[2] = (b3Float4)(0.f);
  74. return m;
  75. }
  76. __inline
  77. b3Mat3x3 mtIdentity()
  78. {
  79. b3Mat3x3 m;
  80. m.m_row[0] = (b3Float4)(1,0,0,0);
  81. m.m_row[1] = (b3Float4)(0,1,0,0);
  82. m.m_row[2] = (b3Float4)(0,0,1,0);
  83. return m;
  84. }
  85. __inline
  86. b3Mat3x3 mtTranspose(b3Mat3x3 m)
  87. {
  88. b3Mat3x3 out;
  89. out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
  90. out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
  91. out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
  92. return out;
  93. }
  94. __inline
  95. b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)
  96. {
  97. b3Mat3x3 transB;
  98. transB = mtTranspose( b );
  99. b3Mat3x3 ans;
  100. // why this doesn't run when 0ing in the for{}
  101. a.m_row[0].w = 0.f;
  102. a.m_row[1].w = 0.f;
  103. a.m_row[2].w = 0.f;
  104. for(int i=0; i<3; i++)
  105. {
  106. // a.m_row[i].w = 0.f;
  107. ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);
  108. ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);
  109. ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);
  110. ans.m_row[i].w = 0.f;
  111. }
  112. return ans;
  113. }
  114. __inline
  115. b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)
  116. {
  117. b3Float4 ans;
  118. ans.x = b3Dot3F4( a.m_row[0], b );
  119. ans.y = b3Dot3F4( a.m_row[1], b );
  120. ans.z = b3Dot3F4( a.m_row[2], b );
  121. ans.w = 0.f;
  122. return ans;
  123. }
  124. __inline
  125. b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)
  126. {
  127. b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  128. b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  129. b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  130. b3Float4 ans;
  131. ans.x = b3Dot3F4( a, colx );
  132. ans.y = b3Dot3F4( a, coly );
  133. ans.z = b3Dot3F4( a, colz );
  134. return ans;
  135. }
  136. #endif
  137. #endif //B3_MAT3x3_H