Sphere.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #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 kClassType = CollisionShapeType::kSphere;
  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. ANKI_ASSERT(x > 0.0f);
  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. ANKI_ASSERT(transform.hasUniformScale());
  82. out.m_radius = m_radius * transform.getScale().x;
  83. return out;
  84. }
  85. /// Get the sphere that includes this sphere and the given. See a drawing in the docs dir for more info about the
  86. /// algorithm
  87. Sphere getCompoundShape(const Sphere& b) const;
  88. /// Compute the GJK support.
  89. Vec4 computeSupport(const Vec4& dir) const
  90. {
  91. return m_center + dir.normalize() * m_radius;
  92. }
  93. private:
  94. Vec4 m_center
  95. #if ANKI_ASSERTIONS_ENABLED
  96. = Vec4(kMaxF32)
  97. #endif
  98. ;
  99. F32 m_radius
  100. #if ANKI_ASSERTIONS_ENABLED
  101. = -1.0f
  102. #endif
  103. ;
  104. void check() const
  105. {
  106. ANKI_ASSERT(m_center.w == 0.0f);
  107. ANKI_ASSERT(m_radius > 0.0f);
  108. }
  109. };
  110. /// @}
  111. } // end namespace anki