Antkeeper  0.0.1
convex-hull.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
20 #ifndef ANTKEEPER_GEOM_CONVEX_HULL_HPP
21 #define ANTKEEPER_GEOM_CONVEX_HULL_HPP
22 
24 #include <engine/geom/plane.hpp>
25 #include <engine/geom/sphere.hpp>
26 #include <engine/geom/aabb.hpp>
27 #include <cstddef>
28 #include <vector>
29 
30 namespace geom {
31 
35 template <class T>
36 struct convex_hull: public bounding_volume<T>
37 {
39  std::vector<plane<T>> planes;
40 
46  explicit convex_hull(std::size_t size);
47 
49  convex_hull();
50 
52  virtual bool intersects(const sphere<T>& sphere) const;
53  virtual bool intersects(const aabb<T>& aabb) const;
54  virtual bool contains(const sphere<T>& sphere) const;
55  virtual bool contains(const aabb<T>& aabb) const;
56  virtual bool contains(const math::vector<T, 3>& point) const;
57 };
58 
59 template <class T>
60 convex_hull<T>::convex_hull(std::size_t size):
61  planes(size, plane<T>())
62 {}
63 
64 template <class T>
66 {}
67 
68 template <class T>
70 {
72 }
73 
74 template <class T>
76 {
77  for (const plane<T>& plane: planes)
79  return false;
80  return true;
81 }
82 
83 template <class T>
85 {
87  for (const plane<T>& plane: planes)
88  {
89  pv.x() = (plane.normal.x() > T(0)) ? aabb.max_point.x() : aabb.min_point.x();
90  pv.y() = (plane.normal.y() > T(0)) ? aabb.max_point.y() : aabb.min_point.y();
91  pv.z() = (plane.normal.z() > T(0)) ? aabb.max_point.z() : aabb.min_point.z();
92  if (plane.signed_distance(pv) < T(0))
93  return false;
94  }
95 
96  return true;
97 }
98 
99 template <class T>
101 {
102  for (const plane<T>& plane: planes)
104  return false;
105  return true;
106 }
107 
108 template <class T>
110 {
113 
114  for (const plane<T>& plane: planes)
115  {
116  pv.x() = (plane.normal.x() > T(0)) ? aabb.max_point.x() : aabb.min_point.x();
117  pv.y() = (plane.normal.y() > T(0)) ? aabb.max_point.y() : aabb.min_point.y();
118  pv.z() = (plane.normal.z() > T(0)) ? aabb.max_point.z() : aabb.min_point.z();
119  nv.x() = (plane.normal.x() < T(0)) ? aabb.max_point.x() : aabb.min_point.x();
120  nv.y() = (plane.normal.y() < T(0)) ? aabb.max_point.y() : aabb.min_point.y();
121  nv.z() = (plane.normal.z() < T(0)) ? aabb.max_point.z() : aabb.min_point.z();
122 
123  if (plane.signed_distance(pv) < T(0) || plane.signed_distance(nv) < T(0))
124  return false;
125  }
126 
127  return true;
128 }
129 
130 template <class T>
132 {
133  for (const plane<T>& plane: planes)
134  if (plane.signed_distance(point) < T(0))
135  return false;
136 
137  return true;
138 }
139 
140 } // namespace geom
141 
142 #endif // ANTKEEPER_GEOM_CONVEX_HULL_HPP
143 
Abstract base class for bounding volumes.
Geometry.
Definition: aabb.hpp:30
bounding_volume_type
Enumerates bounding volume types.
@ convex_hull
Indicates the bounding volume is a convex hull.
Axis-aligned bounding box.
Definition: aabb.hpp:37
vector_type max_point
Definition: aabb.hpp:43
vector_type min_point
Definition: aabb.hpp:42
A plane-bounded convex hull.
Definition: convex-hull.hpp:37
virtual bool contains(const sphere< T > &sphere) const
Tests whether this bounding volume contains a sphere.
std::vector< plane< T > > planes
Vector of planes with descibe the bounds of the convex hull.
Definition: convex-hull.hpp:39
virtual bool intersects(const sphere< T > &sphere) const
Tests for intersection between this bounding volume and a bounding sphere.
Definition: convex-hull.hpp:75
convex_hull()
Creates a convex hull.
Definition: convex-hull.hpp:65
virtual bounding_volume_type get_bounding_volume_type() const
Returns the enumerated type of this bounding volume.
Definition: convex-hull.hpp:69
A flat 2-dimensional surface.
Definition: plane.hpp:32
T signed_distance(const vector_type &v) const
Calculates the signed distance between a plane and a vector.
Definition: plane.hpp:110
vector_type normal
Plane normal vector.
Definition: plane.hpp:36
Bounding sphere.
Definition: sphere.hpp:35
vector_type center
Definition: sphere.hpp:38
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:163
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:179
constexpr element_type & z() noexcept
Returns a reference to the third element.
Definition: vector.hpp:195