sphere.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "math_utils.h"
  26. #include "vector3.h"
  27. namespace crown
  28. {
  29. /// Sphere.
  30. ///
  31. /// Used mainly for collision detection and intersection tests.
  32. class Sphere
  33. {
  34. public:
  35. /// Does nothing for efficiency.
  36. Sphere();
  37. /// Constructs from @a center and @a radius.
  38. Sphere(const Vector3& center, float radius);
  39. Sphere(const Sphere& a);
  40. const Vector3& center() const;
  41. float radius() const;
  42. float volume() const;
  43. void set_center(const Vector3& center);
  44. void set_radius(float radius);
  45. /// Adds @a count @a points to the sphere expanding if necessary.
  46. void add_points(const Vector3* points, uint32_t count);
  47. /// Adds @a count @a spheres expanding if necessary.
  48. void add_spheres(const Sphere* spheres, uint32_t count);
  49. /// Returns whether point @a p is contained into the sphere.
  50. bool contains_point(const Vector3& p) const;
  51. private:
  52. Vector3 m_center;
  53. float m_radius;
  54. };
  55. //-----------------------------------------------------------------------------
  56. inline Sphere::Sphere()
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. inline Sphere::Sphere(const Vector3& center, float radius) : m_center(center), m_radius(radius)
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. inline Sphere::Sphere(const Sphere& a) : m_center(a.m_center), m_radius(a.m_radius)
  65. {
  66. }
  67. //-----------------------------------------------------------------------------
  68. inline const Vector3& Sphere::center() const
  69. {
  70. return m_center;
  71. }
  72. //-----------------------------------------------------------------------------
  73. inline float Sphere::radius() const
  74. {
  75. return m_radius;
  76. }
  77. //-----------------------------------------------------------------------------
  78. inline float Sphere::volume() const
  79. {
  80. return math::FOUR_OVER_THREE_TIMES_PI * m_radius * m_radius * m_radius;
  81. }
  82. //-----------------------------------------------------------------------------
  83. inline void Sphere::set_center(const Vector3& center)
  84. {
  85. m_center = center;
  86. }
  87. //-----------------------------------------------------------------------------
  88. inline void Sphere::set_radius(float radius)
  89. {
  90. m_radius = radius;
  91. }
  92. //-----------------------------------------------------------------------------
  93. inline void Sphere::add_points(const Vector3* points, uint32_t count)
  94. {
  95. for (uint32_t i = 0; i < count; i++)
  96. {
  97. const Vector3& p = points[i];
  98. float dist = vector3::squared_length(p - m_center);
  99. if (dist >= m_radius * m_radius)
  100. {
  101. m_radius = math::sqrt(dist);
  102. }
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. inline void Sphere::add_spheres(const Sphere* spheres, uint32_t count)
  107. {
  108. for (uint32_t i = 0; i < count; i++)
  109. {
  110. const Sphere& s = spheres[i];
  111. float dist = vector3::squared_length(s.m_center - m_center);
  112. if (dist < (s.m_radius + m_radius) * (s.m_radius + m_radius))
  113. {
  114. if (s.m_radius * s.m_radius > m_radius * m_radius)
  115. {
  116. m_radius = math::sqrt(dist + s.m_radius * s.m_radius);
  117. }
  118. }
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. inline bool Sphere::contains_point(const Vector3& p) const
  123. {
  124. float dist = vector3::squared_length(p - m_center);
  125. return (dist < m_radius * m_radius);
  126. }
  127. } // namespace crown