/src/assimp/code/PostProcessing/OptimizeMeshes.cpp
Line | Count | Source |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2026, assimp team |
7 | | |
8 | | |
9 | | |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use of this software in source and binary forms, |
13 | | with or without modification, are permitted provided that the following |
14 | | conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above |
17 | | copyright notice, this list of conditions and the |
18 | | following disclaimer. |
19 | | |
20 | | * Redistributions in binary form must reproduce the above |
21 | | copyright notice, this list of conditions and the |
22 | | following disclaimer in the documentation and/or other |
23 | | materials provided with the distribution. |
24 | | |
25 | | * Neither the name of the assimp team, nor the names of its |
26 | | contributors may be used to endorse or promote products |
27 | | derived from this software without specific prior |
28 | | written permission of the assimp team. |
29 | | |
30 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
33 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
34 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
35 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
36 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
37 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
38 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
39 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
40 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
41 | | --------------------------------------------------------------------------- |
42 | | */ |
43 | | |
44 | | /** @file OptimizeMeshes.cpp |
45 | | * @brief Implementation of the aiProcess_OptimizeMeshes step |
46 | | */ |
47 | | |
48 | | |
49 | | #ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS |
50 | | |
51 | | |
52 | | #include "OptimizeMeshes.h" |
53 | | #include "ProcessHelper.h" |
54 | | #include <assimp/SceneCombiner.h> |
55 | | #include <assimp/Exceptional.h> |
56 | | |
57 | | using namespace Assimp; |
58 | | |
59 | | static const unsigned int NotSet = 0xffffffff; |
60 | | static const unsigned int DeadBeef = 0xdeadbeef; |
61 | | |
62 | | // ------------------------------------------------------------------------------------------------ |
63 | | // Constructor to be privately used by Importer |
64 | | OptimizeMeshesProcess::OptimizeMeshesProcess() |
65 | | : mScene() |
66 | 13.3k | , pts(false) |
67 | 13.3k | , max_verts( NotSet ) |
68 | 13.3k | , max_faces( NotSet ) { |
69 | | // empty |
70 | 13.3k | } |
71 | | |
72 | | // ------------------------------------------------------------------------------------------------ |
73 | | // Returns whether the processing step is present in the given flag field. |
74 | | bool OptimizeMeshesProcess::IsActive( unsigned int pFlags) const |
75 | 2.01k | { |
76 | | // Our behaviour needs to be different if the SortByPType or SplitLargeMeshes |
77 | | // steps are active. Thus we need to query their flags here and store the |
78 | | // information, although we're breaking const-correctness. |
79 | | // That's a serious design flaw, consider redesign. |
80 | 2.01k | if( 0 != (pFlags & aiProcess_OptimizeMeshes) ) { |
81 | 0 | pts = (0 != (pFlags & aiProcess_SortByPType)); |
82 | 0 | max_verts = ( 0 != ( pFlags & aiProcess_SplitLargeMeshes ) ) ? DeadBeef : max_verts; |
83 | 0 | return true; |
84 | 0 | } |
85 | 2.01k | return false; |
86 | 2.01k | } |
87 | | |
88 | | // ------------------------------------------------------------------------------------------------ |
89 | | // Setup properties for the post-processing step |
90 | | void OptimizeMeshesProcess::SetupProperties(const Importer* pImp) |
91 | 0 | { |
92 | 0 | if( max_verts == DeadBeef /* magic hack */ ) { |
93 | 0 | max_faces = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES); |
94 | 0 | max_verts = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES); |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | // ------------------------------------------------------------------------------------------------ |
99 | | // Execute step |
100 | | void OptimizeMeshesProcess::Execute( aiScene* pScene) |
101 | 0 | { |
102 | 0 | const unsigned int num_old = pScene->mNumMeshes; |
103 | 0 | if (num_old <= 1) { |
104 | 0 | ASSIMP_LOG_DEBUG("Skipping OptimizeMeshesProcess"); |
105 | 0 | return; |
106 | 0 | } |
107 | | |
108 | 0 | ASSIMP_LOG_DEBUG("OptimizeMeshesProcess begin"); |
109 | 0 | mScene = pScene; |
110 | | |
111 | | // need to clear persistent members from previous runs |
112 | 0 | merge_list.resize( 0 ); |
113 | 0 | output.resize( 0 ); |
114 | | |
115 | | // ensure we have the right sizes |
116 | 0 | merge_list.reserve(pScene->mNumMeshes); |
117 | 0 | output.reserve(pScene->mNumMeshes); |
118 | | |
119 | | // Prepare lookup tables |
120 | 0 | meshes.resize(pScene->mNumMeshes); |
121 | 0 | FindInstancedMeshes(pScene->mRootNode); |
122 | 0 | if( max_verts == DeadBeef ) /* undo the magic hack */ |
123 | 0 | max_verts = NotSet; |
124 | | |
125 | | // ... instanced meshes are immediately processed and added to the output list |
126 | 0 | for (unsigned int i = 0, n = 0; i < pScene->mNumMeshes;++i) { |
127 | 0 | meshes[i].vertex_format = GetMeshVFormatUnique(pScene->mMeshes[i]); |
128 | |
|
129 | 0 | if (meshes[i].instance_cnt > 1 && meshes[i].output_id == NotSet ) { |
130 | 0 | meshes[i].output_id = n++; |
131 | 0 | output.push_back(mScene->mMeshes[i]); |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | // and process all nodes in the scenegraph recursively |
136 | 0 | ProcessNode(pScene->mRootNode); |
137 | 0 | if (!output.size()) { |
138 | 0 | throw DeadlyImportError("OptimizeMeshes: No meshes remaining; there's definitely something wrong"); |
139 | 0 | } |
140 | | |
141 | 0 | meshes.resize( 0 ); |
142 | 0 | ai_assert(output.size() <= num_old); |
143 | |
|
144 | 0 | mScene->mNumMeshes = static_cast<unsigned int>(output.size()); |
145 | 0 | std::copy(output.begin(),output.end(),mScene->mMeshes); |
146 | |
|
147 | 0 | if (output.size() != num_old) { |
148 | 0 | ASSIMP_LOG_DEBUG("OptimizeMeshesProcess finished. Input meshes: ", num_old, ", Output meshes: ", pScene->mNumMeshes); |
149 | 0 | } else { |
150 | 0 | ASSIMP_LOG_DEBUG( "OptimizeMeshesProcess finished" ); |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | // ------------------------------------------------------------------------------------------------ |
155 | | // Process meshes for a single node |
156 | | void OptimizeMeshesProcess::ProcessNode( aiNode* pNode) |
157 | 0 | { |
158 | 0 | for (unsigned int i = 0; i < pNode->mNumMeshes;++i) { |
159 | 0 | unsigned int& im = pNode->mMeshes[i]; |
160 | |
|
161 | 0 | if (meshes[im].instance_cnt > 1) { |
162 | 0 | im = meshes[im].output_id; |
163 | 0 | } |
164 | 0 | else { |
165 | 0 | merge_list.resize( 0 ); |
166 | 0 | unsigned int verts = 0, faces = 0; |
167 | | |
168 | | // Find meshes to merge with us |
169 | 0 | for (unsigned int a = i+1; a < pNode->mNumMeshes;++a) { |
170 | 0 | unsigned int am = pNode->mMeshes[a]; |
171 | 0 | if (meshes[am].instance_cnt == 1 && CanJoin(im,am,verts,faces)) { |
172 | |
|
173 | 0 | merge_list.push_back(mScene->mMeshes[am]); |
174 | 0 | verts += mScene->mMeshes[am]->mNumVertices; |
175 | 0 | faces += mScene->mMeshes[am]->mNumFaces; |
176 | |
|
177 | 0 | pNode->mMeshes[a] = pNode->mMeshes[pNode->mNumMeshes - 1]; |
178 | 0 | --pNode->mNumMeshes; |
179 | 0 | --a; |
180 | 0 | } |
181 | 0 | } |
182 | | |
183 | | // and merge all meshes which we found, replace the old ones |
184 | 0 | if (!merge_list.empty()) { |
185 | 0 | merge_list.push_back(mScene->mMeshes[im]); |
186 | |
|
187 | 0 | aiMesh* out; |
188 | 0 | SceneCombiner::MergeMeshes(&out,0,merge_list.begin(),merge_list.end()); |
189 | 0 | output.push_back(out); |
190 | 0 | } else { |
191 | 0 | output.push_back(mScene->mMeshes[im]); |
192 | 0 | } |
193 | 0 | im = static_cast<unsigned int>(output.size()-1); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | |
|
198 | 0 | for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) { |
199 | 0 | ProcessNode( pNode->mChildren[ i ] ); |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | | // ------------------------------------------------------------------------------------------------ |
204 | | // Check whether two meshes can be joined |
205 | | bool OptimizeMeshesProcess::CanJoin ( unsigned int a, unsigned int b, unsigned int verts, unsigned int faces ) |
206 | 0 | { |
207 | 0 | if (meshes[a].vertex_format != meshes[b].vertex_format) |
208 | 0 | return false; |
209 | | |
210 | 0 | aiMesh* ma = mScene->mMeshes[a], *mb = mScene->mMeshes[b]; |
211 | |
|
212 | 0 | if ((NotSet != max_verts && verts+mb->mNumVertices > max_verts) || |
213 | 0 | (NotSet != max_faces && faces+mb->mNumFaces > max_faces)) { |
214 | 0 | return false; |
215 | 0 | } |
216 | | |
217 | | // Never merge unskinned meshes with skinned meshes |
218 | 0 | if (ma->mMaterialIndex != mb->mMaterialIndex || ma->HasBones() != mb->HasBones()) |
219 | 0 | return false; |
220 | | |
221 | | // Never merge meshes with different kinds of primitives if SortByPType did already |
222 | | // do its work. We would destroy everything again ... |
223 | 0 | if (pts && ma->mPrimitiveTypes != mb->mPrimitiveTypes) |
224 | 0 | return false; |
225 | | |
226 | | // If both meshes are skinned, check whether we have many bones defined in both meshes. |
227 | | // If yes, we can join them. |
228 | 0 | if (ma->HasBones()) { |
229 | | // TODO |
230 | 0 | return false; |
231 | 0 | } |
232 | 0 | return true; |
233 | 0 | } |
234 | | |
235 | | // ------------------------------------------------------------------------------------------------ |
236 | | // Build a LUT of all instanced meshes |
237 | | void OptimizeMeshesProcess::FindInstancedMeshes (aiNode* pNode) |
238 | 0 | { |
239 | 0 | for( unsigned int i = 0; i < pNode->mNumMeshes; ++i ) { |
240 | 0 | ++meshes[ pNode->mMeshes[ i ] ].instance_cnt; |
241 | 0 | } |
242 | |
|
243 | 0 | for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) { |
244 | 0 | FindInstancedMeshes( pNode->mChildren[ i ] ); |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | // ------------------------------------------------------------------------------------------------ |
249 | | |
250 | | #endif // !! ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS |