From e59b1cfd338a80a769c0e2b6d677b9474b07beb3 Mon Sep 17 00:00:00 2001 From: Hind Date: Mon, 26 Apr 2021 11:47:36 +0200 Subject: Replace assert with GUDHI_CHECK Make the function non-template Change typing and casting --- src/python/gudhi/random_point_generators.cc | 34 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/python/gudhi/random_point_generators.cc b/src/python/gudhi/random_point_generators.cc index 4306ba98..6eb40429 100644 --- a/src/python/gudhi/random_point_generators.cc +++ b/src/python/gudhi/random_point_generators.cc @@ -12,6 +12,7 @@ #include #include +#include #include @@ -20,36 +21,33 @@ namespace py = pybind11; typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Kern; -template py::array_t generate_points_on_sphere(size_t num_points, int dim, double radius) { - - py::array_t points({(int)num_points, dim}); - + + py::array_t points({num_points, (size_t)dim}); + py::buffer_info buf = points.request(); double *ptr = static_cast(buf.ptr); - assert(num_points == buf.shape[0]); - assert(dim == buf.shape[1]); - - std::vector points_generated; - { - py::gil_scoped_release release; - points_generated = Gudhi::generate_points_on_sphere_d(num_points, dim, radius); - - for (size_t i = 0; i < num_points; i++) - for (size_t j = 0; j < (size_t)dim; j++) - ptr[i*dim+j] = points_generated[i][j]; - } + GUDHI_CHECK(num_points == buf.shape[0], "Py array first dimension not matching num_points on sphere"); + GUDHI_CHECK(dim == buf.shape[1], "Py array second dimension not matching the ambient space dimension"); + + + py::gil_scoped_release release; + auto points_generated = Gudhi::generate_points_on_sphere_d(num_points, dim, radius); + + for (size_t i = 0; i < num_points; i++) + for (int j = 0; j < dim; j++) + ptr[i*dim+j] = points_generated[i][j]; return points; } PYBIND11_MODULE(random_point_generators, m) { m.attr("__license__") = "LGPL v3"; - m.def("generate_points_on_sphere_d", &generate_points_on_sphere, + m.def("generate_points_on_sphere_d", &generate_points_on_sphere, py::arg("num_points"), py::arg("dim"), py::arg("radius") = 1, R"pbdoc( - Generate random i.i.d. points uniformly on a (d-1)-sphere in Rd + Generate random i.i.d. points uniformly on a (d-1)-sphere in R^d :param num_points: The number of points to be generated. :type num_points: unsigned integer -- cgit v1.2.3