RemoveRedundantMaterials.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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 RemoveRedundantMaterials.cpp
  35. * @brief Implementation of the "RemoveRedundantMaterials" post processing step
  36. */
  37. // internal headers
  38. #include "AssimpPCH.h"
  39. #include "RemoveRedundantMaterials.h"
  40. #include "ParsingUtils.h"
  41. #include "ProcessHelper.h"
  42. using namespace Assimp;
  43. // ------------------------------------------------------------------------------------------------
  44. // Constructor to be privately used by Importer
  45. RemoveRedundantMatsProcess::RemoveRedundantMatsProcess()
  46. {
  47. // nothing to do here
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor, private as well
  51. RemoveRedundantMatsProcess::~RemoveRedundantMatsProcess()
  52. {
  53. // nothing to do here
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Returns whether the processing step is present in the given flag field.
  57. bool RemoveRedundantMatsProcess::IsActive( unsigned int pFlags) const
  58. {
  59. return (pFlags & aiProcess_RemoveRedundantMaterials) != 0;
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Setup import properties
  63. void RemoveRedundantMatsProcess::SetupProperties(const Importer* pImp)
  64. {
  65. // Get value of AI_CONFIG_PP_RRM_EXCLUDE_LIST
  66. configFixedMaterials = pImp->GetPropertyString(AI_CONFIG_PP_RRM_EXCLUDE_LIST,"");
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Executes the post processing step on the given imported data.
  70. void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
  71. {
  72. DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin");
  73. unsigned int iCnt = 0, unreferenced = 0;
  74. if (pScene->mNumMaterials)
  75. {
  76. // Find out which materials are referenced by meshes
  77. std::vector<bool> abReferenced(pScene->mNumMaterials,false);
  78. for (unsigned int i = 0;i < pScene->mNumMeshes;++i)
  79. abReferenced[pScene->mMeshes[i]->mMaterialIndex] = true;
  80. // If a list of materials to be excluded was given, match the list with
  81. // our imported materials and 'salt' all positive matches to ensure that
  82. // we get unique hashes later.
  83. if (configFixedMaterials.length()) {
  84. std::list<std::string> strings;
  85. ConvertListToStrings(configFixedMaterials,strings);
  86. for (unsigned int i = 0; i < pScene->mNumMaterials;++i) {
  87. aiMaterial* mat = pScene->mMaterials[i];
  88. aiString name;
  89. mat->Get(AI_MATKEY_NAME,name);
  90. if (name.length) {
  91. std::list<std::string>::const_iterator it = std::find(strings.begin(), strings.end(), name.data);
  92. if (it != strings.end()) {
  93. // Our brilliant 'salt': A single material property with ~ as first
  94. // character to mark it as internal and temporary.
  95. const int dummy = 1;
  96. ((MaterialHelper*)mat)->AddProperty(&dummy,1,"~RRM.UniqueMaterial",0,0);
  97. // Keep this material even if no mesh references it
  98. abReferenced[i] = true;
  99. DefaultLogger::get()->debug(std::string("Found positive match in exclusion list: \'") + name.data + "\'");
  100. }
  101. }
  102. }
  103. }
  104. // TODO: reimplement this algorithm to work in-place
  105. unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials];
  106. unsigned int iNewNum = 0;
  107. // Iterate through all materials and calculate a hash for them
  108. // store all hashes in a list and so a quick search whether
  109. // we do already have a specific hash. This allows us to
  110. // determine which materials are identical.
  111. uint32_t* aiHashes;
  112. aiHashes = new uint32_t[pScene->mNumMaterials];
  113. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  114. {
  115. // if the material is not referenced ... remove it
  116. if (!abReferenced[i]) {
  117. ++unreferenced;
  118. continue;
  119. }
  120. uint32_t me = aiHashes[i] = ((MaterialHelper*)pScene->mMaterials[i])->ComputeHash();
  121. for (unsigned int a = 0; a < i;++a)
  122. {
  123. if (abReferenced[a] && me == aiHashes[a]) {
  124. ++iCnt;
  125. me = 0;
  126. aiMappingTable[i] = aiMappingTable[a];
  127. delete pScene->mMaterials[i];
  128. break;
  129. }
  130. }
  131. if (me) {
  132. aiMappingTable[i] = iNewNum++;
  133. }
  134. }
  135. if (iCnt) {
  136. // build an output material list
  137. aiMaterial** ppcMaterials = new aiMaterial*[iNewNum];
  138. ::memset(ppcMaterials,0,sizeof(void*)*iNewNum);
  139. for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
  140. {
  141. // if the material is not referenced ... remove it
  142. if (!abReferenced[p])
  143. continue;
  144. // generate new names for all modified materials
  145. const unsigned int idx = aiMappingTable[p];
  146. if (ppcMaterials[idx])
  147. {
  148. aiString sz;
  149. sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p);
  150. ((MaterialHelper*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
  151. }
  152. else ppcMaterials[idx] = pScene->mMaterials[p];
  153. }
  154. // update all material indices
  155. for (unsigned int p = 0; p < pScene->mNumMeshes;++p) {
  156. aiMesh* mesh = pScene->mMeshes[p];
  157. mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
  158. }
  159. // delete the old material list
  160. delete[] pScene->mMaterials;
  161. pScene->mMaterials = ppcMaterials;
  162. pScene->mNumMaterials = iNewNum;
  163. }
  164. // delete temporary storage
  165. delete[] aiHashes;
  166. delete[] aiMappingTable;
  167. }
  168. if (!iCnt)DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished ");
  169. else
  170. {
  171. char szBuffer[128]; // should be sufficiently large
  172. ::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. %i redundant and %i unused materials",
  173. iCnt,unreferenced);
  174. DefaultLogger::get()->info(szBuffer);
  175. }
  176. }