💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.9 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_GEOM_SPHERE_HPP
  20. #define ANTKEEPER_GEOM_SPHERE_HPP
  21. #include "geom/bounding-volume.hpp"
  22. #include "geom/aabb.hpp"
  23. #include "math/math.hpp"
  24. #include <algorithm>
  25. namespace geom {
  26. /**
  27. * Bounding sphere.
  28. */
  29. template <class T>
  30. struct sphere: public bounding_volume<T>
  31. {
  32. typedef math::vector<T, 3> vector_type;
  33. vector_type center;
  34. T radius;
  35. sphere(const vector_type& center, T radius);
  36. sphere();
  37. virtual bounding_volume_type get_bounding_volume_type() const;
  38. virtual bool intersects(const sphere<T>& sphere) const;
  39. virtual bool intersects(const aabb<T>& aabb) const;
  40. virtual bool contains(const sphere<T>& sphere) const;
  41. virtual bool contains(const aabb<T>& aabb) const;
  42. virtual bool contains(const vector_type& point) const;
  43. };
  44. template <class T>
  45. sphere<T>::sphere(const vector_type& center, T radius):
  46. center(center),
  47. radius(radius)
  48. {}
  49. template <class T>
  50. sphere<T>::sphere()
  51. {}
  52. template <class T>
  53. inline bounding_volume_type sphere<T>::get_bounding_volume_type() const
  54. {
  55. return bounding_volume_type::sphere;
  56. }
  57. template <class T>
  58. bool sphere<T>::intersects(const sphere<T>& sphere) const
  59. {
  60. vector_type d = center - sphere.center;
  61. float r = radius + sphere.radius;
  62. return (math::dot(d, d) <= r * r);
  63. }
  64. template <class T>
  65. bool sphere<T>::intersects(const aabb<T>& aabb) const
  66. {
  67. return aabb.intersects(*this);
  68. }
  69. template <class T>
  70. bool sphere<T>::contains(const sphere<T>& sphere) const
  71. {
  72. float containment_radius = radius - sphere.radius;
  73. if (containment_radius < T(0))
  74. return false;
  75. vector_type d = center - sphere.center;
  76. return (math::dot(d, d) <= containment_radius * containment_radius);
  77. }
  78. template <class T>
  79. bool sphere<T>::contains(const aabb<T>& aabb) const
  80. {
  81. T distance = T(0);
  82. vector_type a = center - aabb.min_point;
  83. vector_type b = center - aabb.max_point;
  84. distance += std::max(a.x * a.x, b.x * b.x);
  85. distance += std::max(a.y * a.y, b.y * b.y);
  86. distance += std::max(a.z * a.z, b.z * b.z);
  87. return (distance <= radius * radius);
  88. }
  89. template <class T>
  90. bool sphere<T>::contains(const vector_type& point) const
  91. {
  92. vector_type d = center - point;
  93. return (math::dot(d, d) <= radius * radius);
  94. }
  95. } // namespace geom
  96. #endif // ANTKEEPER_GEOM_SPHERE_HPP