-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Material pybind11 interface (#340)
Signed-off-by: Alejandro Hernández <[email protected]> Co-authored-by: Louise Poubel <[email protected]>
- Loading branch information
Showing
6 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
*/ | ||
#include <limits> | ||
#include <map> | ||
#include <string> | ||
|
||
#include "Material.hh" | ||
#include <ignition/math/Material.hh> | ||
#include <ignition/math/MaterialType.hh> | ||
|
||
#include <pybind11/operators.h> | ||
#include <pybind11/stl_bind.h> | ||
|
||
namespace ignition | ||
{ | ||
namespace math | ||
{ | ||
namespace python | ||
{ | ||
void defineMathMaterial(py::module &m, const std::string &typestr) | ||
{ | ||
|
||
py::bind_map<std::map< | ||
ignition::math::MaterialType, ignition::math::Material>> | ||
(m, "MaterialMap"); | ||
|
||
using Class = ignition::math::Material; | ||
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 ignition::math::MaterialType>()) | ||
.def(py::init<const std::string&>()) | ||
.def(py::init<const double>()) | ||
.def(py::init<const Class&>()) | ||
.def(py::self != py::self) | ||
.def(py::self == py::self) | ||
.def("predefined", | ||
&Class::Predefined, | ||
"Get all the built-in materials.") | ||
.def("set_to_nearest_density", | ||
&Class::SetToNearestDensity, | ||
py::arg("_value") = 0, | ||
py::arg("_epsilon") = std::numeric_limits<double>::max(), | ||
"Set this Material to the built-in Material with " | ||
"the nearest density value within _epsilon. If a built-in material " | ||
"could not be found, then this Material is not changed.") | ||
.def("type", | ||
&Class::Type, | ||
"Set the material's type. This will only set the type value. " | ||
"Other properties, such as density, will not be changed.") | ||
.def("set_type", | ||
&Class::SetType, | ||
"Set the material's type. This will only set the type value. " | ||
"Other properties, such as density, will not be changed.") | ||
.def("name", | ||
&Class::Name, | ||
"Get the name of the material. This will match the enum type " | ||
"names used in ::MaterialType, but in lowercase, if a built-in " | ||
"material is used.") | ||
.def("set_name", | ||
&Class::SetName, | ||
"Set the name of the material.") | ||
.def("set_density", | ||
&Class::SetDensity, | ||
"Set the name of the material.") | ||
.def("density", &Class::Density, | ||
"Set the density value of the material in kg/m^3."); | ||
|
||
py::enum_<ignition::math::MaterialType>(m, "MaterialType") | ||
.value("STYROFOAM", ignition::math::MaterialType::STYROFOAM) | ||
.value("PINE", ignition::math::MaterialType::PINE) | ||
.value("WOOD", ignition::math::MaterialType::WOOD) | ||
.value("OAK", ignition::math::MaterialType::OAK) | ||
.value("PLASTIC", ignition::math::MaterialType::PLASTIC) | ||
.value("CONCRETE", ignition::math::MaterialType::CONCRETE) | ||
.value("ALUMINUM", ignition::math::MaterialType::ALUMINUM) | ||
.value("STEEL_ALLOY", ignition::math::MaterialType::STEEL_ALLOY) | ||
.value("STEEL_STAINLESS", ignition::math::MaterialType::STEEL_STAINLESS) | ||
.value("IRON", ignition::math::MaterialType::IRON) | ||
.value("BRASS", ignition::math::MaterialType::BRASS) | ||
.value("COPPER", ignition::math::MaterialType::COPPER) | ||
.value("TUNGSTEN", ignition::math::MaterialType::TUNGSTEN) | ||
.value("UNKNOWN_MATERIAL", | ||
ignition::math::MaterialType::UNKNOWN_MATERIAL) | ||
.export_values(); | ||
} | ||
} // namespace python | ||
} // namespace math | ||
} // namespace ignition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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__MATERIAL_HH_ | ||
#define IGNITION_MATH_PYTHON__MATERIAL_HH_ | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <string> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace ignition | ||
{ | ||
namespace math | ||
{ | ||
namespace python | ||
{ | ||
/// Define a pybind11 wrapper for an ignition::math::Material | ||
/** | ||
* \param[in] module a pybind11 module to add the definition to | ||
* \param[in] typestr name of the type used by Python | ||
*/ | ||
void defineMathMaterial(py::module &m, const std::string &typestr); | ||
} // namespace python | ||
} // namespace math | ||
} // namespace ignition | ||
|
||
#endif // IGNITION_MATH_PYTHON__MATERIAL_HH_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters