Repository navigation
Expand file tree
/
Copy pathPLYWriter.cpp
More file actions
320 lines (283 loc) · 11.7 KB
/
Copy pathPLYWriter.cpp
File metadata and controls
320 lines (283 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This file is part of PyMesh. Copyright (c) 2015 by Qingnan Zhou */
#include "PLYWriter.h"
#include <cassert>
#include <iostream>
#include <sstream>
#include <Core/EigenTypedef.h>
#include <Core/Exception.h>
#include <Mesh.h>
#include <MeshFactory.h>
#include "rply.h"
using namespace PyMesh;
namespace PLYWriterHelper {
void assert_success(bool val, std::string message="") {
if (!val) {
if (message == "") {
throw RuntimeError("PLY writing failed");
} else {
throw RuntimeError(message);
}
}
}
/**
* Return the string name with prefix stripped.
*/
std::string strip_prefix(const std::string& name,
const std::string& prefix) {
if (name.substr(0, prefix.size()) == prefix) {
return name.substr(prefix.size());
} else {
return name;
}
}
}
using namespace PLYWriterHelper;
void PLYWriter::with_attribute(const std::string& attr_name) {
m_attr_names.push_back(attr_name);
}
void PLYWriter::write_mesh(Mesh& mesh) {
p_ply ply = ply_create(m_filename.c_str(),
m_in_ascii ? PLY_ASCII : PLY_LITTLE_ENDIAN,
NULL, 0, NULL);
assert_success(ply != NULL, "ply_create_failed");
regroup_attribute_names(mesh);
add_vertex_elements_header(mesh, ply);
add_face_elements_header(mesh, ply);
add_voxel_elements_header(mesh, ply);
if (!is_anonymous()) {
assert_success(ply_add_comment(ply, "Generated by PyMesh"),
"Adding comment failed");
}
assert_success(ply_write_header(ply), "Writting header failed");
write_vertex_elements(mesh, ply);
write_face_elements(mesh, ply);
write_voxel_elements(mesh, ply);
ply_close(ply);
}
void PLYWriter::write(
const VectorF& vertices,
const VectorI& faces,
const VectorI& voxels,
size_t dim, size_t vertex_per_face, size_t vertex_per_voxel) {
if (m_attr_names.size() != 0) {
std::cerr << "Warning: all attributes are ignored" << std::endl;
m_attr_names.clear();
}
Mesh::Ptr mesh = MeshFactory()
.load_data(vertices, faces, voxels, dim, vertex_per_face, vertex_per_voxel)
.create();
write_mesh(*mesh);
}
void PLYWriter::regroup_attribute_names(Mesh& mesh) {
const size_t num_vertices = mesh.get_num_vertices();
const size_t num_faces = mesh.get_num_faces();
const size_t num_voxels = mesh.get_num_voxels();
m_vertex_attr_names.clear();
m_face_attr_names.clear();
m_voxel_attr_names.clear();
for (NameArray::const_iterator itr = m_attr_names.begin();
itr != m_attr_names.end(); itr++) {
const std::string& name = *itr;
if (!mesh.has_attribute(name)) {
std::stringstream err_msg;
err_msg << "Outputing PLY file failed because attribute \""
<< name << "\" does not exist!" << std::endl;
throw RuntimeError(err_msg.str());
}
const VectorF& attr = mesh.get_attribute(name);
const size_t attr_size = attr.size();
if (name.substr(0, 6) == "vertex" && num_vertices > 0 && attr_size % num_vertices == 0) {
m_vertex_attr_names.push_back(name);
} else if (name.substr(0, 4) == "face" && num_faces > 0 && attr_size % num_faces== 0) {
m_face_attr_names.push_back(name);
} else if (name.substr(0, 5) == "voxel" && num_voxels > 0 && attr_size % num_voxels== 0) {
m_voxel_attr_names.push_back(name);
} else if (name == "corner_texture" && num_faces > 0 && attr_size % num_faces == 0) {
m_face_attr_names.push_back(name);
} else {
// Use cardinality to determine attribute type.
if (num_vertices > 0 && attr_size % num_vertices == 0) {
m_vertex_attr_names.push_back(name);
} else if (num_faces > 0 && attr_size % num_faces == 0) {
m_face_attr_names.push_back(name);
} else if (num_voxels > 0 && attr_size % num_voxels == 0) {
m_voxel_attr_names.push_back(name);
} else {
std::cerr << "Unkown attribute type (" << name << ") ignored."
<< std::endl;
}
}
}
}
void PLYWriter::add_vertex_elements_header(Mesh& mesh, p_ply& ply) {
const size_t dim = mesh.get_dim();
const size_t num_vertices = mesh.get_num_vertices();
assert_success(ply_add_element(ply, "vertex", num_vertices),
"Add element failed");
assert_success(ply_add_scalar_property(ply, "x", m_scalar),
"Add X coordinate failed");
assert_success(ply_add_scalar_property(ply, "y", m_scalar),
"Add Y coordinate failed");
if (dim == 3) {
assert_success(ply_add_scalar_property(ply, "z", m_scalar),
"Add Z coordinate failed");
}
for (NameArray::const_iterator itr = m_vertex_attr_names.begin();
itr != m_vertex_attr_names.end(); itr++) {
const std::string name = strip_prefix(*itr, "vertex_");
const VectorF attr = mesh.get_attribute(*itr);
const size_t per_vertex_size = attr.size() / num_vertices;
e_ply_type ply_type = m_scalar;
if (name == "red" || name == "green" || name == "blue") {
ply_type = PLY_UCHAR;
}
if (per_vertex_size == 1) {
assert_success(ply_add_scalar_property(ply, name.c_str(), ply_type),
"Add scalar property failed");
} else {
assert_success(ply_add_list_property(ply, name.c_str(), PLY_UCHAR,
ply_type), "Add list proerty failed");
}
}
}
void PLYWriter::add_face_elements_header(Mesh& mesh, p_ply& ply) {
const size_t num_faces = mesh.get_num_faces();
assert_success(ply_add_element(ply, "face", num_faces), "Add face failed");
assert_success(ply_add_list_property(ply, "vertex_indices", PLY_UCHAR,
PLY_INT), "Add face vertex indices failed");
for (NameArray::const_iterator itr = m_face_attr_names.begin();
itr != m_face_attr_names.end(); itr++) {
std::string name = strip_prefix(*itr, "face_");
const VectorF& attr = mesh.get_attribute(*itr);
const size_t per_face_size = attr.size() / num_faces;
e_ply_type ply_type = m_scalar;
if (name == "red" || name == "green" || name == "blue") {
ply_type = PLY_UCHAR;
}
if (per_face_size == 1) {
assert_success(ply_add_scalar_property(ply, name.c_str(), ply_type),
"Add per face scalar attribute failed");
} else {
if (name == "corner_texture") {
name = "texcoord";
}
assert_success(ply_add_list_property(ply, name.c_str(), PLY_UCHAR,
ply_type), "Add per face vector attribute failed");
}
}
}
void PLYWriter::add_voxel_elements_header(Mesh& mesh, p_ply& ply) {
const size_t num_voxels = mesh.get_num_voxels();
if (num_voxels == 0) return;
assert_success(ply_add_element(ply, "voxel", num_voxels), "Add voxel failed");
assert_success(ply_add_list_property(ply, "vertex_indices", PLY_UCHAR,
PLY_INT), "Add voxel vertex indices failed");
for (NameArray::const_iterator itr = m_voxel_attr_names.begin();
itr != m_voxel_attr_names.end(); itr++) {
const std::string name = strip_prefix(*itr, "voxel_");
const VectorF& attr = mesh.get_attribute(*itr);
const size_t per_voxel_size = attr.size() / num_voxels;
e_ply_type ply_type = m_scalar;
if (name == "red" || name == "green" || name == "blue") {
ply_type = PLY_UCHAR;
}
if (per_voxel_size == 1) {
assert_success(ply_add_scalar_property(ply, name.c_str(), ply_type),
"Add per voxel scalar attribute failed");
} else {
assert_success(ply_add_list_property(ply, name.c_str(), PLY_UCHAR,
ply_type), "Add per voxel vector attribute failed");
}
}
}
void PLYWriter::write_vertex_elements(Mesh& mesh, p_ply& ply) {
const size_t dim = mesh.get_dim();
const size_t num_vertices = mesh.get_num_vertices();
const VectorF& vertices = mesh.get_vertices();
std::vector<const VectorF*> attributes;
std::vector<size_t> per_vertex_sizes;
for (NameArray::const_iterator itr = m_vertex_attr_names.begin();
itr != m_vertex_attr_names.end(); itr++) {
const std::string& name = *itr;
const VectorF& attr = mesh.get_attribute(name);
attributes.push_back(&attr);
per_vertex_sizes.push_back(attr.size() / num_vertices);
}
const size_t num_attributes = attributes.size();
for (size_t i=0; i<num_vertices; i++) {
for (size_t j=0; j<dim; j++) {
ply_write(ply, vertices[i*dim + j]);
}
for (size_t j=0; j<num_attributes; j++) {
const size_t per_vertex_size = per_vertex_sizes[j];
if (per_vertex_size != 1) {
ply_write(ply, per_vertex_size);
}
for (size_t k=0; k<per_vertex_size; k++) {
ply_write(ply, attributes[j]->coeff(i*per_vertex_size + k));
}
}
}
}
void PLYWriter::write_face_elements(Mesh& mesh, p_ply& ply) {
const size_t num_faces = mesh.get_num_faces();
const size_t num_vertex_per_face = mesh.get_vertex_per_face();
const VectorI& faces = mesh.get_faces();
std::vector<const VectorF*> attributes;
std::vector<size_t> per_face_sizes;
for (NameArray::const_iterator itr = m_face_attr_names.begin();
itr != m_face_attr_names.end(); itr++) {
const std::string& name = *itr;
const VectorF& attr = mesh.get_attribute(name);
attributes.push_back(&attr);
per_face_sizes.push_back(attr.size() / num_faces);
}
const size_t num_attributes = attributes.size();
for (size_t i=0; i<num_faces; i++) {
ply_write(ply, num_vertex_per_face);
for (size_t j=0; j<num_vertex_per_face; j++) {
ply_write(ply, faces[i*num_vertex_per_face + j]);
}
for (size_t j=0; j<num_attributes; j++) {
const size_t per_face_size = per_face_sizes[j];
if (per_face_size != 1) {
ply_write(ply, per_face_size);
}
for (size_t k=0; k<per_face_size; k++) {
ply_write(ply, attributes[j]->coeff(i*per_face_size + k));
}
}
}
}
void PLYWriter::write_voxel_elements(Mesh& mesh, p_ply& ply) {
const size_t num_voxels = mesh.get_num_voxels();
if (num_voxels == 0) return;
const size_t num_vertex_per_voxel = mesh.get_vertex_per_voxel();
const VectorI& voxels = mesh.get_voxels();
std::vector<const VectorF*> attributes;
std::vector<size_t> per_voxel_sizes;
for (NameArray::const_iterator itr = m_voxel_attr_names.begin();
itr != m_voxel_attr_names.end(); itr++) {
const std::string& name = *itr;
const VectorF& attr = mesh.get_attribute(name);
attributes.push_back(&attr);
assert(attr.size() % num_voxels == 0);
per_voxel_sizes.push_back(attr.size() / num_voxels);
}
const size_t num_attributes = attributes.size();
for (size_t i=0; i<num_voxels; i++) {
ply_write(ply, num_vertex_per_voxel);
for (size_t j=0; j<num_vertex_per_voxel; j++) {
ply_write(ply, voxels[i*num_vertex_per_voxel + j]);
}
for (size_t j=0; j<num_attributes; j++) {
const size_t per_voxel_size = per_voxel_sizes[j];
if (per_voxel_size != 1) {
ply_write(ply, per_voxel_size);
}
for (size_t k=0; k<per_voxel_size; k++) {
ply_write(ply, attributes[j]->coeff(i*per_voxel_size + k));
}
}
}
}