GeezerVison is showing github.com — Add numpy_indices method · Drdady/tinyobjloader@d0195db
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: python/bindings.cc +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | #include "pybind11/pybind11.h" |
2 | 2 | #include "pybind11/stl.h" |
3 | 3 | #include "pybind11/numpy.h" |
| 4 | +#include <cstring> |
4 | 5 |
|
5 | 6 | // Use double precision for better python integration. |
6 | 7 | #define TINYOBJLOADER_USE_DOUBLE |
@@ -114,7 +115,13 @@ PYBIND11_MODULE(tinyobjloader, tobj_module) |
114 | 115 |
|
115 | 116 | py::class_<mesh_t>(tobj_module, "mesh_t") |
116 | 117 | .def(py::init<>()) |
117 | | - .def_readonly("indices", &mesh_t::indices); |
| 118 | + .def_readonly("indices", &mesh_t::indices) |
| 119 | + .def("numpy_indices", [] (mesh_t &instance) { |
| 120 | + auto ret = py::array_t<int>(instance.indices.size() * 3); |
| 121 | + py::buffer_info buf = ret.request(); |
| 122 | + memcpy(buf.ptr, instance.indices.data(), instance.indices.size() * 3 * sizeof(int)); |
| 123 | + return ret; |
| 124 | + }); |
118 | 125 |
|
119 | 126 | py::class_<lines_t>(tobj_module, "lines_t") |
120 | 127 | .def(py::init<>()); |
|