scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
scene.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <scimesh/mesh.h>
7#include <scimesh/lines.h>
8#include <scimesh/text.h>
10#include <string>
11
12namespace scimesh {
13
22 const Mesh *mesh = nullptr;
24 std::string name;
25};
26
69struct Scene {
71 std::vector<Mesh> meshes;
72
76 std::vector<Mat4> transforms;
77
79 std::vector<std::string> names;
80
87 std::vector<LineLayer> lines;
88
92 std::vector<Mat4> line_transforms;
93
95 std::vector<std::string> line_names;
96
103 std::vector<TextLayer> texts;
104
110 std::vector<Mat4> text_transforms;
111
113 std::vector<std::string> text_names;
114
125 void add(const Mesh &mesh, const Mat4 &transform = Mat4(1.0f),
126 const std::string &name = "") {
127 meshes.push_back(mesh);
128 meshes.back().update_transparency();
129 transforms.push_back(transform);
130 names.push_back(name);
131 }
132
134 void set_transform(size_t index, const Mat4 &transform) {
135 if (index >= meshes.size())
136 return;
137 while (transforms.size() <= index)
138 transforms.push_back(Mat4(1.0f));
139 transforms[index] = transform;
140 }
141
143 void set_name(size_t index, const std::string &name) {
144 if (index >= meshes.size())
145 return;
146 while (names.size() <= index)
147 names.push_back("");
148 names[index] = name;
149 }
150
152 const Mat4 &transform(size_t index) const {
153 static const Mat4 kIdentity(1.0f);
154 if (index >= transforms.size())
155 return kIdentity;
156 return transforms[index];
157 }
158
160 const std::string &name(size_t index) const {
161 static const std::string kEmpty;
162 if (index >= names.size())
163 return kEmpty;
164 return names[index];
165 }
166
168 size_t size() const { return meshes.size(); }
169
171 bool empty() const { return meshes.empty(); }
172
176 SceneNodeRef node(size_t index) const {
177 SceneNodeRef r;
178 r.mesh = &meshes[index];
179 r.transform = transform(index);
180 r.name = name(index);
181 return r;
182 }
183
185 std::vector<SceneNodeRef> nodes() const {
186 std::vector<SceneNodeRef> out;
187 out.reserve(meshes.size());
188 for (size_t i = 0; i < meshes.size(); ++i)
189 out.push_back(node(i));
190 return out;
191 }
192
204 void add_lines(const LineLayer &layer, const Mat4 &transform = Mat4(1.0f),
205 const std::string &name = "") {
206 lines.push_back(layer);
207 line_transforms.push_back(transform);
208 line_names.push_back(name);
209 }
210
212 void set_line_transform(size_t index, const Mat4 &transform) {
213 if (index >= lines.size())
214 return;
215 while (line_transforms.size() <= index)
216 line_transforms.push_back(Mat4(1.0f));
217 line_transforms[index] = transform;
218 }
219
222 const Mat4 &line_transform(size_t index) const {
223 static const Mat4 kIdentity(1.0f);
224 if (index >= line_transforms.size())
225 return kIdentity;
226 return line_transforms[index];
227 }
228
230 const std::string &line_name(size_t index) const {
231 static const std::string kEmpty;
232 if (index >= line_names.size())
233 return kEmpty;
234 return line_names[index];
235 }
236
238 size_t line_count() const { return lines.size(); }
239
244 void set_line_affects_bounds(size_t index, bool affects_bounds) {
245 if (index >= lines.size())
246 return;
247 lines[index].affects_bounds = affects_bounds;
248 }
249
256 bool set_line_affects_bounds(const std::string &name, bool affects_bounds) {
257 for (size_t i = 0; i < lines.size(); ++i) {
258 if (line_name(i) == name) {
259 lines[i].affects_bounds = affects_bounds;
260 return true;
261 }
262 }
263 return false;
264 }
265
269 bool line_affects_bounds(size_t index) const {
270 if (index >= lines.size())
271 return false;
272 return lines[index].affects_bounds;
273 }
274
278 LineNodeRef line_node(size_t index) const {
279 LineNodeRef r;
280 r.layer = &lines[index];
281 r.transform = line_transform(index);
282 r.name = line_name(index);
283 return r;
284 }
285
287 std::vector<LineNodeRef> line_nodes() const {
288 std::vector<LineNodeRef> out;
289 out.reserve(lines.size());
290 for (size_t i = 0; i < lines.size(); ++i)
291 out.push_back(line_node(i));
292 return out;
293 }
294
306 void add_texts(const TextLayer &layer, const Mat4 &transform = Mat4(1.0f),
307 const std::string &name = "") {
308 texts.push_back(layer);
309 text_transforms.push_back(transform);
310 text_names.push_back(name);
311 }
312
314 void set_text_transform(size_t index, const Mat4 &transform) {
315 if (index >= texts.size())
316 return;
317 while (text_transforms.size() <= index)
318 text_transforms.push_back(Mat4(1.0f));
319 text_transforms[index] = transform;
320 }
321
324 const Mat4 &text_transform(size_t index) const {
325 static const Mat4 kIdentity(1.0f);
326 if (index >= text_transforms.size())
327 return kIdentity;
328 return text_transforms[index];
329 }
330
332 const std::string &text_name(size_t index) const {
333 static const std::string kEmpty;
334 if (index >= text_names.size())
335 return kEmpty;
336 return text_names[index];
337 }
338
340 size_t text_count() const { return texts.size(); }
341
345 TextNodeRef text_node(size_t index) const {
346 TextNodeRef r;
347 r.layer = &texts[index];
348 r.transform = text_transform(index);
349 r.name = text_name(index);
350 return r;
351 }
352
354 std::vector<TextNodeRef> text_nodes() const {
355 std::vector<TextNodeRef> out;
356 out.reserve(texts.size());
357 for (size_t i = 0; i < texts.size(); ++i)
358 out.push_back(text_node(i));
359 return out;
360 }
361
372 bool first = true;
373
374 // Add the 8 transformed corners of a box to the bounds.
375 auto add_box = [&](const Vec3 &box_min, const Vec3 &box_max, const Mat4 &m) {
376 for (int c = 0; c < 8; ++c) {
377 Vec3 corner(
378 (c & 1) ? box_max.x : box_min.x,
379 (c & 2) ? box_max.y : box_min.y,
380 (c & 4) ? box_max.z : box_min.z);
382 if (first) {
383 min_bound = t;
384 max_bound = t;
385 first = false;
386 } else {
387 min_bound = glm::min(min_bound, t);
388 max_bound = glm::max(max_bound, t);
389 }
390 }
391 };
392
393 for (size_t i = 0; i < meshes.size(); ++i) {
394 const Mesh &mesh = meshes[i];
395 if (mesh.vertices.empty())
396 continue;
400 }
401
402 // Line layers are content unless they opted out, see
403 // LineLayer::affects_bounds.
404 const bool framed_by_meshes = !first;
405 for (size_t i = 0; i < lines.size(); ++i) {
406 if (!lines[i].affects_bounds)
407 continue;
411 }
412
413 // A scene without meshes and without any layer that claims to define the
414 // extent: frame it with the lines anyway (they can only be decoration
415 // if there is nothing else), because otherwise the scene would have no
416 // geometry to derive a camera from.
417 if (!framed_by_meshes && first) {
418 for (size_t i = 0; i < lines.size(); ++i) {
422 }
423 }
424
425 if (first) { // scene empty or all meshes empty
426 min_bound = Vec3(0.0f);
427 max_bound = Vec3(0.0f);
428 }
429 }
430};
431
432} // namespace scimesh
Screen-space line primitives — the LineLayer.
Low-level math utilities for the rendering pipeline.
The Mesh — the central data structure for 3D geometry in scimesh.
glm::mat4 Mat4
4×4 floating-point matrix.
Definition types.h:65
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
Vec3 transform_point(const Mat4 &m, const Vec3 &p)
Transform a point by a 4×4 matrix (with implicit w=1).
Definition math_utils.h:60
A batch of independent line segments drawn with a fixed screen-space width.
Definition lines.h:49
A non-owning reference to one line layer in a scene, together with its placement transform (and optio...
Definition lines.h:153
const LineLayer * layer
Pointer to the owned layer (never null for a valid node).
Definition lines.h:154
std::string name
Optional node name (used by exporters / debugging).
Definition lines.h:156
Mat4 transform
Model matrix placing the lines in world space.
Definition lines.h:155
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
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
A lightweight, non-owning reference to one mesh in a scene, together with its placement transform (an...
Definition scene.h:21
std::string name
Optional node name (used by exporters / debugging).
Definition scene.h:24
Mat4 transform
Model matrix placing the mesh in world space (identity = as-is).
Definition scene.h:23
const Mesh * mesh
Pointer to the owned mesh (never null for a valid node).
Definition scene.h:22
A collection of Mesh objects to be rendered together.
Definition scene.h:69
bool set_line_affects_bounds(const std::string &name, bool affects_bounds)
Set whether the line layer called name contributes to the scene bounds, see LineLayer::affects_bounds...
Definition scene.h:256
std::vector< std::string > text_names
Optional per-layer names, parallel to texts.
Definition scene.h:113
void set_transform(size_t index, const Mat4 &transform)
Set the placement transform of the mesh at index.
Definition scene.h:134
std::vector< Mesh > meshes
The meshes in this scene, drawn in order.
Definition scene.h:71
void set_line_affects_bounds(size_t index, bool affects_bounds)
Set whether the line layer at index contributes to the scene bounds, see LineLayer::affects_bounds.
Definition scene.h:244
std::vector< TextLayer > texts
Text layers drawn together with the meshes, after them.
Definition scene.h:103
void add_texts(const TextLayer &layer, const Mat4 &transform=Mat4(1.0f), const std::string &name="")
Add a text layer to the scene with an optional placement transform and name.
Definition scene.h:306
std::vector< LineNodeRef > line_nodes() const
Non-owning references to all line layers, in draw order.
Definition scene.h:287
void set_text_transform(size_t index, const Mat4 &transform)
Set the placement transform of the text layer at index.
Definition scene.h:314
const std::string & text_name(size_t index) const
The name of the text layer at index (empty when unset).
Definition scene.h:332
const std::string & line_name(size_t index) const
The name of the line layer at index (empty when unset).
Definition scene.h:230
bool empty() const
Whether the scene holds no meshes.
Definition scene.h:171
std::vector< std::string > line_names
Optional per-layer names, parallel to lines.
Definition scene.h:95
size_t text_count() const
Number of text layers in the scene.
Definition scene.h:340
void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const
Compute the combined axis-aligned bounding box of all meshes, after applying each mesh's placement tr...
Definition scene.h:371
std::vector< Mat4 > transforms
Per-mesh placement transforms, parallel to meshes.
Definition scene.h:76
void set_line_transform(size_t index, const Mat4 &transform)
Set the placement transform of the line layer at index.
Definition scene.h:212
std::vector< Mat4 > line_transforms
Per-layer placement transforms, parallel to lines.
Definition scene.h:92
const Mat4 & text_transform(size_t index) const
The placement transform of the text layer at index (identity when unset).
Definition scene.h:324
size_t line_count() const
Number of line layers in the scene.
Definition scene.h:238
bool line_affects_bounds(size_t index) const
Whether the line layer at index contributes to the scene bounds.
Definition scene.h:269
LineNodeRef line_node(size_t index) const
A non-owning reference to the line layer at index.
Definition scene.h:278
std::vector< Mat4 > text_transforms
Per-layer placement transforms, parallel to texts.
Definition scene.h:110
const Mat4 & transform(size_t index) const
The placement transform of the mesh at index (identity when unset).
Definition scene.h:152
const Mat4 & line_transform(size_t index) const
The placement transform of the line layer at index (identity when unset).
Definition scene.h:222
size_t size() const
Number of meshes in the scene.
Definition scene.h:168
std::vector< LineLayer > lines
Line layers drawn together with the meshes, after them.
Definition scene.h:87
void set_name(size_t index, const std::string &name)
Set the name of the mesh at index.
Definition scene.h:143
std::vector< SceneNodeRef > nodes() const
Non-owning references to all meshes, in draw order.
Definition scene.h:185
std::vector< TextNodeRef > text_nodes() const
Non-owning references to all text layers, in draw order.
Definition scene.h:354
std::vector< std::string > names
Optional per-mesh node names, parallel to meshes.
Definition scene.h:79
TextNodeRef text_node(size_t index) const
A non-owning reference to the text layer at index.
Definition scene.h:345
void add(const Mesh &mesh, const Mat4 &transform=Mat4(1.0f), const std::string &name="")
Add a mesh to the scene with an optional placement transform and name.
Definition scene.h:125
void add_lines(const LineLayer &layer, const Mat4 &transform=Mat4(1.0f), const std::string &name="")
Add a line layer to the scene with an optional placement transform and name.
Definition scene.h:204
const std::string & name(size_t index) const
The name of the mesh at index (empty when unset).
Definition scene.h:160
SceneNodeRef node(size_t index) const
A non-owning reference to the mesh at index (mesh + transform + name).
Definition scene.h:176
A batch of text labels drawn together, in one coordinate space.
Definition text.h:208
A non-owning reference to one text layer in a scene, together with its placement transform (and optio...
Definition text.h:323
const TextLayer * layer
Pointer to the owned layer (never null for a valid node).
Definition text.h:324
Mat4 transform
Model matrix applied to the anchor positions.
Definition text.h:325
std::string name
Optional node name (used by exporters / debugging).
Definition text.h:326
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Text labels — the TextLayer.