Skip to content

Commit

Permalink
Added RollingMean pybind11 interface (#322)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
ahcorde and chapulina authored Dec 23, 2021
1 parent 9051ce3 commit be461ca
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ if (PYTHONLIBS_FOUND)
Pose3_TEST
python_TEST
Quaternion_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
SignalStats_TEST
Expand Down
2 changes: 2 additions & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (${pybind11_FOUND})
src/Color.cc
src/Helpers.cc
src/Rand.cc
src/RollingMean.cc
)

target_link_libraries(math PRIVATE
Expand Down Expand Up @@ -75,6 +76,7 @@ if (${pybind11_FOUND})
Helpers_TEST
Line2_TEST
Rand_TEST
RollingMean_TEST
Vector2_TEST
Vector3_TEST
Vector4_TEST
Expand Down
48 changes: 48 additions & 0 deletions src/python_pybind11/src/RollingMean.cc
Original file line number Diff line number Diff line change
@@ -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 <string>
#include "RollingMean.hh"
#include <ignition/math/RollingMean.hh>

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_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<size_t>(), 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
42 changes: 42 additions & 0 deletions src/python_pybind11/src/RollingMean.hh
Original file line number Diff line number Diff line change
@@ -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 <pybind11/pybind11.h>
#include <string>

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_
3 changes: 3 additions & 0 deletions src/python_pybind11/src/_ignition_math_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,6 +38,8 @@ PYBIND11_MODULE(math, m)

ignition::math::python::defineMathRand(m, "Rand");

ignition::math::python::defineMathRollingMean(m, "RollingMean");

ignition::math::python::defineMathVector2<double>(m, "Vector2d");
ignition::math::python::defineMathVector2<int>(m, "Vector2i");
ignition::math::python::defineMathVector2<float>(m, "Vector2f");
Expand Down
File renamed without changes.

0 comments on commit be461ca

Please sign in to comment.