diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index 6bbe7e369..9f6fee4cd 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -108,7 +108,6 @@ if (PYTHONLIBS_FOUND) Pose3_TEST python_TEST Quaternion_TEST - RollingMean_TEST RotationSpline_TEST SemanticVersion_TEST SignalStats_TEST diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index 2e90e339f..9d81af88b 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -18,6 +18,7 @@ if (${pybind11_FOUND}) src/Color.cc src/Helpers.cc src/Rand.cc + src/RollingMean.cc ) target_link_libraries(math PRIVATE @@ -75,6 +76,7 @@ if (${pybind11_FOUND}) Helpers_TEST Line2_TEST Rand_TEST + RollingMean_TEST Vector2_TEST Vector3_TEST Vector4_TEST diff --git a/src/python_pybind11/src/RollingMean.cc b/src/python_pybind11/src/RollingMean.cc new file mode 100644 index 000000000..c84279451 --- /dev/null +++ b/src/python_pybind11/src/RollingMean.cc @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +#include +#include "RollingMean.hh" +#include + +namespace ignition +{ +namespace math +{ +namespace python +{ +void defineMathRollingMean(py::module &m, const std::string &typestr) +{ + using Class = ignition::math::RollingMean; + std::string pyclass_name = typestr; + py::class_(m, + pyclass_name.c_str(), + py::buffer_protocol(), + py::dynamic_attr()) + .def(py::init(), py::arg("_windowSize") = 10) + .def("mean", &Class::Mean, "Get the mean value.") + .def("count", &Class::Count, "Get the number of data points.") + .def("push", &Class::Push, "Insert a new value.") + .def("clear", &Class::Clear, "Remove all the pushed values.") + .def("set_window_size", + &Class::SetWindowSize, + "Set the new window size. This will also clear the data. " + "Nothing happens if the _windowSize is zero.") + .def("window_size", &Class::WindowSize, "Get the window size."); +} +} // namespace python +} // namespace math +} // namespace ignition diff --git a/src/python_pybind11/src/RollingMean.hh b/src/python_pybind11/src/RollingMean.hh new file mode 100644 index 000000000..122e513fc --- /dev/null +++ b/src/python_pybind11/src/RollingMean.hh @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#ifndef IGNITION_MATH_PYTHON__ROLLINGMEAN_HH_ +#define IGNITION_MATH_PYTHON__ROLLINGMEAN_HH_ + +#include +#include + +namespace py = pybind11; + +namespace ignition +{ +namespace math +{ +namespace python +{ +/// Define a pybind11 wrapper for an ignition::math::RollingMean +/** + * \param[in] module a pybind11 module to add the definition to + * \param[in] typestr name of the type used by Python + */ +void defineMathRollingMean(py::module &m, const std::string &typestr); +} // namespace python +} // namespace math +} // namespace ignition + +#endif // IGNITION_MATH_PYTHON__ROLLINGMEAN_HH_ diff --git a/src/python_pybind11/src/_ignition_math_pybind11.cc b/src/python_pybind11/src/_ignition_math_pybind11.cc index d4216157a..cd1adbecc 100644 --- a/src/python_pybind11/src/_ignition_math_pybind11.cc +++ b/src/python_pybind11/src/_ignition_math_pybind11.cc @@ -19,6 +19,7 @@ #include "Helpers.hh" #include "Line2.hh" #include "Rand.hh" +#include "RollingMean.hh" #include "Vector2.hh" #include "Vector3.hh" #include "Vector4.hh" @@ -37,6 +38,8 @@ PYBIND11_MODULE(math, m) ignition::math::python::defineMathRand(m, "Rand"); + ignition::math::python::defineMathRollingMean(m, "RollingMean"); + ignition::math::python::defineMathVector2(m, "Vector2d"); ignition::math::python::defineMathVector2(m, "Vector2i"); ignition::math::python::defineMathVector2(m, "Vector2f"); diff --git a/src/python/RollingMean_TEST.py b/src/python_pybind11/test/RollingMean_TEST.py similarity index 100% rename from src/python/RollingMean_TEST.py rename to src/python_pybind11/test/RollingMean_TEST.py