From 45917ecf17acacfede909994d7b3a78fc18355da Mon Sep 17 00:00:00 2001 From: Hind Date: Thu, 22 Apr 2021 17:08:17 +0200 Subject: Add random points generator on sphere in python, with an example --- src/python/gudhi/random_point_generators.cc | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/python/gudhi/random_point_generators.cc (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/random_point_generators.cc b/src/python/gudhi/random_point_generators.cc new file mode 100644 index 00000000..39b09a6d --- /dev/null +++ b/src/python/gudhi/random_point_generators.cc @@ -0,0 +1,68 @@ +/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + * Author(s): Hind Montassif + * + * Copyright (C) 2021 Inria + * + * Modification(s): + * - YYYY/MM Author: Description of the modification + */ + +#include +#include + +#include + +#include + +namespace py = pybind11; + + +typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Kern; + +template +py::array_t generate_points_on_sphere(py::object num_points, py::object dim, py::object radius) { + int npoints = num_points.cast(); + int d = dim.cast(); + double rad = radius.cast(); + + py::gil_scoped_release release; + + auto points_generated = Gudhi::generate_points_on_sphere_d(npoints, d, rad); + + py::gil_scoped_acquire acquire; + + py::array_t points({npoints, d}); + + py::buffer_info buf = points.request(); + + double *ptr = static_cast(buf.ptr); + + assert(npoints == buf.shape[0]); + assert(d == buf.shape[1]); + + + for (size_t i = 0; i < (size_t)npoints; i++) + for (size_t j = 0; j < (size_t)d; j++) + ptr[i*d+j] = points_generated.at(i).at(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, + py::arg("num_points"), py::arg("dim"), py::arg("radius"), + R"pbdoc( + Generate points on a sphere + + :param num_points: The number of points to be generated. + :type num_points: integer + :param dim: The sphere dimension. + :type dim: integer + :param radius: The sphere radius. + :type radius: float + :rtype: numpy array of points + :returns: the generated points on a sphere. + )pbdoc"); +} -- cgit v1.2.3