💿🐜 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.

111 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_SPHERE_HPP
  20. #define ANTKEEPER_SPHERE_HPP
  21. #include "geometry/bounding-volume.hpp"
  22. #include "geometry/aabb.hpp"
  23. #include "math/math.hpp"
  24. #include <algorithm>
  25. template <class T>
  26. struct sphere: public bounding_volume<T>
  27. {
  28. typedef math::vector<T, 3> vector_type;
  29. vector_type center;
  30. T radius;
  31. sphere(const vector_type& center, T radius);
  32. sphere();
  33. virtual bounding_volume_type get_bounding_volume_type() const;
  34. virtual bool intersects(const sphere<T>& sphere) const;
  35. virtual bool intersects(const aabb<T>& aabb) const;
  36. virtual bool contains(const sphere<T>& sphere) const;
  37. virtual bool contains(const aabb<T>& aabb) const;
  38. virtual bool contains(const vector_type& point) const;
  39. };
  40. template <class T>
  41. sphere<T>::sphere(const vector_type& center, T radius):
  42. center(center),
  43. radius(radius)
  44. {}
  45. template <class T>
  46. sphere<T>::sphere()
  47. {}
  48. template <class T>
  49. inline bounding_volume_type sphere<T>::get_bounding_volume_type() const
  50. {
  51. return bounding_volume_type::sphere;
  52. }
  53. template <class T>
  54. bool sphere<T>::intersects(const sphere<T>& sphere) const
  55. {
  56. vector_type d = center - sphere.center;
  57. float r = radius + sphere.radius;
  58. return (math::dot(d, d) <= r * r);
  59. }
  60. template <class T>
  61. bool sphere<T>::intersects(const aabb<T>& aabb) const
  62. {
  63. return aabb.intersects(*this);
  64. }
  65. template <class T>
  66. bool sphere<T>::contains(const sphere<T>& sphere) const
  67. {
  68. float containment_radius = radius - sphere.radius;
  69. if (containment_radius < T(0))
  70. return false;
  71. vector_type d = center - sphere.center;
  72. return (math::dot(d, d) <= containment_radius * containment_radius);
  73. }
  74. template <class T>
  75. bool sphere<T>::contains(const aabb<T>& aabb) const
  76. {
  77. T distance = T(0);
  78. vector_type a = center - aabb.min_point;
  79. vector_type b = center - aabb.max_point;
  80. distance += std::max(a.x * a.x, b.x * b.x);
  81. distance += std::max(a.y * a.y, b.y * b.y);
  82. distance += std::max(a.z * a.z, b.z * b.z);
  83. return (distance <= radius * radius);
  84. }
  85. template <class T>
  86. bool sphere<T>::contains(const vector_type& point) const
  87. {
  88. vector_type d = center - point;
  89. return (math::dot(d, d) <= radius * radius);
  90. }
  91. #endif // ANTKEEPER_SPHERE_HPP