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 Polynomial3 Python interface #451

Merged
merged 11 commits into from
Jul 12, 2022
2 changes: 2 additions & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pybind11_add_module(math SHARED
src/Matrix6.cc
src/MovingWindowFilter.cc
src/PID.cc
src/Polynomial3.cc
src/Pose3.cc
src/Quaternion.cc
src/Rand.cc
Expand Down Expand Up @@ -124,6 +125,7 @@ if (BUILD_TESTING)
OrientedBox_TEST
PID_TEST
Plane_TEST
Polynomial3_TEST
Pose3_TEST
Quaternion_TEST
Rand_TEST
Expand Down
34 changes: 34 additions & 0 deletions src/python_pybind11/src/Polynomial3.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 "Polynomial3.hh"

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

} // namespace python
} // namespace math
} // namespace ignition
111 changes: 111 additions & 0 deletions src/python_pybind11/src/Polynomial3.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.
*
*/

#ifndef IGNITION_MATH_PYTHON__POLYNOMIAL3_HH_
#define IGNITION_MATH_PYTHON__POLYNOMIAL3_HH_

#include <string>
#include <sstream>

#include <pybind11/pybind11.h>

#include <ignition/math/Polynomial3.hh>
#include <ignition/math/Vector4.hh>

namespace py = pybind11;
using namespace pybind11::literals;

namespace ignition
{
namespace math
{
namespace python
{
/// Help define a pybind11 wrapper for an ignition::math::Polynomial3
/**
* \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 helpDefineMathPolynomial3(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Polynomial3<T>;
auto toString = [](const Class &si) {
std::stringstream stream;
stream << si;
return stream.str();
};
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<Vector4<T>>())
.def("constant",
&Class::Constant,
"Make a constant polynomial")
.def("coeffs",
&Class::Coeffs,
"Get the polynomial coefficients")
.def("evaluate",
&Class::Evaluate,
"Evaluate the polynomial at `_x`. For non-finite `_x`, this function "
"computes p(z) as z tends to `_x`.")
.def("minimum",
[](const Class &self, const Interval<T> &_interval, T &_xMin)
{
T result = self.Minimum(_interval, _xMin);
return std::make_tuple(result, _xMin);
},
"Compute polynomial minimum in an `_interval`")
.def("minimum",
py::overload_cast<const Interval<T> &>(&Class::Minimum, py::const_),
"Compute polynomial minimum in an `_interval`")
.def("minimum",
[](const Class &self, T &_xMin)
{
T result = self.Minimum(_xMin);
return std::make_tuple(result, _xMin);
},
"Compute polynomial minimum in an `_interval`")
.def("minimum",
py::overload_cast<>(&Class::Minimum, py::const_),
"Compute polynomial minimum in an `_interval`")
.def("__call__", &Class::operator())
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a)
.def("__str__", toString)
.def("__repr__", toString);
}

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

} // namespace python
} // namespace math
} // namespace ignition

#endif // IGNITION_MATH_PYTHON__POLYNOMIAL3_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 @@ -40,6 +40,7 @@
#include "OrientedBox.hh"
#include "PID.hh"
#include "Plane.hh"
#include "Polynomial3.hh"
#include "Pose3.hh"
#include "Quaternion.hh"
#include "Rand.hh"
Expand Down Expand Up @@ -135,6 +136,8 @@ PYBIND11_MODULE(math, m)

ignition::math::python::defineMathInterval(m, "Interval");

ignition::math::python::defineMathPolynomial3(m, "Polynomial3");

ignition::math::python::defineMathLine2(m, "Line2");

ignition::math::python::defineMathLine3(m, "Line3");
Expand Down
Loading