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

143 lines
3.8 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_CONVEX_HULL_HPP
  20. #define ANTKEEPER_GEOM_CONVEX_HULL_HPP
  21. #include "bounding-volume.hpp"
  22. #include "geom/plane.hpp"
  23. #include "geom/sphere.hpp"
  24. #include "geom/aabb.hpp"
  25. #include <cstdlib>
  26. #include <vector>
  27. namespace geom {
  28. /**
  29. * A plane-bounded convex hull.
  30. */
  31. template <class T>
  32. struct convex_hull: public bounding_volume<T>
  33. {
  34. /// Vector of planes with descibe the bounds of the convex hull.
  35. std::vector<plane<T>> planes;
  36. /**
  37. * Creates a convex hull.
  38. *
  39. * @param size Number of planes the convex hull should accommodate.
  40. */
  41. convex_hull(std::size_t size);
  42. /// Creates a convex hull.
  43. convex_hull();
  44. virtual bounding_volume_type get_bounding_volume_type() const;
  45. virtual bool intersects(const sphere<T>& sphere) const;
  46. virtual bool intersects(const aabb<T>& aabb) const;
  47. virtual bool contains(const sphere<T>& sphere) const;
  48. virtual bool contains(const aabb<T>& aabb) const;
  49. virtual bool contains(const math::vector<T, 3>& point) const;
  50. };
  51. template <class T>
  52. convex_hull<T>::convex_hull(std::size_t size):
  53. planes(size, plane<T>())
  54. {}
  55. template <class T>
  56. convex_hull<T>::convex_hull()
  57. {}
  58. template <class T>
  59. inline bounding_volume_type convex_hull<T>::get_bounding_volume_type() const
  60. {
  61. return bounding_volume_type::convex_hull;
  62. }
  63. template <class T>
  64. bool convex_hull<T>::intersects(const sphere<T>& sphere) const
  65. {
  66. for (const plane<T>& plane: planes)
  67. if (plane.signed_distance(sphere.center) < -sphere.radius)
  68. return false;
  69. return true;
  70. }
  71. template <class T>
  72. bool convex_hull<T>::intersects(const aabb<T>& aabb) const
  73. {
  74. math::vector<T, 3> pv;
  75. for (const plane<T>& plane: planes)
  76. {
  77. pv.x = (plane.normal.x > T(0)) ? aabb.max_point.x : aabb.min_point.x;
  78. pv.y = (plane.normal.y > T(0)) ? aabb.max_point.y : aabb.min_point.y;
  79. pv.z = (plane.normal.z > T(0)) ? aabb.max_point.z : aabb.min_point.z;
  80. if (plane.signed_distance(pv) < T(0))
  81. return false;
  82. }
  83. return true;
  84. }
  85. template <class T>
  86. bool convex_hull<T>::contains(const sphere<T>& sphere) const
  87. {
  88. for (const plane<T>& plane: planes)
  89. if (plane.signed_distance(sphere.center) < sphere.radius)
  90. return false;
  91. return true;
  92. }
  93. template <class T>
  94. bool convex_hull<T>::contains(const aabb<T>& aabb) const
  95. {
  96. math::vector<T, 3> pv;
  97. math::vector<T, 3> nv;
  98. for (const plane<T>& plane: planes)
  99. {
  100. pv.x = (plane.normal.x > T(0)) ? aabb.max_point.x : aabb.min_point.x;
  101. pv.y = (plane.normal.y > T(0)) ? aabb.max_point.y : aabb.min_point.y;
  102. pv.z = (plane.normal.z > T(0)) ? aabb.max_point.z : aabb.min_point.z;
  103. nv.x = (plane.normal.x < T(0)) ? aabb.max_point.x : aabb.min_point.x;
  104. nv.y = (plane.normal.y < T(0)) ? aabb.max_point.y : aabb.min_point.y;
  105. nv.z = (plane.normal.z < T(0)) ? aabb.max_point.z : aabb.min_point.z;
  106. if (plane.signed_distance(pv) < T(0) || plane.signed_distance(nv) < T(0))
  107. return false;
  108. }
  109. return true;
  110. }
  111. template <class T>
  112. bool convex_hull<T>::contains(const math::vector<T, 3>& point) const
  113. {
  114. for (const plane<T>& plane: planes)
  115. if (plane.signed_distance(point) < T(0))
  116. return false;
  117. return true;
  118. }
  119. } // namespace geom
  120. #endif // ANTKEEPER_GEOM_CONVEX_HULL_HPP