vrshoot

view libs/assimp/RemoveVCProcess.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
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:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
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.
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.
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 */
45 #include "AssimpPCH.h"
46 #include "RemoveVCProcess.h"
48 using namespace Assimp;
51 // ------------------------------------------------------------------------------------------------
52 // Constructor to be privately used by Importer
53 RemoveVCProcess::RemoveVCProcess()
54 {}
56 // ------------------------------------------------------------------------------------------------
57 // Destructor, private as well
58 RemoveVCProcess::~RemoveVCProcess()
59 {}
61 // ------------------------------------------------------------------------------------------------
62 // Returns whether the processing step is present in the given flag field.
63 bool RemoveVCProcess::IsActive( unsigned int pFlags) const
64 {
65 return (pFlags & aiProcess_RemoveComponent) != 0;
66 }
68 // ------------------------------------------------------------------------------------------------
69 // Small helper function to delete all elements in a T** aray using delete
70 template <typename T>
71 inline void ArrayDelete(T**& in, unsigned int& num)
72 {
73 for (unsigned int i = 0; i < num; ++i)
74 delete in[i];
76 delete[] in;
77 in = NULL;
78 num = 0;
79 }
81 #if 0
82 // ------------------------------------------------------------------------------------------------
83 // Updates the node graph - removes all nodes which have the "remove" flag set and the
84 // "don't remove" flag not set. Nodes with meshes are never deleted.
85 bool UpdateNodeGraph(aiNode* node,std::list<aiNode*>& childsOfParent,bool root)
86 {
87 register bool b = false;
89 std::list<aiNode*> mine;
90 for (unsigned int i = 0; i < node->mNumChildren;++i)
91 {
92 if(UpdateNodeGraph(node->mChildren[i],mine,false))
93 b = true;
94 }
96 // somewhat tricky ... mNumMeshes must be originally 0 and MSB2 may not be set,
97 // so we can do a simple comparison against MSB here
98 if (!root && AI_RC_UINT_MSB == node->mNumMeshes )
99 {
100 // this node needs to be removed
101 if(node->mNumChildren)
102 {
103 childsOfParent.insert(childsOfParent.end(),mine.begin(),mine.end());
105 // set all children to NULL to make sure they are not deleted when we delete ourself
106 for (unsigned int i = 0; i < node->mNumChildren;++i)
107 node->mChildren[i] = NULL;
108 }
109 b = true;
110 delete node;
111 }
112 else
113 {
114 AI_RC_UNMASK(node->mNumMeshes);
115 childsOfParent.push_back(node);
117 if (b)
118 {
119 // reallocate the array of our children here
120 node->mNumChildren = (unsigned int)mine.size();
121 aiNode** const children = new aiNode*[mine.size()];
122 aiNode** ptr = children;
124 for (std::list<aiNode*>::iterator it = mine.begin(), end = mine.end();
125 it != end; ++it)
126 {
127 *ptr++ = *it;
128 }
129 delete[] node->mChildren;
130 node->mChildren = children;
131 return false;
132 }
133 }
134 return b;
135 }
136 #endif
138 // ------------------------------------------------------------------------------------------------
139 // Executes the post processing step on the given imported data.
140 void RemoveVCProcess::Execute( aiScene* pScene)
141 {
142 DefaultLogger::get()->debug("RemoveVCProcess begin");
143 bool bHas = false; //,bMasked = false;
145 mScene = pScene;
147 // handle animations
148 if ( configDeleteFlags & aiComponent_ANIMATIONS)
149 {
151 bHas = true;
152 ArrayDelete(pScene->mAnimations,pScene->mNumAnimations);
153 }
155 // handle textures
156 if ( configDeleteFlags & aiComponent_TEXTURES)
157 {
158 bHas = true;
159 ArrayDelete(pScene->mTextures,pScene->mNumTextures);
160 }
162 // handle materials
163 if ( configDeleteFlags & aiComponent_MATERIALS && pScene->mNumMaterials)
164 {
165 bHas = true;
166 for (unsigned int i = 1;i < pScene->mNumMaterials;++i)
167 delete pScene->mMaterials[i];
169 pScene->mNumMaterials = 1;
170 aiMaterial* helper = (aiMaterial*) pScene->mMaterials[0];
171 ai_assert(NULL != helper);
172 helper->Clear();
174 // gray
175 aiColor3D clr(0.6f,0.6f,0.6f);
176 helper->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
178 // add a small ambient color value
179 clr = aiColor3D(0.05f,0.05f,0.05f);
180 helper->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
182 aiString s;
183 s.Set("Dummy_MaterialsRemoved");
184 helper->AddProperty(&s,AI_MATKEY_NAME);
185 }
187 // handle light sources
188 if ( configDeleteFlags & aiComponent_LIGHTS)
189 {
190 bHas = true;
191 ArrayDelete(pScene->mLights,pScene->mNumLights);
192 }
194 // handle camneras
195 if ( configDeleteFlags & aiComponent_CAMERAS)
196 {
197 bHas = true;
198 ArrayDelete(pScene->mCameras,pScene->mNumCameras);
199 }
201 // handle meshes
202 if (configDeleteFlags & aiComponent_MESHES)
203 {
204 bHas = true;
205 ArrayDelete(pScene->mMeshes,pScene->mNumMeshes);
206 }
207 else
208 {
209 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
210 {
211 if( ProcessMesh( pScene->mMeshes[a]))
212 bHas = true;
213 }
214 }
217 // now check whether the result is still a full scene
218 if (!pScene->mNumMeshes || !pScene->mNumMaterials)
219 {
220 pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
221 DefaultLogger::get()->debug("Setting AI_SCENE_FLAGS_INCOMPLETE flag");
223 // If we have no meshes anymore we should also clear another flag ...
224 if (!pScene->mNumMeshes)
225 pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
226 }
228 if (bHas)DefaultLogger::get()->info("RemoveVCProcess finished. Data structure cleanup has been done.");
229 else DefaultLogger::get()->debug("RemoveVCProcess finished. Nothing to be done ...");
230 }
232 // ------------------------------------------------------------------------------------------------
233 // Setup configuration properties for the step
234 void RemoveVCProcess::SetupProperties(const Importer* pImp)
235 {
236 configDeleteFlags = pImp->GetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS,0x0);
237 if (!configDeleteFlags)
238 {
239 DefaultLogger::get()->warn("RemoveVCProcess: AI_CONFIG_PP_RVC_FLAGS is zero.");
240 }
241 }
243 // ------------------------------------------------------------------------------------------------
244 // Executes the post processing step on the given imported data.
245 bool RemoveVCProcess::ProcessMesh(aiMesh* pMesh)
246 {
247 bool ret = false;
249 // if all materials have been deleted let the material
250 // index of the mesh point to the created default material
251 if ( configDeleteFlags & aiComponent_MATERIALS)
252 pMesh->mMaterialIndex = 0;
254 // handle normals
255 if (configDeleteFlags & aiComponent_NORMALS && pMesh->mNormals)
256 {
257 delete[] pMesh->mNormals;
258 pMesh->mNormals = NULL;
259 ret = true;
260 }
262 // handle tangents and bitangents
263 if (configDeleteFlags & aiComponent_TANGENTS_AND_BITANGENTS && pMesh->mTangents)
264 {
265 delete[] pMesh->mTangents;
266 pMesh->mTangents = NULL;
268 delete[] pMesh->mBitangents;
269 pMesh->mBitangents = NULL;
270 ret = true;
271 }
273 // handle texture coordinates
274 register bool b = (0 != (configDeleteFlags & aiComponent_TEXCOORDS));
275 for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++real)
276 {
277 if (!pMesh->mTextureCoords[i])break;
278 if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b)
279 {
280 delete pMesh->mTextureCoords[i];
281 pMesh->mTextureCoords[i] = NULL;
282 ret = true;
284 if (!b)
285 {
286 // collapse the rest of the array
287 for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
288 pMesh->mTextureCoords[a-1] = pMesh->mTextureCoords[a];
290 pMesh->mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS-1] = NULL;
291 continue;
292 }
293 }
294 ++i;
295 }
297 // handle vertex colors
298 b = (0 != (configDeleteFlags & aiComponent_COLORS));
299 for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_COLOR_SETS; ++real)
300 {
301 if (!pMesh->mColors[i])break;
302 if (configDeleteFlags & aiComponent_COLORSn(i) || b)
303 {
304 delete pMesh->mColors[i];
305 pMesh->mColors[i] = NULL;
306 ret = true;
308 if (!b)
309 {
310 // collapse the rest of the array
311 for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a)
312 pMesh->mColors[a-1] = pMesh->mColors[a];
314 pMesh->mColors[AI_MAX_NUMBER_OF_COLOR_SETS-1] = NULL;
315 continue;
316 }
317 }
318 ++i;
319 }
321 // handle bones
322 if (configDeleteFlags & aiComponent_BONEWEIGHTS && pMesh->mBones)
323 {
324 ArrayDelete(pMesh->mBones,pMesh->mNumBones);
325 ret = true;
326 }
327 return ret;
328 }