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

More pybind11 compilation memory savings #373

Merged
merged 5 commits into from
Feb 3, 2022
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
6 changes: 6 additions & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ pybind11_add_module(math SHARED
src/AxisAlignedBox.cc
src/Color.cc
src/DiffDriveOdometry.cc
src/Filter.cc
src/Frustum.cc
src/GaussMarkovProcess.cc
src/Helpers.cc
src/Kmeans.cc
src/Line2.cc
src/Line3.cc
src/Material.cc
src/MovingWindowFilter.cc
src/PID.cc
src/Rand.cc
src/RollingMean.cc
Expand All @@ -29,6 +33,8 @@ pybind11_add_module(math SHARED
src/StopWatch.cc
src/Temperature.cc
src/Vector2.cc
src/Vector3.cc
src/Vector4.cc
src/Vector3Stats.cc
)

Expand Down
124 changes: 124 additions & 0 deletions src/python_pybind11/src/Filter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (C) 2022 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 "Filter.hh"

namespace ignition
{
namespace math
{
namespace python
{
void defineMathFilter(py::module &m, const std::string &typestr)
{
helpDefineMathFilter<int>(m, typestr + "i");
helpDefineMathFilter<float>(m, typestr + "f");
helpDefineMathFilter<double>(m, typestr + "d");
}

void defineMathOnePole(py::module &m, const std::string &typestr)
{
helpDefineMathOnePole<int>(m, typestr + "i");
helpDefineMathOnePole<float>(m, typestr + "f");
helpDefineMathOnePole<double>(m, typestr + "d");
}

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.");
}

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.");
}

void defineMathBiQuad(py::module &m, const std::string &typestr)
{
helpDefineMathBiQuad<int>(m, typestr + "i");
helpDefineMathBiQuad<float>(m, typestr + "f");
helpDefineMathBiQuad<double>(m, typestr + "d");
}

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
110 changes: 31 additions & 79 deletions src/python_pybind11/src/Filter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public:
}
};

/// Define a pybind11 wrapper for an ignition::math::Filter
/// Help 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)
void helpDefineMathFilter(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Filter<T>;
std::string pyclass_name = typestr;
Expand All @@ -97,6 +97,13 @@ void defineMathFilter(py::module &m, const std::string &typestr)
"Get the output of the filter.");
}

/// 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
*/
void defineMathFilter(py::module &m, const std::string &typestr);

template<typename T>
class OnePoleTrampoline : public OnePole<T> {
public:
Expand All @@ -116,13 +123,13 @@ public:
}
};

/// Define a pybind11 wrapper for an ignition::math::OnePole
/// Help 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)
void helpDefineMathOnePole(py::module &m, const std::string &typestr)
{
using Class = ignition::math::OnePole<T>;
std::string pyclass_name = typestr;
Expand All @@ -146,64 +153,26 @@ void defineMathOnePole(py::module &m, const std::string &typestr)
"Update the filter's output.");
}

/// 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
*/
void defineMathOnePole(py::module &m, const std::string &typestr);

/// 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.");
}
void defineMathOnePoleQuaternion(py::module &m, const std::string &typestr);

/// 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.");
}
void defineMathOnePoleVector3(py::module &m, const std::string &typestr);

template<typename T>
class BiQuadTrampoline : public BiQuad<T>
Expand Down Expand Up @@ -244,13 +213,13 @@ public:
}
};

/// Define a pybind11 wrapper for an ignition::math::BiQuad
/// Help 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)
void helpDefineMathBiQuad(py::module &m, const std::string &typestr)
{
using Class = ignition::math::BiQuad<T>;
std::string pyclass_name = typestr;
Expand All @@ -277,37 +246,20 @@ void defineMathBiQuad(py::module &m, const std::string &typestr)
"Update the filter's output.");
}

/// 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
*/
void defineMathBiQuad(py::module &m, const std::string &typestr);

/// 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.");
}
void defineMathBiQuadVector3(py::module &m, const std::string &typestr);

} // namespace python
} // namespace math
} // namespace ignition
Expand Down
35 changes: 35 additions & 0 deletions src/python_pybind11/src/Line2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2022 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 "Line2.hh"

namespace ignition
{
namespace math
{
namespace python
{
void defineMathLine2(py::module &m, const std::string &typestr)
{
helpDefineMathLine2<int>(m, typestr + "i");
helpDefineMathLine2<float>(m, typestr + "f");
helpDefineMathLine2<double>(m, typestr + "d");
}

} // namespace python
} // namespace math
} // namespace ignition
11 changes: 9 additions & 2 deletions src/python_pybind11/src/Line2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Line2
/// Help define a pybind11 wrapper for an ignition::math::Line2
/**
* \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 defineMathLine2(py::module &m, const std::string &typestr)
void helpDefineMathLine2(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Line2<T>;
auto toString = [](const Class &si) {
Expand Down Expand Up @@ -128,6 +128,13 @@ void defineMathLine2(py::module &m, const std::string &typestr)
.def("__repr__", toString);
}

/// Define a pybind11 wrapper for an ignition::math::Line2
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
void defineMathLine2(py::module &m, const std::string &typestr);

} // namespace python
} // namespace math
} // namespace ignition
Expand Down
Loading