RemoveVCProcess.cpp 11 KB

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