Sphere.h 2.3 KB

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