RemoveRedundantMaterials.cpp 8.0 KB

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