b3Aabb.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef B3_AABB_H
  2. #define B3_AABB_H
  3. #include "Bullet3Common/shared/b3Float4.h"
  4. #include "Bullet3Common/shared/b3Mat3x3.h"
  5. typedef struct b3Aabb b3Aabb_t;
  6. struct b3Aabb
  7. {
  8. union
  9. {
  10. float m_min[4];
  11. b3Float4 m_minVec;
  12. int m_minIndices[4];
  13. };
  14. union
  15. {
  16. float m_max[4];
  17. b3Float4 m_maxVec;
  18. int m_signedMaxIndices[4];
  19. };
  20. };
  21. inline void b3TransformAabb2(b3Float4ConstArg localAabbMin,b3Float4ConstArg localAabbMax, float margin,
  22. b3Float4ConstArg pos,
  23. b3QuatConstArg orn,
  24. b3Float4* aabbMinOut,b3Float4* aabbMaxOut)
  25. {
  26. b3Float4 localHalfExtents = 0.5f*(localAabbMax-localAabbMin);
  27. localHalfExtents+=b3MakeFloat4(margin,margin,margin,0.f);
  28. b3Float4 localCenter = 0.5f*(localAabbMax+localAabbMin);
  29. b3Mat3x3 m;
  30. m = b3QuatGetRotationMatrix(orn);
  31. b3Mat3x3 abs_b = b3AbsoluteMat3x3(m);
  32. b3Float4 center = b3TransformPoint(localCenter,pos,orn);
  33. b3Float4 extent = b3MakeFloat4(b3Dot3F4(localHalfExtents,b3GetRow(abs_b,0)),
  34. b3Dot3F4(localHalfExtents,b3GetRow(abs_b,1)),
  35. b3Dot3F4(localHalfExtents,b3GetRow(abs_b,2)),
  36. 0.f);
  37. *aabbMinOut = center-extent;
  38. *aabbMaxOut = center+extent;
  39. }
  40. /// conservative test for overlap between two aabbs
  41. inline bool b3TestAabbAgainstAabb(b3Float4ConstArg aabbMin1,b3Float4ConstArg aabbMax1,
  42. b3Float4ConstArg aabbMin2, b3Float4ConstArg aabbMax2)
  43. {
  44. bool overlap = true;
  45. overlap = (aabbMin1.x > aabbMax2.x || aabbMax1.x < aabbMin2.x) ? false : overlap;
  46. overlap = (aabbMin1.z > aabbMax2.z || aabbMax1.z < aabbMin2.z) ? false : overlap;
  47. overlap = (aabbMin1.y > aabbMax2.y || aabbMax1.y < aabbMin2.y) ? false : overlap;
  48. return overlap;
  49. }
  50. #endif //B3_AABB_H