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 Filter pybind11 interface #336

Merged
merged 2 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -91,7 +91,6 @@ if (PYTHONLIBS_FOUND)
Box_TEST
Cylinder_TEST
DiffDriveOdometry_TEST
Filter_TEST
Frustum_TEST
GaussMarkovProcess_TEST
Inertial_TEST
Expand Down
1 change: 1 addition & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ if (${pybind11_FOUND})
set(python_tests
Angle_TEST
Color_TEST
Filter_TEST
Helpers_TEST
Line2_TEST
Line3_TEST
Expand Down
315 changes: 315 additions & 0 deletions src/python_pybind11/src/Filter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
/*
* 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__FILTER_HH_
#define IGNITION_MATH_PYTHON__FILTER_HH_

#include <string>

#include <pybind11/pybind11.h>

#include <ignition/math/Filter.hh>
#include <ignition/math/Quaternion.hh>
#include <ignition/math/Vector3.hh>

namespace py = pybind11;

namespace ignition
{
namespace math
{
namespace python
{
template<typename T>
class FilterTrampoline : public Filter<T> {
public:
// Inherit the constructors
FilterTrampoline() : Filter<T>() {}

// Trampoline (need one for each virtual function)
void Set(const T &_val) override
{
PYBIND11_OVERLOAD_PURE(
void, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Set, // Name of function in C++
_val // Argument(s) (...)
);
}
// Trampoline (need one for each virtual function)
void Fc(double _fc, double _fs) override
{
PYBIND11_OVERLOAD_PURE(
void, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Fc, // Name of function in C++ (must match Python name)
_fc, _fs // Argument(s) (...)
);
}
// Trampoline (need one for each virtual function)
const T &Value() const override
{
PYBIND11_OVERLOAD_PURE(
const T&, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Value // Name of function in C++ (must match Python name)
);
}
};

/// Define a pybind11 wrapper for an ignition::math::Filter
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
template<typename T>
void defineMathFilter(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Filter<T>;
std::string pyclass_name = typestr;
py::class_<Class, FilterTrampoline<T>>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("fc",
&Class::Fc,
"Set the cutoff frequency and sample rate.")
.def("value",
&Class::Value,
"Get the output of the filter.");
}

template<typename T>
class OnePoleTrampoline : public OnePole<T> {
public:
OnePoleTrampoline() : OnePole<T>() {}
OnePoleTrampoline(double _fc, double _fs) : OnePole<T>(_fc, _fs) {}

// Trampoline (need one for each virtual function)
void Fc(double _fc, double _fs) override
{
PYBIND11_OVERLOAD_PURE(
void, // Return type (ret_type)
OnePole<T>, // Parent class (cname)
Fc, // Name of function in C++ (must match Python name)
_fc, // Argument(s) (...)
_fs
);
}
};

/// Define a pybind11 wrapper for an ignition::math::OnePole
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
template<typename T>
void defineMathOnePole(py::module &m, const std::string &typestr)
{
using Class = ignition::math::OnePole<T>;
std::string pyclass_name = typestr;
py::class_<Class, OnePoleTrampoline<T>>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double, double>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("value",
&Class::Value,
"Get the output of the filter.")
.def("fc",
&Class::Fc,
"Set the cutoff frequency and sample rate.")
.def("process",
&Class::Process,
"Update the filter's output.");
}

/// Define a pybind11 wrapper for an ignition::math::OnePoleQuaterion
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
void defineMathOnePoleQuaternion(py::module &m, const std::string &typestr)
{
using Class = ignition::math::OnePoleQuaternion;
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double, double>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("value",
&Class::Value,
"Get the output of the filter.")
.def("fc",
&Class::Fc,
"Set the cutoff frequency and sample rate.")
.def("process",
&Class::Process,
"Update the filter's output.");
}

/// Define a pybind11 wrapper for an ignition::math::OnePoleVector3
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
void defineMathOnePoleVector3(py::module &m, const std::string &typestr)
{
using Class = ignition::math::OnePoleVector3;
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double, double>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("value",
&Class::
Value,
"Get the output of the filter.")
.def("fc",
&Class::Fc,
"Set the cutoff frequency and sample rate.")
.def("process",
&Class::Process,
"Update the filter's output.");
}

template<typename T>
class BiQuadTrampoline : public BiQuad<T>
{
public:
BiQuadTrampoline() : BiQuad<T>() {}
BiQuadTrampoline(double _fc, double _fs) : BiQuad<T>(_fc, _fs) {}

// Trampoline (need one for each virtual function)
void Fc(double _fc, double _fs) override
{
PYBIND11_OVERLOAD_PURE(
void, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Fc, // Name of function in C++ (must match Python name)
_fc, _fs // Argument(s) (...)
);
}
// Trampoline (need one for each virtual function)
void Set(const T &_val) override
{
PYBIND11_OVERLOAD_PURE(
void, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Set, // Name of function in C++ (must match Python name)
_val // Argument(s) (...)
);
}
// Trampoline (need one for each virtual function)
const T& Process(const T &_x) override
{
PYBIND11_OVERLOAD_PURE(
const T&, // Return type (ret_type)
Filter<T>, // Parent class (cname)
Process, // Name of function in C++ (must match Python name)
_x // Argument(s) (...)
);
}
};

/// Define a pybind11 wrapper for an ignition::math::BiQuad
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
template<typename T>
void defineMathBiQuad(py::module &m, const std::string &typestr)
{
using Class = ignition::math::BiQuad<T>;
std::string pyclass_name = typestr;
py::class_<Class, BiQuadTrampoline<T>>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double, double>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("fc",
py::overload_cast<double, double>(&Class::Fc),
"Set the cutoff frequency and sample rate.")
.def("value",
&Class::Value,
"Get the output of the filter.")
.def("fc",
py::overload_cast<double, double, double>(&Class::Fc),
"Set the cutoff frequency, sample rate and Q coefficient.")
.def("process",
&Class::Process,
"Update the filter's output.");
}

/// Define a pybind11 wrapper for an ignition::math::BiQuadVector3
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
void defineMathBiQuadVector3(py::module &m, const std::string &typestr)
{
using Class = ignition::math::BiQuadVector3;
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double, double>())
.def("set",
&Class::Set,
"Set the output of the filter.")
.def("fc",
py::overload_cast<double, double>(&Class::Fc),
"Set the cutoff frequency and sample rate.")
.def("value",
&Class::Value,
"Get the output of the filter.")
.def("fc",
py::overload_cast<double, double, double>(&Class::Fc),
"Set the cutoff frequency, sample rate and Q coefficient.")
.def("process",
&Class::Process,
"Update the filter's output.");
}
} // namespace python
} // namespace math
} // namespace ignition

#endif // IGNITION_MATH_PYTHON__FILTER_HH_
21 changes: 21 additions & 0 deletions src/python_pybind11/src/_ignition_math_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "Angle.hh"
#include "Color.hh"
#include "Filter.hh"
#include "Helpers.hh"
#include "Line2.hh"
#include "Line3.hh"
Expand Down Expand Up @@ -76,4 +77,24 @@ PYBIND11_MODULE(math, m)
ignition::math::python::defineMathQuaternion<int>(m, "Quaternioni");
ignition::math::python::defineMathQuaternion<double>(m, "Quaterniond");
ignition::math::python::defineMathQuaternion<float>(m, "Quaternionf");

ignition::math::python::defineMathFilter<int>(m, "Filteri");
ignition::math::python::defineMathFilter<float>(m, "Filterf");
ignition::math::python::defineMathFilter<double>(m, "Filterd");

ignition::math::python::defineMathBiQuad<int>(m, "BiQuadi");
ignition::math::python::defineMathBiQuad<float>(m, "BiQuadf");
ignition::math::python::defineMathBiQuad<double>(m, "BiQuadd");

ignition::math::python::defineMathBiQuadVector3(
m, "BiQuadVector3");

ignition::math::python::defineMathOnePole<int>(m, "OnePolei");
ignition::math::python::defineMathOnePole<float>(m, "OnePolef");
ignition::math::python::defineMathOnePole<double>(m, "OnePoled");

ignition::math::python::defineMathOnePoleQuaternion(
m, "OnePoleQuaternion");
ignition::math::python::defineMathOnePoleVector3(
m, "OnePoleVector3");
}
File renamed without changes.