LimitBoneWeightsProcess.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2023, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ---------------------------------------------------------------------- */
  32. #include "LimitBoneWeightsProcess.h"
  33. #include <assimp/SmallVector.h>
  34. #include <assimp/StringUtils.h>
  35. #include <assimp/postprocess.h>
  36. #include <assimp/DefaultLogger.hpp>
  37. #include <assimp/scene.h>
  38. #include <stdio.h>
  39. namespace Assimp {
  40. // Make sure this value is set.
  41. #ifndef AI_LMW_MAX_WEIGHTS
  42. # define AI_LMW_MAX_WEIGHTS 16
  43. #endif
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. LimitBoneWeightsProcess::LimitBoneWeightsProcess() : mMaxWeights(AI_LMW_MAX_WEIGHTS) {
  47. // empty
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Returns whether the processing step is present in the given flag field.
  51. bool LimitBoneWeightsProcess::IsActive( unsigned int pFlags) const {
  52. return (pFlags & aiProcess_LimitBoneWeights) != 0;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Executes the post processing step on the given imported data.
  56. void LimitBoneWeightsProcess::Execute( aiScene* pScene) {
  57. ai_assert(pScene != nullptr);
  58. ASSIMP_LOG_DEBUG("LimitBoneWeightsProcess begin");
  59. for (unsigned int m = 0; m < pScene->mNumMeshes; ++m) {
  60. ProcessMesh(pScene->mMeshes[m]);
  61. }
  62. ASSIMP_LOG_DEBUG("LimitBoneWeightsProcess end");
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Executes the post processing step on the given imported data.
  66. void LimitBoneWeightsProcess::SetupProperties(const Importer* pImp) {
  67. this->mMaxWeights = pImp->GetPropertyInteger(AI_CONFIG_PP_LBW_MAX_WEIGHTS,AI_LMW_MAX_WEIGHTS);
  68. this->mRemoveEmptyBones = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, 1) != 0;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. static unsigned int removeEmptyBones(aiMesh *pMesh) {
  72. ai_assert(pMesh != nullptr);
  73. unsigned int writeBone = 0;
  74. for (unsigned int readBone = 0; readBone< pMesh->mNumBones; ++readBone) {
  75. aiBone* bone = pMesh->mBones[readBone];
  76. if (bone->mNumWeights > 0) {
  77. pMesh->mBones[writeBone++] = bone;
  78. } else {
  79. delete bone;
  80. }
  81. }
  82. return writeBone;
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // Unites identical vertices in the given mesh
  86. void LimitBoneWeightsProcess::ProcessMesh(aiMesh* pMesh) {
  87. if (!pMesh->HasBones())
  88. return;
  89. // collect all bone weights per vertex
  90. typedef SmallVector<Weight,8> VertexWeightArray;
  91. typedef std::vector<VertexWeightArray> WeightsPerVertex;
  92. WeightsPerVertex vertexWeights(pMesh->mNumVertices);
  93. size_t maxVertexWeights = 0;
  94. for (unsigned int b = 0; b < pMesh->mNumBones; ++b) {
  95. const aiBone* bone = pMesh->mBones[b];
  96. for (unsigned int w = 0; w < bone->mNumWeights; ++w) {
  97. const aiVertexWeight& vw = bone->mWeights[w];
  98. if (vertexWeights.size() <= vw.mVertexId)
  99. continue;
  100. vertexWeights[vw.mVertexId].push_back(Weight(b, vw.mWeight));
  101. maxVertexWeights = std::max(maxVertexWeights, vertexWeights[vw.mVertexId].size());
  102. }
  103. }
  104. if (maxVertexWeights <= mMaxWeights)
  105. return;
  106. unsigned int removed = 0, old_bones = pMesh->mNumBones;
  107. // now cut the weight count if it exceeds the maximum
  108. for (WeightsPerVertex::iterator vit = vertexWeights.begin(); vit != vertexWeights.end(); ++vit) {
  109. if (vit->size() <= mMaxWeights)
  110. continue;
  111. // more than the defined maximum -> first sort by weight in descending order. That's
  112. // why we defined the < operator in such a weird way.
  113. std::sort(vit->begin(), vit->end());
  114. // now kill everything beyond the maximum count
  115. unsigned int m = static_cast<unsigned int>(vit->size());
  116. vit->resize(mMaxWeights);
  117. removed += static_cast<unsigned int>(m - vit->size());
  118. // and renormalize the weights
  119. float sum = 0.0f;
  120. for(const Weight* it = vit->begin(); it != vit->end(); ++it) {
  121. sum += it->mWeight;
  122. }
  123. if (0.0f != sum) {
  124. const float invSum = 1.0f / sum;
  125. for(Weight* it = vit->begin(); it != vit->end(); ++it) {
  126. it->mWeight *= invSum;
  127. }
  128. }
  129. }
  130. // clear weight count for all bone
  131. for (unsigned int a = 0; a < pMesh->mNumBones; ++a) {
  132. pMesh->mBones[a]->mNumWeights = 0;
  133. }
  134. // rebuild the vertex weight array for all bones
  135. for (unsigned int a = 0; a < vertexWeights.size(); ++a) {
  136. const VertexWeightArray& vw = vertexWeights[a];
  137. for (const Weight* it = vw.begin(); it != vw.end(); ++it) {
  138. aiBone* bone = pMesh->mBones[it->mBone];
  139. bone->mWeights[bone->mNumWeights++] = aiVertexWeight(a, it->mWeight);
  140. }
  141. }
  142. // remove empty bones
  143. if (mRemoveEmptyBones) {
  144. pMesh->mNumBones = removeEmptyBones(pMesh);
  145. }
  146. if (!DefaultLogger::isNullLogger()) {
  147. ASSIMP_LOG_INFO("Removed ", removed, " weights. Input bones: ", old_bones, ". Output bones: ", pMesh->mNumBones);
  148. }
  149. }
  150. } // namespace Assimp