Skip to content

Commit 6f8e9e6

Browse files
committed
assm now saves in ply format.
1 parent 02f11a7 commit 6f8e9e6

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

relightlab/normalstask.cpp

+53-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ void NormalsTask::run() {
169169
bool proceed = progressed("Adaptive mesh normal integration...", 50);
170170
if(!proceed)
171171
return;
172-
QString filename = output.left(output.size() -4) + ".obj";
172+
//TODO move to saveply
173+
QString filename = output.left(output.size() -4) + ".ply";
173174

174175
assm(filename, normals, parameters.assm_error);
175176

@@ -198,6 +199,56 @@ void NormalsTask::run() {
198199
progressed("Done", 100);
199200
}
200201

202+
bool savePly(const char *filename, pmp::SurfaceMesh &mesh) {
203+
QFile file(filename);
204+
bool success = file.open(QFile::WriteOnly);
205+
if(!success)
206+
return false;
207+
208+
209+
std::vector<float> vertices;
210+
for(auto vertex: mesh.vertices()) {
211+
auto p = mesh.position(vertex);
212+
vertices.push_back(p[0]);
213+
vertices.push_back(p[1]);
214+
vertices.push_back(p[2]);
215+
}
216+
std::vector<uint8_t> indices;
217+
218+
int count =0 ;
219+
for (auto face : mesh.faces()) {
220+
indices.resize(13*(count+1));
221+
uint8_t *start = &indices[13*count];
222+
start[0] = 3;
223+
int *triangle = (int *)(start + 1);
224+
225+
int i = 0;
226+
for (auto vertex : mesh.vertices(face)) {
227+
triangle[i++] = vertex.idx() + 1;
228+
}
229+
}
230+
231+
{
232+
QTextStream stream(&file);
233+
234+
stream << "ply\n";
235+
stream << "format binary_little_endian 1.0\n";
236+
stream << "element vertex " << vertices.size()/3 << "\n";
237+
stream << "property float x\n";
238+
stream << "property float y\n";
239+
stream << "property float z\n";
240+
stream << "element face " << indices.size()/13 << "\n";
241+
stream << "property list uchar int vertex_index\n";
242+
stream << "end_header\n";
243+
}
244+
245+
246+
file.write((const char *)vertices.data(), vertices.size()*4);
247+
file.write((const char *)indices.data(), indices.size());
248+
file.close();
249+
return true;
250+
}
251+
201252
bool saveObj(const char *filename, pmp::SurfaceMesh &mesh) {
202253
FILE *fp = fopen(filename, "wb");
203254
if(!fp) {
@@ -244,7 +295,7 @@ void NormalsTask::assm(QString filename, vector<float> &_normals, float approx_e
244295
pmp::Integration<double, pmp::Orthographic> integrator(remesher.mesh(), normals, mask);
245296
integrator.run();
246297

247-
saveObj(filename.toStdString().c_str(), remesher.mesh());
298+
savePly(filename.toStdString().c_str(), remesher.mesh());
248299
}
249300

250301

0 commit comments

Comments
 (0)