Skip to content

Commit 572ab53

Browse files
authored
ply io for py (#170)
1 parent 367eb3e commit 572ab53

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/python/glk.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <glk/texture.hpp>
1010
#include <glk/thin_lines.hpp>
1111
#include <glk/pointcloud_buffer.hpp>
12+
#include <glk/io/ply_io.hpp>
1213
#include <glk/primitives/primitives.hpp>
1314
#include <glk/effects/screen_effect.hpp>
1415
#include <glk/effects/naive_screen_space_ambient_occlusion.hpp>
@@ -341,4 +342,94 @@ void define_glk(py::module_& m) {
341342
primitives_.def("wire_icosahedron", [] { return glk::Primitives::wire_icosahedron(); });
342343
primitives_.def("wire_bunny", [] { return glk::Primitives::wire_bunny(); });
343344
primitives_.def("wire_frustum", [] { return glk::Primitives::wire_frustum(); });
345+
346+
// IO
347+
// glk::PLYData
348+
py::class_<glk::PLYData, std::shared_ptr<glk::PLYData>>(glk_, "PLYData") //
349+
.def(py::init<>())
350+
.def_property(
351+
"vertices",
352+
[](glk::PLYData& self) -> Eigen::Matrix<float, -1, -1, Eigen::RowMajor> {
353+
return Eigen::Map<const Eigen::Matrix<float, -1, -1, Eigen::RowMajor>>(self.vertices[0].data(), self.vertices.size(), 3);
354+
},
355+
[](glk::PLYData& self, const Eigen::MatrixXf& vertices) {
356+
if (vertices.cols() != 3 && vertices.cols() != 4) {
357+
std::cerr << "vertices must be 3 or 4 columns (cols=" << vertices.cols() << ")" << std::endl;
358+
return;
359+
}
360+
361+
self.vertices.resize(vertices.rows());
362+
for (int i = 0; i < vertices.rows(); i++) {
363+
self.vertices[i] = vertices.row(i).head<3>();
364+
}
365+
})
366+
.def_property(
367+
"normals",
368+
[](glk::PLYData& self) -> Eigen::Matrix<float, -1, -1, Eigen::RowMajor> {
369+
return Eigen::Map<const Eigen::Matrix<float, -1, -1, Eigen::RowMajor>>(self.normals[0].data(), self.normals.size(), 3);
370+
},
371+
[](glk::PLYData& self, const Eigen::MatrixXf& normals) {
372+
if (normals.cols() != 3 && normals.cols() != 4) {
373+
std::cerr << "normals must be 3 or 4 columns (cols=" << normals.cols() << ")" << std::endl;
374+
return;
375+
}
376+
377+
self.normals.resize(normals.rows());
378+
for (int i = 0; i < normals.rows(); i++) {
379+
self.normals[i] = normals.row(i).head<3>();
380+
}
381+
})
382+
.def_property(
383+
"intensities",
384+
[](glk::PLYData& self) -> Eigen::VectorXf { return Eigen::Map<const Eigen::VectorXf>(self.intensities.data(), self.intensities.size()); },
385+
[](glk::PLYData& self, const Eigen::VectorXf& intensities) {
386+
self.intensities.resize(intensities.size());
387+
for (int i = 0; i < intensities.size(); i++) {
388+
self.intensities[i] = intensities[i];
389+
}
390+
})
391+
.def_property(
392+
"colors",
393+
[](glk::PLYData& self) -> Eigen::Matrix<float, -1, -1, Eigen::RowMajor> {
394+
return Eigen::Map<const Eigen::Matrix<float, -1, -1, Eigen::RowMajor>>(self.colors[0].data(), self.colors.size(), 4);
395+
},
396+
[](glk::PLYData& self, const Eigen::MatrixXf& colors) {
397+
if (colors.cols() != 4) {
398+
std::cerr << "colors must be 3 or 4 columns (cols=" << colors.cols() << ")" << std::endl;
399+
return;
400+
}
401+
402+
self.colors.resize(colors.rows());
403+
for (int i = 0; i < colors.rows(); i++) {
404+
self.colors[i] = colors.row(i);
405+
}
406+
})
407+
.def_property(
408+
"indices",
409+
[](glk::PLYData& self) -> Eigen::VectorXi { return Eigen::Map<const Eigen::VectorXi>(self.indices.data(), self.indices.size()); },
410+
[](glk::PLYData& self, const Eigen::VectorXi& indices) {
411+
self.indices.resize(indices.size());
412+
for (int i = 0; i < indices.size(); i++) {
413+
self.indices[i] = indices[i];
414+
}
415+
})
416+
.def_property(
417+
"comments",
418+
[](glk::PLYData& self) -> std::vector<std::string> { return self.comments; },
419+
[](glk::PLYData& self, const std::vector<std::string>& comments) { self.comments = comments; });
420+
421+
glk_.def("load_ply", &glk::load_ply, py::arg("filename"));
422+
glk_.def("save_ply", &glk::save_ply, py::arg("filename"), py::arg("ply"), py::arg("binary") = true);
423+
glk_.def("save_ply", [](const std::string& filename, const Eigen::Matrix<float, -1, -1, Eigen::RowMajor>& points) {
424+
if (points.cols() != 3 && points.cols() != 4) {
425+
std::cerr << "points must be 3 or 4 columns (cols=" << points.cols() << ")" << std::endl;
426+
return false;
427+
}
428+
429+
if (points.cols() == 3) {
430+
return glk::save_ply_binary(filename, reinterpret_cast<const Eigen::Matrix<float, 3, 1>*>(points.data()), points.rows());
431+
} else {
432+
return glk::save_ply_binary(filename, reinterpret_cast<const Eigen::Matrix<float, 4, 1>*>(points.data()), points.rows());
433+
}
434+
});
344435
}

0 commit comments

Comments
 (0)