Directory: | ./ |
---|---|
File: | include/coal/mesh_loader/assimp.h |
Date: | 2025-04-01 09:23:31 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 13 | 14 | 92.9% |
Branches: | 8 | 48 | 16.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Software License Agreement (BSD License) | ||
3 | * | ||
4 | * Copyright (c) 2011-2014, Willow Garage, Inc. | ||
5 | * Copyright (c) 2014-2015, Open Source Robotics Foundation | ||
6 | * Copyright (c) 2016-2019, CNRS - LAAS | ||
7 | * Copyright (c) 2019, INRIA | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * | ||
14 | * * Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * * Redistributions in binary form must reproduce the above | ||
17 | * copyright notice, this list of conditions and the following | ||
18 | * disclaimer in the documentation and/or other materials provided | ||
19 | * with the distribution. | ||
20 | * * Neither the name of Open Source Robotics Foundation nor the names of its | ||
21 | * contributors may be used to endorse or promote products derived | ||
22 | * from this software without specific prior written permission. | ||
23 | * | ||
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
25 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
26 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
27 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
28 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
30 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
31 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
34 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
35 | * POSSIBILITY OF SUCH DAMAGE. | ||
36 | */ | ||
37 | |||
38 | #ifndef COAL_MESH_LOADER_ASSIMP_H | ||
39 | #define COAL_MESH_LOADER_ASSIMP_H | ||
40 | |||
41 | #include "coal/fwd.hh" | ||
42 | #include "coal/config.hh" | ||
43 | #include "coal/BV/OBBRSS.h" | ||
44 | #include "coal/BVH/BVH_model.h" | ||
45 | |||
46 | struct aiScene; | ||
47 | namespace Assimp { | ||
48 | class Importer; | ||
49 | } | ||
50 | |||
51 | namespace coal { | ||
52 | |||
53 | namespace internal { | ||
54 | |||
55 | struct COAL_DLLAPI TriangleAndVertices { | ||
56 | std::vector<coal::Vec3s> vertices_; | ||
57 | std::vector<coal::Triangle> triangles_; | ||
58 | }; | ||
59 | |||
60 | struct COAL_DLLAPI Loader { | ||
61 | Loader(); | ||
62 | ~Loader(); | ||
63 | |||
64 | void load(const std::string& resource_path); | ||
65 | |||
66 | Assimp::Importer* importer; | ||
67 | aiScene const* scene; | ||
68 | }; | ||
69 | |||
70 | /** | ||
71 | * @brief Recursive procedure for building a mesh | ||
72 | * | ||
73 | * @param[in] scale Scale to apply when reading the ressource | ||
74 | * @param[in] scene Pointer to the assimp scene | ||
75 | * @param[in] vertices_offset Current number of vertices in the model | ||
76 | * @param tv Triangles and Vertices of the mesh submodels | ||
77 | */ | ||
78 | COAL_DLLAPI void buildMesh(const coal::Vec3s& scale, const aiScene* scene, | ||
79 | unsigned vertices_offset, TriangleAndVertices& tv); | ||
80 | |||
81 | /** | ||
82 | * @brief Convert an assimp scene to a mesh | ||
83 | * | ||
84 | * @param[in] scale Scale to apply when reading the ressource | ||
85 | * @param[in] scene Pointer to the assimp scene | ||
86 | * @param[out] mesh The mesh that must be built | ||
87 | */ | ||
88 | template <class BoundingVolume> | ||
89 | 318 | inline void meshFromAssimpScene( | |
90 | const coal::Vec3s& scale, const aiScene* scene, | ||
91 | const shared_ptr<BVHModel<BoundingVolume> >& mesh) { | ||
92 | 318 | TriangleAndVertices tv; | |
93 | |||
94 |
1/2✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
|
318 | int res = mesh->beginModel(); |
95 | |||
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
318 | if (res != coal::BVH_OK) { |
97 | ✗ | COAL_THROW_PRETTY("fcl BVHReturnCode = " << res, std::runtime_error); | |
98 | } | ||
99 | |||
100 |
1/2✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
|
318 | buildMesh(scale, scene, (unsigned)mesh->num_vertices, tv); |
101 |
1/2✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
|
318 | mesh->addSubModel(tv.vertices_, tv.triangles_); |
102 | |||
103 |
1/2✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
|
318 | mesh->endModel(); |
104 | 318 | } | |
105 | |||
106 | } // namespace internal | ||
107 | |||
108 | /** | ||
109 | * @brief Read a mesh file and convert it to a polyhedral mesh | ||
110 | * | ||
111 | * @param[in] resource_path Path to the ressource mesh file to be read | ||
112 | * @param[in] scale Scale to apply when reading the ressource | ||
113 | * @param[out] polyhedron The resulted polyhedron | ||
114 | */ | ||
115 | template <class BoundingVolume> | ||
116 | 318 | inline void loadPolyhedronFromResource( | |
117 | const std::string& resource_path, const coal::Vec3s& scale, | ||
118 | const shared_ptr<BVHModel<BoundingVolume> >& polyhedron) { | ||
119 |
1/2✓ Branch 1 taken 159 times.
✗ Branch 2 not taken.
|
318 | internal::Loader scene; |
120 |
1/2✓ Branch 1 taken 159 times.
✗ Branch 2 not taken.
|
318 | scene.load(resource_path); |
121 | |||
122 |
1/2✓ Branch 1 taken 159 times.
✗ Branch 2 not taken.
|
318 | internal::meshFromAssimpScene(scale, scene.scene, polyhedron); |
123 | 318 | } | |
124 | |||
125 | } // namespace coal | ||
126 | |||
127 | #endif // COAL_MESH_LOADER_ASSIMP_H | ||
128 |