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 Triangle3 pybind11 interface #335

Merged
merged 4 commits into from
Dec 28, 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 @@ -107,7 +107,6 @@ if (PYTHONLIBS_FOUND)
Sphere_TEST
SphericalCoordinates_TEST
Temperature_TEST
Triangle3_TEST
Vector3Stats_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 @@ -90,6 +90,7 @@ if (${pybind11_FOUND})
Spline_TEST
StopWatch_TEST
Triangle_TEST
Triangle3_TEST
Vector2_TEST
Vector3_TEST
Vector4_TEST
Expand Down
106 changes: 106 additions & 0 deletions src/python_pybind11/src/Triangle3.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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__TRIANGLE3_HH_
#define IGNITION_MATH_PYTHON__TRIANGLE3_HH_

#include <string>

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>

#include <ignition/math/Triangle3.hh>

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

namespace ignition
{
namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Triangle3
/**
* \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 defineMathTriangle3(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Triangle3<T>;
py::class_<Class>(m,
typestr.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<const math::Vector3<T> &,
const math::Vector3<T> &,
const math::Vector3<T> &>())
.def(py::init<const Class>())
.def("set",
py::overload_cast<const unsigned int, const math::Vector3<T> &>
(&Class::Set),
"Set one vertex of the triangle.")
.def("set",
py::overload_cast<const math::Vector3<T> &,
const math::Vector3<T> &,
const math::Vector3<T> &>
(&Class::Set),
"Set all vertices of the triangle.")
.def("valid",
&Class::Valid,
"Get whether this triangle is valid, based on triangle "
"inequality: the sum of the lengths of any two sides must be greater "
"than the length of the remaining side.")
.def("side",
&Class::Side,
"Get a line segment for one side of the triangle.")
.def("contains",
py::overload_cast<const Line3<T>&>(&Class::Contains, py::const_),
"Check if this triangle completely contains the given line "
"segment.")
.def("contains",
py::overload_cast<const Vector3<T>&>(&Class::Contains, py::const_),
"Get whether this triangle contains the given point")
.def("intersects",
&Class::Intersects,
"Get whether the given line intersects this triangle.")
.def("perimeter",
&Class::Perimeter,
"Get the length of the triangle's perimeter.")
.def("area", &Class::Area, "Get the area of this triangle.")
.def("normal", &Class::Normal, "Get the triangle's normal vector.")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a)
.def("__getitem__",
(&Class::operator[]))
.def("__getitem__",
py::overload_cast<const std::size_t>(&Class::operator[], py::const_))
.def("__setitem__",
[](Class* vec, unsigned index, T val) { (*vec)[index] = val; });
}

} // namespace python
} // namespace gazebo
} // namespace ignition

#endif // IGNITION_MATH_PYTHON__TRIANGLE3_HH_
5 changes: 5 additions & 0 deletions src/python_pybind11/src/_ignition_math_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Spline.hh"
#include "StopWatch.hh"
#include "Triangle.hh"
#include "Triangle3.hh"
#include "Vector2.hh"
#include "Vector3.hh"
#include "Vector4.hh"
Expand Down Expand Up @@ -91,6 +92,10 @@ PYBIND11_MODULE(math, m)
ignition::math::python::defineMathTriangle<double>(m, "Triangled");
ignition::math::python::defineMathTriangle<float>(m, "Trianglef");

ignition::math::python::defineMathTriangle3<int>(m, "Triangle3i");
ignition::math::python::defineMathTriangle3<double>(m, "Triangle3d");
ignition::math::python::defineMathTriangle3<float>(m, "Triangle3f");

ignition::math::python::defineMathQuaternion<int>(m, "Quaternioni");
ignition::math::python::defineMathQuaternion<double>(m, "Quaterniond");
ignition::math::python::defineMathQuaternion<float>(m, "Quaternionf");
Expand Down