diff --git a/CMakeLists.txt b/CMakeLists.txt index efa38b3cadd903b97d29408bab5be4af971acf15..b0e87423cd9cf3f538c4e2f4b85dada65b1e28a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ ENDIF(WIN32) # --- OPTIONS ---------------------------------------- OPTION (BUILD_BENCHMARK "Build the benchmarks" OFF) +OPTION (BUILD_UTILS "Build the utils" OFF) OPTION (INITIALIZE_WITH_NAN "Initialize Eigen entries with NaN" OFF) IF (INITIALIZE_WITH_NAN) @@ -111,6 +112,7 @@ SET(${PROJECT_NAME}_MULTIBODY_JOINT_HEADERS SET(${PROJECT_NAME}_MULTIBODY_PARSER_HEADERS multibody/parser/sample-models.hpp + multibody/parser/utils.hpp ) SET(${PROJECT_NAME}_MULTIBODY_HEADERS @@ -176,6 +178,7 @@ IF(URDFDOM_FOUND) LIST(APPEND HEADERS ${${PROJECT_NAME}_MULTIBODY_PARSER_HEADERS} ) + ADD_DEFINITIONS(-DWITH_URDFDOM) ENDIF(URDFDOM_FOUND) IF(LUA5_1_FOUND) @@ -222,5 +225,8 @@ ADD_SUBDIRECTORY(unittest) IF (BUILD_BENCHMARK) ADD_SUBDIRECTORY (benchmark) ENDIF (BUILD_BENCHMARK) +IF (BUILD_UTILS) + ADD_SUBDIRECTORY (utils) +ENDIF (BUILD_UTILS) SETUP_PROJECT_FINALIZE() diff --git a/src/multibody/parser/utils.hpp b/src/multibody/parser/utils.hpp new file mode 100644 index 0000000000000000000000000000000000000000..20cc54329ab693102b8e17f664bc3650a017800b --- /dev/null +++ b/src/multibody/parser/utils.hpp @@ -0,0 +1,49 @@ +// +// Copyright (c) 2015 CNRS +// +// This file is part of Pinocchio +// Pinocchio is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation, either version +// 3 of the License, or (at your option) any later version. +// +// Pinocchio is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Lesser Public License for more details. You should have +// received a copy of the GNU Lesser General Public License along with +// Pinocchio If not, see +// <http://www.gnu.org/licenses/>. + +#include <iostream> + +namespace se3 +{ + /// + /// \brief Supported model file extensions + /// + enum ModelFileExtensionType{ + UNKNOWN = 0, + URDF, + LUA + }; + + /// + /// \brief Extract the type of the given model file according to its extension + /// + /// \param[in] filemane The complete path to the model file. + /// + /// \return The type of the extension of the model file + /// + ModelFileExtensionType checkModelFileExtension (const std::string & filename) + { + const std::string extension = filename.substr(filename.find_last_of(".") + 1); + + if (extension == "urdf") + return URDF; + else if (extension == "lua") + return LUA; + + return UNKNOWN; + } +} // namespace se3 diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..90ecd750c5cc2e6becc863759936009c6a4a9223 --- /dev/null +++ b/utils/CMakeLists.txt @@ -0,0 +1,38 @@ +# +# Copyright (c) 2015 CNRS +# +# This file is part of Pinocchio +# hpp-core is free software: you can redistribute it +# and/or modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation, either version +# 3 of the License, or (at your option) any later version. +# hpp-core is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Lesser Public License for more details. You should have +# received a copy of the GNU Lesser General Public License along with +# hpp-core If not, see +# <http://www.gnu.org/licenses/>. + +# --- MACROS ------------------------------------------------------------------ +# --- MACROS ------------------------------------------------------------------ +# --- MACROS ------------------------------------------------------------------ +MACRO(ADD_UTIL NAME UTIL_SRC PKGS) + ADD_EXECUTABLE(${NAME} ${UTIL_SRC}) + FOREACH(PKG ${PKGS}) + PKG_CONFIG_USE_DEPENDENCY(${NAME} ${PKG}) + ENDFOREACH(PKG) + TARGET_LINK_LIBRARIES (${NAME} ${Boost_LIBRARIES} ${PROJECT_NAME}) + ADD_DEPENDENCIES(utils ${NAME}) + INSTALL(TARGETS ${NAME} DESTINATION bin) +ENDMACRO(ADD_UTIL) + +# --- RULES ------------------------------------------------------------------- +# --- RULES ------------------------------------------------------------------- +# --- RULES ------------------------------------------------------------------- +ADD_CUSTOM_TARGET(utils) + +IF(URDFDOM_FOUND) + ADD_UTIL(pinocchio_read_model pinocchio_read_model "eigen3;urdfdom") +ENDIF(URDFDOM_FOUND) + diff --git a/utils/pinocchio_read_model.cpp b/utils/pinocchio_read_model.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa9da976737c0fca2672bb81e0badf60e722bc29 --- /dev/null +++ b/utils/pinocchio_read_model.cpp @@ -0,0 +1,88 @@ +// +// Copyright (c) 2015 CNRS +// +// This file is part of Pinocchio +// Pinocchio is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation, either version +// 3 of the License, or (at your option) any later version. +// +// Pinocchio is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Lesser Public License for more details. You should have +// received a copy of the GNU Lesser General Public License along with +// Pinocchio If not, see +// <http://www.gnu.org/licenses/>. +// +// Code adapted from https://bitbucket.org/rbdl/rbdl + +#include <iostream> + +#include "pinocchio/multibody/model.hpp" + +#ifdef WITH_URDFDOM + #include "pinocchio/multibody/parser/urdf.hpp" +#endif + +#ifdef WITH_LUA + #include "pinocchio/multibody/parser/lua.hpp" +#endif + +#include "pinocchio/multibody/parser/utils.hpp" + +using namespace std; + +void usage (const char* application_name) { + cerr << "Usage: " << application_name << " [-v] [-h] <model.extension>" << endl; + cerr << " -v | --verbose default parser verbosity" << endl; + cerr << " -h | --help print this help" << endl; + exit (1); +} + +int main(int argc, char *argv[]) +{ + if (argc < 2 || argc > 4) { + usage(argv[0]); + } + + std::string filename; + + bool verbose = false; + + for (int i = 1; i < argc; i++) { + if (string(argv[i]) == "-v" || string (argv[i]) == "--verbose") + verbose = true; + else if (string(argv[i]) == "-h" || string (argv[i]) == "--help") + usage(argv[0]); + else + filename = argv[i]; + } + + // Check extension of the file + se3::ModelFileExtensionType extension_type = se3::checkModelFileExtension(filename); + se3::Model model; + + switch(extension_type) + { + case se3::URDF: +#ifdef WITH_URDFDOM + model = se3::urdf::buildModel(filename, verbose); +#else + std::cerr << "It seems that the URDFDOM module has not been found during the Cmake process." << std::endl; +#endif + break; + case se3::LUA: +#ifdef WITH_LUA + model = se3::lua::buildModel(filename, false, verbose); +#else + std::cerr << "It seems that the LUA module has not been found during the Cmake process." << std::endl; +#endif + break; + case se3::UNKNOWN: + std::cerr << "Unknown extension of " << filename << std::endl; + return -1; + break; + } + return 0; +}