scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
mesh.h
Go to the documentation of this file.
1
16
17#pragma once
18
19#include <scimesh/types.h>
20#include <scimesh/image.h>
21#include <vector>
22#include <cmath>
23
24namespace scimesh {
25
76struct Mesh {
79
86 std::vector<Vec3> vertices;
87
94 std::vector<Triangle> triangles;
95
97
100
107 std::vector<Color> colors;
108
115 std::vector<Color> face_colors;
116
125 std::vector<Vec3> normals;
126
134 std::vector<Vec2> uvs;
135
137
140
148
150
153
161
166 bool has_transparency = false;
167
169
170 // ------------------------------------------------------------------
171 // Query methods
172 // ------------------------------------------------------------------
173
177 bool has_colors() const { return !colors.empty(); }
178
182 bool has_face_colors() const { return !face_colors.empty(); }
183
187 bool has_normals() const { return !normals.empty(); }
188
192 bool has_uvs() const { return !uvs.empty(); }
193
197 bool has_texture() const { return texture.width > 0; }
198
199 // ------------------------------------------------------------------
200 // Validation
201 // ------------------------------------------------------------------
202
231 bool is_valid() const {
232 // Geometry must exist
233 if (vertices.empty()) return false;
234 if (triangles.empty()) return false;
235
236 // Triangle indices must be in-bounds
237 const auto nv = vertices.size();
238 for (const auto &tri : triangles) {
239 if (tri.v0 >= nv || tri.v1 >= nv || tri.v2 >= nv)
240 return false;
241 }
242
243 // No degenerate geometry
244 for (const auto &v : vertices) {
245 if (std::isnan(v.x) || std::isnan(v.y) || std::isnan(v.z))
246 return false;
247 if (std::isinf(v.x) || std::isinf(v.y) || std::isinf(v.z))
248 return false;
249 }
250
251 // Optional arrays: if present, must match vertex or face count
252 if (!colors.empty() && colors.size() != nv) return false;
253 if (!face_colors.empty() && face_colors.size() != triangles.size())
254 return false;
255 if (!normals.empty() && normals.size() != nv) return false;
256 if (!uvs.empty() && uvs.size() != nv) return false;
257
258 return true;
259 }
260
265 bool empty() const { return vertices.empty() || triangles.empty(); }
266
267 // ------------------------------------------------------------------
268 // Bounding box
269 // ------------------------------------------------------------------
270
289 void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const {
290 if (vertices.empty()) {
291 min_bound = Vec3(0.0f);
292 max_bound = Vec3(0.0f);
293 return;
294 }
295 min_bound = vertices[0];
296 max_bound = vertices[0];
297 for (const auto &v : vertices) {
298 min_bound = glm::min(min_bound, v);
299 max_bound = glm::max(max_bound, v);
300 }
301 }
302};
303
304} // namespace scimesh
The Image — an RGBA pixel buffer with compositing and I/O operations.
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
constexpr Color DEFAULT_COLOR
The default mesh color: a neutral light gray (0.7, 0.7, 0.7).
Definition types.h:144
An RGBA color with floating-point components.
Definition types.h:88
A 2D RGBA image buffer.
Definition image.h:87
int width
Image width in pixels.
Definition image.h:89
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
bool has_uvs() const
Does the mesh have texture coordinates?
Definition mesh.h:192
bool is_valid() const
Check whether the mesh is in a valid, renderable state.
Definition mesh.h:231
bool has_face_colors() const
Does the mesh have per-face colors?
Definition mesh.h:182
std::vector< Color > face_colors
Per-face RGBA colors.
Definition mesh.h:115
bool has_transparency
Whether the mesh contains any transparent fragments.
Definition mesh.h:166
std::vector< Color > colors
Per-vertex RGBA colors.
Definition mesh.h:107
bool empty() const
Is the mesh empty (no renderable geometry)?
Definition mesh.h:265
bool has_colors() const
Does the mesh have per-vertex colors?
Definition mesh.h:177
std::vector< Vec3 > normals
Per-vertex surface normals (unit-length direction vectors).
Definition mesh.h:125
bool has_normals() const
Does the mesh have per-vertex normals?
Definition mesh.h:187
void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const
Compute the axis-aligned bounding box (AABB) of the mesh.
Definition mesh.h:289
std::vector< Vec3 > vertices
3D vertex positions.
Definition mesh.h:86
Image texture
An optional texture image mapped via UV coordinates.
Definition mesh.h:147
bool has_texture() const
Does the mesh have a texture image?
Definition mesh.h:197
std::vector< Vec2 > uvs
Per-vertex texture coordinates (UVs).
Definition mesh.h:134
std::vector< Triangle > triangles
Triangle index triplets.
Definition mesh.h:94
Color default_color
Fallback color when no per-vertex or per-face color is set.
Definition mesh.h:160
Fundamental types used throughout the scimesh rendering engine.