Aabb.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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((pointStride % sizeof(F32)) == 0 && "Weird strides that breaks strict aliasing rules");
  39. ANKI_ASSERT(buffSize >= pointStride * pointCount);
  40. m_min = Vec4(Vec3(MAX_F32), 0.0f);
  41. m_max = Vec4(Vec3(MIN_F32), 0.0f);
  42. // Iterate
  43. const U8* ptr = reinterpret_cast<const U8*>(pointBuffer);
  44. while(pointCount-- != 0)
  45. {
  46. ANKI_ASSERT((ptrToNumber(ptr) + sizeof(Vec3) - ptrToNumber(pointBuffer)) <= buffSize);
  47. const Vec3& pos = *reinterpret_cast<const Vec3*>(ptr);
  48. for(U j = 0; j < 3; j++)
  49. {
  50. if(pos[j] > m_max[j])
  51. {
  52. m_max[j] = pos[j];
  53. }
  54. else if(pos[j] < m_min[j])
  55. {
  56. m_min[j] = pos[j];
  57. }
  58. }
  59. ptr += pointStride;
  60. }
  61. }
  62. Vec4 Aabb::computeSupport(const Vec4& dir) const
  63. {
  64. Vec4 ret(0.0f);
  65. ret.x() = (dir.x() >= 0.0f) ? m_max.x() : m_min.x();
  66. ret.y() = (dir.y() >= 0.0f) ? m_max.y() : m_min.y();
  67. ret.z() = (dir.z() >= 0.0f) ? m_max.z() : m_min.z();
  68. return ret;
  69. }
  70. } // end namespace anki