scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
to_string.h
Go to the documentation of this file.
1#pragma once
2
3#include <scimesh/types.h>
4#include <scimesh/mesh.h>
5#include <scimesh/scene.h>
6#include <scimesh/camera.h>
8#include <scimesh/image.h>
9#include <ostream>
10#include <iomanip>
11#include <cmath>
12
13namespace scimesh {
14
15// ---- helpers ----
16
17inline const char* str_projection(ProjectionType p) {
18 return p == ProjectionType::ORTHOGRAPHIC ? "ortho" : "persp";
19}
20
21inline const char* str_shading(ShadingMode s) {
22 return s == ShadingMode::FLAT ? "flat" : "smooth";
23}
24
25inline const char* str_merge(MergeDirection d) {
26 switch (d) {
27 case MergeDirection::LEFT: return "left";
28 case MergeDirection::RIGHT: return "right";
29 case MergeDirection::TOP: return "top";
30 case MergeDirection::BOTTOM: return "bottom";
31 }
32 return "?";
33}
34
35inline const char* str_crop(CropContentDirection d) {
36 switch (d) {
37 case CropContentDirection::LEFT: return "left";
38 case CropContentDirection::RIGHT: return "right";
39 case CropContentDirection::HORIZONTAL: return "horizontal";
40 case CropContentDirection::TOP: return "top";
41 case CropContentDirection::BOTTOM: return "bottom";
42 case CropContentDirection::VERTICAL: return "vertical";
43 case CropContentDirection::ALL: return "all";
44 }
45 return "?";
46}
47
48inline std::string fmt_count(size_t n) {
49 if (n >= 1000000) {
50 char buf[32];
51 std::snprintf(buf, sizeof(buf), "%.1fM", n / 1000000.0);
52 return buf;
53 }
54 if (n >= 1000) {
55 char buf[32];
56 std::snprintf(buf, sizeof(buf), "%uk", static_cast<unsigned>(n / 1000));
57 return buf;
58 }
59 return std::to_string(n);
60}
61
62inline std::string fmt_size_bytes(size_t bytes) {
63 if (bytes >= 1024 * 1024) {
64 char buf[32];
65 std::snprintf(buf, sizeof(buf), "%.1fMB", bytes / (1024.0 * 1024.0));
66 return buf;
67 }
68 if (bytes >= 1024) {
69 char buf[32];
70 std::snprintf(buf, sizeof(buf), "%ukB", static_cast<unsigned>(bytes / 1024));
71 return buf;
72 }
73 return std::to_string(bytes) + "B";
74}
75
76// ---- primitive types ----
77
78inline std::ostream& operator<<(std::ostream &os, const Color &c) {
79 os << "rgba(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
80 return os;
81}
82
83inline std::ostream& operator<<(std::ostream &os, const Vec3 &v) {
84 os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
85 return os;
86}
87
88inline std::ostream& operator<<(std::ostream &os, const Triangle &t) {
89 os << "Tri(" << t.v0 << ", " << t.v1 << ", " << t.v2 << ")";
90 return os;
91}
92
93inline std::ostream& operator<<(std::ostream &os, ShadingMode s) {
94 os << str_shading(s);
95 return os;
96}
97
98inline std::ostream& operator<<(std::ostream &os, ProjectionType p) {
99 os << str_projection(p);
100 return os;
101}
102
103// ---- composite types ----
104
105inline std::ostream& operator<<(std::ostream &os, const Light &l) {
106 os << "Light(pos=" << l.position << ", color=" << l.color
107 << ", I=" << l.intensity
108 << ", " << (l.is_directional ? "dir" : "point") << ")";
109 return os;
110}
111
112inline std::ostream& operator<<(std::ostream &os, const ClipPlane &cp) {
113 os << "ClipPlane(n=" << cp.normal << ", off=" << cp.offset << ")";
114 return os;
115}
116
117inline std::ostream& operator<<(std::ostream &os, const Camera &cam) {
118 float dist = glm::length(cam.eye - cam.center);
119 os << "Camera(eye=" << cam.eye << ", ctr=" << cam.center
120 << ", up=" << cam.up
121 << ", " << str_projection(cam.projection)
122 << ", fov=" << std::fixed << std::setprecision(1) << cam.fov_degrees << "°"
123 << ", dist=" << std::fixed << std::setprecision(1) << dist << ")";
124 return os;
125}
126
127inline std::ostream& operator<<(std::ostream &os, const Mesh &m) {
128 os << "Mesh(verts=" << fmt_count(m.vertices.size())
129 << ", tris=" << fmt_count(m.triangles.size());
130 if (m.has_colors()) os << ", cols";
131 if (m.has_normals()) os << ", norms";
132 else os << ", ~norms";
133 if (m.has_uvs()) os << ", uvs";
134 if (m.has_texture()) os << ", tex";
135 if (m.has_transparency) os << ", transp";
136 if (!m.vertices.empty()) {
137 Vec3 bmin, bmax;
138 m.compute_bounding_box(bmin, bmax);
139 os << ", bb=[" << bmin << ", " << bmax << "]";
140 }
141 os << ")";
142 return os;
143}
144
145inline std::ostream& operator<<(std::ostream &os, const Scene &s) {
146 os << "Scene(meshes=" << s.meshes.size();
147 if (!s.meshes.empty()) {
148 Vec3 bmin, bmax;
149 s.compute_bounding_box(bmin, bmax);
150 os << ", bb=[" << bmin << ", " << bmax << "]";
151 }
152 os << ")";
153 return os;
154}
155
156inline std::ostream& operator<<(std::ostream &os, const Image &img) {
157 size_t bytes = img.pixels.size();
158 os << "Image(" << img.width << "x" << img.height
159 << ", RGBA, " << fmt_size_bytes(bytes) << ")";
160 return os;
161}
162
163inline std::ostream& operator<<(std::ostream &os, const RenderOptions &opts) {
164 os << "RenderOpts(" << opts.width << "x" << opts.height
165 << ", " << str_shading(opts.shading);
166 if (opts.backface_culling) os << ", cull";
167 else os << ", ~cull";
168 os << ", bg=" << opts.background_color
169 << ", lights=" << opts.lights.size()
170 << ", amb=" << opts.ambient
171 << ", aa=" << opts.aa_samples;
172 if (opts.wireframe) os << ", wire";
173 if (opts.fog_enabled) os << ", fog";
174 if (opts.ssao_enabled) os << ", ssao(r=" << opts.ssao_radius
175 << ", I=" << opts.ssao_intensity << ")";
176 if (opts.threads > 0) os << ", threads=" << opts.threads;
177 os << ")";
178 return os;
179}
180
181} // namespace scimesh
Camera definition and helper functions for view setup.
The Image — an RGBA pixel buffer with compositing and I/O operations.
The Mesh — the central data structure for 3D geometry in scimesh.
CropContentDirection
Direction(s) for the crop_to_content() operation.
Definition image.h:37
@ BOTTOM
Crop bottom edge only.
@ RIGHT
Crop right edge only.
@ VERTICAL
Crop both top and bottom edges.
@ ALL
Crop all four edges.
@ TOP
Crop top edge only.
@ LEFT
Crop left edge only.
@ HORIZONTAL
Crop both left and right edges.
MergeDirection
Direction for the merge() operation.
Definition image.h:25
@ BOTTOM
Attach other below.
@ RIGHT
Attach other to the right side.
@ TOP
Attach other above.
@ LEFT
Attach other to the left side.
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
std::string fmt_size_bytes(size_t bytes)
Definition to_string.h:62
const char * str_shading(ShadingMode s)
Definition to_string.h:21
std::ostream & operator<<(std::ostream &os, const Color &c)
Definition to_string.h:78
ShadingMode
How surface normals are interpolated across triangles.
@ FLAT
Flat shading: each triangle uses a single normal, giving a faceted, low-poly look.
std::string fmt_count(size_t n)
Definition to_string.h:48
ProjectionType
The type of 3D→2D projection used by the camera.
Definition camera.h:23
@ ORTHOGRAPHIC
Orthographic projection: parallel lines stay parallel.
const char * str_projection(ProjectionType p)
Definition to_string.h:17
const char * str_merge(MergeDirection d)
Definition to_string.h:25
const char * str_crop(CropContentDirection d)
Definition to_string.h:35
RenderOptions — all settings that control how meshes are drawn.
The Scene — a collection of meshes rendered together.
A virtual camera that defines the viewpoint for rendering.
Definition camera.h:77
ProjectionType projection
Which projection type to use.
Definition camera.h:99
Vec3 center
The point the camera looks at.
Definition camera.h:88
Vec3 up
The camera's "up" direction.
Definition camera.h:94
Vec3 eye
Camera position in world space.
Definition camera.h:82
float fov_degrees
Vertical field of view in degrees (perspective only).
Definition camera.h:109
A clipping plane that can hide parts of the scene.
Definition types.h:224
Vec3 normal
The plane normal vector (should be unit-length).
Definition types.h:229
float offset
Offset along the normal.
Definition types.h:234
An RGBA color with floating-point components.
Definition types.h:88
float g
Green channel, [0, 1].
Definition types.h:90
float r
Red channel, [0, 1].
Definition types.h:89
float b
Blue channel, [0, 1].
Definition types.h:91
float a
Alpha (opacity) channel, [0, 1]. 1.0 = fully opaque.
Definition types.h:92
A 2D RGBA image buffer.
Definition image.h:87
int height
Image height in pixels.
Definition image.h:92
int width
Image width in pixels.
Definition image.h:89
std::vector< uint8_t > pixels
Raw pixel data: RGBA bytes, row-major, bottom-left origin.
Definition image.h:98
A light source for the Blinn-Phong shading model.
Definition types.h:179
bool is_directional
Whether this is a directional light (default: true).
Definition types.h:200
Color color
Color of the light (default: white).
Definition types.h:189
Vec3 position
Position of the light.
Definition types.h:184
float intensity
Brightness multiplier (default: 1.0).
Definition types.h:194
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 has_transparency
Whether the mesh contains any transparent fragments.
Definition mesh.h:166
bool has_colors() const
Does the mesh have per-vertex colors?
Definition mesh.h:177
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
bool has_texture() const
Does the mesh have a texture image?
Definition mesh.h:197
std::vector< Triangle > triangles
Triangle index triplets.
Definition mesh.h:94
All settings that control rendering output.
std::vector< Light > lights
List of light sources.
bool fog_enabled
Enable depth fog (default: false).
Color background_color
Background color (default: opaque white).
float ambient
Ambient light level (default: 0.3).
int height
Image height in pixels (default: 600).
int width
Image width in pixels (default: 800).
float ssao_intensity
SSAO darkening intensity (default: 0.8).
bool backface_culling
Enable backface culling (default: true).
bool ssao_enabled
Enable SSAO (default: false).
int threads
Number of render threads (default: 0 = auto-detect).
int aa_samples
Anti-aliasing sample count (default: 1 = no AA).
bool wireframe
Draw triangle edges as lines (default: false).
float ssao_radius
SSAO sample radius in pixels (default: 16).
ShadingMode shading
Shading mode (default: SMOOTH).
A collection of Mesh objects to be rendered together.
Definition scene.h:57
std::vector< Mesh > meshes
The meshes in this scene, drawn in order.
Definition scene.h:59
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
A triangle defined by three vertex indices.
Definition types.h:127
uint32_t v2
Index of the third vertex in Mesh::vertices.
Definition types.h:133
uint32_t v1
Index of the second vertex in Mesh::vertices.
Definition types.h:131
uint32_t v0
Index of the first vertex in Mesh::vertices.
Definition types.h:129
Fundamental types used throughout the scimesh rendering engine.