From 8391132f9a98a91473c5df320fc1d788a96f7f00 Mon Sep 17 00:00:00 2001 From: Joseph Mirabel <jmirabel@laas.fr> Date: Mon, 19 Aug 2019 16:03:38 +0200 Subject: [PATCH] Hide Assimp configuration + code cleaning. --- CMakeLists.txt | 6 -- include/hpp/fcl/BVH/BVH_model.h | 3 +- include/hpp/fcl/fwd.hh | 2 + include/hpp/fcl/mesh_loader/assimp.h | 92 ++++++++------------------ src/BVH/BVH_model.cpp | 1 - src/mesh_loader/assimp.cpp | 98 ++++++++++++++++++++++++++-- test/test_fcl_collision.cpp | 2 +- 7 files changed, 122 insertions(+), 82 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fc08c34..92d1b512 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,10 +97,8 @@ ADD_REQUIRED_DEPENDENCY("assimp >= 2.0") if(ASSIMP_FOUND) if (NOT ${ASSIMP_VERSION} VERSION_LESS "2.0.1150") add_definitions(-DHPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES) - SET(HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES TRUE) message(STATUS "Assimp version has unified headers") else() - SET(HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES FALSE) message(STATUS "Assimp version does not have unified headers") endif() endif() @@ -161,10 +159,6 @@ endif () pkg_config_append_libs("hpp-fcl") PKG_CONFIG_APPEND_BOOST_LIBS(thread date_time filesystem system) -IF(HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES) - #Â FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES kept for backard compatibility reasons. - PKG_CONFIG_APPEND_CFLAGS("-DFCL_USE_ASSIMP_UNIFIED_HEADER_NAMES -DHPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES") -ENDIF(HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES) IF(HPP_FCL_HAVE_OCTOMAP) #Â FCL_HAVE_OCTOMAP kept for backward compatibility reasons. PKG_CONFIG_APPEND_CFLAGS( diff --git a/include/hpp/fcl/BVH/BVH_model.h b/include/hpp/fcl/BVH/BVH_model.h index 1f29a9bb..c07cd818 100644 --- a/include/hpp/fcl/BVH/BVH_model.h +++ b/include/hpp/fcl/BVH/BVH_model.h @@ -54,8 +54,7 @@ namespace fcl /// @brief A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as a degraded version of mesh) template<typename BV> -class BVHModel : public CollisionGeometry, - private boost::noncopyable +class BVHModel : public CollisionGeometry { public: diff --git a/include/hpp/fcl/fwd.hh b/include/hpp/fcl/fwd.hh index 45fc60ec..7c5a3016 100644 --- a/include/hpp/fcl/fwd.hh +++ b/include/hpp/fcl/fwd.hh @@ -37,6 +37,8 @@ #ifndef HPP_FCL_FWD_HH #define HPP_FCL_FWD_HH +#include <boost/shared_ptr.hpp> + namespace hpp { namespace fcl { class CollisionObject; diff --git a/include/hpp/fcl/mesh_loader/assimp.h b/include/hpp/fcl/mesh_loader/assimp.h index 2b3078d4..3849fc7d 100644 --- a/include/hpp/fcl/mesh_loader/assimp.h +++ b/include/hpp/fcl/mesh_loader/assimp.h @@ -37,30 +37,18 @@ #ifndef HPP_FCL_MESH_LOADER_ASSIMP_H #define HPP_FCL_MESH_LOADER_ASSIMP_H -#ifdef HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES - #include <assimp/DefaultLogger.hpp> - #include <assimp/IOStream.hpp> - #include <assimp/IOSystem.hpp> - #include <assimp/scene.h> - #include <assimp/Importer.hpp> - #include <assimp/postprocess.h> -#else - #include <assimp/DefaultLogger.h> - #include <assimp/assimp.hpp> - #include <assimp/IOStream.h> - #include <assimp/IOSystem.h> - #include <assimp/aiScene.h> - #include <assimp/aiPostProcess.h> -#endif - #include <hpp/fcl/BV/OBBRSS.h> #include <hpp/fcl/BVH/BVH_model.h> +class aiScene; + namespace hpp { namespace fcl { +namespace internal +{ struct TriangleAndVertices { @@ -68,6 +56,15 @@ struct TriangleAndVertices std::vector <fcl::Triangle> triangles_; }; +struct Loader { + Loader (); + ~Loader (); + + void load (const std::string& resource_path); + + aiScene* scene; +}; + /** * @brief Recursive procedure for building a mesh * @@ -77,31 +74,26 @@ struct TriangleAndVertices * @param[in] vertices_offset Current number of vertices in the model * @param tv Triangles and Vertices of the mesh submodels */ -unsigned buildMesh (const fcl::Vec3f & scale, - const aiScene* scene, - const aiNode* node, - unsigned vertices_offset, - TriangleAndVertices & tv); +void buildMesh (const fcl::Vec3f & scale, + const aiScene* scene, + unsigned vertices_offset, + TriangleAndVertices & tv); /** * @brief Convert an assimp scene to a mesh * - * @param[in] name File (ressource) transformed into an assimp scene in loa * @param[in] scale Scale to apply when reading the ressource * @param[in] scene Pointer to the assimp scene * @param[out] mesh The mesh that must be built */ template<class BoundingVolume> -inline void meshFromAssimpScene(const std::string & name, +inline void meshFromAssimpScene( const fcl::Vec3f & scale, const aiScene* scene, const boost::shared_ptr < BVHModel<BoundingVolume> > & mesh) { TriangleAndVertices tv; - if (!scene->HasMeshes()) - throw std::invalid_argument (std::string ("No meshes found in file ")+name); - int res = mesh->beginModel (); if (res != fcl::BVH_OK) @@ -111,13 +103,14 @@ inline void meshFromAssimpScene(const std::string & name, throw std::runtime_error (error.str ()); } - buildMesh (scale, scene, scene->mRootNode, - (unsigned) mesh->num_vertices, tv); + buildMesh (scale, scene, (unsigned) mesh->num_vertices, tv); mesh->addSubModel (tv.vertices_, tv.triangles_); mesh->endModel (); } +} // namespace internal + /** * @brief Read a mesh file and convert it to a polyhedral mesh * @@ -130,46 +123,13 @@ inline void loadPolyhedronFromResource (const std::string & resource_path, const fcl::Vec3f & scale, const boost::shared_ptr < BVHModel<BoundingVolume> > & polyhedron) { - Assimp::Importer importer; - // set list of ignored parameters (parameters used for rendering) - importer.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, - aiComponent_TANGENTS_AND_BITANGENTS| - aiComponent_COLORS | - aiComponent_BONEWEIGHTS | - aiComponent_ANIMATIONS | - aiComponent_LIGHTS | - aiComponent_CAMERAS| - aiComponent_TEXTURES | - aiComponent_TEXCOORDS | - aiComponent_MATERIALS | - aiComponent_NORMALS - ); - - const aiScene* scene = importer.ReadFile(resource_path.c_str(), - aiProcess_SortByPType | - aiProcess_Triangulate | - aiProcess_RemoveComponent | - aiProcess_ImproveCacheLocality | - // TODO: I (Joseph Mirabel) have no idea whether degenerated triangles are - // properly handled. Enabling aiProcess_FindDegenerates would throw an - // exception when that happens. Is it too conservative ? - // aiProcess_FindDegenerates | - aiProcess_JoinIdenticalVertices - ); - - if (!scene) - { - const std::string exception_message (std::string ("Could not load resource ") + resource_path + std::string("\n") + - importer.GetErrorString () + std::string("\n") + - "Hint: the mesh directory may be wrong."); - throw std::invalid_argument(exception_message); - } - - meshFromAssimpScene (resource_path, scale, scene, polyhedron); -} + internal::Loader scene; + scene.load (resource_path); + internal::meshFromAssimpScene (scale, scene.scene, polyhedron); } +} // namespace fcl } // namespace hpp -#endif // FCL_MESH_LOADER_ASSIMP_H +#endif // HPP_FCL_MESH_LOADER_ASSIMP_H diff --git a/src/BVH/BVH_model.cpp b/src/BVH/BVH_model.cpp index 58ab5520..7fabab7b 100644 --- a/src/BVH/BVH_model.cpp +++ b/src/BVH/BVH_model.cpp @@ -47,7 +47,6 @@ namespace fcl template<typename BV> BVHModel<BV>::BVHModel(const BVHModel<BV>& other) : CollisionGeometry(other), - boost::noncopyable(), num_tris(other.num_tris), num_vertices(other.num_vertices), build_state(other.build_state), diff --git a/src/mesh_loader/assimp.cpp b/src/mesh_loader/assimp.cpp index 7e58853a..26d157d8 100644 --- a/src/mesh_loader/assimp.cpp +++ b/src/mesh_loader/assimp.cpp @@ -34,16 +34,94 @@ #include <hpp/fcl/mesh_loader/assimp.h> +#ifdef HPP_FCL_USE_ASSIMP_UNIFIED_HEADER_NAMES + #include <assimp/DefaultLogger.hpp> + #include <assimp/IOStream.hpp> + #include <assimp/IOSystem.hpp> + #include <assimp/Importer.hpp> + #include <assimp/postprocess.h> + #include <assimp/scene.h> +#else + #include <assimp/DefaultLogger.h> + #include <assimp/assimp.hpp> + #include <assimp/IOStream.h> + #include <assimp/IOSystem.h> + #include <assimp/aiPostProcess.h> + #include <assimp/aiScene.h> +#endif + namespace hpp { namespace fcl { -unsigned buildMesh (const fcl::Vec3f & scale, - const aiScene* scene, - const aiNode* node, - unsigned vertices_offset, - TriangleAndVertices & tv) +namespace internal +{ + +Loader::Loader () : scene (NULL) {} + +Loader::~Loader () +{ + if (scene) delete scene; +} + +void Loader::load (const std::string & resource_path) +{ + Assimp::Importer importer; + // set list of ignored parameters (parameters used for rendering) + importer.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, + aiComponent_TANGENTS_AND_BITANGENTS| + aiComponent_COLORS | + aiComponent_BONEWEIGHTS | + aiComponent_ANIMATIONS | + aiComponent_LIGHTS | + aiComponent_CAMERAS| + aiComponent_TEXTURES | + aiComponent_TEXCOORDS | + aiComponent_MATERIALS | + aiComponent_NORMALS + ); + + importer.ReadFile(resource_path.c_str(), + aiProcess_SortByPType | + aiProcess_Triangulate | + aiProcess_RemoveComponent | + aiProcess_ImproveCacheLocality | + // TODO: I (Joseph Mirabel) have no idea whether degenerated triangles are + // properly handled. Enabling aiProcess_FindDegenerates would throw an + // exception when that happens. Is it too conservative ? + // aiProcess_FindDegenerates | + aiProcess_JoinIdenticalVertices + ); + + scene = importer.GetOrphanedScene(); + if (!scene) + { + const std::string exception_message (std::string ("Could not load resource ") + resource_path + std::string("\n") + + importer.GetErrorString () + std::string("\n") + + "Hint: the mesh directory may be wrong."); + throw std::invalid_argument(exception_message); + } + + if (!scene->HasMeshes()) + throw std::invalid_argument (std::string ("No meshes found in file ")+resource_path); +} + +/** + * @brief Recursive procedure for building a mesh + * + * @param[in] scale Scale to apply when reading the ressource + * @param[in] scene Pointer to the assimp scene + * @param[in] node Current node of the scene + * @param[in] vertices_offset Current number of vertices in the model + * @param tv Triangles and Vertices of the mesh submodels + */ +unsigned recurseBuildMesh ( + const fcl::Vec3f & scale, + const aiScene* scene, + const aiNode* node, + unsigned vertices_offset, + TriangleAndVertices & tv) { if (!node) return 0; @@ -100,12 +178,20 @@ unsigned buildMesh (const fcl::Vec3f & scale, for (uint32_t i=0; i < node->mNumChildren; ++i) { - nbVertices += buildMesh(scale, scene, node->mChildren[i], nbVertices, tv); + nbVertices += recurseBuildMesh(scale, scene, node->mChildren[i], nbVertices, tv); } return nbVertices; } +void buildMesh (const fcl::Vec3f & scale, + const aiScene* scene, + unsigned vertices_offset, + TriangleAndVertices & tv) +{ + recurseBuildMesh (scale, scene, scene->mRootNode, vertices_offset, tv); } +} // namespace internal +} // namespace fcl } // namespace hpp diff --git a/test/test_fcl_collision.cpp b/test/test_fcl_collision.cpp index 2330248b..6b9f29c9 100644 --- a/test/test_fcl_collision.cpp +++ b/test/test_fcl_collision.cpp @@ -918,7 +918,7 @@ bool collide_Test(const Transform3f& tf, MeshCollisionTraversalNode<BV> node (request); bool success = initialize <BV> (node, m1, pose1, m2, pose2, local_result); - assert (success); + BOOST_REQUIRE (success); node.enable_statistics = verbose; -- GitLab