Coverage Report

Created: 2026-09-14 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/PostProcessing/ConvertToLHProcess.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  MakeLeftHandedProcess.cpp
43
 *  @brief Implementation of the post processing step to convert all
44
 *  imported data to a left-handed coordinate system.
45
 *
46
 *  Face order & UV flip are also implemented here, for the sake of a
47
 *  better location.
48
 */
49
50
#include "ConvertToLHProcess.h"
51
#include <assimp/postprocess.h>
52
#include <assimp/scene.h>
53
#include <assimp/DefaultLogger.hpp>
54
55
using namespace Assimp;
56
57
#ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
58
59
namespace {
60
61
template <typename aiMeshType>
62
55.3k
void flipUVs(aiMeshType *pMesh) {
63
55.3k
    if (pMesh == nullptr) {
64
0
        return;
65
0
    }
66
    // mirror texture y coordinate
67
70.3k
    for (unsigned int tcIdx = 0; tcIdx < AI_MAX_NUMBER_OF_TEXTURECOORDS; tcIdx++) {
68
70.3k
        if (!pMesh->HasTextureCoords(tcIdx)) {
69
55.3k
            break;
70
55.3k
        }
71
72
784k
        for (unsigned int vIdx = 0; vIdx < pMesh->mNumVertices; vIdx++) {
73
769k
            pMesh->mTextureCoords[tcIdx][vIdx].y = 1.0f - pMesh->mTextureCoords[tcIdx][vIdx].y;
74
769k
        }
75
15.0k
    }
76
55.3k
}
ConvertToLHProcess.cpp:void (anonymous namespace)::flipUVs<aiMesh>(aiMesh*)
Line
Count
Source
62
55.3k
void flipUVs(aiMeshType *pMesh) {
63
55.3k
    if (pMesh == nullptr) {
64
0
        return;
65
0
    }
66
    // mirror texture y coordinate
67
70.3k
    for (unsigned int tcIdx = 0; tcIdx < AI_MAX_NUMBER_OF_TEXTURECOORDS; tcIdx++) {
68
70.3k
        if (!pMesh->HasTextureCoords(tcIdx)) {
69
55.3k
            break;
70
55.3k
        }
71
72
784k
        for (unsigned int vIdx = 0; vIdx < pMesh->mNumVertices; vIdx++) {
73
769k
            pMesh->mTextureCoords[tcIdx][vIdx].y = 1.0f - pMesh->mTextureCoords[tcIdx][vIdx].y;
74
769k
        }
75
15.0k
    }
76
55.3k
}
Unexecuted instantiation: ConvertToLHProcess.cpp:void (anonymous namespace)::flipUVs<aiAnimMesh>(aiAnimMesh*)
77
78
} // namespace
79
80
// ------------------------------------------------------------------------------------------------
81
// Returns whether the processing step is present in the given flag field.
82
182k
bool MakeLeftHandedProcess::IsActive(unsigned int pFlags) const {
83
182k
    return 0 != (pFlags & aiProcess_MakeLeftHanded);
84
182k
}
85
86
// ------------------------------------------------------------------------------------------------
87
// Executes the post processing step on the given imported data.
88
12.1k
void MakeLeftHandedProcess::Execute(aiScene *pScene) {
89
    // Check for an existent root node to proceed
90
12.1k
    ai_assert(pScene->mRootNode != nullptr);
91
12.1k
    ASSIMP_LOG_DEBUG("MakeLeftHandedProcess begin");
92
93
    // recursively convert all the nodes
94
12.1k
    ProcessNode(pScene->mRootNode, aiMatrix4x4());
95
96
    // process the meshes accordingly
97
68.5k
    for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
98
56.3k
        ProcessMesh(pScene->mMeshes[a]);
99
56.3k
    }
100
101
    // process the materials accordingly
102
23.3k
    for (unsigned int a = 0; a < pScene->mNumMaterials; ++a) {
103
11.1k
        ProcessMaterial(pScene->mMaterials[a]);
104
11.1k
    }
105
106
    // transform all animation channels as well
107
14.2k
    for (unsigned int a = 0; a < pScene->mNumAnimations; a++) {
108
2.07k
        aiAnimation *anim = pScene->mAnimations[a];
109
10.1k
        for (unsigned int b = 0; b < anim->mNumChannels; b++) {
110
8.11k
            aiNodeAnim *nodeAnim = anim->mChannels[b];
111
8.11k
            ProcessAnimation(nodeAnim);
112
8.11k
        }
113
2.07k
    }
114
115
    // process the cameras accordingly
116
17.9k
    for( unsigned int a = 0; a < pScene->mNumCameras; ++a)
117
5.83k
    {
118
5.83k
        ProcessCamera(pScene->mCameras[a]);
119
5.83k
    }
120
12.1k
    ASSIMP_LOG_DEBUG("MakeLeftHandedProcess finished");
121
12.1k
}
122
123
// ------------------------------------------------------------------------------------------------
124
// Recursively converts a node, all of its children and all of its meshes
125
343k
void MakeLeftHandedProcess::ProcessNode(aiNode *pNode, const aiMatrix4x4 &pParentGlobalRotation) {
126
    // mirror all base vectors at the local Z axis
127
343k
    pNode->mTransformation.c1 = -pNode->mTransformation.c1;
128
343k
    pNode->mTransformation.c2 = -pNode->mTransformation.c2;
129
343k
    pNode->mTransformation.c3 = -pNode->mTransformation.c3;
130
343k
    pNode->mTransformation.c4 = -pNode->mTransformation.c4;
131
132
    // now invert the Z axis again to keep the matrix determinant positive.
133
    // The local meshes will be inverted accordingly so that the result should look just fine again.
134
343k
    pNode->mTransformation.a3 = -pNode->mTransformation.a3;
135
343k
    pNode->mTransformation.b3 = -pNode->mTransformation.b3;
136
343k
    pNode->mTransformation.c3 = -pNode->mTransformation.c3;
137
343k
    pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways...
138
139
    // continue for all children
140
674k
    for (size_t a = 0; a < pNode->mNumChildren; ++a) {
141
331k
        ProcessNode(pNode->mChildren[a], pParentGlobalRotation * pNode->mTransformation);
142
331k
    }
143
343k
}
144
145
// ------------------------------------------------------------------------------------------------
146
// Converts a single mesh to left handed coordinates.
147
56.3k
void MakeLeftHandedProcess::ProcessMesh(aiMesh *pMesh) {
148
56.3k
    if (nullptr == pMesh) {
149
0
        ASSIMP_LOG_ERROR("Nullptr to mesh found.");
150
0
        return;
151
0
    }
152
    // mirror positions, normals and stuff along the Z axis
153
3.88M
    for (size_t a = 0; a < pMesh->mNumVertices; ++a) {
154
3.83M
        pMesh->mVertices[a].z *= -1.0f;
155
3.83M
        if (pMesh->HasNormals()) {
156
3.80M
            pMesh->mNormals[a].z *= -1.0f;
157
3.80M
        }
158
3.83M
        if (pMesh->HasTangentsAndBitangents()) {
159
696k
            pMesh->mTangents[a].z *= -1.0f;
160
696k
            pMesh->mBitangents[a].z *= -1.0f;
161
696k
        }
162
3.83M
    }
163
164
    // mirror anim meshes positions, normals and stuff along the Z axis
165
56.3k
    for (size_t m = 0; m < pMesh->mNumAnimMeshes; ++m) {
166
0
        for (size_t a = 0; a < pMesh->mAnimMeshes[m]->mNumVertices; ++a) {
167
0
            pMesh->mAnimMeshes[m]->mVertices[a].z *= -1.0f;
168
0
            if (pMesh->mAnimMeshes[m]->HasNormals()) {
169
0
                pMesh->mAnimMeshes[m]->mNormals[a].z *= -1.0f;
170
0
            }
171
0
            if (pMesh->mAnimMeshes[m]->HasTangentsAndBitangents()) {
172
0
                pMesh->mAnimMeshes[m]->mTangents[a].z *= -1.0f;
173
0
                pMesh->mAnimMeshes[m]->mBitangents[a].z *= -1.0f;
174
0
            }
175
0
        }
176
0
    }
177
178
    // mirror offset matrices of all bones
179
61.2k
    for (size_t a = 0; a < pMesh->mNumBones; ++a) {
180
4.92k
        aiBone *bone = pMesh->mBones[a];
181
4.92k
        bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3;
182
4.92k
        bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3;
183
4.92k
        bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3;
184
4.92k
        bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1;
185
4.92k
        bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2;
186
4.92k
        bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4;
187
4.92k
    }
188
189
    // mirror bitangents as well as they're derived from the texture coords
190
56.3k
    if (pMesh->HasTangentsAndBitangents()) {
191
710k
        for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
192
696k
            pMesh->mBitangents[a] *= -1.0f;
193
14.7k
    }
194
56.3k
}
195
196
// ------------------------------------------------------------------------------------------------
197
// Converts a single material to left handed coordinates.
198
11.1k
void MakeLeftHandedProcess::ProcessMaterial(aiMaterial *_mat) {
199
11.1k
    if (nullptr == _mat) {
200
0
        ASSIMP_LOG_ERROR("Nullptr to aiMaterial found.");
201
0
        return;
202
0
    }
203
204
11.1k
    aiMaterial *mat = (aiMaterial *)_mat;
205
125k
    for (unsigned int a = 0; a < mat->mNumProperties; ++a) {
206
114k
        aiMaterialProperty *prop = mat->mProperties[a];
207
208
        // Mapping axis for UV mappings?
209
114k
        if (!::strcmp(prop->mKey.data, "$tex.mapaxis")) {
210
59
            ai_assert(prop->mDataLength >= sizeof(aiVector3D)); // something is wrong with the validation if we end up here
211
59
            aiVector3D *pff = (aiVector3D *)prop->mData;
212
59
            pff->z *= -1.f;
213
59
        }
214
114k
    }
215
11.1k
}
216
217
// ------------------------------------------------------------------------------------------------
218
// Converts the given animation to LH coordinates.
219
8.11k
void MakeLeftHandedProcess::ProcessAnimation(aiNodeAnim *pAnim) {
220
    // position keys
221
51.6k
    for (unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
222
43.5k
        pAnim->mPositionKeys[a].mValue.z *= -1.0f;
223
224
    // rotation keys
225
104k
    for (unsigned int a = 0; a < pAnim->mNumRotationKeys; a++) {
226
96.4k
        pAnim->mRotationKeys[a].mValue.x *= -1.0f;
227
96.4k
        pAnim->mRotationKeys[a].mValue.y *= -1.0f;
228
96.4k
    }
229
8.11k
}
230
231
// ------------------------------------------------------------------------------------------------
232
// Converts a single camera to left handed coordinates.
233
void MakeLeftHandedProcess::ProcessCamera( aiCamera* pCam)
234
5.83k
{
235
5.83k
    pCam->mLookAt = ai_real(2.0f) * pCam->mPosition - pCam->mLookAt;
236
5.83k
}
237
238
#endif // !!  ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
239
#ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS
240
// # FlipUVsProcess
241
242
// ------------------------------------------------------------------------------------------------
243
// Constructor to be privately used by Importer
244
180k
FlipUVsProcess::FlipUVsProcess() = default;
245
246
// ------------------------------------------------------------------------------------------------
247
// Destructor, private as well
248
180k
FlipUVsProcess::~FlipUVsProcess() = default;
249
250
// ------------------------------------------------------------------------------------------------
251
// Returns whether the processing step is present in the given flag field.
252
175k
bool FlipUVsProcess::IsActive(unsigned int pFlags) const {
253
175k
    return 0 != (pFlags & aiProcess_FlipUVs);
254
175k
}
255
256
// ------------------------------------------------------------------------------------------------
257
// Executes the post processing step on the given imported data.
258
9.70k
void FlipUVsProcess::Execute(aiScene *pScene) {
259
9.70k
    ASSIMP_LOG_DEBUG("FlipUVsProcess begin");
260
65.0k
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i)
261
55.3k
        ProcessMesh(pScene->mMeshes[i]);
262
263
19.4k
    for (unsigned int i = 0; i < pScene->mNumMaterials; ++i)
264
9.72k
        ProcessMaterial(pScene->mMaterials[i]);
265
9.70k
    ASSIMP_LOG_DEBUG("FlipUVsProcess finished");
266
9.70k
}
267
268
// ------------------------------------------------------------------------------------------------
269
// Converts a single material
270
9.72k
void FlipUVsProcess::ProcessMaterial(aiMaterial *_mat) {
271
9.72k
    aiMaterial *mat = (aiMaterial *)_mat;
272
106k
    for (unsigned int a = 0; a < mat->mNumProperties; ++a) {
273
96.5k
        aiMaterialProperty *prop = mat->mProperties[a];
274
96.5k
        if (!prop) {
275
0
            ASSIMP_LOG_VERBOSE_DEBUG("Property is null");
276
0
            continue;
277
0
        }
278
279
        // UV transformation key?
280
96.5k
        if (!::strcmp(prop->mKey.data, "$tex.uvtrafo")) {
281
932
            ai_assert(prop->mDataLength >= sizeof(aiUVTransform)); // something is wrong with the validation if we end up here
282
932
            aiUVTransform *uv = (aiUVTransform *)prop->mData;
283
284
            // just flip it, that's everything
285
932
            uv->mTranslation.y *= -1.f;
286
932
            uv->mRotation *= -1.f;
287
932
        }
288
96.5k
    }
289
9.72k
}
290
291
// ------------------------------------------------------------------------------------------------
292
// Converts a single mesh
293
55.3k
void FlipUVsProcess::ProcessMesh(aiMesh *pMesh) {
294
55.3k
    flipUVs(pMesh);
295
55.3k
    for (unsigned int idx = 0; idx < pMesh->mNumAnimMeshes; idx++) {
296
0
        flipUVs(pMesh->mAnimMeshes[idx]);
297
0
    }
298
55.3k
}
299
300
#endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS
301
#ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
302
// # FlipWindingOrderProcess
303
304
// ------------------------------------------------------------------------------------------------
305
// Returns whether the processing step is present in the given flag field.
306
175k
bool FlipWindingOrderProcess::IsActive(unsigned int pFlags) const {
307
175k
    return 0 != (pFlags & aiProcess_FlipWindingOrder);
308
175k
}
309
310
// ------------------------------------------------------------------------------------------------
311
// Executes the post processing step on the given imported data.
312
12.1k
void FlipWindingOrderProcess::Execute(aiScene *pScene) {
313
12.1k
    ASSIMP_LOG_DEBUG("FlipWindingOrderProcess begin");
314
68.5k
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i)
315
56.3k
        ProcessMesh(pScene->mMeshes[i]);
316
12.1k
    ASSIMP_LOG_DEBUG("FlipWindingOrderProcess finished");
317
12.1k
}
318
319
// ------------------------------------------------------------------------------------------------
320
// Converts a single mesh
321
56.3k
void FlipWindingOrderProcess::ProcessMesh(aiMesh *pMesh) {
322
    // invert the order of all faces in this mesh
323
1.41M
    for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
324
1.35M
        aiFace &face = pMesh->mFaces[a];
325
2.67M
        for (unsigned int b = 0; b < face.mNumIndices / 2; b++) {
326
1.31M
            std::swap(face.mIndices[b], face.mIndices[face.mNumIndices - 1 - b]);
327
1.31M
        }
328
1.35M
    }
329
330
    // invert the order of all components in this mesh anim meshes
331
56.3k
    for (unsigned int m = 0; m < pMesh->mNumAnimMeshes; m++) {
332
0
        aiAnimMesh *animMesh = pMesh->mAnimMeshes[m];
333
0
        unsigned int numVertices = animMesh->mNumVertices;
334
0
        if (animMesh->HasPositions()) {
335
0
            for (unsigned int a = 0; a < numVertices; a++) {
336
0
                std::swap(animMesh->mVertices[a], animMesh->mVertices[numVertices - 1 - a]);
337
0
            }
338
0
        }
339
0
        if (animMesh->HasNormals()) {
340
0
            for (unsigned int a = 0; a < numVertices; a++) {
341
0
                std::swap(animMesh->mNormals[a], animMesh->mNormals[numVertices - 1 - a]);
342
0
            }
343
0
        }
344
0
        for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++) {
345
0
            if (animMesh->HasTextureCoords(i)) {
346
0
                for (unsigned int a = 0; a < numVertices; a++) {
347
0
                    std::swap(animMesh->mTextureCoords[i][a], animMesh->mTextureCoords[i][numVertices - 1 - a]);
348
0
                }
349
0
            }
350
0
        }
351
0
        if (animMesh->HasTangentsAndBitangents()) {
352
0
            for (unsigned int a = 0; a < numVertices; a++) {
353
0
                std::swap(animMesh->mTangents[a], animMesh->mTangents[numVertices - 1 - a]);
354
0
                std::swap(animMesh->mBitangents[a], animMesh->mBitangents[numVertices - 1 - a]);
355
0
            }
356
0
        }
357
0
        for (unsigned int v = 0; v < AI_MAX_NUMBER_OF_COLOR_SETS; v++) {
358
0
            if (animMesh->HasVertexColors(v)) {
359
0
                for (unsigned int a = 0; a < numVertices; a++) {
360
0
                    std::swap(animMesh->mColors[v][a], animMesh->mColors[v][numVertices - 1 - a]);
361
0
                }
362
0
            }
363
0
        }
364
0
    }
365
56.3k
}
366
367
#endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS