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

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