FunctionsComputeAabb.cpp 1.5 KB

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