assimp.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019-2021 CNRS - LAAS, INRIA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Open Source Robotics Foundation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600))
36 #define nullptr NULL
37 #endif
39 
40 // Assimp >= 5.0 is forcing the use of C++11 keywords. A fix has been submitted
41 // https://github.com/assimp/assimp/pull/2758. The next lines fixes the bug for
42 // current version of Coal.
43 #include <assimp/defs.h>
44 #if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)) && \
45  defined(AI_NO_EXCEPT)
46 #undef AI_NO_EXCEPT
47 #define AI_NO_EXCEPT
48 #endif
49 
50 #include <assimp/DefaultLogger.hpp>
51 #include <assimp/IOStream.hpp>
52 #include <assimp/IOSystem.hpp>
53 #include <assimp/Importer.hpp>
54 #include <assimp/postprocess.h>
55 #include <assimp/scene.h>
56 
57 namespace coal {
58 
59 namespace internal {
60 
61 Loader::Loader() : importer(new Assimp::Importer()) {
62  // set list of ignored parameters (parameters used for rendering)
63  importer->SetPropertyInteger(
64  AI_CONFIG_PP_RVC_FLAGS,
65  aiComponent_TANGENTS_AND_BITANGENTS | aiComponent_COLORS |
66  aiComponent_BONEWEIGHTS | aiComponent_ANIMATIONS |
67  aiComponent_LIGHTS | aiComponent_CAMERAS | aiComponent_TEXTURES |
68  aiComponent_TEXCOORDS | aiComponent_MATERIALS | aiComponent_NORMALS);
69 
70  // remove LINES and POINTS
71  importer->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,
72  aiPrimitiveType_LINE | aiPrimitiveType_POINT);
73 }
74 
76  if (importer) delete importer;
77 }
78 
79 void Loader::load(const std::string& resource_path) {
80  scene = importer->ReadFile(
81  resource_path.c_str(),
82  aiProcess_SortByPType | aiProcess_Triangulate |
83  aiProcess_RemoveComponent | aiProcess_ImproveCacheLocality |
84  aiProcess_FindDegenerates | aiProcess_JoinIdenticalVertices);
85 
86  if (!scene) {
87  const std::string exception_message(
88  std::string("Could not load resource ") + resource_path +
89  std::string("\n") + importer->GetErrorString() + std::string("\n") +
90  "Hint: the mesh directory may be wrong.");
91  COAL_THROW_PRETTY(exception_message.c_str(), std::invalid_argument);
92  }
93 
94  if (!scene->HasMeshes())
96  (std::string("No meshes found in file ") + resource_path).c_str(),
97  std::invalid_argument);
98 }
99 
109 unsigned recurseBuildMesh(const coal::Vec3s& scale, const aiScene* scene,
110  const aiNode* node, unsigned vertices_offset,
111  TriangleAndVertices& tv) {
112  if (!node) return 0;
113 
114  aiMatrix4x4 transform = node->mTransformation;
115  aiNode* pnode = node->mParent;
116  while (pnode) {
117  // Don't convert to y-up orientation, which is what the root node in
118  // Assimp does
119  if (pnode->mParent != NULL) {
120  transform = pnode->mTransformation * transform;
121  }
122  pnode = pnode->mParent;
123  }
124 
125  unsigned nbVertices = 0;
126  for (uint32_t i = 0; i < node->mNumMeshes; i++) {
127  aiMesh* input_mesh = scene->mMeshes[node->mMeshes[i]];
128 
129  // Add the vertices
130  for (uint32_t j = 0; j < input_mesh->mNumVertices; j++) {
131  aiVector3D p = input_mesh->mVertices[j];
132  p *= transform;
133  tv.vertices_.push_back(
134  coal::Vec3s(p.x * scale[0], p.y * scale[1], p.z * scale[2]));
135  }
136 
137  // add the indices
138  for (uint32_t j = 0; j < input_mesh->mNumFaces; j++) {
139  aiFace& face = input_mesh->mFaces[j];
140  assert(face.mNumIndices == 3 && "The size of the face is not valid.");
141  tv.triangles_.push_back(
142  coal::Triangle(vertices_offset + face.mIndices[0],
143  vertices_offset + face.mIndices[1],
144  vertices_offset + face.mIndices[2]));
145  }
146 
147  nbVertices += input_mesh->mNumVertices;
148  }
149 
150  for (uint32_t i = 0; i < node->mNumChildren; ++i) {
151  nbVertices +=
152  recurseBuildMesh(scale, scene, node->mChildren[i], nbVertices, tv);
153  }
154 
155  return nbVertices;
156 }
157 
158 void buildMesh(const coal::Vec3s& scale, const aiScene* scene,
159  unsigned vertices_offset, TriangleAndVertices& tv) {
160  recurseBuildMesh(scale, scene, scene->mRootNode, vertices_offset, tv);
161 }
162 
163 } // namespace internal
164 } // namespace coal
coal::Vec3s
Eigen::Matrix< CoalScalar, 3, 1 > Vec3s
Definition: coal/data_types.h:77
coal::internal::TriangleAndVertices::vertices_
std::vector< coal::Vec3s > vertices_
Definition: coal/mesh_loader/assimp.h:56
coal::internal::TriangleAndVertices::triangles_
std::vector< coal::Triangle > triangles_
Definition: coal/mesh_loader/assimp.h:57
uint32_t
uint32_t
coal::internal::Loader::Loader
Loader()
Definition: assimp.cpp:61
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
coal::internal::Loader::scene
aiScene const * scene
Definition: coal/mesh_loader/assimp.h:67
coal::transform
COAL_DLLAPI Halfspace transform(const Halfspace &a, const Transform3s &tf)
Definition: geometric_shapes_utility.cpp:248
COAL_THROW_PRETTY
#define COAL_THROW_PRETTY(message, exception)
Definition: include/coal/fwd.hh:64
coal::internal::TriangleAndVertices
Definition: coal/mesh_loader/assimp.h:55
coal::internal::Loader::importer
Assimp::Importer * importer
Definition: coal/mesh_loader/assimp.h:66
coal::internal::Loader::~Loader
~Loader()
Definition: assimp.cpp:75
assimp.h
coal::internal::recurseBuildMesh
unsigned recurseBuildMesh(const coal::Vec3s &scale, const aiScene *scene, const aiNode *node, unsigned vertices_offset, TriangleAndVertices &tv)
Recursive procedure for building a mesh.
Definition: assimp.cpp:109
coal::internal::buildMesh
COAL_DLLAPI void buildMesh(const coal::Vec3s &scale, const aiScene *scene, unsigned vertices_offset, TriangleAndVertices &tv)
Recursive procedure for building a mesh.
Definition: assimp.cpp:158
coal::internal::Loader::load
void load(const std::string &resource_path)
Definition: assimp.cpp:79
coal::Triangle
Triangle with 3 indices for points.
Definition: coal/data_types.h:111
Assimp
Definition: coal/mesh_loader/assimp.h:47


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:57