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

95 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_BOUNDING_VOLUME_HPP
  20. #define ANTKEEPER_GEOM_BOUNDING_VOLUME_HPP
  21. #include "math/math.hpp"
  22. #include <stdexcept>
  23. namespace geom {
  24. template <class T>
  25. struct sphere;
  26. template <class T>
  27. struct aabb;
  28. /// Enumerates bounding volume types.
  29. enum class bounding_volume_type
  30. {
  31. aabb, ///< Indicates the bounding volume is an axis-aligned bounding box.
  32. sphere, ///< Indicates the bounding volume is a sphere.
  33. convex_hull ///< Indicates the bounding volume is a convex hull.
  34. };
  35. /**
  36. * Abstract base class for bounding volumes.
  37. *
  38. * @tparam T Scalar type.
  39. */
  40. template <class T>
  41. class bounding_volume
  42. {
  43. public:
  44. /// Returns the enumerated type of this bounding volume.
  45. virtual bounding_volume_type get_bounding_volume_type() const = 0;
  46. /// Tests for intersection between this bounding volume and a bounding sphere.
  47. virtual bool intersects(const sphere<T>& sphere) const = 0;
  48. /// Tests for intersection between this bounding volume and an axis-aligned bounding box.
  49. virtual bool intersects(const aabb<T>& aabb) const = 0;
  50. /// Tests whether this bounding volume contains a sphere.
  51. virtual bool contains(const sphere<T>& sphere) const = 0;
  52. /// Tests whether this bounding volume contains an axis-aligned bounding box.
  53. virtual bool contains(const aabb<T>& aabb) const = 0;
  54. /// Tests whether this bounding volume contains a point.
  55. virtual bool contains(const math::vector<T, 3>& point) const = 0;
  56. /// Tests for intersection between this bounding volume and another bounding volume.
  57. bool intersects(const bounding_volume& volume) const;
  58. };
  59. /// Tests for intersection between this bounding volume and another bounding volume.
  60. template <class T>
  61. bool bounding_volume<T>::intersects(const bounding_volume& volume) const
  62. {
  63. const bounding_volume_type type = volume.get_bounding_volume_type();
  64. switch (type)
  65. {
  66. case bounding_volume_type::sphere:
  67. return intersects(static_cast<const sphere<T>&>(volume));
  68. break;
  69. case bounding_volume_type::aabb:
  70. return intersects(static_cast<const aabb<T>&>(volume));
  71. break;
  72. default:
  73. throw std::runtime_error("unimplemented");
  74. break;
  75. }
  76. }
  77. } // namespace geom
  78. #endif // ANTKEEPER_GEOM_BOUNDING_VOLUME_HPP