scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
ply_io.cpp
Go to the documentation of this file.
1#define TINYPLY_IMPLEMENTATION
2#include "tinyply.h"
3#include <scimesh/ply_io.h>
4#include <scimesh/types.h>
5
6#include <fstream>
7#include <stdexcept>
8
9namespace scimesh {
10namespace ply_io {
11
12Mesh read_ply(const std::string &path) {
13 std::ifstream ifs(path, std::ios::binary);
14 if (!ifs.good()) {
15 throw std::runtime_error("Failed to open PLY file: " + path);
16 }
17
18 tinyply::PlyFile ply;
19 ply.parse_header(ifs);
20
21 bool has_vertex_colors = false;
22 tinyply::Type vertex_color_type = tinyply::Type::UINT8;
23 for (const auto &el : ply.get_elements()) {
24 if (el.name == "vertex") {
25 for (const auto &prop : el.properties) {
26 if (prop.name == "red" || prop.name == "green" || prop.name == "blue") {
27 has_vertex_colors = true;
28 vertex_color_type = prop.propertyType;
29 break;
30 }
31 }
32 }
33 }
34
35 auto vertices_data = ply.request_properties_from_element("vertex", {"x", "y", "z"});
36 auto faces_data = ply.request_properties_from_element("face", {"vertex_indices"}, 3);
37 std::shared_ptr<tinyply::PlyData> colors_data;
38 if (has_vertex_colors) {
39 colors_data = ply.request_properties_from_element("vertex", {"red", "green", "blue"});
40 }
41
42 ply.read(ifs);
43
44 Mesh mesh;
45 const size_t num_vertices = vertices_data->count;
46 mesh.vertices.resize(num_vertices);
47
48 const float *vptr = reinterpret_cast<const float *>(vertices_data->buffer.get_const());
49 for (size_t i = 0; i < num_vertices; ++i) {
50 mesh.vertices[i] = Vec3(vptr[i * 3], vptr[i * 3 + 1], vptr[i * 3 + 2]);
51 }
52
53 if (has_vertex_colors && colors_data) {
54 mesh.colors.resize(num_vertices);
55 if (vertex_color_type == tinyply::Type::UINT8) {
56 const uint8_t *cptr = reinterpret_cast<const uint8_t *>(colors_data->buffer.get_const());
57 for (size_t i = 0; i < num_vertices; ++i) {
58 mesh.colors[i] = Color(
59 cptr[i * 3] / 255.0f,
60 cptr[i * 3 + 1] / 255.0f,
61 cptr[i * 3 + 2] / 255.0f,
62 1.0f);
63 }
64 } else if (vertex_color_type == tinyply::Type::FLOAT32) {
65 const float *cptr = reinterpret_cast<const float *>(colors_data->buffer.get_const());
66 for (size_t i = 0; i < num_vertices; ++i) {
67 mesh.colors[i] = Color(cptr[i * 3], cptr[i * 3 + 1], cptr[i * 3 + 2], 1.0f);
68 }
69 }
70 }
71
72 const size_t num_faces = faces_data->count;
73 mesh.triangles.resize(num_faces);
74 const int32_t *fptr = reinterpret_cast<const int32_t *>(faces_data->buffer.get_const());
75 for (size_t i = 0; i < num_faces; ++i) {
76 mesh.triangles[i] = Triangle{
77 static_cast<uint32_t>(fptr[i * 3]),
78 static_cast<uint32_t>(fptr[i * 3 + 1]),
79 static_cast<uint32_t>(fptr[i * 3 + 2])};
80 }
81
82 return mesh;
83}
84
85} // namespace ply_io
86} // namespace scimesh
Mesh read_ply(const std::string &path)
Load a mesh from a Stanford PLY file.
Definition ply_io.cpp:12
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
Read Stanford PLY files (.ply).
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
std::vector< Color > colors
Per-vertex RGBA colors.
Definition mesh.h:107
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
Fundamental types used throughout the scimesh rendering engine.