Coverage Report

Created: 2026-04-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/PostProcessing/FindDegenerates.cpp
Line
Count
Source
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2026, assimp team
7
8
All rights reserved.
9
10
Redistribution and use of this software in source and binary forms,
11
with or without modification, are permitted provided that the following
12
conditions are met:
13
14
* Redistributions of source code must retain the above
15
  copyright notice, this list of conditions and the
16
  following disclaimer.
17
18
* Redistributions in binary form must reproduce the above
19
  copyright notice, this list of conditions and the
20
  following disclaimer in the documentation and/or other
21
  materials provided with the distribution.
22
23
* Neither the name of the assimp team, nor the names of its
24
  contributors may be used to endorse or promote products
25
  derived from this software without specific prior
26
  written permission of the assimp team.
27
28
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
---------------------------------------------------------------------------
40
*/
41
42
/** @file  FindDegenerates.cpp
43
 *  @brief Implementation of the FindDegenerates post-process step.
44
 */
45
46
#include "FindDegenerates.h"
47
#include "Geometry/GeometryUtils.h"
48
#include "ProcessHelper.h"
49
50
#include <assimp/Exceptional.h>
51
52
#include <unordered_map>
53
54
using namespace Assimp;
55
56
// Correct node indices to meshes and remove references to deleted mesh
57
static void updateSceneGraph(aiNode *pNode, const std::unordered_map<unsigned int, unsigned int> &meshMap);
58
59
// ------------------------------------------------------------------------------------------------
60
// Constructor to be privately used by Importer
61
FindDegeneratesProcess::FindDegeneratesProcess() :
62
13.1k
        mConfigRemoveDegenerates(false),
63
13.1k
        mConfigCheckAreaOfTriangle(false) {
64
    // empty
65
13.1k
}
66
67
// ------------------------------------------------------------------------------------------------
68
// Returns whether the processing step is present in the given flag field.
69
3.19k
bool FindDegeneratesProcess::IsActive(unsigned int pFlags) const {
70
3.19k
    return 0 != (pFlags & aiProcess_FindDegenerates);
71
3.19k
}
72
73
// ------------------------------------------------------------------------------------------------
74
// Setup import configuration
75
3.19k
void FindDegeneratesProcess::SetupProperties(const Importer *pImp) {
76
    // Get the current value of AI_CONFIG_PP_FD_REMOVE
77
3.19k
    mConfigRemoveDegenerates = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_REMOVE, 0));
78
3.19k
    mConfigCheckAreaOfTriangle = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_CHECKAREA));
79
3.19k
}
80
81
// ------------------------------------------------------------------------------------------------
82
// Executes the post processing step on the given imported data.
83
3.19k
void FindDegeneratesProcess::Execute(aiScene *pScene) {
84
3.19k
    ASSIMP_LOG_DEBUG("FindDegeneratesProcess begin");
85
3.19k
    if (nullptr == pScene) {
86
0
        return;
87
0
    }
88
89
3.19k
    std::unordered_map<unsigned int, unsigned int> meshMap;
90
3.19k
    meshMap.reserve(pScene->mNumMeshes);
91
92
3.19k
    const unsigned int originalNumMeshes = pScene->mNumMeshes;
93
3.19k
    unsigned int targetIndex = 0;
94
8.59k
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
95
        // Do not process point cloud, ExecuteOnMesh works only with faces data
96
5.40k
        if ((pScene->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType::aiPrimitiveType_POINT) && ExecuteOnMesh(pScene->mMeshes[i])) {
97
0
            delete pScene->mMeshes[i];
98
            // Not strictly required, but clean:
99
0
            pScene->mMeshes[i] = nullptr;
100
5.40k
        } else {
101
5.40k
            meshMap[i] = targetIndex;
102
5.40k
            pScene->mMeshes[targetIndex] = pScene->mMeshes[i];
103
5.40k
            ++targetIndex;
104
5.40k
        }
105
5.40k
    }
106
3.19k
    pScene->mNumMeshes = targetIndex;
107
108
3.19k
    if (meshMap.size() < originalNumMeshes) {
109
0
        updateSceneGraph(pScene->mRootNode, meshMap);
110
0
    }
111
112
3.19k
    ASSIMP_LOG_DEBUG("FindDegeneratesProcess finished");
113
3.19k
}
114
115
0
static void updateSceneGraph(aiNode *pNode, const std::unordered_map<unsigned int, unsigned int> &meshMap) {
116
0
    unsigned int targetIndex = 0;
117
0
    for (unsigned i = 0; i < pNode->mNumMeshes; ++i) {
118
0
        const unsigned int sourceMeshIndex = pNode->mMeshes[i];
119
0
        auto it = meshMap.find(sourceMeshIndex);
120
0
        if (it != meshMap.end()) {
121
0
            pNode->mMeshes[targetIndex] = it->second;
122
0
            ++targetIndex;
123
0
        }
124
0
    }
125
0
    pNode->mNumMeshes = targetIndex;
126
    // recurse to all children
127
0
    for (unsigned i = 0; i < pNode->mNumChildren; ++i) {
128
0
        updateSceneGraph(pNode->mChildren[i], meshMap);
129
0
    }
130
0
}
131
132
// ------------------------------------------------------------------------------------------------
133
// Executes the post processing step on the given imported mesh
134
5.40k
bool FindDegeneratesProcess::ExecuteOnMesh(aiMesh *mesh) {
135
5.40k
    mesh->mPrimitiveTypes = 0;
136
137
5.40k
    std::vector<bool> remove_me;
138
5.40k
    if (mConfigRemoveDegenerates) {
139
0
        remove_me.resize(mesh->mNumFaces, false);
140
0
    }
141
142
5.40k
    unsigned int deg = 0, limit;
143
1.99M
    for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
144
1.99M
        aiFace &face = mesh->mFaces[a];
145
1.99M
        bool first = true;
146
10.1M
        auto vertex_in_range = [numVertices = mesh->mNumVertices](unsigned int vertex_idx) { return vertex_idx < numVertices; };
147
148
        // check whether the face contains degenerated entries
149
5.36M
        for (unsigned int i = 0; i < face.mNumIndices; ++i) {
150
3.37M
            if (!std::all_of(face.mIndices, face.mIndices + face.mNumIndices, vertex_in_range))
151
0
                continue;
152
153
            // Polygons with more than 4 points are allowed to have double points, that is
154
            // simulating polygons with holes just with concave polygons. However,
155
            // double points may not come directly after another.
156
3.37M
            limit = face.mNumIndices;
157
3.37M
            if (face.mNumIndices > 4) {
158
0
                limit = std::min(limit, i + 2);
159
0
            }
160
161
8.04M
            for (unsigned int t = i + 1; t < limit; ++t) {
162
4.67M
                if (mesh->mVertices[face.mIndices[i]] == mesh->mVertices[face.mIndices[t]]) {
163
                    // we have found a matching vertex position
164
                    // remove the corresponding index from the array
165
2.59M
                    --face.mNumIndices;
166
2.59M
                    --limit;
167
3.89M
                    for (unsigned int m = t; m < face.mNumIndices; ++m) {
168
1.29M
                        face.mIndices[m] = face.mIndices[m + 1];
169
1.29M
                    }
170
2.59M
                    --t;
171
172
                    // NOTE: we set the removed vertex index to an unique value
173
                    // to make sure the developer gets notified when his
174
                    // application attempts to access this data.
175
2.59M
                    face.mIndices[face.mNumIndices] = 0xdeadbeef;
176
177
2.59M
                    if (first) {
178
1.30M
                        ++deg;
179
1.30M
                        first = false;
180
1.30M
                    }
181
182
2.59M
                    if (mConfigRemoveDegenerates) {
183
0
                        remove_me[a] = true;
184
0
                        goto evil_jump_outside; // hrhrhrh ... yeah, this rocks baby!
185
0
                    }
186
2.59M
                }
187
4.67M
            }
188
189
3.37M
            if (mConfigCheckAreaOfTriangle) {
190
3.37M
                if (face.mNumIndices == 3) {
191
2.06M
                    ai_real area = GeometryUtils::calculateAreaOfTriangle(face, mesh);
192
2.06M
                    if (area < ai_epsilon) {
193
34.6k
                        if (mConfigRemoveDegenerates) {
194
0
                            remove_me[a] = true;
195
0
                            ++deg;
196
0
                            goto evil_jump_outside;
197
0
                        }
198
199
                        // todo: check for index which is corrupt.
200
34.6k
                    }
201
2.06M
                }
202
3.37M
            }
203
3.37M
        }
204
205
        // We need to update the primitive flags array of the mesh.
206
1.99M
        switch (face.mNumIndices) {
207
1.29M
        case 1u:
208
1.29M
            mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
209
1.29M
            break;
210
13.9k
        case 2u:
211
13.9k
            mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
212
13.9k
            break;
213
684k
        case 3u:
214
684k
            mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
215
684k
            break;
216
0
        default:
217
0
            mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
218
0
            break;
219
1.99M
        };
220
1.99M
    evil_jump_outside:
221
1.99M
        continue;
222
1.99M
    }
223
224
    // If AI_CONFIG_PP_FD_REMOVE is true, remove degenerated faces from the import
225
5.40k
    if (mConfigRemoveDegenerates && deg) {
226
0
        unsigned int n = 0;
227
0
        for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
228
0
            aiFace &face_src = mesh->mFaces[a];
229
0
            if (!remove_me[a]) {
230
0
                aiFace &face_dest = mesh->mFaces[n++];
231
232
                // Do a manual copy, keep the index array
233
0
                face_dest.mNumIndices = face_src.mNumIndices;
234
0
                face_dest.mIndices = face_src.mIndices;
235
236
0
                if (&face_src != &face_dest) {
237
                    // clear source
238
0
                    face_src.mNumIndices = 0;
239
0
                    face_src.mIndices = nullptr;
240
0
                }
241
0
            } else {
242
                // Otherwise delete it if we don't need this face
243
0
                delete[] face_src.mIndices;
244
0
                face_src.mIndices = nullptr;
245
0
                face_src.mNumIndices = 0;
246
0
            }
247
0
        }
248
        // Just leave the rest of the array unreferenced, we don't care for now
249
0
        mesh->mNumFaces = n;
250
0
        if (!mesh->mNumFaces) {
251
            // The whole mesh consists of degenerated faces
252
            // signal upward, that this mesh should be deleted.
253
0
            ASSIMP_LOG_VERBOSE_DEBUG("FindDegeneratesProcess removed a mesh full of degenerated primitives");
254
0
            return true;
255
0
        }
256
0
    }
257
258
5.40k
    if (deg && !DefaultLogger::isNullLogger()) {
259
        ASSIMP_LOG_WARN("Found ", deg, " degenerated primitives");
260
0
    }
261
5.40k
    return false;
262
5.40k
}