Skip to content

Commit

Permalink
Merge pull request #1540 from SamFlt/python_improvements
Browse files Browse the repository at this point in the history
Python bindings improvements
  • Loading branch information
fspindle authored Jan 20, 2025
2 parents e3dfc17 + 7413305 commit 4f308c6
Show file tree
Hide file tree
Showing 6 changed files with 1,357 additions and 73 deletions.
59 changes: 59 additions & 0 deletions modules/python/bindings/include/core/arrays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ py::buffer_info get_buffer_info(vpTranslationVector &array)
return make_array_buffer<double, 1>(array.data, { 3 }, false);
}
template<>
py::buffer_info get_buffer_info(vpPoseVector &array)
{
return make_array_buffer<double, 1>(array.data, { 6 }, false);
}
template<>
py::buffer_info get_buffer_info(vpRotationVector &array)
{
return make_array_buffer<double, 1>(array.data, { array.getRows() }, false);
}
template<>
py::buffer_info get_buffer_info(vpRotationMatrix &array)
{
return make_array_buffer<double, 2>(array.data, { array.getRows(), array.getCols() }, true);
Expand Down Expand Up @@ -605,5 +615,54 @@ Construct a row vector by **copying** a 1D numpy array.
add_cpp_print_helper(pyRowVector, &vpRowVector::cppPrint);
}

void bindings_vpPoseVector(py::class_<vpPoseVector, std::shared_ptr<vpPoseVector>, vpArray2D<double>> &pyPoseVector)
{
pyPoseVector.def_buffer(&get_buffer_info<vpPoseVector>);

pyPoseVector.def("numpy", [](vpPoseVector &self) -> np_array_cf<double> {
return py::cast(self).cast<np_array_cf<double>>();
}, numpy_fn_doc_writable, py::keep_alive<0, 1>());

pyPoseVector.def(py::init([](np_array_cf<double> np_array) {
verify_array_shape_and_dims(np_array, 1, "ViSP pose vector");
const std::vector<py::ssize_t> shape = np_array.request().shape;
vpPoseVector result;
copy_data_from_np(np_array, result.data);
return result;
}), R"doc(
Construct a pose vector by **copying** a 1D numpy array.
:param np_array: The numpy 1D array to copy.
)doc", py::arg("np_array"));
define_get_item_1d_array<py::class_<vpPoseVector, std::shared_ptr<vpPoseVector>, vpArray2D<double>>, vpPoseVector, double>(pyPoseVector);
define_set_item_1d_array<py::class_<vpPoseVector, std::shared_ptr<vpPoseVector>, vpArray2D<double>>, vpPoseVector, double>(pyPoseVector);
}

void bindings_vpRotationVector(py::class_<vpRotationVector, std::shared_ptr<vpRotationVector>, vpArray2D<double>> &pyRotationVector)
{
pyRotationVector.def_buffer(&get_buffer_info<vpRotationVector>);

pyRotationVector.def("numpy", [](vpRotationVector &self) -> np_array_cf<double> {
return py::cast(self).cast<np_array_cf<double>>();
}, numpy_fn_doc_writable, py::keep_alive<0, 1>());

pyRotationVector.def(py::init([](np_array_cf<double> np_array) {
verify_array_shape_and_dims(np_array, 1, "ViSP rotation vector");
const std::vector<py::ssize_t> shape = np_array.request().shape;
vpRotationVector result(shape[0]);
copy_data_from_np(np_array, result.data);
return result;
}), R"doc(
Construct a rotaiton vector by **copying** a 1D numpy array.
:param np_array: The numpy 1D array to copy.
)doc", py::arg("np_array"));
define_get_item_1d_array<py::class_<vpRotationVector, std::shared_ptr<vpRotationVector>, vpArray2D<double>>, vpPoseVector, double>(pyRotationVector);
define_set_item_1d_array<py::class_<vpRotationVector, std::shared_ptr<vpRotationVector>, vpArray2D<double>>, vpPoseVector, double>(pyRotationVector);
}



#endif
127 changes: 127 additions & 0 deletions modules/python/config/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@
"static": false,
"signature": "void npz_save(std::string, std::string, const std::vector<T>, std::string)",
"ignore": true
},
{
"static": false,
"signature": "bool convertFromTypeAndBuildFrom(const nlohmann::json&, T&)",
"ignore": true
},
{
"static": false,
"signature": "nlohmann::json flagsToJSON(const int, const std::vector<E>&)",
"ignore": true
},
{
"static": false,
"signature": "int flagsFromJSON(const nlohmann::json&)",
"ignore": true
}
],
"classes": {
Expand Down Expand Up @@ -478,6 +493,36 @@
"param_is_output": [
true
]
},
{
"static": true,
"signature": "vpBasisFunction* computeBasisFuns(double, unsigned int, unsigned int, const std::vector<double>&)",
"ignore": true
},
{
"static": true,
"signature": "vpBasisFunction** computeDersBasisFuns(double, unsigned int, unsigned int, unsigned int, const std::vector<double>&)",
"ignore": true
},
{
"static": false,
"signature": "vpBasisFunction* computeBasisFuns(double)",
"ignore": true
},
{
"static": false,
"signature": "vpBasisFunction** computeDersBasisFuns(double, unsigned int)",
"ignore": true
},
{
"static": true,
"signature": "vpImagePoint* computeCurveDers(double, unsigned int, unsigned int, unsigned int, const std::vector<double>&, const std::vector<vpImagePoint>&)",
"ignore": true
},
{
"static": false,
"signature": "vpImagePoint* computeCurveDers(double, unsigned int)",
"ignore": true
}
]
},
Expand Down Expand Up @@ -624,6 +669,24 @@
}
]
},
"vpStatisticalTestAbstract": {
"acknowledge_pointer_or_ref_fields": [
"float*"
],
"methods": [
{
"static": false,
"signature": "void getLimits(float&, float&)",
"use_default_param_policy": true
}
]
},
"vpMoment": {
"acknowledge_pointer_or_ref_fields": [
"vpMomentObject*",
"vpMomentDatabase*"
]
},
"vpMomentDatabase": {
"methods": [
{
Expand All @@ -641,6 +704,11 @@
}
]
},
"vpMomentCommon": {
"acknowledge_pointer_or_ref_fields": [
"vpMomentCInvariant*"
]
},
"vpMomentObject": {
"methods": [
{
Expand Down Expand Up @@ -986,6 +1054,22 @@
}
]
},
"vpParticleFilter": {
"specializations": [
{
"python_name": "ParticleFilterVector",
"arguments": [
"vpColVector"
]
},
{
"python_name": "ParticleFilterMatrix",
"arguments": [
"vpMatrix"
]
}
]
},
"vpImageFilter": {
"methods": [
{
Expand Down Expand Up @@ -1095,6 +1179,15 @@
}
]
},
"vpRequest": {
"methods": [
{
"static": false,
"signature": "void addParameterObject(T*, const int&)",
"ignore": true
}
]
},
"vpUDPClient": {
"methods": [
{
Expand Down Expand Up @@ -1164,6 +1257,15 @@
"TypePythonScalar"
]
]
},
{
"static": true,
"signature": "std::vector<T> shuffleVector(const std::vector<T>&, const int32_t&)",
"specializations": [
[
"TypePythonScalar"
]
]
}
]
},
Expand Down Expand Up @@ -1203,6 +1305,31 @@
]
}
]
},
"vpXmlParserCamera": {
"acknowledge_pointer_or_ref_fields": [
"Impl*"
]
},
"vpXmlParserHomogeneousMatrix": {
"acknowledge_pointer_or_ref_fields": [
"Impl*"
]
},
"vpXmlParserRectOriented": {
"acknowledge_pointer_or_ref_fields": [
"Impl*"
]
},
"vpGaussianFilter": {
"acknowledge_pointer_or_ref_fields": [
"Impl*"
]
},
"vpCannyEdgeDetection": {
"acknowledge_pointer_or_ref_fields": [
"const vpImage<bool>*"
]
}
}
}
Loading

0 comments on commit 4f308c6

Please sign in to comment.