LimitBoneWeightsProcess.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/DefaultLogger.h"
  41. using namespace Assimp;
  42. /*static*/ unsigned int LimitBoneWeightsProcess::mMaxWeights = AI_LMW_MAX_WEIGHTS;
  43. extern "C" {
  44. // ------------------------------------------------------------------------------------------------
  45. aiReturn aiSetBoneWeightLimit(unsigned int pLimit)
  46. {
  47. if (0 == pLimit)
  48. {
  49. LimitBoneWeightsProcess::mMaxWeights = 0xFFFFFFFF;
  50. return AI_FAILURE;
  51. }
  52. LimitBoneWeightsProcess::mMaxWeights = pLimit;
  53. DefaultLogger::get()->debug("aiSetBoneWeightLimit() - bone weight limit was changed");
  54. return AI_SUCCESS;
  55. }
  56. };
  57. // ------------------------------------------------------------------------------------------------
  58. // Constructor to be privately used by Importer
  59. LimitBoneWeightsProcess::LimitBoneWeightsProcess()
  60. {
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. // Destructor, private as well
  64. LimitBoneWeightsProcess::~LimitBoneWeightsProcess()
  65. {
  66. // nothing to do here
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Returns whether the processing step is present in the given flag field.
  70. bool LimitBoneWeightsProcess::IsActive( unsigned int pFlags) const
  71. {
  72. return (pFlags & aiProcess_LimitBoneWeights) != 0;
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Executes the post processing step on the given imported data.
  76. void LimitBoneWeightsProcess::Execute( aiScene* pScene)
  77. {
  78. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  79. ProcessMesh( pScene->mMeshes[a]);
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. // Unites identical vertices in the given mesh
  83. void LimitBoneWeightsProcess::ProcessMesh( aiMesh* pMesh)
  84. {
  85. if( !pMesh->HasBones())
  86. return;
  87. // collect all bone weights per vertex
  88. typedef std::vector< std::vector< Weight > > WeightsPerVertex;
  89. WeightsPerVertex vertexWeights( pMesh->mNumVertices);
  90. // collect all weights per vertex
  91. for( unsigned int a = 0; a < pMesh->mNumBones; a++)
  92. {
  93. const aiBone* bone = pMesh->mBones[a];
  94. for( unsigned int b = 0; b < bone->mNumWeights; b++)
  95. {
  96. const aiVertexWeight& w = bone->mWeights[b];
  97. vertexWeights[w.mVertexId].push_back( Weight( a, w.mWeight));
  98. }
  99. }
  100. // now cut the weight count if it exceeds the maximum
  101. for( WeightsPerVertex::iterator vit = vertexWeights.begin(); vit != vertexWeights.end(); ++vit)
  102. {
  103. if( vit->size() <= mMaxWeights)
  104. continue;
  105. // more than the defined maximum -> first sort by weight in descending order. That's
  106. // why we defined the < operator in such a weird way.
  107. std::sort( vit->begin(), vit->end());
  108. // now kill everything beyond the maximum count
  109. vit->erase( vit->begin() + mMaxWeights, vit->end());
  110. // and renormalize the weights
  111. float sum = 0.0f;
  112. for( std::vector<Weight>::const_iterator it = vit->begin(); it != vit->end(); ++it)
  113. sum += it->mWeight;
  114. for( std::vector<Weight>::iterator it = vit->begin(); it != vit->end(); ++it)
  115. it->mWeight /= sum;
  116. }
  117. // rebuild the vertex weight array for all bones
  118. typedef std::vector< std::vector< aiVertexWeight > > WeightsPerBone;
  119. WeightsPerBone boneWeights( pMesh->mNumBones);
  120. for( unsigned int a = 0; a < vertexWeights.size(); a++)
  121. {
  122. const std::vector<Weight>& vw = vertexWeights[a];
  123. for( std::vector<Weight>::const_iterator it = vw.begin(); it != vw.end(); ++it)
  124. boneWeights[it->mBone].push_back( aiVertexWeight( a, it->mWeight));
  125. }
  126. // and finally copy the vertex weight list over to the mesh's bones
  127. for( unsigned int a = 0; a < pMesh->mNumBones; a++)
  128. {
  129. const std::vector<aiVertexWeight>& bw = boneWeights[a];
  130. aiBone* bone = pMesh->mBones[a];
  131. // ignore the bone if no vertex weights were removed there
  132. if( bw.size() == bone->mNumWeights)
  133. continue;
  134. // copy the weight list. should always be less weights than before, so we don't need a new allocation
  135. assert( bw.size() < bone->mNumWeights);
  136. bone->mNumWeights = (unsigned int) bw.size();
  137. memcpy( bone->mWeights, &bw[0], bw.size() * sizeof( aiVertexWeight));
  138. }
  139. }