From dbcca72f0c94d49f6c39722f8661e5294af2cf02 Mon Sep 17 00:00:00 2001 From: Alexander Dielen Date: Tue, 30 Jan 2018 13:26:33 +0100 Subject: [PATCH] make sure arrays are dense and c style --- src/Mesh.hh | 8 +++--- tests/test_property_array.py | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Mesh.hh b/src/Mesh.hh index f70f653..702b6f1 100644 --- a/src/Mesh.hh +++ b/src/Mesh.hh @@ -1361,16 +1361,16 @@ void expose_mesh(py::module& m, const char *_name) { // set_property_array //====================================================================== - .def("set_property_array", [] (Mesh& _self, OM::VPropHandleT _ph, py::array_t _arr) { + .def("set_property_array", [] (Mesh& _self, OM::VPropHandleT _ph, py::array_t _arr) { return set_property_array, OM::VertexHandle>(_self, _ph, _arr, _self.n_vertices()); }) - .def("set_property_array", [] (Mesh& _self, OM::HPropHandleT _ph, py::array_t _arr) { + .def("set_property_array", [] (Mesh& _self, OM::HPropHandleT _ph, py::array_t _arr) { return set_property_array, OM::HalfedgeHandle>(_self, _ph, _arr, _self.n_halfedges()); }) - .def("set_property_array", [] (Mesh& _self, OM::EPropHandleT _ph, py::array_t _arr) { + .def("set_property_array", [] (Mesh& _self, OM::EPropHandleT _ph, py::array_t _arr) { return set_property_array, OM::EdgeHandle>(_self, _ph, _arr, _self.n_edges()); }) - .def("set_property_array", [] (Mesh& _self, OM::FPropHandleT _ph, py::array_t _arr) { + .def("set_property_array", [] (Mesh& _self, OM::FPropHandleT _ph, py::array_t _arr) { return set_property_array, OM::FaceHandle>(_self, _ph, _arr, _self.n_faces()); }) ; diff --git a/tests/test_property_array.py b/tests/test_property_array.py index faf1ca8..afdfd3c 100644 --- a/tests/test_property_array.py +++ b/tests/test_property_array.py @@ -12,34 +12,82 @@ class Python(unittest.TestCase): def test_vertex_property_array(self): prop = openmesh.VPropHandle() self.mesh.add_property(prop) + # c_contiguous arr1 = np.random.rand(self.mesh.n_vertices(), 10) self.mesh.set_property_array(prop, arr1) arr2 = self.mesh.property_array(prop) self.assertTrue(np.allclose(arr1, arr2)) + for vh in self.mesh.vertices(): + arr3 = self.mesh.property(prop, vh) + self.assertTrue(np.allclose(arr1[vh.idx()], arr3)) + # f_contiguous + arr1 = np.random.rand(10, self.mesh.n_vertices()) + self.mesh.set_property_array(prop, arr1.T) + arr2 = self.mesh.property_array(prop) + self.assertTrue(np.allclose(arr1.T, arr2)) + for vh in self.mesh.vertices(): + arr3 = self.mesh.property(prop, vh) + self.assertTrue(np.allclose(arr1.T[vh.idx()], arr3)) def test_halfedge_property_array(self): prop = openmesh.HPropHandle() self.mesh.add_property(prop) + # c_contiguous arr1 = np.random.rand(self.mesh.n_halfedges(), 10) self.mesh.set_property_array(prop, arr1) arr2 = self.mesh.property_array(prop) self.assertTrue(np.allclose(arr1, arr2)) + for hh in self.mesh.halfedges(): + arr3 = self.mesh.property(prop, hh) + self.assertTrue(np.allclose(arr1[hh.idx()], arr3)) + # f_contiguous + arr1 = np.random.rand(10, self.mesh.n_halfedges()) + self.mesh.set_property_array(prop, arr1.T) + arr2 = self.mesh.property_array(prop) + self.assertTrue(np.allclose(arr1.T, arr2)) + for hh in self.mesh.halfedges(): + arr3 = self.mesh.property(prop, hh) + self.assertTrue(np.allclose(arr1.T[hh.idx()], arr3)) def test_edge_property_array(self): prop = openmesh.EPropHandle() self.mesh.add_property(prop) + # c_contiguous arr1 = np.random.rand(self.mesh.n_edges(), 10) self.mesh.set_property_array(prop, arr1) arr2 = self.mesh.property_array(prop) self.assertTrue(np.allclose(arr1, arr2)) + for eh in self.mesh.edges(): + arr3 = self.mesh.property(prop, eh) + self.assertTrue(np.allclose(arr1[eh.idx()], arr3)) + # f_contiguous + arr1 = np.random.rand(10, self.mesh.n_edges()) + self.mesh.set_property_array(prop, arr1.T) + arr2 = self.mesh.property_array(prop) + self.assertTrue(np.allclose(arr1.T, arr2)) + for eh in self.mesh.edges(): + arr3 = self.mesh.property(prop, eh) + self.assertTrue(np.allclose(arr1.T[eh.idx()], arr3)) def test_face_property_array(self): prop = openmesh.FPropHandle() self.mesh.add_property(prop) + # c_contiguous arr1 = np.random.rand(self.mesh.n_faces(), 10) self.mesh.set_property_array(prop, arr1) arr2 = self.mesh.property_array(prop) self.assertTrue(np.allclose(arr1, arr2)) + for fh in self.mesh.faces(): + arr3 = self.mesh.property(prop, fh) + self.assertTrue(np.allclose(arr1[fh.idx()], arr3)) + # f_contiguous + arr1 = np.random.rand(10, self.mesh.n_faces()) + self.mesh.set_property_array(prop, arr1.T) + arr2 = self.mesh.property_array(prop) + self.assertTrue(np.allclose(arr1.T, arr2)) + for fh in self.mesh.faces(): + arr3 = self.mesh.property(prop, fh) + self.assertTrue(np.allclose(arr1.T[fh.idx()], arr3)) if __name__ == '__main__': -- GitLab