Commit 45efc7b9 authored by Alexander Dielen's avatar Alexander Dielen

return normals, colors, etc. as numpy arrays

parent b9abfe76
......@@ -201,40 +201,54 @@ OM::FaceHandle add_face(Mesh& _self, const py::list& _vhandles) {
* @param _vec The vector to be converted.
*/
template<class Mesh, class Vector>
py::array_t<typename Vector::value_type> vec2numpy(Mesh& _mesh, Vector& _vec) {
py::array_t<typename Vector::value_type> vec2numpy(Mesh& _mesh, Vector& _vec, size_t _n = 1) {
typedef typename Vector::value_type dtype;
return py::array_t<dtype>({_vec.size()}, {sizeof(dtype)}, _vec.data(), py::cast(_mesh));
std::vector<size_t> shape;
std::vector<size_t> strides;
if (_n == 1) {
shape = {_vec.size()};
strides = {sizeof(dtype)};
}
else {
shape = {_n, _vec.size()};
strides = {_vec.size() * sizeof(dtype), sizeof(dtype)};
}
return py::array_t<dtype>(shape, strides, _vec.data(), py::cast(_mesh));
}
template<class Mesh>
py::array_t<float> flt2numpy(Mesh& _mesh, const float& _flt) {
return py::array_t<float>({1}, {sizeof(float)}, &_flt, py::cast(_mesh));
py::array_t<float> flt2numpy(Mesh& _mesh, const float& _flt, size_t _n = 1) {
return py::array_t<float>({_n}, {sizeof(float)}, &_flt, py::cast(_mesh));
}
template<class Mesh>
py::array_t<double> flt2numpy(Mesh& _mesh, const double& _flt) {
return py::array_t<double>({1}, {sizeof(double)}, &_flt, py::cast(_mesh));
py::array_t<double> flt2numpy(Mesh& _mesh, const double& _flt, size_t _n = 1) {
return py::array_t<double>({_n}, {sizeof(double)}, &_flt, py::cast(_mesh));
}
/**
* Converts entire properties to numpy arrays with shape (n, dim).
*
* The returned array references the vector's underlying data, i.e. changes
* made to the returned array affect the original mesh.
*
* @tparam Mesh A Mesh type.
* @tparam vector A Vector type.
*
* @param _mesh The mesh that owns the vector's underlying memory. In order
* to avaoid dangling pointers, the lifetime of this mesh is tied to the
* lifetime of the returned numpy array.
*/
template<class Mesh, class Vector>
py::array_t<typename Vector::value_type> prop2numpy(Mesh& _mesh, Vector& _vec, size_t _n) {
typedef typename Vector::value_type dtype;
auto shape = {_n, _vec.size()};
auto strides = {_vec.size() * sizeof(dtype), sizeof(dtype)};
return py::array_t<dtype>(shape, strides, _vec.data(), py::cast(_mesh));
template <class Mesh, class IndexHandle, class Array>
void set_normal(Mesh& _self, IndexHandle _h, const Array& _arr) {
_self.set_normal(_h, typename Mesh::Normal(_arr.at(0), _arr.at(1), _arr.at(2)));
}
template <class Mesh, class IndexHandle, class Array>
void set_color(Mesh& _self, IndexHandle _h, const Array& _arr) {
_self.set_color(_h, typename Mesh::Color(_arr.at(0), _arr.at(1), _arr.at(2), _arr.at(3)));
}
template <class Mesh, class IndexHandle, class Array>
void set_texcoord1d(Mesh& _self, IndexHandle _h, const Array& _arr) {
_self.set_texcoord1D(_h, _arr.at(0));
}
template <class Mesh, class IndexHandle, class Array>
void set_texcoord2d(Mesh& _self, IndexHandle _h, const Array& _arr) {
_self.set_texcoord2D(_h, typename Mesh::TexCoord2D(_arr.at(0), _arr.at(1)));
}
template <class Mesh, class IndexHandle, class Array>
void set_texcoord3d(Mesh& _self, IndexHandle _h, const Array& _arr) {
_self.set_texcoord3D(_h, typename Mesh::TexCoord3D(_arr.at(0), _arr.at(1), _arr.at(2)));
}
/**
......@@ -357,6 +371,10 @@ void expose_mesh(py::module& m, const char *_name) {
typedef typename Mesh::Normal Normal;
typedef typename Mesh::Color Color;
typedef typename Mesh::TexCoord1D TexCoord1D;
typedef typename Mesh::TexCoord2D TexCoord2D;
typedef typename Mesh::TexCoord3D TexCoord3D;
//======================================================================
// KernelT Function Pointers
//======================================================================
......@@ -943,7 +961,7 @@ void expose_mesh(py::module& m, const char *_name) {
.def("is_trimesh", &Mesh::is_trimesh)
//======================================================================
// numpy
// numpy vector getter
//======================================================================
.def("point", [](Mesh& _self, OM::VertexHandle _h) { return vec2numpy(_self, _self.point(_h)); })
......@@ -964,7 +982,54 @@ void expose_mesh(py::module& m, const char *_name) {
.def("texcoord3D", [](Mesh& _self, OM::VertexHandle _h) { return vec2numpy(_self, _self.texcoord3D(_h)); })
.def("texcoord3D", [](Mesh& _self, OM::HalfedgeHandle _h) { return vec2numpy(_self, _self.texcoord3D(_h)); })
.def("points", [](Mesh& _self) { return prop2numpy(_self, _self.point(OM::VertexHandle(0)), _self.n_vertices()); })
//======================================================================
// numpy vector setter
//======================================================================
.def("set_point", [](Mesh& _self, OM::VertexHandle _h, py::array_t<typename Point::value_type> _arr)
{_self.point(_h) = Point(_arr.at(0), _arr.at(1), _arr.at(2));})
.def("set_normal", &set_normal<Mesh, OM::VertexHandle, py::array_t<typename Normal::value_type> >)
.def("set_normal", &set_normal<Mesh, OM::HalfedgeHandle, py::array_t<typename Normal::value_type> >)
.def("set_normal", &set_normal<Mesh, OM::FaceHandle, py::array_t<typename Normal::value_type> >)
.def("set_color", &set_color<Mesh, OM::VertexHandle, py::array_t<typename Color::value_type> >)
.def("set_color", &set_color<Mesh, OM::HalfedgeHandle, py::array_t<typename Color::value_type> >)
.def("set_color", &set_color<Mesh, OM::EdgeHandle, py::array_t<typename Color::value_type> >)
.def("set_color", &set_color<Mesh, OM::FaceHandle, py::array_t<typename Color::value_type> >)
.def("set_texcoord1D", &set_texcoord1d<Mesh, OM::VertexHandle, py::array_t<TexCoord1D> >)
.def("set_texcoord1D", &set_texcoord1d<Mesh, OM::HalfedgeHandle, py::array_t<TexCoord1D> >)
.def("set_texcoord2D", &set_texcoord2d<Mesh, OM::VertexHandle, py::array_t<typename TexCoord2D::value_type> >)
.def("set_texcoord2D", &set_texcoord2d<Mesh, OM::HalfedgeHandle, py::array_t<typename TexCoord2D::value_type> >)
.def("set_texcoord3D", &set_texcoord3d<Mesh, OM::VertexHandle, py::array_t<typename TexCoord3D::value_type> >)
.def("set_texcoord3D", &set_texcoord3d<Mesh, OM::HalfedgeHandle, py::array_t<typename TexCoord3D::value_type> >)
//======================================================================
// numpy matrix getter
//======================================================================
.def("points", [](Mesh& _self) { return vec2numpy(_self, _self.point(OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_normals", [](Mesh& _self) { return vec2numpy(_self, _self.normal (OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_colors", [](Mesh& _self) { return vec2numpy(_self, _self.color (OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_colors", [](Mesh& _self) { return vec2numpy(_self, _self.color (OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_texcoords1D", [](Mesh& _self) { return flt2numpy(_self, _self.texcoord1D(OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_texcoords2D", [](Mesh& _self) { return vec2numpy(_self, _self.texcoord2D(OM::VertexHandle(0)), _self.n_vertices()); })
.def("vertex_texcoords3D", [](Mesh& _self) { return vec2numpy(_self, _self.texcoord3D(OM::VertexHandle(0)), _self.n_vertices()); })
.def("halfedge_normals", [](Mesh& _self) { return vec2numpy(_self, _self.normal (OM::HalfedgeHandle(0)), _self.n_halfedges()); })
.def("halfedge_colors", [](Mesh& _self) { return vec2numpy(_self, _self.color (OM::HalfedgeHandle(0)), _self.n_halfedges()); })
.def("halfedge_texcoords1D", [](Mesh& _self) { return flt2numpy(_self, _self.texcoord1D(OM::HalfedgeHandle(0)), _self.n_halfedges()); })
.def("halfedge_texcoords2D", [](Mesh& _self) { return vec2numpy(_self, _self.texcoord2D(OM::HalfedgeHandle(0)), _self.n_halfedges()); })
.def("halfedge_texcoords3D", [](Mesh& _self) { return vec2numpy(_self, _self.texcoord3D(OM::HalfedgeHandle(0)), _self.n_halfedges()); })
.def("edge_colors", [](Mesh& _self) { return vec2numpy(_self, _self.color(OM::EdgeHandle(0)), _self.n_edges()); })
.def("face_normals", [](Mesh& _self) { return vec2numpy(_self, _self.normal(OM::FaceHandle(0)), _self.n_faces()); })
.def("face_colors", [](Mesh& _self) { return vec2numpy(_self, _self.color (OM::FaceHandle(0)), _self.n_faces()); })
;
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