Skip to main content

Mesh.ixx File

Mesh abstraction: immutable vertex/index data and configuration. More...

Included Headers

#include <memory> #include <stdexcept> #include <vector> #include <cmath> #include <limits> #include <helios.math.types> #include <helios.util.log.Logger> #include <helios.util.log.LogManager> #include <helios.rendering.asset.shape.Shape> #include <helios.rendering.Vertex> #include <helios.rendering.mesh.MeshConfig>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespacemesh

Mesh rendering abstractions and command types. More...

Classes Index

classMesh

Representative of a renderable 3D mesh. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::rendering::mesh::Mesh"

Description

Mesh abstraction: immutable vertex/index data and configuration.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::rendering::mesh::Mesh"

Definition at line 25 of file Mesh.ixx.

25#define HELIOS_LOG_SCOPE "helios::rendering::mesh::Mesh"

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Mesh.ixx
3 * @brief Mesh abstraction: immutable vertex/index data and configuration.
4 */
5module;
6
7#include <memory>
8#include <stdexcept>
9#include <vector>
10#include <cmath>
11#include <limits>
12
13
14export module helios.rendering.mesh.Mesh;
15
16import helios.math.types;
17import helios.rendering.mesh.MeshConfig;
18import helios.rendering.Vertex;
19import helios.rendering.asset.shape.Shape;
20import helios.util.log.LogManager;
21import helios.util.log.Logger;
22import helios.math.types;
23
24
25#define HELIOS_LOG_SCOPE "helios::rendering::mesh::Mesh"
26export namespace helios::rendering::mesh {
27
28 /**
29 * @brief Representative of a renderable 3D mesh.
30 *
31 * A Mesh instance contains vertex data and indices provided by geometric shapes.
32 * Meshes contain references to immutable, shared vertex data and indices.
33 * Concrete implementations will handle resource management.
34 *
35 * @see [Gre19, 631]
36 */
37 class Mesh {
38
39 protected:
40
41 Mesh() = default;
42
43
44 /**
45 * @brief Flag indicating whether the mesh requires updates to its computed data.
46 *
47 * This variable is used to track whether the mesh's derived properties, such
48 * as its axis-aligned bounding box (AABB), are outdated and need to be recalculated.
49 * It is marked as mutable to allow modification within const member functions.
50 */
51 mutable bool needsUpdate_ = true;
52
53
54 /**
55 * @brief Represents the axis-aligned bounding box (AABB) for the mesh.
56 *
57 * This member variable stores the computed or cached AABB for the mesh in
58 * 3D space. The AABB is a minimal bounding box aligned with the coordinate
59 * axes that encloses all the vertices of the mesh. It is marked mutable to
60 * allow updates during read-only operations, such as recalculating bounds
61 * as vertex positions are modified or updated dynamically.
62 *
63 * @see helios::math::aabb
64 */
66
67 /**
68 * @brief Shared pointer to the raw, immutable vertices.
69 */
70 std::shared_ptr<const std::vector<Vertex>> vertices_ = nullptr;
71
72 /**
73 * @brief Shared pointer to the raw, immutable indices.
74 */
75 std::shared_ptr<const std::vector<unsigned int>> indices_ = nullptr;
76
77 /**
78 * @brief Shared pointer to the MeshConfig used with this Mesh.
79 */
80 std::shared_ptr<const helios::rendering::mesh::MeshConfig> meshConfig_;
81
82 /**
83 * @todo
84 * std::shared_ptr<const std::vector<Texture>> textures_;
85 */
86
87 /**
88 * @brief Initializes the Mesh by setting up vertex attributes and buffers with the
89 * underlying GL API.
90 * This method should be called _once_ by the derived class before using
91 * this Mesh in a rendering pass.
92 *
93 * @throws std::runtime_error if the Mesh could not be initialized.
94 */
95 virtual void init() = 0;
96
97 /**
98 * @brief Shared logger instance for all Mesh objects.
99 */
102 );
103
104 public:
105
106 virtual ~Mesh() = default;
107
108 /**
109 * @brief Creates a new Mesh instance.
110 *
111 * @param vertices A shared_ptr to a vector of Vertex.
112 * @param indices A shared_ptr to a vector of indices.
113 * @param meshConfig A shared_ptr to the MeshConfig used with this Mesh.
114 *
115 * @throws std::invalid_argument if either "vertices", "indices" or meshConfig is a null shared_ptr
116 */
117 explicit Mesh(
118 std::shared_ptr<const std::vector<Vertex>> vertices,
119 std::shared_ptr<const std::vector<unsigned int>> indices,
120 std::shared_ptr<const helios::rendering::mesh::MeshConfig> meshConfig
121 ) : vertices_(std::move(vertices)),
122 indices_(std::move(indices)),
123 meshConfig_(std::move(meshConfig)) {
124
125 if (!vertices_ || !indices_ || !meshConfig_) {
126 const std::string msg = "Mesh constructor received a null shared pointer.";
127 logger_.error(msg);
128 throw std::invalid_argument(msg);
129 }
130 }
131
132 /**
133 * @brief Creates a new Mesh instance from the specified Shape.
134 *
135 * @param shape A const reference to the Shape.
136 * @param meshConfig A shared_ptr to the MeshConfig used with this Mesh.
137 *
138 * @throws std::invalid_argument if meshConfig is a null shared_ptr, or if the
139 * shape contained null data.
140 */
141 explicit Mesh(
143 std::shared_ptr<const helios::rendering::mesh::MeshConfig> meshConfig
144 ) :
145 vertices_(shape.vertices),
146 indices_(shape.indices),
147 meshConfig_(std::move(meshConfig)) {
148
149 if (!vertices_ || !indices_ || !meshConfig_) {
150 const std::string msg = "Mesh constructor received a Shape with null shared pointer.";
151 logger_.error(msg);
152 throw std::invalid_argument(msg);
153 }
154 }
155
156 /**
157 * @brief Returns the index count of this Mesh.
158 *
159 * @return The number of indices handled by this Mesh.
160 */
161 [[nodiscard]] unsigned int indexCount() const noexcept {
162 return indices_->size();
163 }
164
165 /**
166 * @brief Returns a const reference to the underlying vertices.
167 * The returned data is guaranteed to be a valid reference to existing data.
168 *
169 * @return A const ref to the vector of Vertex.
170 */
171 [[nodiscard]] const std::vector<Vertex>& vertices() const noexcept {
172 return *vertices_;
173 }
174
175 /**
176 * @brief Returns a const reference to the underlying indices.
177 * The returned data is guaranteed to be a valid reference to existing data.
178 *
179 * @return A const ref to the vector of indices.
180 */
181 [[nodiscard]] const std::vector<unsigned int>& indices() const noexcept {
182 return *indices_;
183 }
184
185 /**
186 * @brief Returns a const reference to MeshConfig used with this Mesh.
187 * The returned MeshConfig is guaranteed to be a valid reference to existing data.
188 *
189 * @return The MeshConfig used with this Mesh.
190 */
191 [[nodiscard]] const helios::rendering::mesh::MeshConfig& meshConfig() const noexcept {
192 return *meshConfig_;
193 }
194
195
196 /**
197 * @brief Retrieves the axis-aligned bounding box (AABB) of the mesh.
198 *
199 * The method calculates and returns the AABB of the mesh, which encloses
200 * the mesh's vertices. If the mesh requires an update to its bounding box,
201 * this method recomputes the bounds based on the vertex positions and caches
202 * the result for future calls. The returned AABB represents the minimal
203 * rectangle in 3D space that contains all the vertices of the mesh.
204 *
205 * @return Reference to the computed axis-aligned bounding box.
206 */
207 [[nodiscard]] const helios::math::aabbf& aabb() const noexcept {
208 if (needsUpdate_) {
209 if (!vertices_->empty()) {
210 float minX = std::numeric_limits<float>::max();
211 float minY = std::numeric_limits<float>::max();
212 float minZ = std::numeric_limits<float>::max();
213 float maxX = std::numeric_limits<float>::lowest();
214 float maxY = std::numeric_limits<float>::lowest();
215 float maxZ = std::numeric_limits<float>::lowest();
216
217 for (const auto& v: *vertices_) {
218 // min
219 minX = std::min(minX, v.position[0]);
220 minY = std::min(minY, v.position[1]);
221 minZ = std::min(minZ, v.position[2]);
222
223 // max
224 maxX = std::max(maxX, v.position[0]);
225 maxY = std::max(maxY, v.position[1]);
226 maxZ = std::max(maxZ, v.position[2]);
227 }
228
229 aabb_ = helios::math::aabbf(minX, minY, minZ, maxX, maxY, maxZ);
230 } else {
231 aabb_ = helios::math::aabbf(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
232 }
233
234 needsUpdate_ = false;
235 }
236
237 return aabb_;
238 }
239 };
240
241}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.