LimitBoneWeightsProcess.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. */
  33. /** Implementation of the LimitBoneWeightsProcess post processing step */
  34. #include "AssimpPCH.h"
  35. #include "LimitBoneWeightsProcess.h"
  36. using namespace Assimp;
  37. // ------------------------------------------------------------------------------------------------
  38. // Constructor to be privately used by Importer
  39. LimitBoneWeightsProcess::LimitBoneWeightsProcess()
  40. {
  41. mMaxWeights = AI_LMW_MAX_WEIGHTS;
  42. }
  43. // ------------------------------------------------------------------------------------------------
  44. // Destructor, private as well
  45. LimitBoneWeightsProcess::~LimitBoneWeightsProcess()
  46. {
  47. // nothing to do here
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Returns whether the processing step is present in the given flag field.
  51. bool LimitBoneWeightsProcess::IsActive( unsigned int pFlags) const
  52. {
  53. return (pFlags & aiProcess_LimitBoneWeights) != 0;
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Executes the post processing step on the given imported data.
  57. void LimitBoneWeightsProcess::Execute( aiScene* pScene)
  58. {
  59. DefaultLogger::get()->debug("LimitBoneWeightsProcess begin");
  60. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  61. ProcessMesh( pScene->mMeshes[a]);
  62. DefaultLogger::get()->debug("LimitBoneWeightsProcess end");
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Executes the post processing step on the given imported data.
  66. void LimitBoneWeightsProcess::SetupProperties(const Importer* pImp)
  67. {
  68. // get the current value of the property
  69. this->mMaxWeights = pImp->GetPropertyInteger(AI_CONFIG_PP_LBW_MAX_WEIGHTS,AI_LMW_MAX_WEIGHTS);
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. // Unites identical vertices in the given mesh
  73. void LimitBoneWeightsProcess::ProcessMesh( aiMesh* pMesh)
  74. {
  75. if( !pMesh->HasBones())
  76. return;
  77. // collect all bone weights per vertex
  78. typedef std::vector< std::vector< Weight > > WeightsPerVertex;
  79. WeightsPerVertex vertexWeights( pMesh->mNumVertices);
  80. // collect all weights per vertex
  81. for( unsigned int a = 0; a < pMesh->mNumBones; a++)
  82. {
  83. const aiBone* bone = pMesh->mBones[a];
  84. for( unsigned int b = 0; b < bone->mNumWeights; b++)
  85. {
  86. const aiVertexWeight& w = bone->mWeights[b];
  87. vertexWeights[w.mVertexId].push_back( Weight( a, w.mWeight));
  88. }
  89. }
  90. unsigned int removed = 0, old_bones = pMesh->mNumBones;
  91. // now cut the weight count if it exceeds the maximum
  92. bool bChanged = false;
  93. for( WeightsPerVertex::iterator vit = vertexWeights.begin(); vit != vertexWeights.end(); ++vit)
  94. {
  95. if( vit->size() <= mMaxWeights)
  96. continue;
  97. bChanged = true;
  98. // more than the defined maximum -> first sort by weight in descending order. That's
  99. // why we defined the < operator in such a weird way.
  100. std::sort( vit->begin(), vit->end());
  101. // now kill everything beyond the maximum count
  102. unsigned int m = vit->size();
  103. vit->erase( vit->begin() + mMaxWeights, vit->end());
  104. removed += m-vit->size();
  105. // and renormalize the weights
  106. float sum = 0.0f;
  107. for( std::vector<Weight>::const_iterator it = vit->begin(); it != vit->end(); ++it)
  108. sum += it->mWeight;
  109. for( std::vector<Weight>::iterator it = vit->begin(); it != vit->end(); ++it)
  110. it->mWeight /= sum;
  111. }
  112. if (bChanged) {
  113. // rebuild the vertex weight array for all bones
  114. typedef std::vector< std::vector< aiVertexWeight > > WeightsPerBone;
  115. WeightsPerBone boneWeights( pMesh->mNumBones);
  116. for( unsigned int a = 0; a < vertexWeights.size(); a++)
  117. {
  118. const std::vector<Weight>& vw = vertexWeights[a];
  119. for( std::vector<Weight>::const_iterator it = vw.begin(); it != vw.end(); ++it)
  120. boneWeights[it->mBone].push_back( aiVertexWeight( a, it->mWeight));
  121. }
  122. // and finally copy the vertex weight list over to the mesh's bones
  123. std::vector<bool> abNoNeed(pMesh->mNumBones,false);
  124. bChanged = false;
  125. for( unsigned int a = 0; a < pMesh->mNumBones; a++)
  126. {
  127. const std::vector<aiVertexWeight>& bw = boneWeights[a];
  128. aiBone* bone = pMesh->mBones[a];
  129. // ignore the bone if no vertex weights were removed there
  130. // FIX (Aramis, 07|22|08)
  131. // NO! we can't ignore it in this case ... it is possible that
  132. // the number of weights did not change, but the weight values did.
  133. // if( bw.size() == bone->mNumWeights)
  134. // continue;
  135. // FIX (Aramis, 07|21|08)
  136. // It is possible that all weights of a bone have been removed.
  137. // This would naturally cause an exception in &bw[0].
  138. if ( bw.empty() )
  139. {
  140. abNoNeed[a] = bChanged = true;
  141. continue;
  142. }
  143. // copy the weight list. should always be less weights than before, so we don't need a new allocation
  144. ai_assert( bw.size() <= bone->mNumWeights);
  145. bone->mNumWeights = (unsigned int) bw.size();
  146. ::memcpy( bone->mWeights, &bw[0], bw.size() * sizeof( aiVertexWeight));
  147. }
  148. if (bChanged) {
  149. // the number of new bones is smaller than before, so we can reuse the old array
  150. aiBone** ppcCur = pMesh->mBones;aiBone** ppcSrc = ppcCur;
  151. for (std::vector<bool>::const_iterator iter = abNoNeed.begin();iter != abNoNeed.end() ;++iter) {
  152. if (*iter) {
  153. delete *ppcSrc;
  154. --pMesh->mNumBones;
  155. }
  156. else *ppcCur++ = *ppcSrc;
  157. ++ppcSrc;
  158. }
  159. }
  160. if (!DefaultLogger::isNullLogger()) {
  161. char buffer[1024];
  162. ::sprintf(buffer,"Removed %i weights. Input bones: %i. Output bones: %i",removed,old_bones,pMesh->mNumBones);
  163. DefaultLogger::get()->info(buffer);
  164. }
  165. }
  166. }