scimesh 0.3.4
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_plane_space(PlaneSpace s) {
26 return s == PlaneSpace::EYE ? "eye" : "world";
27}
28
29inline const char* str_fog_space(FogSpace s) {
30 return s == FogSpace::NDC ? "ndc" : "world";
31}
32
33inline const char* str_merge(MergeDirection d) {
34 switch (d) {
35 case MergeDirection::LEFT: return "left";
36 case MergeDirection::RIGHT: return "right";
37 case MergeDirection::TOP: return "top";
38 case MergeDirection::BOTTOM: return "bottom";
39 }
40 return "?";
41}
42
43inline const char* str_crop(CropContentDirection d) {
44 switch (d) {
45 case CropContentDirection::LEFT: return "left";
46 case CropContentDirection::RIGHT: return "right";
47 case CropContentDirection::HORIZONTAL: return "horizontal";
48 case CropContentDirection::TOP: return "top";
49 case CropContentDirection::BOTTOM: return "bottom";
50 case CropContentDirection::VERTICAL: return "vertical";
51 case CropContentDirection::ALL: return "all";
52 }
53 return "?";
54}
55
56inline std::string fmt_count(size_t n) {
57 if (n >= 1000000) {
58 char buf[32];
59 std::snprintf(buf, sizeof(buf), "%.1fM", n / 1000000.0);
60 return buf;
61 }
62 if (n >= 1000) {
63 char buf[32];
64 std::snprintf(buf, sizeof(buf), "%uk", static_cast<unsigned>(n / 1000));
65 return buf;
66 }
67 return std::to_string(n);
68}
69
70inline std::string fmt_size_bytes(size_t bytes) {
71 if (bytes >= 1024 * 1024) {
72 char buf[32];
73 std::snprintf(buf, sizeof(buf), "%.1fMB", bytes / (1024.0 * 1024.0));
74 return buf;
75 }
76 if (bytes >= 1024) {
77 char buf[32];
78 std::snprintf(buf, sizeof(buf), "%ukB", static_cast<unsigned>(bytes / 1024));
79 return buf;
80 }
81 return std::to_string(bytes) + "B";
82}
83
84// ---- primitive types ----
85
86inline std::ostream& operator<<(std::ostream &os, const Color &c) {
87 os << "rgba(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
88 return os;
89}
90
91inline std::ostream& operator<<(std::ostream &os, const Vec3 &v) {
92 os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
93 return os;
94}
95
96inline std::ostream& operator<<(std::ostream &os, const Triangle &t) {
97 os << "Tri(" << t.v0 << ", " << t.v1 << ", " << t.v2 << ")";
98 return os;
99}
100
101inline std::ostream& operator<<(std::ostream &os, ShadingMode s) {
102 os << str_shading(s);
103 return os;
104}
105
106inline std::ostream& operator<<(std::ostream &os, ProjectionType p) {
107 os << str_projection(p);
108 return os;
109}
110
111// ---- composite types ----
112
113inline std::ostream& operator<<(std::ostream &os, const Light &l) {
114 os << "Light(pos=" << l.position << ", color=" << l.color
115 << ", I=" << l.intensity
116 << ", " << (l.is_directional ? "dir" : "point") << ")";
117 return os;
118}
119
120inline std::ostream& operator<<(std::ostream &os, const ClipPlane &cp) {
121 os << "ClipPlane(n=" << cp.normal << ", off=" << cp.offset
122 << ", " << str_plane_space(cp.space) << ")";
123 return os;
124}
125
126inline std::ostream& operator<<(std::ostream &os, const Camera &cam) {
127 float dist = glm::length(cam.eye - cam.center);
128 os << "Camera(eye=" << cam.eye << ", ctr=" << cam.center
129 << ", up=" << cam.up
130 << ", " << str_projection(cam.projection)
131 << ", fov=" << std::fixed << std::setprecision(1) << cam.fov_degrees << "°"
132 << ", dist=" << std::fixed << std::setprecision(1) << dist << ")";
133 return os;
134}
135
136inline std::ostream& operator<<(std::ostream &os, const Mesh &m) {
137 os << "Mesh(verts=" << fmt_count(m.vertices.size())
138 << ", tris=" << fmt_count(m.triangles.size());
139 if (m.has_colors()) os << ", cols";
140 if (m.has_normals()) os << ", norms";
141 else os << ", ~norms";
142 if (m.has_uvs()) os << ", uvs";
143 if (m.has_texture()) os << ", tex";
144 if (m.has_transparency) os << ", transp";
145 if (!m.vertices.empty()) {
146 Vec3 bmin, bmax;
147 m.compute_bounding_box(bmin, bmax);
148 os << ", bb=[" << bmin << ", " << bmax << "]";
149 }
150 os << ")";
151 return os;
152}
153
154inline std::ostream& operator<<(std::ostream &os, const Scene &s) {
155 os << "Scene(meshes=" << s.meshes.size();
156 if (!s.meshes.empty()) {
157 Vec3 bmin, bmax;
158 s.compute_bounding_box(bmin, bmax);
159 os << ", bb=[" << bmin << ", " << bmax << "]";
160 }
161 os << ")";
162 return os;
163}
164
165inline std::ostream& operator<<(std::ostream &os, const Image &img) {
166 size_t bytes = img.pixels.size();
167 os << "Image(" << img.width << "x" << img.height
168 << ", RGBA, " << fmt_size_bytes(bytes) << ")";
169 return os;
170}
171
172inline std::ostream& operator<<(std::ostream &os, const RenderOptions &opts) {
173 os << "RenderOpts(" << opts.width << "x" << opts.height
174 << ", " << str_shading(opts.shading);
175 if (opts.backface_culling) os << ", cull";
176 else os << ", ~cull";
177 os << ", bg=" << opts.background_color
178 << ", lights=" << opts.lights.size()
179 << ", amb=" << opts.ambient
180 << ", aa=" << opts.aa_samples;
181 if (opts.wireframe) os << ", wire";
182 if (opts.fog_enabled) os << ", fog(" << str_fog_space(opts.fog_space)
183 << ":" << opts.fog_start << "-" << opts.fog_end << ")";
184 if (opts.ssao_enabled) os << ", ssao(r=" << opts.ssao_radius
185 << ", I=" << opts.ssao_intensity << ")";
186 if (opts.threads > 0) os << ", threads=" << opts.threads;
187 os << ")";
188 return os;
189}
190
191} // 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:38
@ 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.
const char * str_plane_space(PlaneSpace s)
Definition to_string.h:25
MergeDirection
Direction for the merge() operation.
Definition image.h:26
@ 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:70
PlaneSpace
Coordinate space in which a ClipPlane is defined.
Definition types.h:210
@ EYE
Eye (view/camera) space: the plane is anchored to the camera and therefore travels and rotates with i...
const char * str_shading(ShadingMode s)
Definition to_string.h:21
std::ostream & operator<<(std::ostream &os, const Color &c)
Definition to_string.h:86
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:56
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:33
const char * str_fog_space(FogSpace s)
Definition to_string.h:29
FogSpace
Coordinate space (and hence the unit) of the fog distances RenderOptions::fog_start and RenderOptions...
Definition types.h:297
@ NDC
Normalized device depth: the raw values of the depth buffer, in [-1, 1], where -1 is the near plane,...
const char * str_crop(CropContentDirection d)
Definition to_string.h:43
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
A clipping plane that can hide parts of the scene.
Definition types.h:263
An RGBA color with floating-point components.
Definition types.h:88
A 2D RGBA image buffer.
Definition image.h:92
A light source for the Blinn-Phong shading model.
Definition types.h:179
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
All settings that control rendering output.
A collection of Mesh objects to be rendered together.
Definition scene.h:69
A triangle defined by three vertex indices.
Definition types.h:127
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.