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

112 lines
2.9 KiB

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