Coverage Report

Created: 2026-03-12 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/PostProcessing/JoinVerticesProcess.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 Implementation of the post processing step to join identical vertices
43
 * for all imported meshes
44
 */
45
46
#ifndef ASSIMP_BUILD_NO_JOINVERTICES_PROCESS
47
48
#include "JoinVerticesProcess.h"
49
#include "ProcessHelper.h"
50
#include <assimp/Vertex.h>
51
#include <assimp/TinyFormatter.h>
52
53
#include <stdio.h>
54
#include <unordered_set>
55
#include <unordered_map>
56
#include <memory>
57
#include <map>
58
59
using namespace Assimp;
60
61
// ------------------------------------------------------------------------------------------------
62
// Returns whether the processing step is present in the given flag field.
63
11.0k
bool JoinVerticesProcess::IsActive( unsigned int pFlags) const {
64
11.0k
    return (pFlags & aiProcess_JoinIdenticalVertices) != 0;
65
11.0k
}
66
// ------------------------------------------------------------------------------------------------
67
// Executes the post processing step on the given imported data.
68
10.8k
void JoinVerticesProcess::Execute( aiScene* pScene) {
69
10.8k
    ASSIMP_LOG_DEBUG("JoinVerticesProcess begin");
70
71
    // get the total number of vertices BEFORE the step is executed
72
10.8k
    int iNumOldVertices = 0;
73
10.8k
    if (!DefaultLogger::isNullLogger()) {
74
0
        for( unsigned int a = 0; a < pScene->mNumMeshes; a++)   {
75
0
            iNumOldVertices +=  pScene->mMeshes[a]->mNumVertices;
76
0
        }
77
0
    }
78
79
    // execute the step
80
10.8k
    int iNumVertices = 0;
81
230k
    for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
82
219k
        iNumVertices += ProcessMesh( pScene->mMeshes[a],a);
83
219k
    }
84
85
10.8k
    pScene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
86
87
    // if logging is active, print detailed statistics
88
10.8k
    if (!DefaultLogger::isNullLogger()) {
89
0
        if (iNumOldVertices == iNumVertices) {
90
0
            ASSIMP_LOG_DEBUG("JoinVerticesProcess finished ");
91
0
            return;
92
0
        }
93
94
        // Show statistics
95
0
        ASSIMP_LOG_INFO("JoinVerticesProcess finished | Verts in: ", iNumOldVertices,
96
0
            " out: ", iNumVertices, " | ~",
97
0
            ((iNumOldVertices - iNumVertices) / (float)iNumOldVertices) * 100.f );
98
0
    }
99
10.8k
}
100
101
namespace {
102
103
struct CompareVerticesAlmostEqual {
104
16.3M
    bool operator () (const Vertex & a, const Vertex & b) const {
105
16.3M
        static const float epsilon = 1e-5f;
106
16.3M
        static const float squareEpsilon = epsilon * epsilon;
107
108
16.3M
        if ((a.position - b.position).SquareLength() > squareEpsilon) {
109
200
            return false;
110
200
        }
111
112
        // We just test the other attributes even if they're not present in the mesh.
113
        // In this case they're initialized to 0 so the comparison succeeds.
114
        // By this method the non-present attributes are effectively ignored in the comparison.
115
116
16.3M
        if ((a.normal - b.normal).SquareLength() > squareEpsilon) {
117
4.32M
            return false;
118
4.32M
        }
119
120
11.9M
        if ((a.tangent - b.tangent).SquareLength() > squareEpsilon) {
121
2.22M
            return false;
122
2.22M
        }
123
124
9.75M
        if ((a.bitangent - b.bitangent).SquareLength() > squareEpsilon) {
125
39.2k
            return false;
126
39.2k
        }
127
128
86.7M
        for (uint32_t i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i ++) {
129
77.1M
            if ((a.texcoords[i] - b.texcoords[i]).SquareLength() > squareEpsilon) {
130
86.1k
                return false;
131
86.1k
            }
132
77.1M
        }
133
134
76.3M
        for (uint32_t i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; i ++) {
135
68.0M
            if (GetColorDifference(a.colors[i], b.colors[i]) > squareEpsilon) {
136
1.29M
                return false;
137
1.29M
            }
138
68.0M
        }
139
140
        // If reached this point, they are ~equal
141
8.33M
        return true;
142
9.63M
    }
143
};
144
145
struct HashVertex {
146
43.1M
    inline void hash_combine(std::size_t& seed, const ai_real& v) const {
147
43.1M
        std::hash<ai_real> hasher;
148
43.1M
        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
149
43.1M
    }
150
151
14.3M
    size_t operator () (const Vertex & v) const {
152
14.3M
        size_t hash = 0;
153
154
14.3M
        hash_combine(hash, v.position.x);
155
14.3M
        hash_combine(hash, v.position.y);
156
14.3M
        hash_combine(hash, v.position.z);
157
158
14.3M
        return hash;
159
14.3M
    }
160
};
161
162
template<class XMesh>
163
219k
void updateXMeshVertices(XMesh *pMesh, std::vector<int> &uniqueVertices) {
164
    // replace vertex data with the unique data sets
165
219k
    pMesh->mNumVertices = (unsigned int)uniqueVertices.size();
166
167
    // ----------------------------------------------------------------------------
168
    // NOTE - we're *not* calling Vertex::SortBack() because it would check for
169
    // presence of every single vertex component once PER VERTEX. And our CPU
170
    // dislikes branches, even if they're easily predictable.
171
    // ----------------------------------------------------------------------------
172
173
    // Position, if present (check made for aiAnimMesh)
174
219k
    if (pMesh->mVertices) {
175
219k
        std::unique_ptr<aiVector3D[]> oldVertices(pMesh->mVertices);
176
219k
        pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
177
3.35M
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
178
3.13M
            pMesh->mVertices[a] = oldVertices[uniqueVertices[a]];
179
219k
    }
180
181
    // Normals, if present
182
219k
    if (pMesh->mNormals) {
183
181k
        std::unique_ptr<aiVector3D[]> oldNormals(pMesh->mNormals);
184
181k
        pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
185
3.22M
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
186
3.04M
            pMesh->mNormals[a] = oldNormals[uniqueVertices[a]];
187
181k
    }
188
    // Tangents, if present
189
219k
    if (pMesh->mTangents) {
190
7.79k
        std::unique_ptr<aiVector3D[]> oldTangents(pMesh->mTangents);
191
7.79k
        pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
192
818k
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
193
810k
            pMesh->mTangents[a] = oldTangents[uniqueVertices[a]];
194
7.79k
    }
195
    // Bitangents as well
196
219k
    if (pMesh->mBitangents) {
197
7.79k
        std::unique_ptr<aiVector3D[]> oldBitangents(pMesh->mBitangents);
198
7.79k
        pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
199
818k
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
200
810k
            pMesh->mBitangents[a] = oldBitangents[uniqueVertices[a]];
201
7.79k
    }
202
    // Vertex colors
203
253k
    for (unsigned int a = 0; pMesh->HasVertexColors(a); a++) {
204
33.7k
        std::unique_ptr<aiColor4D[]> oldColors(pMesh->mColors[a]);
205
33.7k
        pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices];
206
933k
        for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
207
899k
            pMesh->mColors[a][b] = oldColors[uniqueVertices[b]];
208
33.7k
    }
209
    // Texture coords
210
229k
    for (unsigned int a = 0; pMesh->HasTextureCoords(a); a++) {
211
9.69k
        std::unique_ptr<aiVector3D[]> oldTextureCoords(pMesh->mTextureCoords[a]);
212
9.69k
        pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices];
213
839k
        for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
214
830k
            pMesh->mTextureCoords[a][b] = oldTextureCoords[uniqueVertices[b]];
215
9.69k
    }
216
219k
}
JoinVerticesProcess.cpp:void (anonymous namespace)::updateXMeshVertices<aiMesh>(aiMesh*, std::__1::vector<int, std::__1::allocator<int> >&)
Line
Count
Source
163
219k
void updateXMeshVertices(XMesh *pMesh, std::vector<int> &uniqueVertices) {
164
    // replace vertex data with the unique data sets
165
219k
    pMesh->mNumVertices = (unsigned int)uniqueVertices.size();
166
167
    // ----------------------------------------------------------------------------
168
    // NOTE - we're *not* calling Vertex::SortBack() because it would check for
169
    // presence of every single vertex component once PER VERTEX. And our CPU
170
    // dislikes branches, even if they're easily predictable.
171
    // ----------------------------------------------------------------------------
172
173
    // Position, if present (check made for aiAnimMesh)
174
219k
    if (pMesh->mVertices) {
175
219k
        std::unique_ptr<aiVector3D[]> oldVertices(pMesh->mVertices);
176
219k
        pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
177
3.35M
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
178
3.13M
            pMesh->mVertices[a] = oldVertices[uniqueVertices[a]];
179
219k
    }
180
181
    // Normals, if present
182
219k
    if (pMesh->mNormals) {
183
181k
        std::unique_ptr<aiVector3D[]> oldNormals(pMesh->mNormals);
184
181k
        pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
185
3.22M
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
186
3.04M
            pMesh->mNormals[a] = oldNormals[uniqueVertices[a]];
187
181k
    }
188
    // Tangents, if present
189
219k
    if (pMesh->mTangents) {
190
7.79k
        std::unique_ptr<aiVector3D[]> oldTangents(pMesh->mTangents);
191
7.79k
        pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
192
818k
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
193
810k
            pMesh->mTangents[a] = oldTangents[uniqueVertices[a]];
194
7.79k
    }
195
    // Bitangents as well
196
219k
    if (pMesh->mBitangents) {
197
7.79k
        std::unique_ptr<aiVector3D[]> oldBitangents(pMesh->mBitangents);
198
7.79k
        pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
199
818k
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
200
810k
            pMesh->mBitangents[a] = oldBitangents[uniqueVertices[a]];
201
7.79k
    }
202
    // Vertex colors
203
253k
    for (unsigned int a = 0; pMesh->HasVertexColors(a); a++) {
204
33.7k
        std::unique_ptr<aiColor4D[]> oldColors(pMesh->mColors[a]);
205
33.7k
        pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices];
206
933k
        for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
207
899k
            pMesh->mColors[a][b] = oldColors[uniqueVertices[b]];
208
33.7k
    }
209
    // Texture coords
210
229k
    for (unsigned int a = 0; pMesh->HasTextureCoords(a); a++) {
211
9.69k
        std::unique_ptr<aiVector3D[]> oldTextureCoords(pMesh->mTextureCoords[a]);
212
9.69k
        pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices];
213
839k
        for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
214
830k
            pMesh->mTextureCoords[a][b] = oldTextureCoords[uniqueVertices[b]];
215
9.69k
    }
216
219k
}
Unexecuted instantiation: JoinVerticesProcess.cpp:void (anonymous namespace)::updateXMeshVertices<aiAnimMesh>(aiAnimMesh*, std::__1::vector<int, std::__1::allocator<int> >&)
217
218
} // namespace
219
220
// ------------------------------------------------------------------------------------------------
221
222
static constexpr size_t JOINED_VERTICES_MARK = 0x80000000u;
223
224
// now start the JoinVerticesProcess
225
219k
int JoinVerticesProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) {
226
219k
    static_assert( AI_MAX_NUMBER_OF_COLOR_SETS    == 8, "AI_MAX_NUMBER_OF_COLOR_SETS    == 8");
227
219k
  static_assert( AI_MAX_NUMBER_OF_TEXTURECOORDS == 8, "AI_MAX_NUMBER_OF_TEXTURECOORDS == 8");
228
229
    // Return early if we don't have any positions
230
219k
    if (!pMesh->HasPositions() || !pMesh->HasFaces()) {
231
0
        return 0;
232
0
    }
233
234
    // We should care only about used vertices, not all of them
235
    // (this can happen due to original file vertices buffer being used by
236
    // multiple meshes)
237
219k
    std::vector<bool> usedVertexIndicesMask;
238
219k
    usedVertexIndicesMask.resize(pMesh->mNumVertices, false);
239
5.37M
    for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
240
5.15M
        aiFace& face = pMesh->mFaces[a];
241
18.6M
        for (unsigned int b = 0; b < face.mNumIndices; b++) {
242
13.4M
            usedVertexIndicesMask[face.mIndices[b]] = true;
243
13.4M
        }
244
5.15M
    }
245
246
    // We'll never have more vertices afterwards.
247
219k
    std::vector<int> uniqueVertices;
248
249
    // For each vertex the index of the vertex it was replaced by.
250
    // Since the maximal number of vertices is 2^31-1, the most significand bit can be used to mark
251
    //  whether a new vertex was created for the index (true) or if it was replaced by an existing
252
    //  unique vertex (false). This saves an additional std::vector<bool> and greatly enhances
253
    //  branching performance.
254
219k
    static_assert(AI_MAX_VERTICES == 0x7fffffff, "AI_MAX_VERTICES == 0x7fffffff");
255
219k
    std::vector<unsigned int> replaceIndex( pMesh->mNumVertices, 0xffffffff);
256
257
    // Run an optimized code path if we don't have multiple UVs or vertex colors.
258
    // This should yield false in more than 99% of all imports ...
259
219k
    const bool hasAnimMeshes = pMesh->mNumAnimMeshes > 0;
260
261
    // We'll never have more vertices afterwards.
262
219k
    std::vector<std::vector<int>> uniqueAnimatedVertices;
263
219k
    if (hasAnimMeshes) {
264
0
        uniqueAnimatedVertices.resize(pMesh->mNumAnimMeshes);
265
0
        for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
266
0
            uniqueAnimatedVertices[animMeshIndex].reserve(pMesh->mNumVertices);
267
0
        }
268
0
    }
269
    // a map that maps a vertex to its new index
270
219k
    std::unordered_map<Vertex, int, HashVertex, CompareVerticesAlmostEqual> vertex2Index = {};
271
    // we can not end up with more vertices than we started with
272
    // Now check each vertex if it brings something new to the table
273
219k
    int newIndex = 0;
274
20.7M
    for( unsigned int a = 0; a < pMesh->mNumVertices; a++)  {
275
        // if the vertex is unused Do nothing
276
20.5M
        if (!usedVertexIndicesMask[a]) {
277
9.03M
            continue;
278
9.03M
        }
279
        // collect the vertex data
280
11.4M
        Vertex v(pMesh,a);
281
        // is the vertex already in the map?
282
11.4M
        auto it = vertex2Index.find(v);
283
        // if the vertex is not in the map then it is a new vertex add it.
284
11.4M
        if (it == vertex2Index.end()) {
285
            // this is a new vertex give it a new index
286
3.13M
            vertex2Index.emplace(v, newIndex);
287
            // keep track of its index and increment 1
288
3.13M
            replaceIndex[a] = newIndex++;
289
            // add the vertex to the unique vertices
290
3.13M
            uniqueVertices.push_back(a);
291
3.13M
            if (hasAnimMeshes) {
292
0
                for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
293
0
                    uniqueAnimatedVertices[animMeshIndex].emplace_back(a);
294
0
                }
295
0
            }
296
8.33M
        } else{
297
            // if the vertex is already there just find the replace index that is appropriate to it
298
      // mark it with JOINED_VERTICES_MARK
299
8.33M
            replaceIndex[a] = it->second | JOINED_VERTICES_MARK;
300
8.33M
        }
301
11.4M
    }
302
303
219k
    if (!DefaultLogger::isNullLogger() && DefaultLogger::get()->getLogSeverity() == Logger::VERBOSE)    {
304
0
        ASSIMP_LOG_VERBOSE_DEBUG(
305
0
            "Mesh ",meshIndex,
306
0
            " (",
307
0
            (pMesh->mName.length ? pMesh->mName.data : "unnamed"),
308
0
            ") | Verts in: ",pMesh->mNumVertices,
309
0
            " out: ",
310
0
            uniqueVertices.size(),
311
0
            " | ~",
312
0
            ((pMesh->mNumVertices - uniqueVertices.size()) / (float)pMesh->mNumVertices) * 100.f,
313
0
            "%"
314
0
        );
315
0
    }
316
317
219k
    updateXMeshVertices(pMesh, uniqueVertices);
318
219k
    if (hasAnimMeshes) {
319
0
        for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
320
0
            updateXMeshVertices(pMesh->mAnimMeshes[animMeshIndex], uniqueAnimatedVertices[animMeshIndex]);
321
0
        }
322
0
    }
323
324
    // adjust the indices in all faces
325
5.37M
    for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
326
5.15M
        aiFace& face = pMesh->mFaces[a];
327
18.6M
        for( unsigned int b = 0; b < face.mNumIndices; b++) {
328
13.4M
            face.mIndices[b] = replaceIndex[face.mIndices[b]] & ~JOINED_VERTICES_MARK;
329
13.4M
        }
330
5.15M
    }
331
332
    // adjust bone vertex weights.
333
333k
    for( int a = 0; a < (int)pMesh->mNumBones; a++) {
334
114k
        aiBone* bone = pMesh->mBones[a];
335
114k
        std::vector<aiVertexWeight> newWeights;
336
114k
        newWeights.reserve( bone->mNumWeights);
337
338
114k
        if (nullptr != bone->mWeights) {
339
2.32M
            for ( unsigned int b = 0; b < bone->mNumWeights; b++ ) {
340
2.21M
                const aiVertexWeight& ow = bone->mWeights[ b ];
341
                // if the vertex is a unique one, translate it
342
        // filter out joined vertices by JOINED_VERTICES_MARK.
343
2.21M
                if ( !( replaceIndex[ ow.mVertexId ] & JOINED_VERTICES_MARK ) ) {
344
546k
                    aiVertexWeight nw;
345
546k
                    nw.mVertexId = replaceIndex[ ow.mVertexId ];
346
546k
                    nw.mWeight = ow.mWeight;
347
546k
                    newWeights.push_back( nw );
348
546k
                }
349
2.21M
            }
350
114k
        } else {
351
0
            ASSIMP_LOG_ERROR( "X-Export: aiBone shall contain weights, but pointer to them is nullptr." );
352
0
        }
353
354
114k
        if (newWeights.size() > 0) {
355
            // kill the old and replace them with the translated weights
356
12.9k
            delete [] bone->mWeights;
357
12.9k
            bone->mNumWeights = (unsigned int)newWeights.size();
358
359
12.9k
            bone->mWeights = new aiVertexWeight[bone->mNumWeights];
360
12.9k
            memcpy( bone->mWeights, &newWeights[0], bone->mNumWeights * sizeof( aiVertexWeight));
361
12.9k
        }
362
114k
    }
363
219k
    return pMesh->mNumVertices;
364
219k
}
365
366
#endif // !! ASSIMP_BUILD_NO_JOINVERTICES_PROCESS