GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
TriangleMesh.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_TRIANGLE_MESH_HPP
5#define GREM_CORE_DATA_TRIANGLE_MESH_HPP
6
8
15#include <GREM/core/math.hpp>
16
17#include <utility> // std::move
18
19namespace grem {
20
27template <size_t N, typename Scalar>
29
30using TriangleMeshVertexIndex = uint32_t;
31using TriangleMeshFaceIndex = uint32_t;
32
39template <size_t N, typename Scalar>
41public:
45
59 : vertices(std::move(vertices))
60 , indices(std::move(indices))
61 , boundingBox(calculateBoundingBox<N, Scalar>(this->vertices))
62 , boundingRadius(calculateBoundingRadius<N, Scalar>(this->vertices))
63 , faceOrthtree(boundingBox, maxComponent(boundingBox.max - boundingBox.min) * Scalar{0.002}) {
64 GREM_ASSERT(this->indices.size() % 3 == 0);
65 GREM_ASSERT(this->indices.size() / 3 <= size_t{Limits<FaceIndex>::MAX});
66 const FaceIndex faceCount = static_cast<FaceIndex>(this->indices.size() / 3);
67 for (FaceIndex faceIndex = 0; faceIndex < faceCount; ++faceIndex) {
68 const size_t indexOffset = static_cast<size_t>(faceIndex) * 3;
69 GREM_ASSERT(this->indices[indexOffset + 0] < this->vertices.size());
70 GREM_ASSERT(this->indices[indexOffset + 1] < this->vertices.size());
71 GREM_ASSERT(this->indices[indexOffset + 2] < this->vertices.size());
72 const Triangle<N, Scalar> triangle{
73 .pointA = this->vertices[this->indices[indexOffset + 0]],
74 .pointB = this->vertices[this->indices[indexOffset + 1]],
75 .pointC = this->vertices[this->indices[indexOffset + 2]],
76 };
77 const Box<N, Scalar> triangleAABB = triangle.getBoundingBox();
78 faceOrthtree.insert(triangleAABB, faceIndex);
79 }
80 }
81
96 : TriangleMesh(Allocation<Vertex>{vertices.begin(), vertices.end()}, Allocation<VertexIndex>{indices.begin(), indices.end()}) {}
97
103 [[nodiscard]] Span<const Vertex> getVertices() const noexcept {
104 return vertices;
105 }
106
115 [[nodiscard]] Span<const VertexIndex> getIndices() const noexcept {
116 return indices;
117 }
118
125 [[nodiscard]] Box<N, Scalar> getBoundingBox() const noexcept {
126 return boundingBox;
127 }
128
134 [[nodiscard]] Scalar getBoundingRadius() const noexcept {
135 return boundingRadius;
136 }
137
146 [[nodiscard]] const LooseOrthtree<N, Scalar, FaceIndex>& getFaceOrthtree() const noexcept {
147 return faceOrthtree;
148 }
149
150private:
151 Allocation<Vertex> vertices;
153 Box<N, Scalar> boundingBox;
154 Scalar boundingRadius;
156};
157
158} // namespace grem
159
160#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Allocation.hpp:25
constexpr iterator begin() noexcept
Definition Allocation.hpp:433
constexpr size_type size() const noexcept
Definition Allocation.hpp:485
constexpr iterator end() noexcept
Definition Allocation.hpp:445
Orthtree-based space subdivision structure, optimized for intersection queries between axis-aligned b...
Definition LooseOrthtree.hpp:65
Definition Span.hpp:146
TriangleMeshVertexIndex VertexIndex
Vertex index type of the triangle mesh.
Definition TriangleMesh.hpp:43
TriangleMeshVertex< N, Scalar > Vertex
Vertex type of the triangle mesh.
Definition TriangleMesh.hpp:42
TriangleMeshFaceIndex FaceIndex
Face index type of the triangle mesh.
Definition TriangleMesh.hpp:44
const LooseOrthtree< N, Scalar, FaceIndex > & getFaceOrthtree() const noexcept
Get the orthtree of faces in the mesh.
Definition TriangleMesh.hpp:146
TriangleMesh(Allocation< Vertex > vertices, Allocation< VertexIndex > indices)
Construct a triangle mesh from a set of vertices.
Definition TriangleMesh.hpp:58
Scalar getBoundingRadius() const noexcept
Get the bounding radius of the mesh.
Definition TriangleMesh.hpp:134
TriangleMesh(Span< const Vertex > vertices, Span< const VertexIndex > indices)
Construct a triangle mesh from a set of vertices and indices.
Definition TriangleMesh.hpp:95
Span< const Vertex > getVertices() const noexcept
Get the vertices of the mesh.
Definition TriangleMesh.hpp:103
Box< N, Scalar > getBoundingBox() const noexcept
Get the bounding box of the mesh.
Definition TriangleMesh.hpp:125
Span< const VertexIndex > getIndices() const noexcept
Get the vertex indices of the mesh.
Definition TriangleMesh.hpp:115
Definition aliases.hpp:9
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
uint32_t TriangleMeshVertexIndex
Index of a vertex in a triangle mesh.
Definition TriangleMesh.hpp:30
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
vec< N, Scalar > TriangleMeshVertex
Vertex in a triangle mesh.
Definition TriangleMesh.hpp:28
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
uint32_t TriangleMeshFaceIndex
Index of a triangle in a triangle mesh, equal to the index of the first vertex index divided by 3.
Definition TriangleMesh.hpp:31
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
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
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL maxComponent(vec< 1, T > a)
Definition math.hpp:1920
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
Generic triangle shape in N-dimensional space.
Definition geometry.hpp:586
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the triangle.
Definition geometry.hpp:760
Definition math.hpp:58