Aabb.cpp 1.9 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. {
  8. Aabb Aabb::getTransformed(const Transform& trf) const
  9. {
  10. Mat3x4 absM;
  11. for(U i = 0; i < 12; ++i)
  12. {
  13. absM[i] = absolute(trf.getRotation()[i]);
  14. }
  15. Vec4 center = (m_min + m_max) * 0.5f;
  16. Vec4 extend = (m_max - m_min) * 0.5f;
  17. Vec4 newC = trf.transform(center);
  18. Vec4 newE = Vec4(absM * (extend * trf.getScale()), 0.0f);
  19. return Aabb(newC - newE, newC + newE);
  20. }
  21. Aabb Aabb::getCompoundShape(const Aabb& b) const
  22. {
  23. Aabb out;
  24. out.m_min.w() = out.m_max.w() = 0.0f;
  25. for(U i = 0; i < 3; i++)
  26. {
  27. out.m_min[i] = (m_min[i] < b.m_min[i]) ? m_min[i] : b.m_min[i];
  28. out.m_max[i] = (m_max[i] > b.m_max[i]) ? m_max[i] : b.m_max[i];
  29. }
  30. return out;
  31. }
  32. void Aabb::setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  33. {
  34. // Preconditions
  35. ANKI_ASSERT(pointBuffer);
  36. ANKI_ASSERT(pointCount > 1);
  37. ANKI_ASSERT(pointStride >= sizeof(Vec3));
  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