LimitBoneWeightsProcess.cpp 7.1 KB

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