Commit a0d5d21e authored by Alexander Dielen's avatar Alexander Dielen

return points as numpy arrays

parent e1aa9eb5
......@@ -7,6 +7,11 @@
#include <memory>
#include <boost/python/numpy.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
namespace OpenMesh {
namespace Python {
......@@ -100,6 +105,9 @@ void expose_status_bits_and_info() {
}
BOOST_PYTHON_MODULE(openmesh) {
Py_Initialize();
np::initialize();
expose_items();
expose_handles();
expose_status_bits_and_info();
......
......@@ -5,8 +5,12 @@
#include "Iterator.hh"
#include "Circulator.hh"
#include <boost/python/numpy.hpp>
#include <boost/python/stl_iterator.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
namespace OpenMesh {
namespace Python {
......@@ -186,6 +190,46 @@ FaceHandle add_face(Mesh& _self, const list& _vhandles) {
return _self.add_face(vector);
}
/**
* Returns a single point of a mesh as a numpy array with shape (3).
*
* The returned array references the underlying point data, i.e. changes made to
* this array affect the original point/mesh.
*
* @tparam Mesh A Mesh type.
*
* @param _self The mesh instance that is to be used.
* @param _vh The vertex handle.
*/
template<class Mesh>
np::ndarray point_np(Mesh& _self, VertexHandle _vh) {
double *ptr = _self.point(_vh).data();
np::ndarray array = np::from_data(ptr, np::dtype::get_builtin<double>(),
py::make_tuple(3), py::make_tuple(sizeof(double)), py::object());
return array;
}
/**
* Returns all points of a mesh as a numpy array with shape (n, 3).
*
* The returned array references the underlying point data, i.e. changes made to
* this array affect the original point/mesh.
*
* @tparam Mesh A Mesh type.
*
* @param _self The mesh instance that is to be used.
*/
template<class Mesh>
np::ndarray points_np(Mesh& _self) {
// const double *ptr = _self.points()->data();
double *ptr = _self.point(VertexHandle(0)).data();
np::ndarray array = np::from_data(ptr, np::dtype::get_builtin<double>(),
py::make_tuple(_self.n_vertices(), 3),
py::make_tuple(3 * sizeof(double), sizeof(double)),
py::object());
return array;
}
/**
* This function template is used to expose mesh member functions that are only
* available for a specific type of mesh (i.e. they are available for polygon
......@@ -888,6 +932,13 @@ void expose_mesh(const char *_name) {
.def("is_trimesh", &Mesh::is_trimesh)
.staticmethod("is_trimesh")
//======================================================================
// numpy
//======================================================================
.def("point_np", &point_np<Mesh>)
.def("points_np", &points_np<Mesh>)
;
expose_type_specific_functions(class_mesh);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment