Coverage Report

Created: 2026-02-05 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/PostProcessing/RemoveVCProcess.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
/** @file Implementation of the post processing step to remove
42
 *        any parts of the mesh structure from the imported data.
43
*/
44
45
#include "RemoveVCProcess.h"
46
#include <assimp/postprocess.h>
47
#include <assimp/scene.h>
48
#include <assimp/DefaultLogger.hpp>
49
50
using namespace Assimp;
51
52
// ------------------------------------------------------------------------------------------------
53
// Constructor to be privately used by Importer
54
RemoveVCProcess::RemoveVCProcess() :
55
31.9k
        configDeleteFlags(), mScene() {}
56
57
// ------------------------------------------------------------------------------------------------
58
// Returns whether the processing step is present in the given flag field.
59
10.4k
bool RemoveVCProcess::IsActive(unsigned int pFlags) const {
60
10.4k
    return (pFlags & aiProcess_RemoveComponent) != 0;
61
10.4k
}
62
63
// ------------------------------------------------------------------------------------------------
64
// Small helper function to delete all elements in a T** array using delete
65
template <typename T>
66
0
inline void ArrayDelete(T **&in, unsigned int &num) {
67
0
    for (unsigned int i = 0; i < num; ++i)
68
0
        delete in[i];
69
70
0
    delete[] in;
71
0
    in = nullptr;
72
0
    num = 0;
73
0
}
Unexecuted instantiation: void ArrayDelete<aiAnimation>(aiAnimation**&, unsigned int&)
Unexecuted instantiation: void ArrayDelete<aiTexture>(aiTexture**&, unsigned int&)
Unexecuted instantiation: void ArrayDelete<aiLight>(aiLight**&, unsigned int&)
Unexecuted instantiation: void ArrayDelete<aiCamera>(aiCamera**&, unsigned int&)
Unexecuted instantiation: void ArrayDelete<aiMesh>(aiMesh**&, unsigned int&)
Unexecuted instantiation: void ArrayDelete<aiBone>(aiBone**&, unsigned int&)
74
75
// ------------------------------------------------------------------------------------------------
76
// Executes the post processing step on the given imported data.
77
0
void RemoveVCProcess::Execute(aiScene *pScene) {
78
0
    ASSIMP_LOG_DEBUG("RemoveVCProcess begin");
79
0
    bool bHas = false; //,bMasked = false;
80
81
0
    mScene = pScene;
82
83
    // handle animations
84
0
    if (configDeleteFlags & aiComponent_ANIMATIONS) {
85
86
0
        bHas = true;
87
0
        ArrayDelete(pScene->mAnimations, pScene->mNumAnimations);
88
0
    }
89
90
    // handle textures
91
0
    if (configDeleteFlags & aiComponent_TEXTURES) {
92
0
        bHas = true;
93
0
        ArrayDelete(pScene->mTextures, pScene->mNumTextures);
94
0
    }
95
96
    // handle materials
97
0
    if (configDeleteFlags & aiComponent_MATERIALS && pScene->mNumMaterials) {
98
0
        bHas = true;
99
0
        for (unsigned int i = 1; i < pScene->mNumMaterials; ++i)
100
0
            delete pScene->mMaterials[i];
101
102
0
        pScene->mNumMaterials = 1;
103
0
        aiMaterial *helper = (aiMaterial *)pScene->mMaterials[0];
104
0
        ai_assert(nullptr != helper);
105
0
        helper->Clear();
106
107
        // gray
108
0
        aiColor3D clr(0.6f, 0.6f, 0.6f);
109
0
        helper->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
110
111
        // add a small ambient color value
112
0
        clr = aiColor3D(0.05f, 0.05f, 0.05f);
113
0
        helper->AddProperty(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
114
115
0
        aiString s;
116
0
        s.Set("Dummy_MaterialsRemoved");
117
0
        helper->AddProperty(&s, AI_MATKEY_NAME);
118
0
    }
119
120
    // handle light sources
121
0
    if (configDeleteFlags & aiComponent_LIGHTS) {
122
0
        bHas = true;
123
0
        ArrayDelete(pScene->mLights, pScene->mNumLights);
124
0
    }
125
126
    // handle camneras
127
0
    if (configDeleteFlags & aiComponent_CAMERAS) {
128
0
        bHas = true;
129
0
        ArrayDelete(pScene->mCameras, pScene->mNumCameras);
130
0
    }
131
132
    // handle meshes
133
0
    if (configDeleteFlags & aiComponent_MESHES) {
134
0
        bHas = true;
135
0
        ArrayDelete(pScene->mMeshes, pScene->mNumMeshes);
136
0
    } else {
137
0
        for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
138
0
            if (ProcessMesh(pScene->mMeshes[a]))
139
0
                bHas = true;
140
0
        }
141
0
    }
142
143
    // now check whether the result is still a full scene
144
0
    if (!pScene->mNumMeshes || !pScene->mNumMaterials) {
145
0
        pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
146
0
        ASSIMP_LOG_DEBUG("Setting AI_SCENE_FLAGS_INCOMPLETE flag");
147
148
        // If we have no meshes anymore we should also clear another flag ...
149
0
        if (!pScene->mNumMeshes)
150
0
            pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
151
0
    }
152
153
0
    if (bHas) {
154
0
        ASSIMP_LOG_INFO("RemoveVCProcess finished. Data structure cleanup has been done.");
155
0
    } else {
156
0
        ASSIMP_LOG_DEBUG("RemoveVCProcess finished. Nothing to be done ...");
157
0
    }
158
0
}
159
160
// ------------------------------------------------------------------------------------------------
161
// Setup configuration properties for the step
162
0
void RemoveVCProcess::SetupProperties(const Importer *pImp) {
163
0
    configDeleteFlags = pImp->GetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, 0x0);
164
0
    if (!configDeleteFlags) {
165
0
        ASSIMP_LOG_WARN("RemoveVCProcess: AI_CONFIG_PP_RVC_FLAGS is zero.");
166
0
    }
167
0
}
168
169
// ------------------------------------------------------------------------------------------------
170
// Executes the post processing step on the given imported data.
171
0
bool RemoveVCProcess::ProcessMesh(aiMesh *pMesh) {
172
0
    bool ret = false;
173
174
    // if all materials have been deleted let the material
175
    // index of the mesh point to the created default material
176
0
    if (configDeleteFlags & aiComponent_MATERIALS)
177
0
        pMesh->mMaterialIndex = 0;
178
179
    // handle normals
180
0
    if (configDeleteFlags & aiComponent_NORMALS && pMesh->mNormals) {
181
0
        delete[] pMesh->mNormals;
182
0
        pMesh->mNormals = nullptr;
183
0
        ret = true;
184
0
    }
185
186
    // handle tangents and bitangents
187
0
    if (configDeleteFlags & aiComponent_TANGENTS_AND_BITANGENTS && pMesh->mTangents) {
188
0
        delete[] pMesh->mTangents;
189
0
        pMesh->mTangents = nullptr;
190
191
0
        delete[] pMesh->mBitangents;
192
0
        pMesh->mBitangents = nullptr;
193
0
        ret = true;
194
0
    }
195
196
    // handle texture coordinates
197
0
    bool b = (0 != (configDeleteFlags & aiComponent_TEXCOORDS));
198
0
    for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++real) {
199
0
        if (!pMesh->mTextureCoords[i]) break;
200
0
        if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b) {
201
0
            delete[] pMesh->mTextureCoords[i];
202
0
            pMesh->mTextureCoords[i] = nullptr;
203
0
            ret = true;
204
205
0
            if (!b) {
206
                // collapse the rest of the array
207
0
                for (unsigned int a = i + 1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
208
0
                    pMesh->mTextureCoords[a - 1] = pMesh->mTextureCoords[a];
209
210
0
                pMesh->mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS - 1] = nullptr;
211
0
                continue;
212
0
            }
213
0
        }
214
0
        ++i;
215
0
    }
216
217
    // handle vertex colors
218
0
    b = (0 != (configDeleteFlags & aiComponent_COLORS));
219
0
    for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_COLOR_SETS; ++real) {
220
0
        if (!pMesh->mColors[i]) break;
221
0
        if (configDeleteFlags & aiComponent_COLORSn(i) || b) {
222
0
            delete[] pMesh->mColors[i];
223
0
            pMesh->mColors[i] = nullptr;
224
0
            ret = true;
225
226
0
            if (!b) {
227
                // collapse the rest of the array
228
0
                for (unsigned int a = i + 1; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a)
229
0
                    pMesh->mColors[a - 1] = pMesh->mColors[a];
230
231
0
                pMesh->mColors[AI_MAX_NUMBER_OF_COLOR_SETS - 1] = nullptr;
232
0
                continue;
233
0
            }
234
0
        }
235
0
        ++i;
236
0
    }
237
238
    // handle bones
239
0
    if (configDeleteFlags & aiComponent_BONEWEIGHTS && pMesh->mBones) {
240
0
        ArrayDelete(pMesh->mBones, pMesh->mNumBones);
241
0
        ret = true;
242
0
    }
243
0
    return ret;
244
0
}