scimesh 0.3.4
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
148 std::vector<Vec2> uvs;
149
151
154
162
164
167
175
187 bool has_transparency = false;
188
190
191 // ------------------------------------------------------------------
192 // Query methods
193 // ------------------------------------------------------------------
194
198 bool has_colors() const { return !colors.empty(); }
199
203 bool has_face_colors() const { return !face_colors.empty(); }
204
208 bool has_normals() const { return !normals.empty(); }
209
213 bool has_uvs() const { return !uvs.empty(); }
214
218 bool has_texture() const { return texture.width > 0; }
219
227 bool has_alpha() const {
228 const float threshold = 1.0f - 1e-6f;
229 if (default_color.a < threshold) return true;
230 for (const auto &c : colors) {
231 if (c.a < threshold) return true;
232 }
233 for (const auto &c : face_colors) {
234 if (c.a < threshold) return true;
235 }
236 return false;
237 }
238
248 bool is_transparent() const { return has_transparency || has_alpha(); }
249
257 if (has_alpha()) has_transparency = true;
258 }
259
260 // ------------------------------------------------------------------
261 // Validation
262 // ------------------------------------------------------------------
263
292 bool is_valid() const {
293 // Geometry must exist
294 if (vertices.empty()) return false;
295 if (triangles.empty()) return false;
296
297 // Triangle indices must be in-bounds
298 const auto nv = vertices.size();
299 for (const auto &tri : triangles) {
300 if (tri.v0 >= nv || tri.v1 >= nv || tri.v2 >= nv)
301 return false;
302 }
303
304 // No degenerate geometry
305 for (const auto &v : vertices) {
306 if (std::isnan(v.x) || std::isnan(v.y) || std::isnan(v.z))
307 return false;
308 if (std::isinf(v.x) || std::isinf(v.y) || std::isinf(v.z))
309 return false;
310 }
311
312 // Optional arrays: if present, must match vertex or face count
313 if (!colors.empty() && colors.size() != nv) return false;
314 if (!face_colors.empty() && face_colors.size() != triangles.size())
315 return false;
316 if (!normals.empty() && normals.size() != nv) return false;
317 if (!uvs.empty() && uvs.size() != nv) return false;
318
319 return true;
320 }
321
326 bool empty() const { return vertices.empty() || triangles.empty(); }
327
328 // ------------------------------------------------------------------
329 // Bounding box
330 // ------------------------------------------------------------------
331
351 if (vertices.empty()) {
352 min_bound = Vec3(0.0f);
353 max_bound = Vec3(0.0f);
354 return;
355 }
356 min_bound = vertices[0];
357 max_bound = vertices[0];
358 for (const auto &v : vertices) {
359 min_bound = glm::min(min_bound, v);
360 max_bound = glm::max(max_bound, v);
361 }
362 }
363};
364
365} // 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
float a
Alpha (opacity) channel, [0, 1]. 1.0 = fully opaque.
Definition types.h:92
A 2D RGBA image buffer.
Definition image.h:92
int width
Image width in pixels.
Definition image.h:94
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
bool has_alpha() const
Do any of the mesh's colors have alpha < 1.0?
Definition mesh.h:227
bool has_uvs() const
Does the mesh have texture coordinates?
Definition mesh.h:213
bool is_valid() const
Check whether the mesh is in a valid, renderable state.
Definition mesh.h:292
bool has_face_colors() const
Does the mesh have per-face colors?
Definition mesh.h:203
std::vector< Color > face_colors
Per-face RGBA colors.
Definition mesh.h:115
void update_transparency()
Recompute has_transparency from the mesh's colors.
Definition mesh.h:256
bool has_transparency
Whether the mesh contains any transparent fragments.
Definition mesh.h:187
bool is_transparent() const
Does the mesh need the alpha-blending pass?
Definition mesh.h:248
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:326
bool has_colors() const
Does the mesh have per-vertex colors?
Definition mesh.h:198
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:208
void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const
Compute the axis-aligned bounding box (AABB) of the mesh.
Definition mesh.h:350
std::vector< Vec3 > vertices
3D vertex positions.
Definition mesh.h:86
Image texture
An optional texture image mapped via UV coordinates.
Definition mesh.h:161
bool has_texture() const
Does the mesh have a texture image?
Definition mesh.h:218
std::vector< Vec2 > uvs
Per-vertex texture coordinates (UVs).
Definition mesh.h:148
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:174
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.