Commit 2b617621 authored by jcarpent's avatar jcarpent
Browse files

[Parsers] Move Python parsers to bindings/python

Add a new macro SYMLINK_AND_INSTALL_HEADERS to the SYMLINK and install
procedure of headers
parent 9d73db7a
......@@ -21,7 +21,6 @@ INCLUDE(cmake/base.cmake)
INCLUDE(cmake/boost.cmake)
INCLUDE(cmake/eigen.cmake)
INCLUDE(cmake/python.cmake)
INCLUDE(CMakeDependentOption)
SET(PROJECT_NAME pinocchio)
SET(PROJECT_DESCRIPTION "Rigid multi body dynamics algorithms")
......@@ -71,17 +70,12 @@ OPTION (BUILD_UTILS "Build the utils" OFF)
OPTION (INITIALIZE_WITH_NAN "Initialize Eigen entries with NaN" OFF)
OPTION (BUILD_TESTS_WITH_HPP "Build geom tests and benchmarks with hpp to do comparisons" OFF)
OPTION (BUILD_PYTHON_INTERFACE "Build the python binding" ON)
CMAKE_DEPENDENT_OPTION(BUILD_PYTHON_PARSER "Build pinocchio with the Python parser" ON "BUILD_PYTHON_INTERFACE" ON)
IF (INITIALIZE_WITH_NAN)
MESSAGE (STATUS "Initialize with NaN all the Eigen entries.")
ADD_DEFINITIONS(-DEIGEN_INITIALIZE_MATRICES_BY_NAN)
ENDIF (INITIALIZE_WITH_NAN)
IF(BUILD_PYTHON_PARSER)
SET(BUILD_PYTHON_INTERFACE ON)
ENDIF(BUILD_PYTHON_PARSER)
# ----------------------------------------------------
# --- DEPENDANCIES -----------------------------------
# ----------------------------------------------------
......@@ -113,7 +107,6 @@ SET(BOOST_REQUIERED_COMPONENTS filesystem system)
SET(BOOST_BUILD_COMPONENTS unit_test_framework)
SET(BOOST_OPTIONAL_COMPONENTS "")
IF(BUILD_PYTHON_INTERFACE)
ADD_SEPARATE_DEPENDENCY("eigenpy >= 1.3.0")
SET(BOOST_OPTIONAL_COMPONENTS ${BOOST_OPTIONAL_COMPONENTS} python)
......@@ -225,12 +218,6 @@ SET(${PROJECT_NAME}_PARSERS_HEADERS
parsers/srdf.hpp
)
IF(BUILD_PYTHON_PARSER)
LIST(APPEND ${PROJECT_NAME}_PARSERS_HEADERS
parsers/python.hpp
)
ENDIF(BUILD_PYTHON_PARSER)
IF(URDFDOM_FOUND)
LIST(APPEND ${PROJECT_NAME}_PARSERS_HEADERS
parsers/urdf.hpp
......@@ -323,9 +310,6 @@ ADD_SUBDIRECTORY(benchmark)
# --- PACKAGING ----------------------------------------------------------------
PKG_CONFIG_APPEND_LIBS (${PROJECT_NAME})
PKG_CONFIG_APPEND_BOOST_LIBS(${BOOST_REQUIERED_COMPONENTS})
IF(BUILD_PYTHON_PARSER)
PKG_CONFIG_APPEND_BOOST_LIBS(${BOOST_OPTIONAL_COMPONENTS})
ENDIF(BUILD_PYTHON_PARSER)
IF(EIGEN3_FUTURE)
PKG_CONFIG_APPEND_CFLAGS("-DEIGEN3_FUTURE")
......
......@@ -15,6 +15,8 @@
# Pinocchio If not, see
# <http://www.gnu.org/licenses/>.
# --- MACROS --- #
MACRO(ADD_GROUP GROUP_NAME FILENAMES)
FOREACH(filename ${${FILENAMES}})
GET_FILENAME_COMPONENT(filenamePath ${filename} PATH)
......@@ -35,6 +37,20 @@ MACRO(ADD_SOURCE_GROUP FILENAMES)
ADD_GROUP("Source Files" ${FILENAMES})
ENDMACRO(ADD_SOURCE_GROUP FILENAMES)
MACRO(SYMLINK_AND_INSTALL_HEADERS HEADERS SUBPATH)
FOREACH(header ${HEADERS})
GET_FILENAME_COMPONENT(headerName ${header} NAME)
GET_FILENAME_COMPONENT(headerPath ${header} PATH)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E ${LINK}
${CMAKE_CURRENT_SOURCE_DIR}/${header}
${${PROJECT_NAME}_BINARY_DIR}/include/${PROJECT_NAME}/${SUBPATH}/${header})
INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${header}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}/SUBPATH
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE)
ENDFOREACH(header)
ENDMACRO(SYMLINK_AND_INSTALL_HEADERS HEADERS SUBPATH)
# --- LIBRARY --- #
IF(BUILD_PYTHON_INTERFACE)
SET(${PROJECT_NAME}_PYTHON_HEADERS
......@@ -60,6 +76,14 @@ IF(BUILD_PYTHON_INTERFACE)
geometry-data.hpp
)
SET(${PROJECT_NAME}_PARSER_PYTHON_HEADERS
parsers/python.hpp
)
SET(${PROJECT_NAME}_PARSER_PYTHON_SOURCES
parsers/python/model.cpp
)
SET(${PROJECT_NAME}_PYTHON_SOURCES
module.cpp
expose-SE3.cpp
......@@ -100,27 +124,20 @@ IF(BUILD_PYTHON_INTERFACE)
LIST(APPEND HEADERS ${${PROJECT_NAME}_PYTHON_HEADERS})
LIST(REMOVE_DUPLICATES HEADERS)
# Headers of the python binding
MAKE_DIRECTORY("${${PROJECT_NAME}_BINARY_DIR}/include/pinocchio/bindings/python")
MAKE_DIRECTORY("${${PROJECT_NAME}_BINARY_DIR}/include/pinocchio/bindings/python/algorithm")
FOREACH(header ${${PROJECT_NAME}_PYTHON_HEADERS})
GET_FILENAME_COMPONENT(headerName ${header} NAME)
GET_FILENAME_COMPONENT(headerPath ${header} PATH)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E ${LINK}
${${PROJECT_NAME}_SOURCE_DIR}/bindings/python/${header}
${${PROJECT_NAME}_BINARY_DIR}/include/${PROJECT_NAME}/bindings/python/${header})
INSTALL(FILES ${${PROJECT_NAME}_SOURCE_DIR}/bindings/python/${header}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}/bindings/python
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE)
ENDFOREACH(header)
MAKE_DIRECTORY("${${PROJECT_NAME}_BINARY_DIR}/lib/python/${PROJECT_NAME}")
SYMLINK_AND_INSTALL_HEADERS("${${PROJECT_NAME}_PYTHON_HEADERS}" "bindings/python")
# Headers of the python parser
MAKE_DIRECTORY("${${PROJECT_NAME}_BINARY_DIR}/include/pinocchio/parsers/python")
SYMLINK_AND_INSTALL_HEADERS("${${PROJECT_NAME}_PARSER_PYTHON_HEADERS}" "")
# --- COMPILE WRAPPER
SET(PYWRAP ${PROJECT_NAME}_pywrap)
ADD_LIBRARY(${PYWRAP} SHARED ${${PROJECT_NAME}_PYTHON_SOURCES} ${${PROJECT_NAME}_PYTHON_HEADERS})
ADD_HEADER_GROUP(${PROJECT_NAME}_PYTHON_HEADERS)
ADD_SOURCE_GROUP(${PROJECT_NAME}_PYTHON_SOURCES)
PKG_CONFIG_USE_DEPENDENCY(${PYWRAP} eigenpy)
MAKE_DIRECTORY("${${PROJECT_NAME}_BINARY_DIR}/lib/python/${PROJECT_NAME}")
SET(${PYWRAP}_SOURCES ${${PROJECT_NAME}_PYTHON_SOURCES} ${${PROJECT_NAME}_PARSER_PYTHON_SOURCES})
SET(${PYWRAP}_HEADERS ${${PROJECT_NAME}_PYTHON_HEADERS} ${${PROJECT_NAME}_PARSER_PYTHON_HEADERS})
IF(URDFDOM_FOUND)
PKG_CONFIG_USE_DEPENDENCY(${PYWRAP} urdfdom)
......@@ -131,6 +148,10 @@ IF(BUILD_PYTHON_INTERFACE)
IF(LUA5_1_FOUND)
PKG_CONFIG_USE_DEPENDENCY(${PYWRAP} lua5.1)
ENDIF(LUA5_1_FOUND)
ADD_LIBRARY(${PYWRAP} SHARED ${${PYWRAP}_SOURCES} ${${PYWRAP}_HEADERS})
ADD_HEADER_GROUP(${PYWRAP}_PYTHON_HEADERS)
ADD_SOURCE_GROUP(${PYWRAP}_PYTHON_SOURCES)
PKG_CONFIG_USE_DEPENDENCY(${PYWRAP} eigenpy)
TARGET_LINK_LIBRARIES(${PYWRAP} ${PROJECT_NAME})
TARGET_LINK_BOOST_PYTHON(${PYWRAP})
......
......@@ -16,6 +16,7 @@
// <http://www.gnu.org/licenses/>.
#include "pinocchio/parsers/python.hpp"
#include "pinocchio/bindings/python/model.hpp"
#include <iostream>
#include <Python.h>
......@@ -30,18 +31,13 @@ namespace se3
Model buildModel(const std::string & filename, const std::string & model_name, bool verbose) throw (bp::error_already_set)
{
Py_Initialize();
// Get a dict for the global namespace to exec further python code with
bp::object main_module = bp::import("__main__");
// Get a dict for the global namespace to exec further python code with
bp::dict globals = bp::extract<bp::dict>(main_module.attr("__dict__"));
// We need to link to the pinocchio PyWrap. We delegate the dynamic loading to the python interpreter.
bp::exec("import pinocchio", globals);
// Create a new Model, get a shared_ptr to it, and include this pointer
// into the global namespace. See python/handler.hpp for more details.
boost::shared_ptr<Model> model(new Model());
bp::object obj(model);
globals[model_name] = obj;
bp::object cpp_module( (bp::handle<>(PyImport_AddModule("libpinocchio_pywrap"))) );
// That's it, you can exec your python script, starting with a model you
// can update as you want.
......@@ -53,13 +49,25 @@ namespace se3
PyErr_PrintEx(0);
}
Model * model_ptr;
try
{
bp::object obj_model = globals[model_name];
ModelHandler model_handler = bp::extract<ModelHandler>(obj_model);
model_ptr = model_handler.ptr();
}
catch (bp::error_already_set & e)
{
PyErr_PrintEx(0);
}
if (verbose)
{
std::cout << "Your model has been built. It has " << model->nv;
std::cout << "Your model has been built. It has " << model_ptr->nv;
std::cout << " degrees of freedom." << std::endl;
}
return *model;
// Py_Finalize();
return *model_ptr;
}
} // namespace python
} // namespace se3
......@@ -51,12 +51,6 @@ SET(${PROJECT_NAME}_PARSERS_SOURCES
parsers/sample-models.cpp
)
IF(BUILD_PYTHON_PARSER)
LIST(APPEND ${PROJECT_NAME}_PARSERS_SOURCES
parsers/python.cpp
)
ENDIF(BUILD_PYTHON_PARSER)
IF(URDFDOM_FOUND)
LIST(APPEND ${PROJECT_NAME}_PARSERS_SOURCES
parsers/urdf/model.cpp
......@@ -86,10 +80,6 @@ IF(UNIX)
PKG_CONFIG_USE_DEPENDENCY(${PROJECT_NAME} eigen3)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
IF(BUILD_PYTHON_PARSER)
TARGET_LINK_BOOST_PYTHON(${PROJECT_NAME})
ENDIF(BUILD_PYTHON_PARSER)
IF(URDFDOM_FOUND)
PKG_CONFIG_USE_DEPENDENCY(${PROJECT_NAME} urdfdom)
ENDIF(URDFDOM_FOUND)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment