b3Aabb.h 1.7 KB

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