vrshoot

diff libs/assimp/LimitBoneWeightsProcess.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/LimitBoneWeightsProcess.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,204 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/** Implementation of the LimitBoneWeightsProcess post processing step */
    1.45 +
    1.46 +#include "AssimpPCH.h"
    1.47 +#include "LimitBoneWeightsProcess.h"
    1.48 +
    1.49 +
    1.50 +using namespace Assimp;
    1.51 +
    1.52 +
    1.53 +// ------------------------------------------------------------------------------------------------
    1.54 +// Constructor to be privately used by Importer
    1.55 +LimitBoneWeightsProcess::LimitBoneWeightsProcess()
    1.56 +{
    1.57 +	mMaxWeights = AI_LMW_MAX_WEIGHTS;
    1.58 +}
    1.59 +
    1.60 +// ------------------------------------------------------------------------------------------------
    1.61 +// Destructor, private as well
    1.62 +LimitBoneWeightsProcess::~LimitBoneWeightsProcess()
    1.63 +{
    1.64 +	// nothing to do here
    1.65 +}
    1.66 +
    1.67 +// ------------------------------------------------------------------------------------------------
    1.68 +// Returns whether the processing step is present in the given flag field.
    1.69 +bool LimitBoneWeightsProcess::IsActive( unsigned int pFlags) const
    1.70 +{
    1.71 +	return (pFlags & aiProcess_LimitBoneWeights) != 0;
    1.72 +}
    1.73 +
    1.74 +// ------------------------------------------------------------------------------------------------
    1.75 +// Executes the post processing step on the given imported data.
    1.76 +void LimitBoneWeightsProcess::Execute( aiScene* pScene)
    1.77 +{
    1.78 +	DefaultLogger::get()->debug("LimitBoneWeightsProcess begin");
    1.79 +	for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
    1.80 +		ProcessMesh( pScene->mMeshes[a]);
    1.81 +
    1.82 +	DefaultLogger::get()->debug("LimitBoneWeightsProcess end");
    1.83 +}
    1.84 +
    1.85 +// ------------------------------------------------------------------------------------------------
    1.86 +// Executes the post processing step on the given imported data.
    1.87 +void LimitBoneWeightsProcess::SetupProperties(const Importer* pImp)
    1.88 +{
    1.89 +	// get the current value of the property
    1.90 +	this->mMaxWeights = pImp->GetPropertyInteger(AI_CONFIG_PP_LBW_MAX_WEIGHTS,AI_LMW_MAX_WEIGHTS);
    1.91 +}
    1.92 +
    1.93 +// ------------------------------------------------------------------------------------------------
    1.94 +// Unites identical vertices in the given mesh
    1.95 +void LimitBoneWeightsProcess::ProcessMesh( aiMesh* pMesh)
    1.96 +{
    1.97 +	if( !pMesh->HasBones())
    1.98 +		return;
    1.99 +
   1.100 +	// collect all bone weights per vertex
   1.101 +	typedef std::vector< std::vector< Weight > > WeightsPerVertex;
   1.102 +	WeightsPerVertex vertexWeights( pMesh->mNumVertices);
   1.103 +
   1.104 +	// collect all weights per vertex
   1.105 +	for( unsigned int a = 0; a < pMesh->mNumBones; a++)
   1.106 +	{
   1.107 +		const aiBone* bone = pMesh->mBones[a];
   1.108 +		for( unsigned int b = 0; b < bone->mNumWeights; b++)
   1.109 +		{
   1.110 +			const aiVertexWeight& w = bone->mWeights[b];
   1.111 +			vertexWeights[w.mVertexId].push_back( Weight( a, w.mWeight));
   1.112 +		}
   1.113 +	}
   1.114 +
   1.115 +	unsigned int removed = 0, old_bones = pMesh->mNumBones;
   1.116 +
   1.117 +	// now cut the weight count if it exceeds the maximum
   1.118 +	bool bChanged = false;
   1.119 +	for( WeightsPerVertex::iterator vit = vertexWeights.begin(); vit != vertexWeights.end(); ++vit)
   1.120 +	{
   1.121 +		if( vit->size() <= mMaxWeights)
   1.122 +			continue;
   1.123 +
   1.124 +		bChanged = true;
   1.125 +
   1.126 +		// more than the defined maximum -> first sort by weight in descending order. That's 
   1.127 +		// why we defined the < operator in such a weird way.
   1.128 +		std::sort( vit->begin(), vit->end());
   1.129 +
   1.130 +		// now kill everything beyond the maximum count
   1.131 +		unsigned int m = vit->size();
   1.132 +		vit->erase( vit->begin() + mMaxWeights, vit->end());
   1.133 +		removed += m-vit->size();
   1.134 +
   1.135 +		// and renormalize the weights
   1.136 +		float sum = 0.0f;
   1.137 +		for( std::vector<Weight>::const_iterator it = vit->begin(); it != vit->end(); ++it)
   1.138 +			sum += it->mWeight;
   1.139 +		for( std::vector<Weight>::iterator it = vit->begin(); it != vit->end(); ++it)
   1.140 +			it->mWeight /= sum;
   1.141 +	}
   1.142 +
   1.143 +	if (bChanged)	{
   1.144 +		// rebuild the vertex weight array for all bones 
   1.145 +		typedef std::vector< std::vector< aiVertexWeight > > WeightsPerBone;
   1.146 +		WeightsPerBone boneWeights( pMesh->mNumBones);
   1.147 +		for( unsigned int a = 0; a < vertexWeights.size(); a++)
   1.148 +		{
   1.149 +			const std::vector<Weight>& vw = vertexWeights[a];
   1.150 +			for( std::vector<Weight>::const_iterator it = vw.begin(); it != vw.end(); ++it)
   1.151 +				boneWeights[it->mBone].push_back( aiVertexWeight( a, it->mWeight));
   1.152 +		}
   1.153 +
   1.154 +		// and finally copy the vertex weight list over to the mesh's bones
   1.155 +		std::vector<bool> abNoNeed(pMesh->mNumBones,false);
   1.156 +		bChanged = false;
   1.157 +
   1.158 +		for( unsigned int a = 0; a < pMesh->mNumBones; a++)
   1.159 +		{
   1.160 +			const std::vector<aiVertexWeight>& bw = boneWeights[a];
   1.161 +			aiBone* bone = pMesh->mBones[a];
   1.162 +
   1.163 +			// ignore the bone if no vertex weights were removed there
   1.164 +
   1.165 +			// FIX (Aramis, 07|22|08)
   1.166 +			// NO! we can't ignore it in this case ... it is possible that
   1.167 +			// the number of weights did not change, but the weight values did.
   1.168 +
   1.169 +			// if( bw.size() == bone->mNumWeights)
   1.170 +			//	continue;
   1.171 +
   1.172 +			// FIX (Aramis, 07|21|08)
   1.173 +			// It is possible that all weights of a bone have been removed.
   1.174 +			// This would naturally cause an exception in &bw[0].
   1.175 +			if ( bw.empty() )
   1.176 +			{
   1.177 +				abNoNeed[a] = bChanged = true;
   1.178 +				continue;
   1.179 +			}
   1.180 +
   1.181 +			// copy the weight list. should always be less weights than before, so we don't need a new allocation
   1.182 +			ai_assert( bw.size() <= bone->mNumWeights);
   1.183 +			bone->mNumWeights = (unsigned int) bw.size();
   1.184 +			::memcpy( bone->mWeights, &bw[0], bw.size() * sizeof( aiVertexWeight));
   1.185 +		}
   1.186 +
   1.187 +		if (bChanged)	{
   1.188 +			// the number of new bones is smaller than before, so we can reuse the old array
   1.189 +			aiBone** ppcCur = pMesh->mBones;aiBone** ppcSrc = ppcCur;
   1.190 +
   1.191 +			for (std::vector<bool>::const_iterator iter  = abNoNeed.begin();iter != abNoNeed.end()  ;++iter)	{
   1.192 +				if (*iter)	{
   1.193 +					delete *ppcSrc;
   1.194 +					--pMesh->mNumBones;
   1.195 +				}
   1.196 +				else *ppcCur++ = *ppcSrc;
   1.197 +				++ppcSrc;
   1.198 +			}
   1.199 +		}
   1.200 +
   1.201 +		if (!DefaultLogger::isNullLogger()) {
   1.202 +			char buffer[1024];
   1.203 +			::sprintf(buffer,"Removed %i weights. Input bones: %i. Output bones: %i",removed,old_bones,pMesh->mNumBones);
   1.204 +			DefaultLogger::get()->info(buffer);
   1.205 +		}
   1.206 +	}
   1.207 +}