Aabb.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2009-2021, 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(U i = 0; i < 12; ++i)
  11. {
  12. absM[i] = absolute(trf.getRotation()[i]);
  13. }
  14. Vec4 center = (m_min + m_max) * 0.5f;
  15. Vec4 extend = (m_max - m_min) * 0.5f;
  16. Vec4 newC = trf.transform(center);
  17. Vec4 newE = Vec4(absM * (extend * trf.getScale()), 0.0f);
  18. return Aabb(newC - newE, newC + newE);
  19. }
  20. Aabb Aabb::getCompoundShape(const Aabb& b) const
  21. {
  22. Aabb out;
  23. out.m_min.w() = out.m_max.w() = 0.0f;
  24. for(U i = 0; i < 3; i++)
  25. {
  26. out.m_min[i] = (m_min[i] < b.m_min[i]) ? m_min[i] : b.m_min[i];
  27. out.m_max[i] = (m_max[i] > b.m_max[i]) ? m_max[i] : b.m_max[i];
  28. }
  29. return out;
  30. }
  31. void Aabb::setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  32. {
  33. // Preconditions
  34. ANKI_ASSERT(pointBuffer);
  35. ANKI_ASSERT(pointCount > 1);
  36. ANKI_ASSERT(pointStride >= sizeof(Vec3));
  37. ANKI_ASSERT((pointStride % sizeof(F32)) == 0 && "Weird strides that breaks strict aliasing rules");
  38. ANKI_ASSERT(buffSize >= pointStride * pointCount);
  39. m_min = Vec4(Vec3(MAX_F32), 0.0f);
  40. m_max = Vec4(Vec3(MIN_F32), 0.0f);
  41. // Iterate
  42. const U8* ptr = reinterpret_cast<const U8*>(pointBuffer);
  43. while(pointCount-- != 0)
  44. {
  45. ANKI_ASSERT((ptrToNumber(ptr) + sizeof(Vec3) - ptrToNumber(pointBuffer)) <= buffSize);
  46. const Vec3& pos = *reinterpret_cast<const Vec3*>(ptr);
  47. for(U j = 0; j < 3; j++)
  48. {
  49. if(pos[j] > m_max[j])
  50. {
  51. m_max[j] = pos[j];
  52. }
  53. else if(pos[j] < m_min[j])
  54. {
  55. m_min[j] = pos[j];
  56. }
  57. }
  58. ptr += pointStride;
  59. }
  60. }
  61. Vec4 Aabb::computeSupport(const Vec4& dir) const
  62. {
  63. Vec4 ret(0.0f);
  64. ret.x() = (dir.x() >= 0.0f) ? m_max.x() : m_min.x();
  65. ret.y() = (dir.y() >= 0.0f) ? m_max.y() : m_min.y();
  66. ret.z() = (dir.z() >= 0.0f) ? m_max.z() : m_min.z();
  67. return ret;
  68. }
  69. } // end namespace anki