31 #ifndef SRC_TYPE_CASTERS_H_ 32 #define SRC_TYPE_CASTERS_H_ 34 #include <pybind11/cast.h> 35 #include <pybind11/numpy.h> 37 #include <Rcs_MatNd.h> 50 typedef pybind11::array_t<double, pybind11::array::forcecast | pybind11::array::c_style> ArrayMatNd;
55 static MatNd MatNd_fromNumpy(ArrayMatNd& nparray)
60 if (nparray.ndim() >= 1) {
63 if (nparray.ndim() == 2) {
66 return MatNd_fromPtr(m, n, const_cast<double*>(nparray.data()));
76 static handle MatNd_toNumpy(
const MatNd* mat, handle parent = handle(),
bool writeable =
true)
82 array = ArrayMatNd(mat->m, mat->ele, parent);
86 array = ArrayMatNd({mat->m, mat->n}, mat->ele, parent);
91 array_proxy(array.ptr())->flags &= ~detail::npy_api::NPY_ARRAY_WRITEABLE_;
95 return array.release();
101 static handle MatNd_toNumpyRef(
const MatNd* mat, handle parent = none())
103 return MatNd_toNumpy(mat, parent,
false);
109 static handle MatNd_toNumpyRef(MatNd* mat, handle parent = none())
111 return MatNd_toNumpy(mat, parent,
true);
117 static handle MatNd_toNumpyOwn(MatNd* mat)
119 capsule caps(mat, [](
void* ref) { MatNd_destroy((MatNd*) ref); });
120 return MatNd_toNumpy(mat, caps);
125 struct type_caster<MatNd>
131 #if PYBIND11_VERSION_MINOR >= 3 132 static constexpr
auto name = _(
"MatNd");
134 static PYBIND11_DESCR name() {
return type_descr(_(
"MatNd")); }
143 ArrayMatNd copyOrRef;
146 make_caster<double> scalar_caster;
166 throw value_error(
"Cannot convert None to a MatNd ref");
172 template<
typename T_>
using cast_op_type = pybind11::detail::cast_op_type<T_>;
179 bool load(handle src,
bool convert)
181 if (!convert && !isinstance<ArrayMatNd>(src)) {
190 if (scalar_caster.load(src, convert)) {
193 value.ele = cast_op<double*>(scalar_caster);
197 value.stackMem =
true;
202 copyOrRef = ArrayMatNd::ensure(src);
205 auto dims = copyOrRef.ndim();
211 value = MatNd_fromNumpy(copyOrRef);
221 static handle cast(MatNd* src, return_value_policy policy, handle parent)
224 case return_value_policy::take_ownership:
225 case return_value_policy::automatic:
229 case return_value_policy::move:
231 return MatNd_toNumpyOwn(src);
232 case return_value_policy::copy:
233 return MatNd_toNumpy(src);
234 case return_value_policy::reference:
235 case return_value_policy::automatic_reference:
236 return MatNd_toNumpyRef(src);
237 case return_value_policy::reference_internal:
238 return MatNd_toNumpyRef(src, parent);
240 pybind11_fail(
"Invalid return_value_policy for Rcs MatNd type");
244 static handle cast(
const MatNd* src, return_value_policy policy, handle parent)
247 case return_value_policy::copy:
248 return MatNd_toNumpy(src);
249 case return_value_policy::reference:
250 case return_value_policy::automatic_reference:
251 return MatNd_toNumpyRef(src);
252 case return_value_policy::reference_internal:
253 return MatNd_toNumpyRef(src, parent);
256 pybind11_fail(
"Invalid return_value_policy for Rcs MatNd type");