Antkeeper  0.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hypersphere.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_HYPERSPHERE_HPP
21 #define ANTKEEPER_GEOM_PRIMITIVES_HYPERSPHERE_HPP
22 
23 #include <engine/math/numbers.hpp>
24 #include <engine/math/vector.hpp>
25 
26 namespace geom {
27 namespace primitives {
28 
35 template <class T, std::size_t N>
37 {
40 
43 
45  T radius;
46 
54  [[nodiscard]] inline constexpr bool contains(const vector_type& point) const noexcept
55  {
57  }
58 
66  [[nodiscard]] constexpr bool contains(const hypersphere& other) const noexcept
67  {
68  const T containment_radius = radius - other.radius;
69  if (containment_radius < T{0})
70  {
71  return false;
72  }
73 
74  return math::sqr_distance(center, other.center) <= containment_radius * containment_radius;
75  }
76 
84  [[nodiscard]] inline T distance(const vector_type& point) const noexcept
85  {
86  const T d = math::sqr_distance(center, point);
87  return (d ? std::sqrt(d) : d) - radius;
88  }
89 
97  [[nodiscard]] constexpr bool intersects(const hypersphere& other) const noexcept
98  {
99  const T intersection_radius = radius + other.radius;
100  return math::sqr_distance(center, other.center) <= intersection_radius * intersection_radius;
101  }
102 
114  template <std::size_t M>
115  [[nodiscard]] static constexpr T volume(T r) noexcept
116  {
117  return (math::two_pi<T> / static_cast<T>(M)) * r * r * volume<M - 2>(r);
118  }
119 
120  template <>
121  [[nodiscard]] static constexpr T volume<1>(T r) noexcept
122  {
123  return r * T{2};
124  }
125 
126  template <>
127  [[nodiscard]] static constexpr T volume<0>(T r) noexcept
128  {
129  return T{1};
130  }
132 
134  [[nodiscard]] inline constexpr T volume() const noexcept
135  {
136  return volume<N>(radius);
137  }
138 };
139 
140 } // namespace primitives
141 
142 using namespace primitives;
143 
144 } // namespace geom
145 
146 #endif // ANTKEEPER_GEOM_PRIMITIVES_HYPERSPHERE_HPP
Geometric algorithms.
constexpr T sqr_distance(const vector< T, N > &p0, const vector< T, N > &p1) noexcept
Calculates the square distance between two points.
Definition: vector.hpp:1514
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
n-dimensional sphere.
Definition: hypersphere.hpp:37
vector_type center
Hypersphere center.
Definition: hypersphere.hpp:42
constexpr bool contains(const hypersphere &other) const noexcept
Tests whether another hypersphere is contained within this hypersphere.
Definition: hypersphere.hpp:66
constexpr bool contains(const vector_type &point) const noexcept
Tests whether a point is contained within this hypersphere.
Definition: hypersphere.hpp:54
constexpr T volume() const noexcept
Calculates the volume of the hypersphere.
T radius
Hypersphere radius.
Definition: hypersphere.hpp:45
T distance(const vector_type &point) const noexcept
Calculates the signed distance from the hypersphere to a point.
Definition: hypersphere.hpp:84
constexpr bool intersects(const hypersphere &other) const noexcept
Tests whether another hypersphere intersects this hypersphere.
Definition: hypersphere.hpp:97
n-dimensional vector.
Definition: vector.hpp:44