|
| 1 | +/*! |
| 2 | + * \file bindings/python/src/Function.cxx |
| 3 | + * \brief |
| 4 | + * \author Thomas Helfer |
| 5 | + * \date 18/11/2025 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <pybind11/pybind11.h> |
| 9 | +#include <pybind11/stl.h> |
| 10 | +#include "MGIS/Python/NumPySupport.hxx" |
| 11 | +#include "MGIS/Function/Function.hxx" |
| 12 | +#include "MGIS/Function/BasicLinearSpace.hxx" |
| 13 | + |
| 14 | +mgis::function::FunctionView<mgis::function::BasicLinearSpace, |
| 15 | + mgis::function::FunctionDataLayoutDescription{}, |
| 16 | + false> |
| 17 | +mgis_convert_to_span(const pybind11::array_t<double> &o) { |
| 18 | + using namespace mgis::function; |
| 19 | + const auto i = o.request(); |
| 20 | + if (i.ndim != 1) { |
| 21 | + const auto s = static_cast<mgis::size_type>(i.size); |
| 22 | + const auto values = std::span{static_cast<const double *>(i.ptr), s}; |
| 23 | + return FunctionView<BasicLinearSpace, FunctionDataLayoutDescription{}, |
| 24 | + false>{BasicLinearSpace{s}, values, 1}; |
| 25 | + } |
| 26 | + mgis::raise("convert_to_span: expected one dimensional array"); |
| 27 | +} // end of mgis_convert_to_span |
| 28 | + |
| 29 | +void declareFunction(pybind11::module_ &m) { |
| 30 | + // FunctionView<BasicLinearSpace, FunctionDataLayoutDescription{}, false |
| 31 | + using mgis::real; |
| 32 | + // |
| 33 | + using BasicFunction = |
| 34 | + mgis::function::Function<mgis::function::BasicLinearSpace>; |
| 35 | + // |
| 36 | + using BasicFunctionView = mgis::function::FunctionView< |
| 37 | + mgis::function::BasicLinearSpace, |
| 38 | + mgis::function::FunctionDataLayoutDescription{}, true>; |
| 39 | + |
| 40 | + pybind11::class_<BasicFunction>(m, "BasicFunction", |
| 41 | + pybind11::buffer_protocol()) |
| 42 | + .def_buffer([](BasicFunction &f) -> pybind11::buffer_info { |
| 43 | + if (f.getNumberOfComponents() == 1) { |
| 44 | + return pybind11::buffer_info( |
| 45 | + f.data().data(), sizeof(real), |
| 46 | + pybind11::format_descriptor<real>::format(), 1, |
| 47 | + {getSpaceSize(f.getSpace())}, {sizeof(real)}); |
| 48 | + } |
| 49 | + return pybind11::buffer_info( |
| 50 | + f.data().data(), sizeof(real), |
| 51 | + pybind11::format_descriptor<real>::format(), 2, |
| 52 | + {getSpaceSize(f.getSpace()), f.getNumberOfComponents()}, |
| 53 | + {sizeof(real) * f.getNumberOfComponents(), sizeof(real)}); |
| 54 | + }); |
| 55 | + |
| 56 | + pybind11::class_<BasicFunctionView>(m, "BasicFunctionView", |
| 57 | + pybind11::buffer_protocol()) |
| 58 | + .def_buffer([](BasicFunctionView &f) -> pybind11::buffer_info { |
| 59 | + if (f.getNumberOfComponents() == 1) { |
| 60 | + return pybind11::buffer_info( |
| 61 | + f.data().data(), sizeof(real), |
| 62 | + pybind11::format_descriptor<real>::format(), 1, |
| 63 | + {getSpaceSize(f.getSpace())}, {sizeof(real) * f.getDataStride()}); |
| 64 | + } |
| 65 | + return pybind11::buffer_info( |
| 66 | + f.data().data(), sizeof(real), |
| 67 | + pybind11::format_descriptor<real>::format(), 2, |
| 68 | + {getSpaceSize(f.getSpace()), f.getNumberOfComponents()}, |
| 69 | + {sizeof(real) * f.getDataStride(), sizeof(real)}); |
| 70 | + }); |
| 71 | + |
| 72 | +} // end of declareFunction |
0 commit comments