Aabb.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Collision/Aabb.h>
  6. namespace anki {
  7. Aabb Aabb::getTransformed(const Transform& trf) const
  8. {
  9. Mat3x4 absM;
  10. for(U32 i = 0; i < 12; ++i)
  11. {
  12. absM[i] = absolute(trf.getRotation()[i]);
  13. }
  14. const Vec4 center = (m_min + m_max) * 0.5f;
  15. const Vec4 extend = (m_max - m_min) * 0.5f;
  16. const Vec4 newC = trf.transform(center);
  17. const Vec4 newE = Vec4(absM * (extend * trf.getScale()), 0.0f);
  18. const Vec4 min = newC - newE;
  19. const Vec4 max = newC + newE;
  20. const F32 epsilon = kEpsilonf * 100.0f;
  21. return Aabb(min, max + Vec4(epsilon, epsilon, epsilon, 0.0f));
  22. }
  23. Aabb Aabb::getCompoundShape(const Aabb& b) const
  24. {
  25. Aabb out;
  26. out.m_min.w = out.m_max.w = 0.0f;
  27. for(U32 i = 0; i < 3; i++)
  28. {
  29. out.m_min[i] = (m_min[i] < b.m_min[i]) ? m_min[i] : b.m_min[i];
  30. out.m_max[i] = (m_max[i] > b.m_max[i]) ? m_max[i] : b.m_max[i];
  31. }
  32. return out;
  33. }
  34. void Aabb::setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, [[maybe_unused]] PtrSize buffSize)
  35. {
  36. // Preconditions
  37. ANKI_ASSERT(pointBuffer);
  38. ANKI_ASSERT(pointCount > 1);
  39. ANKI_ASSERT(pointStride >= sizeof(Vec3));
  40. ANKI_ASSERT((pointStride % sizeof(F32)) == 0 && "Weird strides that breaks strict aliasing rules");
  41. ANKI_ASSERT(buffSize >= pointStride * pointCount);
  42. m_min = Vec4(Vec3(kMaxF32), 0.0f);
  43. m_max = Vec4(Vec3(kMinF32), 0.0f);
  44. // Iterate
  45. const U8* ptr = reinterpret_cast<const U8*>(pointBuffer);
  46. while(pointCount-- != 0)
  47. {
  48. ANKI_ASSERT((ptrToNumber(ptr) + sizeof(Vec3) - ptrToNumber(pointBuffer)) <= buffSize);
  49. const Vec3& pos = *reinterpret_cast<const Vec3*>(ptr);
  50. for(U32 j = 0; j < 3; j++)
  51. {
  52. if(pos[j] > m_max[j])
  53. {
  54. m_max[j] = pos[j];
  55. }
  56. else if(pos[j] < m_min[j])
  57. {
  58. m_min[j] = pos[j];
  59. }
  60. }
  61. ptr += pointStride;
  62. }
  63. }
  64. Vec4 Aabb::computeSupport(const Vec4& dir) const
  65. {
  66. Vec4 ret(0.0f);
  67. ret.x = (dir.x >= 0.0f) ? m_max.x : m_min.x;
  68. ret.y = (dir.y >= 0.0f) ? m_max.y : m_min.y;
  69. ret.z = (dir.z >= 0.0f) ? m_max.z : m_min.z;
  70. return ret;
  71. }
  72. } // end namespace anki