2
0

RemoveVCProcess.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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/scene.h>
  40. #include <assimp/DefaultLogger.hpp>
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. // Constructor to be privately used by Importer
  44. RemoveVCProcess::RemoveVCProcess() :
  45. configDeleteFlags(), mScene() {}
  46. // ------------------------------------------------------------------------------------------------
  47. // Destructor, private as well
  48. RemoveVCProcess::~RemoveVCProcess() {}
  49. // ------------------------------------------------------------------------------------------------
  50. // Returns whether the processing step is present in the given flag field.
  51. bool RemoveVCProcess::IsActive(unsigned int pFlags) const {
  52. return (pFlags & aiProcess_RemoveComponent) != 0;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Small helper function to delete all elements in a T** array using delete
  56. template <typename T>
  57. inline void ArrayDelete(T **&in, unsigned int &num) {
  58. for (unsigned int i = 0; i < num; ++i)
  59. delete in[i];
  60. delete[] in;
  61. in = nullptr;
  62. num = 0;
  63. }
  64. #if 0
  65. // ------------------------------------------------------------------------------------------------
  66. // Updates the node graph - removes all nodes which have the "remove" flag set and the
  67. // "don't remove" flag not set. Nodes with meshes are never deleted.
  68. bool UpdateNodeGraph(aiNode* node,std::list<aiNode*>& childsOfParent,bool root)
  69. {
  70. bool b = false;
  71. std::list<aiNode*> mine;
  72. for (unsigned int i = 0; i < node->mNumChildren;++i)
  73. {
  74. if(UpdateNodeGraph(node->mChildren[i],mine,false))
  75. b = true;
  76. }
  77. // somewhat tricky ... mNumMeshes must be originally 0 and MSB2 may not be set,
  78. // so we can do a simple comparison against MSB here
  79. if (!root && AI_RC_UINT_MSB == node->mNumMeshes )
  80. {
  81. // this node needs to be removed
  82. if(node->mNumChildren)
  83. {
  84. childsOfParent.insert(childsOfParent.end(),mine.begin(),mine.end());
  85. // set all children to nullptr to make sure they are not deleted when we delete ourself
  86. for (unsigned int i = 0; i < node->mNumChildren;++i)
  87. node->mChildren[i] = nullptr;
  88. }
  89. b = true;
  90. delete node;
  91. }
  92. else
  93. {
  94. AI_RC_UNMASK(node->mNumMeshes);
  95. childsOfParent.push_back(node);
  96. if (b)
  97. {
  98. // reallocate the array of our children here
  99. node->mNumChildren = (unsigned int)mine.size();
  100. aiNode** const children = new aiNode*[mine.size()];
  101. aiNode** ptr = children;
  102. for (std::list<aiNode*>::iterator it = mine.begin(), end = mine.end();
  103. it != end; ++it)
  104. {
  105. *ptr++ = *it;
  106. }
  107. delete[] node->mChildren;
  108. node->mChildren = children;
  109. return false;
  110. }
  111. }
  112. return b;
  113. }
  114. #endif
  115. // ------------------------------------------------------------------------------------------------
  116. // Executes the post processing step on the given imported data.
  117. void RemoveVCProcess::Execute(aiScene *pScene) {
  118. ASSIMP_LOG_DEBUG("RemoveVCProcess begin");
  119. bool bHas = false; //,bMasked = false;
  120. mScene = pScene;
  121. // handle animations
  122. if (configDeleteFlags & aiComponent_ANIMATIONS) {
  123. bHas = true;
  124. ArrayDelete(pScene->mAnimations, pScene->mNumAnimations);
  125. }
  126. // handle textures
  127. if (configDeleteFlags & aiComponent_TEXTURES) {
  128. bHas = true;
  129. ArrayDelete(pScene->mTextures, pScene->mNumTextures);
  130. }
  131. // handle materials
  132. if (configDeleteFlags & aiComponent_MATERIALS && pScene->mNumMaterials) {
  133. bHas = true;
  134. for (unsigned int i = 1; i < pScene->mNumMaterials; ++i)
  135. delete pScene->mMaterials[i];
  136. pScene->mNumMaterials = 1;
  137. aiMaterial *helper = (aiMaterial *)pScene->mMaterials[0];
  138. ai_assert(nullptr != helper);
  139. helper->Clear();
  140. // gray
  141. aiColor3D clr(0.6f, 0.6f, 0.6f);
  142. helper->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  143. // add a small ambient color value
  144. clr = aiColor3D(0.05f, 0.05f, 0.05f);
  145. helper->AddProperty(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
  146. aiString s;
  147. s.Set("Dummy_MaterialsRemoved");
  148. helper->AddProperty(&s, AI_MATKEY_NAME);
  149. }
  150. // handle light sources
  151. if (configDeleteFlags & aiComponent_LIGHTS) {
  152. bHas = true;
  153. ArrayDelete(pScene->mLights, pScene->mNumLights);
  154. }
  155. // handle camneras
  156. if (configDeleteFlags & aiComponent_CAMERAS) {
  157. bHas = true;
  158. ArrayDelete(pScene->mCameras, pScene->mNumCameras);
  159. }
  160. // handle meshes
  161. if (configDeleteFlags & aiComponent_MESHES) {
  162. bHas = true;
  163. ArrayDelete(pScene->mMeshes, pScene->mNumMeshes);
  164. } else {
  165. for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  166. if (ProcessMesh(pScene->mMeshes[a]))
  167. bHas = true;
  168. }
  169. }
  170. // now check whether the result is still a full scene
  171. if (!pScene->mNumMeshes || !pScene->mNumMaterials) {
  172. pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  173. ASSIMP_LOG_DEBUG("Setting AI_SCENE_FLAGS_INCOMPLETE flag");
  174. // If we have no meshes anymore we should also clear another flag ...
  175. if (!pScene->mNumMeshes)
  176. pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
  177. }
  178. if (bHas) {
  179. ASSIMP_LOG_INFO("RemoveVCProcess finished. Data structure cleanup has been done.");
  180. } else {
  181. ASSIMP_LOG_DEBUG("RemoveVCProcess finished. Nothing to be done ...");
  182. }
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. // Setup configuration properties for the step
  186. void RemoveVCProcess::SetupProperties(const Importer *pImp) {
  187. configDeleteFlags = pImp->GetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, 0x0);
  188. if (!configDeleteFlags) {
  189. ASSIMP_LOG_WARN("RemoveVCProcess: AI_CONFIG_PP_RVC_FLAGS is zero.");
  190. }
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. // Executes the post processing step on the given imported data.
  194. bool RemoveVCProcess::ProcessMesh(aiMesh *pMesh) {
  195. bool ret = false;
  196. // if all materials have been deleted let the material
  197. // index of the mesh point to the created default material
  198. if (configDeleteFlags & aiComponent_MATERIALS)
  199. pMesh->mMaterialIndex = 0;
  200. // handle normals
  201. if (configDeleteFlags & aiComponent_NORMALS && pMesh->mNormals) {
  202. delete[] pMesh->mNormals;
  203. pMesh->mNormals = nullptr;
  204. ret = true;
  205. }
  206. // handle tangents and bitangents
  207. if (configDeleteFlags & aiComponent_TANGENTS_AND_BITANGENTS && pMesh->mTangents) {
  208. delete[] pMesh->mTangents;
  209. pMesh->mTangents = nullptr;
  210. delete[] pMesh->mBitangents;
  211. pMesh->mBitangents = nullptr;
  212. ret = true;
  213. }
  214. // handle texture coordinates
  215. bool b = (0 != (configDeleteFlags & aiComponent_TEXCOORDS));
  216. for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++real) {
  217. if (!pMesh->mTextureCoords[i]) break;
  218. if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b) {
  219. delete[] pMesh->mTextureCoords[i];
  220. pMesh->mTextureCoords[i] = nullptr;
  221. ret = true;
  222. if (!b) {
  223. // collapse the rest of the array
  224. for (unsigned int a = i + 1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  225. pMesh->mTextureCoords[a - 1] = pMesh->mTextureCoords[a];
  226. pMesh->mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS - 1] = nullptr;
  227. continue;
  228. }
  229. }
  230. ++i;
  231. }
  232. // handle vertex colors
  233. b = (0 != (configDeleteFlags & aiComponent_COLORS));
  234. for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_COLOR_SETS; ++real) {
  235. if (!pMesh->mColors[i]) break;
  236. if (configDeleteFlags & aiComponent_COLORSn(i) || b) {
  237. delete[] pMesh->mColors[i];
  238. pMesh->mColors[i] = nullptr;
  239. ret = true;
  240. if (!b) {
  241. // collapse the rest of the array
  242. for (unsigned int a = i + 1; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a)
  243. pMesh->mColors[a - 1] = pMesh->mColors[a];
  244. pMesh->mColors[AI_MAX_NUMBER_OF_COLOR_SETS - 1] = nullptr;
  245. continue;
  246. }
  247. }
  248. ++i;
  249. }
  250. // handle bones
  251. if (configDeleteFlags & aiComponent_BONEWEIGHTS && pMesh->mBones) {
  252. ArrayDelete(pMesh->mBones, pMesh->mNumBones);
  253. ret = true;
  254. }
  255. return ret;
  256. }