/src/assimp/code/PostProcessing/ProcessHelper.cpp
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2026, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /// @file ProcessHelper.cpp |
43 | | /** Implement shared utility functions for postprocessing steps */ |
44 | | |
45 | | #include "ProcessHelper.h" |
46 | | |
47 | | #include <limits> |
48 | | |
49 | | namespace Assimp { |
50 | | |
51 | | // ------------------------------------------------------------------------------- |
52 | 0 | void ConvertListToStrings(const std::string &in, std::list<std::string> &out) { |
53 | 0 | const char *s = in.c_str(); |
54 | 0 | const char *end = in.c_str() + in.size(); |
55 | 0 | while (*s) { |
56 | 0 | SkipSpacesAndLineEnd(&s, end); |
57 | 0 | if (*s == '\'') { |
58 | 0 | const char *base = ++s; |
59 | 0 | while (*s != '\'') { |
60 | 0 | ++s; |
61 | 0 | if (*s == '\0') { |
62 | 0 | ASSIMP_LOG_ERROR("ConvertListToString: String list is ill-formatted"); |
63 | 0 | return; |
64 | 0 | } |
65 | 0 | } |
66 | 0 | out.emplace_back(base, (size_t)(s - base)); |
67 | 0 | ++s; |
68 | 0 | } else { |
69 | 0 | out.push_back(GetNextToken(s, end)); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | // ------------------------------------------------------------------------------- |
75 | | void FindAABBTransformed(const aiMesh *mesh, aiVector3D &min, aiVector3D &max, |
76 | 0 | const aiMatrix4x4 &m) { |
77 | 0 | min = aiVector3D(ai_real(10e10), ai_real(10e10), ai_real(10e10)); |
78 | 0 | max = aiVector3D(ai_real(-10e10), ai_real(-10e10), ai_real(-10e10)); |
79 | 0 | for (unsigned int i = 0; i < mesh->mNumVertices; ++i) { |
80 | 0 | const aiVector3D v = m * mesh->mVertices[i]; |
81 | 0 | min = std::min(v, min); |
82 | 0 | max = std::max(v, max); |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | // ------------------------------------------------------------------------------- |
87 | 0 | void FindMeshCenter(aiMesh *mesh, aiVector3D &out, aiVector3D &min, aiVector3D &max) { |
88 | 0 | ArrayBounds(mesh->mVertices, mesh->mNumVertices, min, max); |
89 | 0 | out = min + (max - min) * (ai_real)0.5; |
90 | 0 | } |
91 | | |
92 | | // ------------------------------------------------------------------------------- |
93 | 0 | void FindSceneCenter(aiScene *scene, aiVector3D &out, aiVector3D &min, aiVector3D &max) { |
94 | 0 | if (nullptr == scene) { |
95 | 0 | return; |
96 | 0 | } |
97 | | |
98 | 0 | if (0 == scene->mNumMeshes) { |
99 | 0 | return; |
100 | 0 | } |
101 | 0 | FindMeshCenter(scene->mMeshes[0], out, min, max); |
102 | 0 | for (unsigned int i = 1; i < scene->mNumMeshes; ++i) { |
103 | 0 | aiVector3D tout, tmin, tmax; |
104 | 0 | FindMeshCenter(scene->mMeshes[i], tout, tmin, tmax); |
105 | 0 | if (min[0] > tmin[0]) min[0] = tmin[0]; |
106 | 0 | if (min[1] > tmin[1]) min[1] = tmin[1]; |
107 | 0 | if (min[2] > tmin[2]) min[2] = tmin[2]; |
108 | 0 | if (max[0] < tmax[0]) max[0] = tmax[0]; |
109 | 0 | if (max[1] < tmax[1]) max[1] = tmax[1]; |
110 | 0 | if (max[2] < tmax[2]) max[2] = tmax[2]; |
111 | 0 | } |
112 | 0 | out = min + (max - min) * (ai_real)0.5; |
113 | 0 | } |
114 | | |
115 | | // ------------------------------------------------------------------------------- |
116 | | void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out, aiVector3D &min, |
117 | 0 | aiVector3D &max, const aiMatrix4x4 &m) { |
118 | 0 | FindAABBTransformed(mesh, min, max, m); |
119 | 0 | out = min + (max - min) * (ai_real)0.5; |
120 | 0 | } |
121 | | |
122 | | // ------------------------------------------------------------------------------- |
123 | 0 | void FindMeshCenter(aiMesh *mesh, aiVector3D &out) { |
124 | 0 | aiVector3D min, max; |
125 | 0 | FindMeshCenter(mesh, out, min, max); |
126 | 0 | } |
127 | | |
128 | | // ------------------------------------------------------------------------------- |
129 | | void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out, |
130 | 0 | const aiMatrix4x4 &m) { |
131 | 0 | aiVector3D min, max; |
132 | 0 | FindMeshCenterTransformed(mesh, out, min, max, m); |
133 | 0 | } |
134 | | |
135 | | // ------------------------------------------------------------------------------- |
136 | 172k | ai_real ComputePositionEpsilon(const aiMesh *pMesh) { |
137 | 172k | const ai_real epsilon = ai_real(1e-4); |
138 | | |
139 | | // calculate the position bounds so we have a reliable epsilon to check position differences against |
140 | 172k | aiVector3D minVec, maxVec; |
141 | 172k | ArrayBounds(pMesh->mVertices, pMesh->mNumVertices, minVec, maxVec); |
142 | 172k | return (maxVec - minVec).Length() * epsilon; |
143 | 172k | } |
144 | | |
145 | | // ------------------------------------------------------------------------------- |
146 | 8 | ai_real ComputePositionEpsilon(const aiMesh *const *pMeshes, size_t num) { |
147 | 8 | ai_assert(nullptr != pMeshes); |
148 | | |
149 | 8 | const ai_real epsilon = ai_real(1e-4); |
150 | | |
151 | | // calculate the position bounds so we have a reliable epsilon to check position differences against |
152 | 8 | aiVector3D minVec, maxVec, mi, ma; |
153 | 8 | MinMaxChooser<aiVector3D>()(minVec, maxVec); |
154 | | |
155 | 18 | for (size_t a = 0; a < num; ++a) { |
156 | 10 | const aiMesh *pMesh = pMeshes[a]; |
157 | 10 | ArrayBounds(pMesh->mVertices, pMesh->mNumVertices, mi, ma); |
158 | | |
159 | 10 | minVec = std::min(minVec, mi); |
160 | 10 | maxVec = std::max(maxVec, ma); |
161 | 10 | } |
162 | 8 | return (maxVec - minVec).Length() * epsilon; |
163 | 8 | } |
164 | | |
165 | | // ------------------------------------------------------------------------------- |
166 | 0 | unsigned int GetMeshVFormatUnique(const aiMesh *pcMesh) { |
167 | 0 | ai_assert(nullptr != pcMesh); |
168 | | |
169 | | // FIX: the hash may never be 0. Otherwise a comparison against |
170 | | // nullptr could be successful |
171 | 0 | unsigned int iRet = 1; |
172 | | |
173 | | // normals |
174 | 0 | if (pcMesh->HasNormals()) iRet |= 0x2; |
175 | | // tangents and bitangents |
176 | 0 | if (pcMesh->HasTangentsAndBitangents()) iRet |= 0x4; |
177 | | |
178 | |
|
179 | 0 | static_assert(8 >= AI_MAX_NUMBER_OF_COLOR_SETS, "static_assert(8 >= AI_MAX_NUMBER_OF_COLOR_SETS)"); |
180 | 0 | static_assert(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS, "static_assert(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS)"); |
181 | | |
182 | | // texture coordinates |
183 | 0 | unsigned int p = 0; |
184 | 0 | while (pcMesh->HasTextureCoords(p)) { |
185 | 0 | iRet |= (0x100 << p); |
186 | 0 | if (3 == pcMesh->mNumUVComponents[p]) |
187 | 0 | iRet |= (0x10000 << p); |
188 | |
|
189 | 0 | ++p; |
190 | 0 | } |
191 | | // vertex colors |
192 | 0 | p = 0; |
193 | 0 | while (pcMesh->HasVertexColors(p)) |
194 | 0 | iRet |= (0x1000000 << p++); |
195 | 0 | return iRet; |
196 | 0 | } |
197 | | |
198 | | // ------------------------------------------------------------------------------- |
199 | 41.7k | VertexWeightTable *ComputeVertexBoneWeightTable(const aiMesh *pMesh) { |
200 | 41.7k | if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones) { |
201 | 40.9k | return nullptr; |
202 | 40.9k | } |
203 | | |
204 | 844 | VertexWeightTable *avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices]; |
205 | 20.5k | for (unsigned int i = 0; i < pMesh->mNumBones; ++i) { |
206 | | |
207 | 19.6k | aiBone *bone = pMesh->mBones[i]; |
208 | 473k | for (unsigned int a = 0; a < bone->mNumWeights; ++a) { |
209 | 453k | const aiVertexWeight &weight = bone->mWeights[a]; |
210 | 453k | avPerVertexWeights[weight.mVertexId].emplace_back(i, weight.mWeight); |
211 | 453k | } |
212 | 19.6k | } |
213 | 844 | return avPerVertexWeights; |
214 | 41.7k | } |
215 | | |
216 | | // ------------------------------------------------------------------------------- |
217 | 0 | const char *MappingTypeToString(aiTextureMapping in) { |
218 | 0 | switch (in) { |
219 | 0 | case aiTextureMapping_UV: |
220 | 0 | return "UV"; |
221 | 0 | case aiTextureMapping_BOX: |
222 | 0 | return "Box"; |
223 | 0 | case aiTextureMapping_SPHERE: |
224 | 0 | return "Sphere"; |
225 | 0 | case aiTextureMapping_CYLINDER: |
226 | 0 | return "Cylinder"; |
227 | 0 | case aiTextureMapping_PLANE: |
228 | 0 | return "Plane"; |
229 | 0 | case aiTextureMapping_OTHER: |
230 | 0 | return "Other"; |
231 | 0 | default: |
232 | 0 | break; |
233 | 0 | } |
234 | | |
235 | 0 | ai_assert(false); |
236 | 0 | return "BUG"; |
237 | 0 | } |
238 | | |
239 | | // ------------------------------------------------------------------------------- |
240 | 0 | aiMesh *MakeSubmesh(const aiMesh *pMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags) { |
241 | 0 | aiMesh *oMesh = new aiMesh(); |
242 | 0 | std::vector<unsigned int> vMap(pMesh->mNumVertices, UINT_MAX); |
243 | |
|
244 | 0 | size_t numSubVerts = 0; |
245 | 0 | size_t numSubFaces = subMeshFaces.size(); |
246 | |
|
247 | 0 | for (unsigned int i = 0; i < numSubFaces; i++) { |
248 | 0 | const aiFace &f = pMesh->mFaces[subMeshFaces[i]]; |
249 | |
|
250 | 0 | for (unsigned int j = 0; j < f.mNumIndices; j++) { |
251 | 0 | if (vMap[f.mIndices[j]] == UINT_MAX) { |
252 | 0 | vMap[f.mIndices[j]] = static_cast<unsigned int>(numSubVerts++); |
253 | 0 | } |
254 | 0 | } |
255 | 0 | } |
256 | |
|
257 | 0 | oMesh->mName = pMesh->mName; |
258 | |
|
259 | 0 | oMesh->mMaterialIndex = pMesh->mMaterialIndex; |
260 | 0 | oMesh->mPrimitiveTypes = pMesh->mPrimitiveTypes; |
261 | | |
262 | | // create all the arrays for this mesh if the old mesh contained them |
263 | |
|
264 | 0 | oMesh->mNumFaces = static_cast<unsigned int>(subMeshFaces.size()); |
265 | 0 | oMesh->mNumVertices = static_cast<unsigned int>(numSubVerts); |
266 | 0 | oMesh->mVertices = new aiVector3D[numSubVerts]; |
267 | 0 | if (pMesh->HasNormals()) { |
268 | 0 | oMesh->mNormals = new aiVector3D[numSubVerts]; |
269 | 0 | } |
270 | |
|
271 | 0 | if (pMesh->HasTangentsAndBitangents()) { |
272 | 0 | oMesh->mTangents = new aiVector3D[numSubVerts]; |
273 | 0 | oMesh->mBitangents = new aiVector3D[numSubVerts]; |
274 | 0 | } |
275 | |
|
276 | 0 | for (size_t a = 0; pMesh->HasTextureCoords(static_cast<unsigned int>(a)); ++a) { |
277 | 0 | oMesh->mTextureCoords[a] = new aiVector3D[numSubVerts]; |
278 | 0 | oMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a]; |
279 | 0 | } |
280 | |
|
281 | 0 | for (size_t a = 0; pMesh->HasVertexColors(static_cast<unsigned int>(a)); ++a) { |
282 | 0 | oMesh->mColors[a] = new aiColor4D[numSubVerts]; |
283 | 0 | } |
284 | | |
285 | | // and copy over the data, generating faces with linear indices along the way |
286 | 0 | oMesh->mFaces = new aiFace[numSubFaces]; |
287 | |
|
288 | 0 | for (unsigned int a = 0; a < numSubFaces; ++a) { |
289 | |
|
290 | 0 | const aiFace &srcFace = pMesh->mFaces[subMeshFaces[a]]; |
291 | 0 | aiFace &dstFace = oMesh->mFaces[a]; |
292 | 0 | dstFace.mNumIndices = srcFace.mNumIndices; |
293 | 0 | dstFace.mIndices = new unsigned int[dstFace.mNumIndices]; |
294 | | |
295 | | // accumulate linearly all the vertices of the source face |
296 | 0 | for (size_t b = 0; b < dstFace.mNumIndices; ++b) { |
297 | 0 | dstFace.mIndices[b] = vMap[srcFace.mIndices[b]]; |
298 | 0 | } |
299 | 0 | } |
300 | |
|
301 | 0 | for (unsigned int srcIndex = 0; srcIndex < pMesh->mNumVertices; ++srcIndex) { |
302 | 0 | unsigned int nvi = vMap[srcIndex]; |
303 | 0 | if (nvi == UINT_MAX) { |
304 | 0 | continue; |
305 | 0 | } |
306 | | |
307 | 0 | oMesh->mVertices[nvi] = pMesh->mVertices[srcIndex]; |
308 | 0 | if (pMesh->HasNormals()) { |
309 | 0 | oMesh->mNormals[nvi] = pMesh->mNormals[srcIndex]; |
310 | 0 | } |
311 | |
|
312 | 0 | if (pMesh->HasTangentsAndBitangents()) { |
313 | 0 | oMesh->mTangents[nvi] = pMesh->mTangents[srcIndex]; |
314 | 0 | oMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex]; |
315 | 0 | } |
316 | 0 | for (size_t c = 0, cc = pMesh->GetNumUVChannels(); c < cc; ++c) { |
317 | 0 | oMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex]; |
318 | 0 | } |
319 | 0 | for (size_t c = 0, cc = pMesh->GetNumColorChannels(); c < cc; ++c) { |
320 | 0 | oMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex]; |
321 | 0 | } |
322 | 0 | } |
323 | |
|
324 | 0 | if (~subFlags & AI_SUBMESH_FLAGS_SANS_BONES) { |
325 | 0 | std::vector<unsigned int> subBones(pMesh->mNumBones, 0); |
326 | |
|
327 | 0 | for (unsigned int a = 0; a < pMesh->mNumBones; ++a) { |
328 | 0 | const aiBone *bone = pMesh->mBones[a]; |
329 | |
|
330 | 0 | for (unsigned int b = 0; b < bone->mNumWeights; b++) { |
331 | 0 | unsigned int v = vMap[bone->mWeights[b].mVertexId]; |
332 | |
|
333 | 0 | if (v != UINT_MAX) { |
334 | 0 | subBones[a]++; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | |
|
339 | 0 | for (unsigned int a = 0; a < pMesh->mNumBones; ++a) { |
340 | 0 | if (subBones[a] > 0) { |
341 | 0 | oMesh->mNumBones++; |
342 | 0 | } |
343 | 0 | } |
344 | |
|
345 | 0 | if (oMesh->mNumBones) { |
346 | 0 | oMesh->mBones = new aiBone *[oMesh->mNumBones](); |
347 | 0 | unsigned int nbParanoia = oMesh->mNumBones; |
348 | |
|
349 | 0 | oMesh->mNumBones = 0; //rewind |
350 | |
|
351 | 0 | for (unsigned int a = 0; a < pMesh->mNumBones; ++a) { |
352 | 0 | if (subBones[a] == 0) { |
353 | 0 | continue; |
354 | 0 | } |
355 | 0 | aiBone *newBone = new aiBone; |
356 | 0 | oMesh->mBones[oMesh->mNumBones++] = newBone; |
357 | |
|
358 | 0 | const aiBone *bone = pMesh->mBones[a]; |
359 | |
|
360 | 0 | newBone->mName = bone->mName; |
361 | 0 | newBone->mOffsetMatrix = bone->mOffsetMatrix; |
362 | 0 | newBone->mWeights = new aiVertexWeight[subBones[a]]; |
363 | |
|
364 | 0 | for (unsigned int b = 0; b < bone->mNumWeights; b++) { |
365 | 0 | const unsigned int v = vMap[bone->mWeights[b].mVertexId]; |
366 | |
|
367 | 0 | if (v != UINT_MAX) { |
368 | 0 | aiVertexWeight w(v, bone->mWeights[b].mWeight); |
369 | 0 | newBone->mWeights[newBone->mNumWeights++] = w; |
370 | 0 | } |
371 | 0 | } |
372 | 0 | } |
373 | |
|
374 | 0 | ai_assert(nbParanoia == oMesh->mNumBones); |
375 | 0 | (void)nbParanoia; // remove compiler warning on release build |
376 | 0 | } |
377 | 0 | } |
378 | |
|
379 | 0 | return oMesh; |
380 | 0 | } |
381 | | |
382 | | } // namespace Assimp |