scimesh 0.3.2
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>
8#include <string>
9
10namespace scimesh {
11
20 const Mesh *mesh = nullptr;
22 std::string name;
23};
24
57struct Scene {
59 std::vector<Mesh> meshes;
60
64 std::vector<Mat4> transforms;
65
67 std::vector<std::string> names;
68
75 void add(const Mesh &mesh, const Mat4 &transform = Mat4(1.0f),
76 const std::string &name = "") {
77 meshes.push_back(mesh);
78 transforms.push_back(transform);
79 names.push_back(name);
80 }
81
83 void set_transform(size_t index, const Mat4 &transform) {
84 if (index >= meshes.size())
85 return;
86 while (transforms.size() <= index)
87 transforms.push_back(Mat4(1.0f));
88 transforms[index] = transform;
89 }
90
92 void set_name(size_t index, const std::string &name) {
93 if (index >= meshes.size())
94 return;
95 while (names.size() <= index)
96 names.push_back("");
97 names[index] = name;
98 }
99
101 const Mat4 &transform(size_t index) const {
102 static const Mat4 kIdentity(1.0f);
103 if (index >= transforms.size())
104 return kIdentity;
105 return transforms[index];
106 }
107
109 const std::string &name(size_t index) const {
110 static const std::string kEmpty;
111 if (index >= names.size())
112 return kEmpty;
113 return names[index];
114 }
115
117 size_t size() const { return meshes.size(); }
118
120 bool empty() const { return meshes.empty(); }
121
125 SceneNodeRef node(size_t index) const {
126 SceneNodeRef r;
127 r.mesh = &meshes[index];
128 r.transform = transform(index);
129 r.name = name(index);
130 return r;
131 }
132
134 std::vector<SceneNodeRef> nodes() const {
135 std::vector<SceneNodeRef> out;
136 out.reserve(meshes.size());
137 for (size_t i = 0; i < meshes.size(); ++i)
138 out.push_back(node(i));
139 return out;
140 }
141
152 void compute_bounding_box(Vec3 &min_bound, Vec3 &max_bound) const {
153 bool first = true;
154 for (size_t i = 0; i < meshes.size(); ++i) {
155 const Mesh &mesh = meshes[i];
156 if (mesh.vertices.empty())
157 continue;
158 Vec3 mesh_min, mesh_max;
159 mesh.compute_bounding_box(mesh_min, mesh_max);
160 const Mat4 &m = transform(i);
161 for (int c = 0; c < 8; ++c) {
162 Vec3 corner(
163 (c & 1) ? mesh_max.x : mesh_min.x,
164 (c & 2) ? mesh_max.y : mesh_min.y,
165 (c & 4) ? mesh_max.z : mesh_min.z);
166 Vec3 t = transform_point(m, corner);
167 if (first) {
168 min_bound = t;
169 max_bound = t;
170 first = false;
171 } else {
172 min_bound = glm::min(min_bound, t);
173 max_bound = glm::max(max_bound, t);
174 }
175 }
176 }
177 if (first) { // scene empty or all meshes empty
178 min_bound = Vec3(0.0f);
179 max_bound = Vec3(0.0f);
180 }
181 }
182};
183
184} // namespace scimesh
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 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:289
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:19
std::string name
Optional node name (used by exporters / debugging).
Definition scene.h:22
Mat4 transform
Model matrix placing the mesh in world space (identity = as-is).
Definition scene.h:21
const Mesh * mesh
Pointer to the owned mesh (never null for a valid node).
Definition scene.h:20
A collection of Mesh objects to be rendered together.
Definition scene.h:57
void set_transform(size_t index, const Mat4 &transform)
Set the placement transform of the mesh at index.
Definition scene.h:83
std::vector< Mesh > meshes
The meshes in this scene, drawn in order.
Definition scene.h:59
bool empty() const
Whether the scene holds no meshes.
Definition scene.h:120
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:152
std::vector< Mat4 > transforms
Per-mesh placement transforms, parallel to meshes.
Definition scene.h:64
const Mat4 & transform(size_t index) const
The placement transform of the mesh at index (identity when unset).
Definition scene.h:101
size_t size() const
Number of meshes in the scene.
Definition scene.h:117
void set_name(size_t index, const std::string &name)
Set the name of the mesh at index.
Definition scene.h:92
std::vector< SceneNodeRef > nodes() const
Non-owning references to all meshes, in draw order.
Definition scene.h:134
std::vector< std::string > names
Optional per-mesh node names, parallel to meshes.
Definition scene.h:67
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:75
const std::string & name(size_t index) const
The name of the mesh at index (empty when unset).
Definition scene.h:109
SceneNodeRef node(size_t index) const
A non-owning reference to the mesh at index (mesh + transform + name).
Definition scene.h:125