Sphere.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #pragma once
  6. #include <AnKi/Collision/Common.h>
  7. namespace anki {
  8. /// @addtogroup collision
  9. /// @{
  10. /// Sphere collision shape.
  11. class Sphere
  12. {
  13. public:
  14. static constexpr CollisionShapeType CLASS_TYPE = CollisionShapeType::SPHERE;
  15. /// Will not initialize any memory, nothing.
  16. Sphere()
  17. {
  18. }
  19. /// Copy constructor
  20. Sphere(const Sphere& b)
  21. {
  22. operator=(b);
  23. }
  24. /// Constructor
  25. Sphere(const Vec4& center, F32 radius)
  26. : m_center(center)
  27. , m_radius(radius)
  28. {
  29. check();
  30. }
  31. /// Constructor
  32. Sphere(const Vec3& center, F32 radius)
  33. : m_center(center.xyz0())
  34. , m_radius(radius)
  35. {
  36. check();
  37. }
  38. /// Set from point cloud.
  39. Sphere(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  40. {
  41. setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
  42. }
  43. Sphere& operator=(const Sphere& b)
  44. {
  45. b.check();
  46. m_center = b.m_center;
  47. m_radius = b.m_radius;
  48. return *this;
  49. }
  50. const Vec4& getCenter() const
  51. {
  52. check();
  53. return m_center;
  54. }
  55. void setCenter(const Vec4& x)
  56. {
  57. ANKI_ASSERT(x.w() == 0.0f);
  58. m_center = x;
  59. }
  60. void setCenter(const Vec3& x)
  61. {
  62. m_center = x.xyz0();
  63. }
  64. F32 getRadius() const
  65. {
  66. check();
  67. return m_radius;
  68. }
  69. void setRadius(const F32 x)
  70. {
  71. m_radius = x;
  72. }
  73. /// Calculate from a set of points
  74. void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
  75. Sphere getTransformed(const Transform& transform) const
  76. {
  77. check();
  78. Sphere out;
  79. out.m_center = transform.transform(m_center);
  80. out.m_radius = m_radius * transform.getScale();
  81. return out;
  82. }
  83. /// Get the sphere that includes this sphere and the given. See a drawing in the docs dir for more info about the
  84. /// algorithm
  85. Sphere getCompoundShape(const Sphere& b) const;
  86. /// Compute the GJK support.
  87. Vec4 computeSupport(const Vec4& dir) const
  88. {
  89. return m_center + dir.getNormalized() * m_radius;
  90. }
  91. private:
  92. Vec4 m_center
  93. #if ANKI_ENABLE_ASSERTIONS
  94. = Vec4(MAX_F32)
  95. #endif
  96. ;
  97. F32 m_radius
  98. #if ANKI_ENABLE_ASSERTIONS
  99. = -1.0f
  100. #endif
  101. ;
  102. void check() const
  103. {
  104. ANKI_ASSERT(m_center.w() == 0.0f);
  105. ANKI_ASSERT(m_radius > 0.0f);
  106. }
  107. };
  108. /// @}
  109. } // end namespace anki