5 #ifdef PINOCCHIO_WITH_URDFDOM
12 #include <boost/python.hpp>
21 #ifdef PINOCCHIO_WITH_URDFDOM
24 void buildGeomFromUrdf_existing(
26 const std::istream & stream,
28 GeometryModel & geometry_model,
29 bp::object py_pkg_dirs,
30 bp::object py_mesh_loader)
33 if (!py_mesh_loader.is_none())
35 #ifdef PINOCCHIO_WITH_HPP_FCL
38 mesh_loader = bp::extract<::hpp::fcl::MeshLoaderPtr>(py_mesh_loader);
42 PyExc_UserWarning,
"Mesh loader is ignored because Pinocchio is not built with hpp-fcl",
47 std::vector<std::string> pkg_dirs;
48 if (py_pkg_dirs.ptr() == Py_None)
51 else if (PyList_Check(py_pkg_dirs.ptr()))
57 pkg_dirs.push_back(
path(py_pkg_dirs));
64 GeometryModel * buildGeomFromUrdfStream(
66 const std::istream & stream,
68 bp::object py_geom_model,
70 bp::object mesh_loader)
73 if (py_geom_model.is_none())
77 bp::extract<GeometryModel *> geom_model_extract(py_geom_model);
78 if (geom_model_extract.check())
86 "You passed package dir(s) via argument geometry_model and provided package_dirs.", 1);
90 bp::object new_pkg_dirs = py_geom_model;
92 throw std::invalid_argument(
93 "package_dirs and mesh_loader cannot be both provided since you passed the package "
94 "dirs via argument geometry_model.");
95 if (mesh_loader.is_none())
104 catch (std::invalid_argument
const & e)
106 std::cout <<
"Caught: " << e.what() << std::endl;
107 throw std::invalid_argument(
"Argument geometry_model should be a GeometryModel");
115 GeometryModel * buildGeomFromUrdfFile(
117 const bp::object & filename,
121 bp::object mesh_loader)
123 const std::string filename_s =
path(filename);
124 std::ifstream stream(filename_s.c_str());
125 if (!stream.is_open())
127 throw std::invalid_argument(filename_s +
" does not seem to be a valid file.");
132 GeometryModel * buildGeomFromUrdfString(
134 const std::string & xmlString,
138 bp::object mesh_loader)
140 std::istringstream stream(xmlString);
144 #ifdef PINOCCHIO_WITH_HPP_FCL
145 #define MESH_LOADER_DOC \
146 "\tmesh_loader: an hpp-fcl mesh loader (to load only once the related geometries).\n"
147 #else // #ifdef PINOCCHIO_WITH_HPP_FCL
148 #define MESH_LOADER_DOC "\tmesh_loader: unused because the Pinocchio is built without hpp-fcl\n"
149 #endif // #ifdef PINOCCHIO_WITH_HPP_FCL
150 template<std::
size_t owner_arg = 1>
151 struct return_value_policy : bp::return_internal_reference<owner_arg>
154 template<
class ArgumentPackage>
155 static PyObject * postcall(ArgumentPackage
const & args_, PyObject * result)
159 PyObject * patient = bp::detail::get_prev<owner_arg>::execute(args_, result);
160 if (patient != Py_None)
161 return bp::return_internal_reference<owner_arg>::postcall(args_, result);
163 bp::extract<GeometryModel *> geom_model_extract(result);
164 if (geom_model_extract.check())
166 return bp::to_python_indirect<GeometryModel, bp::detail::make_owning_holder>()(
167 geom_model_extract());
172 "pinocchio::python::return_value_policy only works on GeometryModel* data type");
178 void defBuildUrdf(
const char *
name, F
f,
const char * urdf_arg,
const char * urdf_doc)
180 std::ostringstream doc;
181 doc <<
"Parse the URDF file given as input looking for the geometry of the given input model "
183 "and store either the collision geometries (GeometryType.COLLISION) or the visual "
184 "geometries (GeometryType.VISUAL) in a GeometryModel object.\n"
186 "\tmodel: model of the robot\n"
188 << urdf_arg <<
": " << urdf_doc
190 "\tgeom_type: type of geometry to extract from the URDF file (either the VISUAL for "
191 "display or the COLLISION for collision detection).\n"
192 "\tgeometry_model: if provided, this geometry model will be used to store the parsed "
193 "information instead of creating a new one\n"
194 "\tpackage_dirs: either a single path or a vector of paths pointing to folders "
195 "containing the model of the robot\n" MESH_LOADER_DOC
"\n"
197 "\ta new GeometryModel if `geometry_model` is None else `geometry_model` (that has "
202 (bp::arg(
"model"), bp::arg(urdf_arg), bp::arg(
"geom_type"),
203 bp::arg(
"geometry_model") =
static_cast<GeometryModel *
>(NULL),
204 bp::arg(
"package_dirs") = bp::object(), bp::arg(
"mesh_loader") = bp::object()),
205 doc.str().c_str(), return_value_policy<4>());
212 #ifdef PINOCCHIO_WITH_URDFDOM
214 "buildGeomFromUrdf", buildGeomFromUrdfFile,
"urdf_filename",
215 "path to the URDF file containing the model of the robot");
217 "buildGeomFromUrdfString", buildGeomFromUrdfString,
"urdf_string",
218 "a string containing the URDF model of the robot");
219 #endif // #ifdef PINOCCHIO_WITH_URDFDOM