Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RollingMean pybind11 interface #322

Merged
merged 5 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ if (PYTHONLIBS_FOUND)
python_TEST
Quaternion_TEST
Rand_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
SignalStats_TEST
Expand Down
4 changes: 3 additions & 1 deletion src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ if (${pybind11_FOUND})
pybind11_add_module(math SHARED
src/_ignition_math_pybind11.cc
src/Angle.cc
)
src/RollingMean.cc
)

target_link_libraries(math PRIVATE
${PROJECT_LIBRARY_TARGET_NAME}
Expand Down Expand Up @@ -68,6 +69,7 @@ if (${pybind11_FOUND})
# Add the Python tests
set(python_tests
Angle_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 @@ -15,6 +15,7 @@
#include <pybind11/pybind11.h>

#include "Angle.hh"
#include "RollingMean.hh"
#include "Vector2.hh"
#include "Vector3.hh"
#include "Vector4.hh"
Expand All @@ -27,6 +28,8 @@ PYBIND11_MODULE(math, m)

ignition::math::python::defineMathAngle(m, "Angle");

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