GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
ConvexPolytope.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_CONVEX_POLYTOPE_HPP
5#define GREM_CORE_DATA_CONVEX_POLYTOPE_HPP
6
8
14#include <GREM/core/math.hpp>
15
16#include <type_traits> // std::conditional_t
17#include <utility> // std::move
18
19namespace grem {
20
25
30
35
42template <size_t N, typename Scalar>
44
51template <size_t N, typename Scalar>
53
54template <typename Scalar>
55struct ConvexPolytopeEdge<2, Scalar> {};
56
57template <typename Scalar>
84
91template <size_t N, typename Scalar>
93
94template <typename Scalar>
95struct ConvexPolytopeFace<2, Scalar> {
101};
102
103template <typename Scalar>
119
120namespace detail {
121
122struct NoEdges {};
123
124template <size_t N, typename Scalar>
125struct ConvexPolytopeData {
126 Allocation<ConvexPolytopeVertex<N, Scalar>> vertices{};
127 [[no_unique_address]] std::conditional_t<N == 3, Allocation<ConvexPolytopeEdgeIndex>, NoEdges> vertexEdgeIndices{};
128 [[no_unique_address]] std::conditional_t<N == 3, Allocation<ConvexPolytopeEdge<3, Scalar>>, NoEdges> edges{};
129 Allocation<ConvexPolytopeFace<N, Scalar>> faces{};
130};
131
132GREM_API(core)
133ConvexPolytopeData<2, float> buildConvexHull2DFloat(Span<const ConvexPolytopeVertex<2, float>> vertices, const mat3& transformation, ConvexPolytopeVertexIndex maxVertexCount);
134
135GREM_API(core)
136ConvexPolytopeData<2, double> buildConvexHull2DDouble(Span<const ConvexPolytopeVertex<2, double>> vertices, const dmat3& transformation, ConvexPolytopeVertexIndex maxVertexCount);
137
138GREM_API(core)
139ConvexPolytopeData<3, float> buildConvexHull3DFloat(Span<const ConvexPolytopeVertex<3, float>> vertices, const mat4& transformation, ConvexPolytopeVertexIndex maxVertexCount);
140
141GREM_API(core)
142ConvexPolytopeData<3, double> buildConvexHull3DDouble(Span<const ConvexPolytopeVertex<3, double>> vertices, const dmat4& transformation, ConvexPolytopeVertexIndex maxVertexCount);
143
144template <size_t N, typename Scalar>
145[[nodiscard]] inline ConvexPolytopeData<N, Scalar> buildConvexHull(Span<const ConvexPolytopeVertex<N, Scalar>> vertices, const mat<N + 1, N + 1, Scalar>& transformation,
146 ConvexPolytopeVertexIndex maxVertexCount) {
147 if constexpr (N == 2) {
148 if constexpr (same_as<Scalar, float>) {
149 return buildConvexHull2DFloat(vertices, transformation, maxVertexCount);
150 } else {
151 return buildConvexHull2DDouble(vertices, transformation, maxVertexCount);
152 }
153 } else {
154 if constexpr (same_as<Scalar, float>) {
155 return buildConvexHull3DFloat(vertices, transformation, maxVertexCount);
156 } else {
157 return buildConvexHull3DDouble(vertices, transformation, maxVertexCount);
158 }
159 }
160}
161
162} // namespace detail
163
170template <size_t N, typename Scalar>
172public:
176
190 ConvexPolytope(Allocation<Vertex> vertices, Allocation<Face> faces, const Box<2, Scalar>& boundingBox, Scalar boundingRadius) requires(N == 2)
191 : data{.vertices = std::move(vertices), .faces = std::move(faces)}
192 , boundingBox(boundingBox)
193 , boundingRadius(boundingRadius) {}
194
211 const Box<3, Scalar>& boundingBox, Scalar boundingRadius) requires(N == 3)
212 : data{.vertices = std::move(vertices), .vertexEdgeIndices = std::move(vertexEdgeIndices), .edges = std::move(edges), .faces = std::move(faces)}
213 , boundingBox(boundingBox)
214 , boundingRadius(boundingRadius) {}
215
230 : data(detail::buildConvexHull<N>(vertices, transformation, maxVertexCount))
231 , boundingBox(calculateBoundingBox<N, Scalar>(data.vertices))
232 , boundingRadius(calculateBoundingRadius<N, Scalar>(data.vertices)) {}
233
246 : ConvexPolytope(vertices, mat<N + 1, N + 1, Scalar>{Scalar{1}}, maxVertexCount) {}
247
253 [[nodiscard]] Span<const Vertex> getVertices() const noexcept {
254 return data.vertices;
255 }
256
262 [[nodiscard]] Span<const ConvexPolytopeEdgeIndex> getVertexEdgeIndices() const noexcept requires(N == 3) {
263 return data.vertexEdgeIndices;
264 }
265
271 [[nodiscard]] Span<const Edge> getEdges() const noexcept requires(N == 3) {
272 return data.edges;
273 }
274
280 [[nodiscard]] Span<const Face> getFaces() const noexcept {
281 return data.faces;
282 }
283
290 [[nodiscard]] Box<N, Scalar> getBoundingBox() const noexcept {
291 return boundingBox;
292 }
293
299 [[nodiscard]] Scalar getBoundingRadius() const noexcept {
300 return boundingRadius;
301 }
302
303private:
304 detail::ConvexPolytopeData<N, Scalar> data{};
305 Box<N, Scalar> boundingBox{};
306 Scalar boundingRadius{};
307};
308
309} // namespace grem
310
311#endif
Definition Allocation.hpp:25
Span< const ConvexPolytopeEdgeIndex > getVertexEdgeIndices() const noexcept
Get the vertex edge indices of the convex polytope.
Definition ConvexPolytope.hpp:262
Box< N, Scalar > getBoundingBox() const noexcept
Get the bounding box of the convex polytope.
Definition ConvexPolytope.hpp:290
ConvexPolytopeFace< N, Scalar > Face
Face type of the convex polytope.
Definition ConvexPolytope.hpp:175
ConvexPolytopeEdge< N, Scalar > Edge
Edge type of the convex polytope.
Definition ConvexPolytope.hpp:174
ConvexPolytope(Allocation< Vertex > vertices, Allocation< Face > faces, const Box< 2, Scalar > &boundingBox, Scalar boundingRadius)
Construct a convex polygon from pre-calculated data.
Definition ConvexPolytope.hpp:190
Scalar getBoundingRadius() const noexcept
Get the bounding radius of the convex polytope.
Definition ConvexPolytope.hpp:299
Span< const Edge > getEdges() const noexcept
Get the edges of the convex polytope.
Definition ConvexPolytope.hpp:271
ConvexPolytopeVertex< N, Scalar > Vertex
Vertex type of the convex polytope.
Definition ConvexPolytope.hpp:173
ConvexPolytope(Span< const Vertex > vertices, ConvexPolytopeVertexIndex maxVertexCount=Limits< ConvexPolytopeVertexIndex >::MAX)
Construct a convex polytope from a set of vertices.
Definition ConvexPolytope.hpp:245
Span< const Face > getFaces() const noexcept
Get the faces of the convex polytope.
Definition ConvexPolytope.hpp:280
ConvexPolytope(Allocation< Vertex > vertices, Allocation< ConvexPolytopeEdgeIndex > vertexEdgeIndices, Allocation< Edge > edges, Allocation< Face > faces, const Box< 3, Scalar > &boundingBox, Scalar boundingRadius)
Construct a convex polyhedron from pre-calculated data.
Definition ConvexPolytope.hpp:210
ConvexPolytope(Span< const Vertex > vertices, const mat< N+1, N+1, Scalar > &transformation, ConvexPolytopeVertexIndex maxVertexCount=Limits< ConvexPolytopeVertexIndex >::MAX)
Construct a convex polytope from the convex hull of a set of vertices.
Definition ConvexPolytope.hpp:229
Span< const Vertex > getVertices() const noexcept
Get the vertices of the convex polytope.
Definition ConvexPolytope.hpp:253
Definition Span.hpp:146
@ N
'n'
Definition keyboard.hpp:84
float Scalar
Underlying scalar type of physics quantity components.
Definition quantities.hpp:1480
Definition aliases.hpp:9
uint8_t ConvexPolytopeEdgeIndex
Index of an edge in a convex polytope.
Definition ConvexPolytope.hpp:29
vec< N, Scalar > ConvexPolytopeVertex
Vertex of a convex polytope.
Definition ConvexPolytope.hpp:43
constexpr T calculateBoundingRadius(StridedSpan< const Point< N, T > > points) noexcept
Calculate the bounding radius from the origin of a set of points.
Definition geometry.hpp:1618
uint8_t ConvexPolytopeVertexIndex
Index of a vertex in a convex polytope.
Definition ConvexPolytope.hpp:24
mat< 4, 4, double > dmat4
Definition math.hpp:810
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
mat< 3, 3, float > mat3
Definition math.hpp:805
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
mat< 3, 3, double > dmat3
Definition math.hpp:809
uint8_t ConvexPolytopeFaceIndex
Index of a face in a convex polytope.
Definition ConvexPolytope.hpp:34
mat< 4, 4, float > mat4
Definition math.hpp:806
constexpr Box< N, T > calculateBoundingBox(StridedSpan< const Point< N, T > > points) noexcept
Calculate the axis-aligned bounding box of a set of points.
Definition geometry.hpp:1595
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
ConvexPolytopeVertexIndex vertexIndex
Index of the vertex corresponding to the first point of the edge.
Definition ConvexPolytope.hpp:65
ConvexPolytopeFaceIndex faceIndex
The index of the face that this edge belongs to.
Definition ConvexPolytope.hpp:73
ConvexPolytopeEdgeIndex nextEdgeIndex
Index of the next edge on the face, which will eventually loop back around to this edge again.
Definition ConvexPolytope.hpp:82
Edge of a convex polytope.
Definition ConvexPolytope.hpp:52
vec< 2, Scalar > normal
Direction perpendicular to the face, pointing outwards from the center of the shape.
Definition ConvexPolytope.hpp:100
vec< 3, Scalar > normal
Direction perpendicular to the face, pointing outwards from the center of the shape.
Definition ConvexPolytope.hpp:109
ConvexPolytopeEdgeIndex firstEdgeIndex
Index of the first edge that makes up the face.
Definition ConvexPolytope.hpp:117
Face on a convex polytope.
Definition ConvexPolytope.hpp:92
static constexpr T MAX
Definition fundamentals.hpp:29
Definition math.hpp:663
Definition math.hpp:58