Skip to content

Commit

Permalink
Merge pull request #162 from SimonRohou/codac2_ellipsoids
Browse files Browse the repository at this point in the history
[ellips] py binding: C++ cast of AnalyticFunction py objects
  • Loading branch information
SimonRohou authored Dec 16, 2024
2 parents b7444b5 + 100f61f commit 05cc2b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions python/src/core/domains/ellipsoid/codac2_py_Ellipsoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <pybind11/stl.h>
#include <codac2_Ellipsoid.h>
#include "codac2_py_Ellipsoid_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
#include "codac2_py_AnalyticFunction.h"

using namespace std;
using namespace codac2;
Expand Down Expand Up @@ -80,11 +81,17 @@ void export_Ellipsoid(py::module& m)
ELLIPSOID_UNRELIABLE_LINEAR_MAPPING_CONST_ELLIPSOID_REF_CONST_MATRIX_REF_CONST_VECTOR_REF,
"e"_a, "A"_a, "b"_a)

.def("nonlinear_mapping_", (Ellipsoid (*)(const Ellipsoid&,const AnalyticFunction<VectorOpValue>&))&codac2::nonlinear_mapping,
.def("nonlinear_mapping", [](const Ellipsoid& e, py::object f)
{
return nonlinear_mapping(e, pyobject_to_AnalyticFunction(f));
},
ELLIPSOID_NONLINEAR_MAPPING_CONST_ELLIPSOID_REF_CONST_ANALYTICFUNCTION_VECTOROPVALUE_REF,
"e"_a, "f"_a)

.def("nonlinear_mapping_", (Ellipsoid (*)(const Ellipsoid&,const AnalyticFunction<VectorOpValue>&,const Vector&,const Vector&))&codac2::nonlinear_mapping,
.def("nonlinear_mapping", [](const Ellipsoid& e, py::object f, const Vector &trig, const Vector &q)
{
return nonlinear_mapping(e, pyobject_to_AnalyticFunction(f), trig, q);
},
ELLIPSOID_NONLINEAR_MAPPING_CONST_ELLIPSOID_REF_CONST_ANALYTICFUNCTION_VECTOROPVALUE_REF_CONST_VECTOR_REF_CONST_VECTOR_REF,
"e"_a, "f"_a, "trig"_a, "q"_a)

Expand Down
16 changes: 16 additions & 0 deletions python/src/core/functions/analytic/codac2_py_AnalyticFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

inline AnalyticFunction<VectorOpValue> pyobject_to_AnalyticFunction(py::object f)
{
if(py::isinstance<AnalyticFunction<VectorOpValue>>(f))
return f.cast<AnalyticFunction<VectorOpValue>>();

else
{
// Trying to extract an AnalyticFunction<VectorOpValue>
// from the python wrapper 'AnalyticFunction'
py::object f_ = f.attr("f");
if(!py::isinstance<AnalyticFunction<VectorOpValue>>(f_))
assert_release("invalid argument: AnalyticFunction<VectorOpValue> expected");
return f_.cast<AnalyticFunction<VectorOpValue>>();
}
}

#define bind_(exported, op_name, op, doc) \
\
exported \
Expand Down

0 comments on commit 05cc2b0

Please sign in to comment.