FunctionsComputeAabb.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/Functions.h>
  6. #include <AnKi/Collision/ConvexHullShape.h>
  7. #include <AnKi/Collision/Obb.h>
  8. #include <AnKi/Collision/LineSegment.h>
  9. #include <AnKi/Collision/Cone.h>
  10. #include <AnKi/Collision/Sphere.h>
  11. namespace anki
  12. {
  13. Aabb computeAabb(const Sphere& sphere)
  14. {
  15. Aabb aabb;
  16. aabb.setMin((sphere.getCenter() - sphere.getRadius()).xyz0());
  17. aabb.setMax((sphere.getCenter() + sphere.getRadius()).xyz0());
  18. return aabb;
  19. }
  20. Aabb computeAabb(const Obb& obb)
  21. {
  22. Mat3x4 absM;
  23. for(U i = 0; i < 12; ++i)
  24. {
  25. absM[i] = absolute(obb.getRotation()[i]);
  26. }
  27. const Vec4 newE = Vec4(absM * obb.getExtend(), 0.0f);
  28. // Add a small epsilon to avoid some assertions
  29. const Vec4 epsilon(Vec3(EPSILON * 100.0f), 0.0f);
  30. return Aabb(obb.getCenter() - newE, obb.getCenter() + newE + epsilon);
  31. }
  32. Aabb computeAabb(const ConvexHullShape& hull)
  33. {
  34. Vec4 mina(MAX_F32);
  35. Vec4 maxa(MIN_F32);
  36. for(const Vec4& point : hull.getPoints())
  37. {
  38. const Vec4 o = (hull.isTransformIdentity()) ? point : hull.getTransform().transform(point);
  39. mina = mina.min(o);
  40. maxa = maxa.max(o);
  41. }
  42. return Aabb(mina.xyz0(), maxa.xyz0());
  43. }
  44. Aabb computeAabb(const LineSegment& ls)
  45. {
  46. const Vec4 p0 = ls.getOrigin();
  47. const Vec4 p1 = ls.getOrigin() + ls.getDirection();
  48. const Vec4 min = p0.min(p1);
  49. const Vec4 max = p0.max(p1);
  50. return Aabb(min, max);
  51. }
  52. } // end namespace anki