scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
transforms.cpp
Go to the documentation of this file.
3#include <glm/gtc/matrix_transform.hpp>
4
5namespace scimesh {
6
7void translate_mesh(Mesh &mesh, const Vec3 &translation) {
8 Mat4 m = glm::translate(Mat4(1.0f), translation);
9 for (auto &v : mesh.vertices) {
10 v = transform_point(m, v);
11 }
12}
13
14void scale_mesh(Mesh &mesh, const Vec3 &scale) {
15 Mat4 m = glm::scale(Mat4(1.0f), scale);
16 for (auto &v : mesh.vertices) {
17 v = transform_point(m, v);
18 }
19}
20
21void scale_mesh(Mesh &mesh, float uniform_scale) {
22 scale_mesh(mesh, Vec3(uniform_scale));
23}
24
25void rotate_mesh(Mesh &mesh, float angle_radians, const Vec3 &axis) {
26 Mat4 m = glm::rotate(Mat4(1.0f), angle_radians, axis);
27 for (auto &v : mesh.vertices) {
28 v = transform_point(m, v);
29 }
30}
31
32void transform_mesh(Mesh &mesh, const Mat4 &matrix) {
33 for (auto &v : mesh.vertices) {
34 v = transform_point(matrix, v);
35 }
36}
37
38Mesh mesh_from_fs(const std::vector<float> &fs_vertices,
39 const std::vector<uint32_t> &fs_faces,
40 const std::vector<float> &per_vertex_values,
41 const std::vector<uint8_t> &rgb_bytes,
42 bool detect_transparency) {
43 Mesh out;
44 size_t nv = fs_vertices.size() / 3;
45 out.vertices.reserve(nv);
46
47 bool have_values = !per_vertex_values.empty();
48 bool have_rgb = !rgb_bytes.empty();
49
50 for (size_t i = 0; i < nv; i++) {
51 out.vertices.push_back(Vec3(
52 fs_vertices[i * 3],
53 fs_vertices[i * 3 + 1],
54 fs_vertices[i * 3 + 2]));
55
56 if (have_values && std::isnan(per_vertex_values[i])) {
57 out.colors.push_back(Color(1.0f, 1.0f, 1.0f, 1.0f));
58 } else if (have_rgb) {
59 out.colors.push_back(Color(
60 rgb_bytes[i * 3] / 255.0f,
61 rgb_bytes[i * 3 + 1] / 255.0f,
62 rgb_bytes[i * 3 + 2] / 255.0f,
63 1.0f));
64 }
65 }
66
67 size_t nf = fs_faces.size() / 3;
68 out.triangles.reserve(nf);
69 for (size_t i = 0; i < nf; i++) {
70 out.triangles.push_back(Triangle{
71 fs_faces[i * 3],
72 fs_faces[i * 3 + 1],
73 fs_faces[i * 3 + 2]});
74 }
75
76 if (detect_transparency && out.has_colors()) {
77 for (const auto &c : out.colors) {
78 if (c.a < 1.0f - 1e-6f) {
79 out.has_transparency = true;
80 break;
81 }
82 }
83 }
84
85 return out;
86}
87
88} // namespace scimesh
Low-level math utilities for the rendering pipeline.
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
void scale_mesh(Mesh &mesh, const Vec3 &scale)
Scale a mesh non-uniformly along each axis.
void rotate_mesh(Mesh &mesh, float angle_radians, const Vec3 &axis)
Rotate a mesh around an arbitrary axis.
Mesh mesh_from_fs(const std::vector< float > &fs_vertices, const std::vector< uint32_t > &fs_faces, const std::vector< float > &per_vertex_values, const std::vector< uint8_t > &rgb_bytes, bool detect_transparency)
Convert a FreeSurfer-format mesh (flat vertex/face arrays) to a scimesh Mesh.
void transform_mesh(Mesh &mesh, const Mat4 &matrix)
Apply an arbitrary 4×4 transformation matrix to a mesh.
void translate_mesh(Mesh &mesh, const Vec3 &translation)
Translate (move) a mesh by a displacement vector.
Definition transforms.cpp:7
An RGBA color with floating-point components.
Definition types.h:88
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
bool has_transparency
Whether the mesh contains any transparent fragments.
Definition mesh.h:166
std::vector< Color > colors
Per-vertex RGBA colors.
Definition mesh.h:107
bool has_colors() const
Does the mesh have per-vertex colors?
Definition mesh.h:177
std::vector< Vec3 > vertices
3D vertex positions.
Definition mesh.h:86
std::vector< Triangle > triangles
Triangle index triplets.
Definition mesh.h:94
A triangle defined by three vertex indices.
Definition types.h:127
Mesh transformation functions: translate, scale, rotate, and general matrix transforms.