Skip to content

Commit

Permalink
Merge branch 'ign-math6' into ahcorde/pybind11/signalstats
Browse files Browse the repository at this point in the history
  • Loading branch information
ahcorde authored Dec 30, 2021
2 parents c94b63a + f2b462d commit 34f1e8a
Show file tree
Hide file tree
Showing 18 changed files with 1,076 additions and 178 deletions.
7 changes: 0 additions & 7 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@ if (PYTHONLIBS_FOUND)
if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Box_TEST
Cylinder_TEST
Frustum_TEST
Inertial_TEST
OrientedBox_TEST
Plane_TEST
python_TEST
Sphere_TEST
SphericalCoordinates_TEST
Vector3Stats_TEST
)
Expand Down
12 changes: 10 additions & 2 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (${pybind11_FOUND})
src/AxisAlignedBox.cc
src/Color.cc
src/DiffDriveOdometry.cc
src/Frustum.cc
src/GaussMarkovProcess.cc
src/Helpers.cc
src/Kmeans.cc
Expand Down Expand Up @@ -85,32 +86,39 @@ if (${pybind11_FOUND})
set(python_tests
Angle_TEST
AxisAlignedBox_TEST
Box_TEST
Color_TEST
Cylinder_TEST
DiffDriveOdometry_TEST
Filter_TEST
Frustum_TEST
GaussMarkovProcess_TEST
Helpers_TEST
Inertial_TEST
Kmeans_TEST
Line2_TEST
Line3_TEST
MassMatrix3_TEST
Material_TEST
Matrix3_TEST
Matrix4_TEST
MassMatrix3_TEST
MovingWindowFilter_TEST
OrientedBox_TEST
PID_TEST
Plane_TEST
Pose3_TEST
Quaternion_TEST
Rand_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
SignalStats_TEST
Sphere_TEST
Spline_TEST
StopWatch_TEST
Temperature_TEST
Triangle_TEST
Triangle3_TEST
Triangle_TEST
Vector2_TEST
Vector3_TEST
Vector4_TEST
Expand Down
138 changes: 138 additions & 0 deletions src/python_pybind11/src/Box.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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__BOX_HH_
#define IGNITION_MATH_PYTHON__BOX_HH_

#include <string>
#include <vector>

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

#include <ignition/math/Box.hh>

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

namespace ignition
{
namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Box
/**
* \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 defineMathBox(py::module &m, const std::string &typestr)
{

using Class = ignition::math::Box<T>;
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<const T, const T, const T>())
.def(py::init<const T, const T, const T, const ignition::math::Material&>())
.def(py::init<const T, const T, const T, const ignition::math::Material&>())
.def(py::init<const ignition::math::Vector3<T>&>())
.def(py::init<const ignition::math::Vector3<T>&,
const ignition::math::Material&>())
.def(py::self != py::self)
.def(py::self == py::self)
.def("size",
&Class::Size,
"Get the size of the box.")
.def("set_size",
py::overload_cast<const ignition::math::Vector3<T>&>
(&Class::SetSize),
"Set the size of the box.")
.def("set_size",
py::overload_cast<const T, const T, const T>
(&Class::SetSize),
"Set the size of the box.")
.def("material",
&Class::Material,
"Get the material associated with this box.")
.def("set_material",
&Class::SetMaterial,
"Set the material associated with this box.")
.def("volume",
&Class::Volume,
"Get the volume of the box in m^3.")
.def("volume_below",
&Class::VolumeBelow,
"Get the volume of the box below a plane.")
.def("center_of_volume_below",
&Class::CenterOfVolumeBelow,
"Center of volume below the plane. This is useful when "
"calculating where buoyancy should be applied, for example.")
.def("vertices_below",
[](const Class &self, const Plane<T> &_plane)
{
std::vector<ignition::math::Vector3<T>> result;
auto vertices = self.VerticesBelow(_plane);
for (auto & v : vertices)
{
result.push_back(v);
}
return result;
},
"All the vertices which are on or below the plane.")
.def("density_from_mass",
&Class::DensityFromMass,
"Compute the box's density given a mass value.")
.def("set_density_from_mass",
&Class::SetDensityFromMass,
"Set the density of this box based on a mass value.")
.def("mass_matrix",
&Class::MassMatrix,
"Get the mass matrix for this box. This function "
"is only meaningful if the box's size and material "
"have been set.")
.def("intersections",
[](const Class &self, const Plane<T> &_plane)
{
std::vector<ignition::math::Vector3<T>> result;
auto vertices = self.Intersections(_plane);
for (auto & v : vertices)
{
result.push_back(v);
}
return result;
},
"Get intersection between a plane and the box's edges. "
"Edges contained on the plane are ignored.")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a);
}

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

#endif // IGNITION_MATH_PYTHON__BOX_HH_
118 changes: 118 additions & 0 deletions src/python_pybind11/src/Cylinder.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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__CYLINDER_HH_
#define IGNITION_MATH_PYTHON__CYLINDER_HH_

#include <string>

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

#include <ignition/math/Cylinder.hh>

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

namespace ignition
{
namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Cylinder
/**
* \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 defineMathCylinder(py::module &m, const std::string &typestr)
{

using Class = ignition::math::Cylinder<T>;
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<const T, const T,
const ignition::math::Quaternion<T>&>(),
py::arg("_length") = 0,
py::arg("_radius") = 0,
py::arg("_rotOffset") = ignition::math::Quaternion<T>::Identity)
.def(py::init<const T, const T,
const ignition::math::Material&,
const ignition::math::Quaternion<T>&>(),
py::arg("_length") = 0,
py::arg("_radius") = 0,
py::arg("_material") = ignition::math::Material(),
py::arg("_rotOffset") = ignition::math::Quaternion<T>::Identity)
.def(py::self == py::self)
.def("radius",
&Class::Radius,
"Get the radius in meters.")
.def("set_radius",
&Class::SetRadius,
"Set the radius in meters.")
.def("length",
&Class::Length,
"Get the length in meters.")
.def("set_length",
&Class::SetLength,
"Set the length in meters.")
.def("rotational_offset",
&Class::RotationalOffset,
"Get the rotation offset.")
.def("set_rotational_offset",
&Class::SetRotationalOffset,
"Set the rotation offset.")
.def("mat",
&Class::Mat,
py::return_value_policy::reference,
"Get the material associated with this box.")
.def("set_mat",
&Class::SetMat,
"Set the material associated with this box.")
.def("volume",
&Class::Volume,
"Get the volume of the box in m^3.")
.def("density_from_mass",
&Class::DensityFromMass,
"Compute the box's density given a mass value.")
.def("set_density_from_mass",
&Class::SetDensityFromMass,
"Set the density of this box based on a mass value.")
.def("mass_matrix",
&Class::MassMatrix,
"Get the mass matrix for this box. This function "
"is only meaningful if the box's size and material "
"have been set.")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a);
}

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

#endif // IGNITION_MATH_PYTHON__CYLINDER_HH_
Loading

0 comments on commit 34f1e8a

Please sign in to comment.