/src/assimp/code/PostProcessing/RemoveRedundantMaterials.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2025, 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 RemoveRedundantMaterials.cpp |
42 | | * @brief Implementation of the "RemoveRedundantMaterials" post processing step |
43 | | */ |
44 | | |
45 | | // internal headers |
46 | | #include "RemoveRedundantMaterials.h" |
47 | | #include <assimp/ParsingUtils.h> |
48 | | #include "ProcessHelper.h" |
49 | | #include "Material/MaterialSystem.h" |
50 | | #include <assimp/Exceptional.h> |
51 | | #include <stdio.h> |
52 | | |
53 | | using namespace Assimp; |
54 | | |
55 | | // ------------------------------------------------------------------------------------------------ |
56 | | // Constructor to be privately used by Importer |
57 | 1.77k | RemoveRedundantMatsProcess::RemoveRedundantMatsProcess() : mConfigFixedMaterials() {} |
58 | | |
59 | | // ------------------------------------------------------------------------------------------------ |
60 | | // Returns whether the processing step is present in the given flag field. |
61 | 483 | bool RemoveRedundantMatsProcess::IsActive( unsigned int pFlags) const { |
62 | 483 | return (pFlags & aiProcess_RemoveRedundantMaterials) != 0; |
63 | 483 | } |
64 | | |
65 | | // ------------------------------------------------------------------------------------------------ |
66 | | // Setup import properties |
67 | 319 | void RemoveRedundantMatsProcess::SetupProperties(const Importer* pImp) { |
68 | | // Get value of AI_CONFIG_PP_RRM_EXCLUDE_LIST |
69 | 319 | mConfigFixedMaterials = pImp->GetPropertyString(AI_CONFIG_PP_RRM_EXCLUDE_LIST,""); |
70 | 319 | } |
71 | | |
72 | | // ------------------------------------------------------------------------------------------------ |
73 | | // Executes the post processing step on the given imported data. |
74 | 319 | void RemoveRedundantMatsProcess::Execute( aiScene* pScene) { |
75 | 319 | ASSIMP_LOG_DEBUG("RemoveRedundantMatsProcess begin"); |
76 | | |
77 | 319 | unsigned int redundantRemoved = 0, unreferencedRemoved = 0; |
78 | 319 | if (pScene->mNumMaterials == 0) { |
79 | 88 | return; |
80 | 88 | } |
81 | | |
82 | | // Find out which materials are referenced by meshes |
83 | 231 | std::vector<bool> abReferenced(pScene->mNumMaterials,false); |
84 | 5.78k | for (unsigned int i = 0;i < pScene->mNumMeshes;++i) { |
85 | 5.55k | abReferenced[pScene->mMeshes[i]->mMaterialIndex] = true; |
86 | 5.55k | } |
87 | | |
88 | | // If a list of materials to be excluded was given, match the list with |
89 | | // our imported materials and 'salt' all positive matches to ensure that |
90 | | // we get unique hashes later. |
91 | 231 | if (mConfigFixedMaterials.length()) { |
92 | 0 | std::list<std::string> strings; |
93 | 0 | ConvertListToStrings(mConfigFixedMaterials,strings); |
94 | |
|
95 | 0 | for (unsigned int i = 0; i < pScene->mNumMaterials;++i) { |
96 | 0 | aiMaterial* mat = pScene->mMaterials[i]; |
97 | 0 | ai_assert(mat != nullptr); |
98 | 0 | aiString name; |
99 | 0 | mat->Get(AI_MATKEY_NAME,name); |
100 | |
|
101 | 0 | if (name.length != 0) { |
102 | 0 | std::list<std::string>::const_iterator it = std::find(strings.begin(), strings.end(), name.data); |
103 | 0 | if (it != strings.end()) { |
104 | | // Our brilliant 'salt': A single material property with ~ as first |
105 | | // character to mark it as internal and temporary. |
106 | 0 | const int dummy = 1; |
107 | 0 | ((aiMaterial*)mat)->AddProperty(&dummy,1,"~RRM.UniqueMaterial",0,0); |
108 | | |
109 | | // Keep this material even if no mesh references it |
110 | 0 | abReferenced[i] = true; |
111 | 0 | ASSIMP_LOG_VERBOSE_DEBUG( "Found positive match in exclusion list: \'", name.data, "\'"); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | // TODO: re-implement this algorithm to work in-place |
118 | 231 | unsigned int *aiMappingTable = new unsigned int[pScene->mNumMaterials]; |
119 | 1.32k | for ( unsigned int i=0; i<pScene->mNumMaterials; i++ ) { |
120 | 1.09k | aiMappingTable[ i ] = 0; |
121 | 1.09k | } |
122 | 231 | unsigned int iNewNum = 0; |
123 | | |
124 | | // Iterate through all materials and calculate a hash for them |
125 | | // store all hashes in a list and so a quick search whether |
126 | | // we do already have a specific hash. This allows us to |
127 | | // determine which materials are identical. |
128 | 231 | uint32_t *aiHashes = new uint32_t[ pScene->mNumMaterials ]; |
129 | 1.32k | for (unsigned int i = 0; i < pScene->mNumMaterials;++i) { |
130 | | // No mesh is referencing this material, remove it. |
131 | 1.09k | if (!abReferenced[i]) { |
132 | 325 | ++unreferencedRemoved; |
133 | 325 | delete pScene->mMaterials[i]; |
134 | 325 | pScene->mMaterials[i] = nullptr; |
135 | 325 | continue; |
136 | 325 | } |
137 | | |
138 | | // Check all previously mapped materials for a matching hash. |
139 | | // On a match we can delete this material and just make it ref to the same index. |
140 | 766 | uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]); |
141 | 1.91k | for (unsigned int a = 0; a < i;++a) { |
142 | 1.65k | if (abReferenced[a] && me == aiHashes[a]) { |
143 | 500 | ++redundantRemoved; |
144 | 500 | me = 0; |
145 | 500 | aiMappingTable[i] = aiMappingTable[a]; |
146 | 500 | delete pScene->mMaterials[i]; |
147 | 500 | pScene->mMaterials[i] = nullptr; |
148 | 500 | break; |
149 | 500 | } |
150 | 1.65k | } |
151 | | // This is a new material that is referenced, add to the map. |
152 | 766 | if (me) { |
153 | 266 | aiMappingTable[i] = iNewNum++; |
154 | 266 | } |
155 | 766 | } |
156 | | // If the new material count differs from the original, |
157 | | // we need to rebuild the material list and remap mesh material indexes. |
158 | 231 | if (iNewNum < 1) { |
159 | 2 | delete [] aiMappingTable; |
160 | 2 | delete [] aiHashes; |
161 | 2 | pScene->mNumMaterials = 0; |
162 | 2 | return; |
163 | 2 | } |
164 | 229 | if (iNewNum != pScene->mNumMaterials) { |
165 | 124 | ai_assert(iNewNum > 0); |
166 | 124 | aiMaterial** ppcMaterials = new aiMaterial*[iNewNum]; |
167 | 124 | ::memset(ppcMaterials,0,sizeof(void*)*iNewNum); |
168 | 1.10k | for (unsigned int p = 0; p < pScene->mNumMaterials;++p) { |
169 | | // if the material is not referenced ... remove it |
170 | 981 | if (!abReferenced[p]) { |
171 | 321 | continue; |
172 | 321 | } |
173 | | |
174 | | // generate new names for modified materials that had no names |
175 | 660 | const unsigned int idx = aiMappingTable[p]; |
176 | 660 | if (ppcMaterials[idx]) { |
177 | 500 | aiString sz; |
178 | 500 | if( ppcMaterials[idx]->Get(AI_MATKEY_NAME, sz) != AI_SUCCESS ) { |
179 | 0 | sz.length = ::ai_snprintf(sz.data, AI_MAXLEN,"JoinedMaterial_#%u",p); |
180 | 0 | ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); |
181 | 0 | } |
182 | 500 | } else { |
183 | 160 | ppcMaterials[idx] = pScene->mMaterials[p]; |
184 | 160 | } |
185 | 660 | } |
186 | | // update all material indices |
187 | 5.14k | for (unsigned int p = 0; p < pScene->mNumMeshes;++p) { |
188 | 5.01k | aiMesh* mesh = pScene->mMeshes[p]; |
189 | 5.01k | ai_assert(nullptr != mesh); |
190 | 5.01k | mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex]; |
191 | 5.01k | } |
192 | | // delete the old material list |
193 | 124 | delete[] pScene->mMaterials; |
194 | 124 | pScene->mMaterials = ppcMaterials; |
195 | 124 | pScene->mNumMaterials = iNewNum; |
196 | 124 | } |
197 | | // delete temporary storage |
198 | 229 | delete[] aiHashes; |
199 | 229 | delete[] aiMappingTable; |
200 | | |
201 | 229 | if (redundantRemoved == 0 && unreferencedRemoved == 0) { |
202 | 105 | ASSIMP_LOG_DEBUG("RemoveRedundantMatsProcess finished "); |
203 | 124 | } else { |
204 | 124 | ASSIMP_LOG_INFO("RemoveRedundantMatsProcess finished. Removed ", redundantRemoved, " redundant and ", |
205 | 124 | unreferencedRemoved, " unused materials."); |
206 | 124 | } |
207 | 229 | } |