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 Angle Pybind11 interface #311

Merged
merged 3 commits into from
Dec 22, 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 @@ -87,7 +87,6 @@ if (PYTHONLIBS_FOUND)
if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Angle_TEST
AxisAlignedBox_TEST
Box_TEST
Color_TEST
Expand Down
6 changes: 6 additions & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ if (${pybind11_FOUND})
# Split from main extension and converted to pybind11
pybind11_add_module(math SHARED
src/_ignition_math_pybind11.cc
src/Angle.cc
)

target_link_libraries(math PRIVATE
${PROJECT_LIBRARY_TARGET_NAME}
)

if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
Expand Down Expand Up @@ -62,6 +67,7 @@ if (${pybind11_FOUND})
if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Angle_TEST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see these tests running on CI 🕵️‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might happens on Bionic. We at least require pybind11 2.2, but Bionic installs 2.0.1-4 Get:20 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-pybind11 all 2.0.1-4 [4652 B]

Focal is fine https://github.com/ignitionrobotics/ign-math/runs/4559329091?check_suite_focus=true

maybe pip could fix this issue, I will check it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha, thanks! I wouldn't worry about Bionic, targeting Focal is fine!

Vector2_TEST
Vector3_TEST
Vector4_TEST
Expand Down
101 changes: 101 additions & 0 deletions src/python_pybind11/src/Angle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 <pybind11/operators.h>

#include "Angle.hh"

using namespace pybind11::literals;

namespace ignition
{
namespace math
{
namespace python
{
void defineMathAngle(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Angle;
auto toString = [](const Class &si) {
std::stringstream stream;
stream << si;
return stream.str();
};
py::class_<Class>(
m,
typestr.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<const double>())
.def(py::init<const Class>())
.def("radian",
py::overload_cast<>(&Class::Radian, py::const_),
"Set the value from an angle in radians.")
.def("radian",
py::overload_cast<double>(&Class::Radian),
"Set the value from an angle in radians.")
.def("set_radian",
&Class::SetRadian,
"Set the value from an angle in radians.")
.def("degree",
py::overload_cast<>(&Class::Degree, py::const_),
"Set the value from an angle in degree.")
.def("degree",
py::overload_cast<double>(&Class::Degree),
"Set the value from an angle in degree.")
.def("set_degree",
&Class::SetDegree,
"Set the value from an angle in degree.")
.def("normalize",
&Class::Normalize,
"Normalize the vector length")
.def("normalized",
&Class::Normalized,
"Return a normalized vector")
.def(py::self + py::self)
.def(py::self += py::self)
.def(py::self * py::self)
.def(py::self *= py::self)
.def(py::self - py::self)
.def(py::self -= py::self)
.def(py::self / py::self)
.def(py::self /= py::self)
.def(py::self != py::self)
.def(py::self == py::self)
.def(py::self < py::self)
.def(py::self <= py::self)
.def(py::self > py::self)
.def(py::self >= py::self)
.def_readonly_static("ZERO", &Class::Zero, "math::Angle(0)")
.def_readonly_static("PI", &Class::Pi, "math::Angle(IGN_PI)")
.def_readonly_static("HALF_PI", &Class::HalfPi, "math::Angle(IGN_PI * 0.5)")
.def_readonly_static("TWO_PI", &Class::TwoPi, "math::Angle(IGN_PI * 2)")
.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);
}
} // namespace python
} // namespace math
} // namespace ignition
44 changes: 44 additions & 0 deletions src/python_pybind11/src/Angle.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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__ANGLE_HPP_
#define IGNITION_MATH_PYTHON__ANGLE_HPP_

#include <string>

#include <pybind11/pybind11.h>

#include <ignition/math/Angle.hh>

namespace py = pybind11;

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

#endif // IGNITION_MATH_PYTHON__ANGLE_HPP_
4 changes: 2 additions & 2 deletions src/python_pybind11/src/_ignition_math_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <pybind11/pybind11.h>

#include "Angle.hh"
#include "Vector2.hh"
#include "Vector3.hh"
#include "Vector4.hh"
Expand All @@ -24,8 +25,7 @@ PYBIND11_MODULE(math, m)
{
m.doc() = "Ignition Math Python Library.";



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

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