vrshoot

annotate 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
rev   line source
nuclear@0 1 /*
nuclear@0 2 ---------------------------------------------------------------------------
nuclear@0 3 Open Asset Import Library (assimp)
nuclear@0 4 ---------------------------------------------------------------------------
nuclear@0 5
nuclear@0 6 Copyright (c) 2006-2012, assimp team
nuclear@0 7
nuclear@0 8 All rights reserved.
nuclear@0 9
nuclear@0 10 Redistribution and use of this software in source and binary forms,
nuclear@0 11 with or without modification, are permitted provided that the following
nuclear@0 12 conditions are met:
nuclear@0 13
nuclear@0 14 * Redistributions of source code must retain the above
nuclear@0 15 copyright notice, this list of conditions and the
nuclear@0 16 following disclaimer.
nuclear@0 17
nuclear@0 18 * Redistributions in binary form must reproduce the above
nuclear@0 19 copyright notice, this list of conditions and the
nuclear@0 20 following disclaimer in the documentation and/or other
nuclear@0 21 materials provided with the distribution.
nuclear@0 22
nuclear@0 23 * Neither the name of the assimp team, nor the names of its
nuclear@0 24 contributors may be used to endorse or promote products
nuclear@0 25 derived from this software without specific prior
nuclear@0 26 written permission of the assimp team.
nuclear@0 27
nuclear@0 28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 39 ---------------------------------------------------------------------------
nuclear@0 40 */
nuclear@0 41 /** @file Implementation of the post processing step to remove
nuclear@0 42 * any parts of the mesh structure from the imported data.
nuclear@0 43 */
nuclear@0 44
nuclear@0 45 #include "AssimpPCH.h"
nuclear@0 46 #include "RemoveVCProcess.h"
nuclear@0 47
nuclear@0 48 using namespace Assimp;
nuclear@0 49
nuclear@0 50
nuclear@0 51 // ------------------------------------------------------------------------------------------------
nuclear@0 52 // Constructor to be privately used by Importer
nuclear@0 53 RemoveVCProcess::RemoveVCProcess()
nuclear@0 54 {}
nuclear@0 55
nuclear@0 56 // ------------------------------------------------------------------------------------------------
nuclear@0 57 // Destructor, private as well
nuclear@0 58 RemoveVCProcess::~RemoveVCProcess()
nuclear@0 59 {}
nuclear@0 60
nuclear@0 61 // ------------------------------------------------------------------------------------------------
nuclear@0 62 // Returns whether the processing step is present in the given flag field.
nuclear@0 63 bool RemoveVCProcess::IsActive( unsigned int pFlags) const
nuclear@0 64 {
nuclear@0 65 return (pFlags & aiProcess_RemoveComponent) != 0;
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 // ------------------------------------------------------------------------------------------------
nuclear@0 69 // Small helper function to delete all elements in a T** aray using delete
nuclear@0 70 template <typename T>
nuclear@0 71 inline void ArrayDelete(T**& in, unsigned int& num)
nuclear@0 72 {
nuclear@0 73 for (unsigned int i = 0; i < num; ++i)
nuclear@0 74 delete in[i];
nuclear@0 75
nuclear@0 76 delete[] in;
nuclear@0 77 in = NULL;
nuclear@0 78 num = 0;
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 #if 0
nuclear@0 82 // ------------------------------------------------------------------------------------------------
nuclear@0 83 // Updates the node graph - removes all nodes which have the "remove" flag set and the
nuclear@0 84 // "don't remove" flag not set. Nodes with meshes are never deleted.
nuclear@0 85 bool UpdateNodeGraph(aiNode* node,std::list<aiNode*>& childsOfParent,bool root)
nuclear@0 86 {
nuclear@0 87 register bool b = false;
nuclear@0 88
nuclear@0 89 std::list<aiNode*> mine;
nuclear@0 90 for (unsigned int i = 0; i < node->mNumChildren;++i)
nuclear@0 91 {
nuclear@0 92 if(UpdateNodeGraph(node->mChildren[i],mine,false))
nuclear@0 93 b = true;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 // somewhat tricky ... mNumMeshes must be originally 0 and MSB2 may not be set,
nuclear@0 97 // so we can do a simple comparison against MSB here
nuclear@0 98 if (!root && AI_RC_UINT_MSB == node->mNumMeshes )
nuclear@0 99 {
nuclear@0 100 // this node needs to be removed
nuclear@0 101 if(node->mNumChildren)
nuclear@0 102 {
nuclear@0 103 childsOfParent.insert(childsOfParent.end(),mine.begin(),mine.end());
nuclear@0 104
nuclear@0 105 // set all children to NULL to make sure they are not deleted when we delete ourself
nuclear@0 106 for (unsigned int i = 0; i < node->mNumChildren;++i)
nuclear@0 107 node->mChildren[i] = NULL;
nuclear@0 108 }
nuclear@0 109 b = true;
nuclear@0 110 delete node;
nuclear@0 111 }
nuclear@0 112 else
nuclear@0 113 {
nuclear@0 114 AI_RC_UNMASK(node->mNumMeshes);
nuclear@0 115 childsOfParent.push_back(node);
nuclear@0 116
nuclear@0 117 if (b)
nuclear@0 118 {
nuclear@0 119 // reallocate the array of our children here
nuclear@0 120 node->mNumChildren = (unsigned int)mine.size();
nuclear@0 121 aiNode** const children = new aiNode*[mine.size()];
nuclear@0 122 aiNode** ptr = children;
nuclear@0 123
nuclear@0 124 for (std::list<aiNode*>::iterator it = mine.begin(), end = mine.end();
nuclear@0 125 it != end; ++it)
nuclear@0 126 {
nuclear@0 127 *ptr++ = *it;
nuclear@0 128 }
nuclear@0 129 delete[] node->mChildren;
nuclear@0 130 node->mChildren = children;
nuclear@0 131 return false;
nuclear@0 132 }
nuclear@0 133 }
nuclear@0 134 return b;
nuclear@0 135 }
nuclear@0 136 #endif
nuclear@0 137
nuclear@0 138 // ------------------------------------------------------------------------------------------------
nuclear@0 139 // Executes the post processing step on the given imported data.
nuclear@0 140 void RemoveVCProcess::Execute( aiScene* pScene)
nuclear@0 141 {
nuclear@0 142 DefaultLogger::get()->debug("RemoveVCProcess begin");
nuclear@0 143 bool bHas = false; //,bMasked = false;
nuclear@0 144
nuclear@0 145 mScene = pScene;
nuclear@0 146
nuclear@0 147 // handle animations
nuclear@0 148 if ( configDeleteFlags & aiComponent_ANIMATIONS)
nuclear@0 149 {
nuclear@0 150
nuclear@0 151 bHas = true;
nuclear@0 152 ArrayDelete(pScene->mAnimations,pScene->mNumAnimations);
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 // handle textures
nuclear@0 156 if ( configDeleteFlags & aiComponent_TEXTURES)
nuclear@0 157 {
nuclear@0 158 bHas = true;
nuclear@0 159 ArrayDelete(pScene->mTextures,pScene->mNumTextures);
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 // handle materials
nuclear@0 163 if ( configDeleteFlags & aiComponent_MATERIALS && pScene->mNumMaterials)
nuclear@0 164 {
nuclear@0 165 bHas = true;
nuclear@0 166 for (unsigned int i = 1;i < pScene->mNumMaterials;++i)
nuclear@0 167 delete pScene->mMaterials[i];
nuclear@0 168
nuclear@0 169 pScene->mNumMaterials = 1;
nuclear@0 170 aiMaterial* helper = (aiMaterial*) pScene->mMaterials[0];
nuclear@0 171 ai_assert(NULL != helper);
nuclear@0 172 helper->Clear();
nuclear@0 173
nuclear@0 174 // gray
nuclear@0 175 aiColor3D clr(0.6f,0.6f,0.6f);
nuclear@0 176 helper->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
nuclear@0 177
nuclear@0 178 // add a small ambient color value
nuclear@0 179 clr = aiColor3D(0.05f,0.05f,0.05f);
nuclear@0 180 helper->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
nuclear@0 181
nuclear@0 182 aiString s;
nuclear@0 183 s.Set("Dummy_MaterialsRemoved");
nuclear@0 184 helper->AddProperty(&s,AI_MATKEY_NAME);
nuclear@0 185 }
nuclear@0 186
nuclear@0 187 // handle light sources
nuclear@0 188 if ( configDeleteFlags & aiComponent_LIGHTS)
nuclear@0 189 {
nuclear@0 190 bHas = true;
nuclear@0 191 ArrayDelete(pScene->mLights,pScene->mNumLights);
nuclear@0 192 }
nuclear@0 193
nuclear@0 194 // handle camneras
nuclear@0 195 if ( configDeleteFlags & aiComponent_CAMERAS)
nuclear@0 196 {
nuclear@0 197 bHas = true;
nuclear@0 198 ArrayDelete(pScene->mCameras,pScene->mNumCameras);
nuclear@0 199 }
nuclear@0 200
nuclear@0 201 // handle meshes
nuclear@0 202 if (configDeleteFlags & aiComponent_MESHES)
nuclear@0 203 {
nuclear@0 204 bHas = true;
nuclear@0 205 ArrayDelete(pScene->mMeshes,pScene->mNumMeshes);
nuclear@0 206 }
nuclear@0 207 else
nuclear@0 208 {
nuclear@0 209 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
nuclear@0 210 {
nuclear@0 211 if( ProcessMesh( pScene->mMeshes[a]))
nuclear@0 212 bHas = true;
nuclear@0 213 }
nuclear@0 214 }
nuclear@0 215
nuclear@0 216
nuclear@0 217 // now check whether the result is still a full scene
nuclear@0 218 if (!pScene->mNumMeshes || !pScene->mNumMaterials)
nuclear@0 219 {
nuclear@0 220 pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
nuclear@0 221 DefaultLogger::get()->debug("Setting AI_SCENE_FLAGS_INCOMPLETE flag");
nuclear@0 222
nuclear@0 223 // If we have no meshes anymore we should also clear another flag ...
nuclear@0 224 if (!pScene->mNumMeshes)
nuclear@0 225 pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
nuclear@0 226 }
nuclear@0 227
nuclear@0 228 if (bHas)DefaultLogger::get()->info("RemoveVCProcess finished. Data structure cleanup has been done.");
nuclear@0 229 else DefaultLogger::get()->debug("RemoveVCProcess finished. Nothing to be done ...");
nuclear@0 230 }
nuclear@0 231
nuclear@0 232 // ------------------------------------------------------------------------------------------------
nuclear@0 233 // Setup configuration properties for the step
nuclear@0 234 void RemoveVCProcess::SetupProperties(const Importer* pImp)
nuclear@0 235 {
nuclear@0 236 configDeleteFlags = pImp->GetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS,0x0);
nuclear@0 237 if (!configDeleteFlags)
nuclear@0 238 {
nuclear@0 239 DefaultLogger::get()->warn("RemoveVCProcess: AI_CONFIG_PP_RVC_FLAGS is zero.");
nuclear@0 240 }
nuclear@0 241 }
nuclear@0 242
nuclear@0 243 // ------------------------------------------------------------------------------------------------
nuclear@0 244 // Executes the post processing step on the given imported data.
nuclear@0 245 bool RemoveVCProcess::ProcessMesh(aiMesh* pMesh)
nuclear@0 246 {
nuclear@0 247 bool ret = false;
nuclear@0 248
nuclear@0 249 // if all materials have been deleted let the material
nuclear@0 250 // index of the mesh point to the created default material
nuclear@0 251 if ( configDeleteFlags & aiComponent_MATERIALS)
nuclear@0 252 pMesh->mMaterialIndex = 0;
nuclear@0 253
nuclear@0 254 // handle normals
nuclear@0 255 if (configDeleteFlags & aiComponent_NORMALS && pMesh->mNormals)
nuclear@0 256 {
nuclear@0 257 delete[] pMesh->mNormals;
nuclear@0 258 pMesh->mNormals = NULL;
nuclear@0 259 ret = true;
nuclear@0 260 }
nuclear@0 261
nuclear@0 262 // handle tangents and bitangents
nuclear@0 263 if (configDeleteFlags & aiComponent_TANGENTS_AND_BITANGENTS && pMesh->mTangents)
nuclear@0 264 {
nuclear@0 265 delete[] pMesh->mTangents;
nuclear@0 266 pMesh->mTangents = NULL;
nuclear@0 267
nuclear@0 268 delete[] pMesh->mBitangents;
nuclear@0 269 pMesh->mBitangents = NULL;
nuclear@0 270 ret = true;
nuclear@0 271 }
nuclear@0 272
nuclear@0 273 // handle texture coordinates
nuclear@0 274 register bool b = (0 != (configDeleteFlags & aiComponent_TEXCOORDS));
nuclear@0 275 for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++real)
nuclear@0 276 {
nuclear@0 277 if (!pMesh->mTextureCoords[i])break;
nuclear@0 278 if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b)
nuclear@0 279 {
nuclear@0 280 delete pMesh->mTextureCoords[i];
nuclear@0 281 pMesh->mTextureCoords[i] = NULL;
nuclear@0 282 ret = true;
nuclear@0 283
nuclear@0 284 if (!b)
nuclear@0 285 {
nuclear@0 286 // collapse the rest of the array
nuclear@0 287 for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
nuclear@0 288 pMesh->mTextureCoords[a-1] = pMesh->mTextureCoords[a];
nuclear@0 289
nuclear@0 290 pMesh->mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS-1] = NULL;
nuclear@0 291 continue;
nuclear@0 292 }
nuclear@0 293 }
nuclear@0 294 ++i;
nuclear@0 295 }
nuclear@0 296
nuclear@0 297 // handle vertex colors
nuclear@0 298 b = (0 != (configDeleteFlags & aiComponent_COLORS));
nuclear@0 299 for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_COLOR_SETS; ++real)
nuclear@0 300 {
nuclear@0 301 if (!pMesh->mColors[i])break;
nuclear@0 302 if (configDeleteFlags & aiComponent_COLORSn(i) || b)
nuclear@0 303 {
nuclear@0 304 delete pMesh->mColors[i];
nuclear@0 305 pMesh->mColors[i] = NULL;
nuclear@0 306 ret = true;
nuclear@0 307
nuclear@0 308 if (!b)
nuclear@0 309 {
nuclear@0 310 // collapse the rest of the array
nuclear@0 311 for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a)
nuclear@0 312 pMesh->mColors[a-1] = pMesh->mColors[a];
nuclear@0 313
nuclear@0 314 pMesh->mColors[AI_MAX_NUMBER_OF_COLOR_SETS-1] = NULL;
nuclear@0 315 continue;
nuclear@0 316 }
nuclear@0 317 }
nuclear@0 318 ++i;
nuclear@0 319 }
nuclear@0 320
nuclear@0 321 // handle bones
nuclear@0 322 if (configDeleteFlags & aiComponent_BONEWEIGHTS && pMesh->mBones)
nuclear@0 323 {
nuclear@0 324 ArrayDelete(pMesh->mBones,pMesh->mNumBones);
nuclear@0 325 ret = true;
nuclear@0 326 }
nuclear@0 327 return ret;
nuclear@0 328 }