Antkeeper  0.0.1
hyperrectangle.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_PRIMITIVES_HYPERRECTANGLE_HPP
21 #define ANTKEEPER_GEOM_PRIMITIVES_HYPERRECTANGLE_HPP
22 
23 #include <engine/math/vector.hpp>
24 #include <algorithm>
25 
26 namespace geom {
27 namespace primitives {
28 
35 template <class T, std::size_t N>
37 {
40 
43 
46 
54  [[nodiscard]] constexpr bool contains(const vector_type& point) const noexcept
55  {
56  for (std::size_t i = 0; i < N; ++i)
57  {
58  if (point[i] < min[i] || point[i] > max[i])
59  {
60  return false;
61  }
62  }
63  return true;
64  }
65 
73  [[nodiscard]] constexpr bool contains(const hyperrectangle& other) const noexcept
74  {
75  for (std::size_t i = 0; i < N; ++i)
76  {
77  if (other.min[i] < min[i] || other.max[i] > max[i])
78  {
79  return false;
80  }
81  }
82  return true;
83  }
84 
86  [[nodiscard]] inline constexpr vector_type center() const noexcept
87  {
88  return (min + max) / T{2};
89  }
90 
98  [[nodiscard]] T distance(const vector_type& point) const noexcept
99  {
100  const vector_type d = math::abs(point - center()) - extents();
101  return math::length(math::max(vector_type::zero(), d)) + std::min<T>(T{0}, math::max(d));
102  }
103 
109  void extend(const vector_type& point) noexcept
110  {
111  min = math::min(min, point);
112  max = math::max(max, point);
113  }
114 
120  void extend(const hyperrectangle& other) noexcept
121  {
122  min = math::min(min, other.min);
123  max = math::max(max, other.max);
124  }
125 
133  [[nodiscard]] constexpr bool intersects(const hyperrectangle& other) const noexcept
134  {
135  for (std::size_t i = 0; i < N; ++i)
136  {
137  if (other.min[i] > max[i] || other.max[i] < min[i])
138  {
139  return false;
140  }
141  }
142  return true;
143  }
144 
146  [[nodiscard]] inline constexpr vector_type size() const noexcept
147  {
148  return max - min;
149  }
150 
152  [[nodiscard]] inline constexpr vector_type extents() const noexcept
153  {
154  return size() / T{2};
155  }
156 
160  [[nodiscard]] constexpr bool degenerate() const noexcept
161  {
162  for (std::size_t i = 0; i < N; ++i)
163  {
164  if (min[i] > max[i])
165  {
166  return true;
167  }
168  }
169 
170  return false;
171  }
172 
174  [[nodiscard]] constexpr T volume() const noexcept
175  {
176  T v = max[0] - min[0];
177  for (std::size_t i = 1; i < N; ++i)
178  {
179  v *= max[i] - min[i];
180  }
181  return v;
182  }
183 
189  [[nodiscard]] constexpr vector_type corner(std::size_t index) const noexcept
190  {
191  vector_type p;
192 
193  for (std::size_t i = 0; i < N; ++i)
194  {
195  p[i] = ((index >> ((N - 1) - i)) & 1) ? max[i] : min[i];
196  }
197 
198  return p;
199  }
200 };
201 
202 } // namespace primitives
203 
204 using namespace primitives;
205 
206 } // namespace geom
207 
208 #endif // ANTKEEPER_GEOM_PRIMITIVES_HYPERRECTANGLE_HPP
Geometric algorithms.
constexpr vector< T, N > max(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the maximum elements of two vectors.
Definition: vector.hpp:1328
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
constexpr vector< T, N > abs(const vector< T, N > &x)
Returns the absolute values of each element.
Definition: vector.hpp:985
constexpr vector< T, N > min(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the minimum elements of two vectors.
Definition: vector.hpp:1347
n-dimensional axis-aligned rectangle.
vector_type min
Minimum extent of the hyperrectangle.
constexpr bool intersects(const hyperrectangle &other) const noexcept
Tests whether another hyperrectangle intersects this hyperrectangle.
constexpr bool contains(const hyperrectangle &other) const noexcept
Tests whether another hyperrectangle is contained within this hyperrectangle.
constexpr bool contains(const vector_type &point) const noexcept
Tests whether a point is contained within this hyperrectangle.
constexpr vector_type extents() const noexcept
Calculates the extents of the hyperrectangle.
vector_type max
Maximum extent of the hyperrectangle.
void extend(const vector_type &point) noexcept
Extends the hyperrectangle to include a point.
constexpr bool degenerate() const noexcept
Returns true if any coordinates of min are greater than max, false otherwise.
constexpr T volume() const noexcept
Calculates the volume of the hyperrectangle.
constexpr vector_type center() const noexcept
Returns the center position of the hyperrectangle.
constexpr vector_type corner(std::size_t index) const noexcept
Returns the nth corner of the hyperrectangle.
constexpr vector_type size() const noexcept
Calculates the size of the hyperrectangle.
T distance(const vector_type &point) const noexcept
Calculates the signed distance from the hyperrectangle to a point.
void extend(const hyperrectangle &other) noexcept
Extends the hyperrectangle to include another hyperrectangle.
n-dimensional vector.
Definition: vector.hpp:44
static constexpr vector zero() noexcept
Returns a zero vector, where every element is equal to zero.
Definition: vector.hpp:306