vrshoot
view 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 source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** Implementation of the LimitBoneWeightsProcess post processing step */
43 #include "AssimpPCH.h"
44 #include "LimitBoneWeightsProcess.h"
47 using namespace Assimp;
50 // ------------------------------------------------------------------------------------------------
51 // Constructor to be privately used by Importer
52 LimitBoneWeightsProcess::LimitBoneWeightsProcess()
53 {
54 mMaxWeights = AI_LMW_MAX_WEIGHTS;
55 }
57 // ------------------------------------------------------------------------------------------------
58 // Destructor, private as well
59 LimitBoneWeightsProcess::~LimitBoneWeightsProcess()
60 {
61 // nothing to do here
62 }
64 // ------------------------------------------------------------------------------------------------
65 // Returns whether the processing step is present in the given flag field.
66 bool LimitBoneWeightsProcess::IsActive( unsigned int pFlags) const
67 {
68 return (pFlags & aiProcess_LimitBoneWeights) != 0;
69 }
71 // ------------------------------------------------------------------------------------------------
72 // Executes the post processing step on the given imported data.
73 void LimitBoneWeightsProcess::Execute( aiScene* pScene)
74 {
75 DefaultLogger::get()->debug("LimitBoneWeightsProcess begin");
76 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
77 ProcessMesh( pScene->mMeshes[a]);
79 DefaultLogger::get()->debug("LimitBoneWeightsProcess end");
80 }
82 // ------------------------------------------------------------------------------------------------
83 // Executes the post processing step on the given imported data.
84 void LimitBoneWeightsProcess::SetupProperties(const Importer* pImp)
85 {
86 // get the current value of the property
87 this->mMaxWeights = pImp->GetPropertyInteger(AI_CONFIG_PP_LBW_MAX_WEIGHTS,AI_LMW_MAX_WEIGHTS);
88 }
90 // ------------------------------------------------------------------------------------------------
91 // Unites identical vertices in the given mesh
92 void LimitBoneWeightsProcess::ProcessMesh( aiMesh* pMesh)
93 {
94 if( !pMesh->HasBones())
95 return;
97 // collect all bone weights per vertex
98 typedef std::vector< std::vector< Weight > > WeightsPerVertex;
99 WeightsPerVertex vertexWeights( pMesh->mNumVertices);
101 // collect all weights per vertex
102 for( unsigned int a = 0; a < pMesh->mNumBones; a++)
103 {
104 const aiBone* bone = pMesh->mBones[a];
105 for( unsigned int b = 0; b < bone->mNumWeights; b++)
106 {
107 const aiVertexWeight& w = bone->mWeights[b];
108 vertexWeights[w.mVertexId].push_back( Weight( a, w.mWeight));
109 }
110 }
112 unsigned int removed = 0, old_bones = pMesh->mNumBones;
114 // now cut the weight count if it exceeds the maximum
115 bool bChanged = false;
116 for( WeightsPerVertex::iterator vit = vertexWeights.begin(); vit != vertexWeights.end(); ++vit)
117 {
118 if( vit->size() <= mMaxWeights)
119 continue;
121 bChanged = true;
123 // more than the defined maximum -> first sort by weight in descending order. That's
124 // why we defined the < operator in such a weird way.
125 std::sort( vit->begin(), vit->end());
127 // now kill everything beyond the maximum count
128 unsigned int m = vit->size();
129 vit->erase( vit->begin() + mMaxWeights, vit->end());
130 removed += m-vit->size();
132 // and renormalize the weights
133 float sum = 0.0f;
134 for( std::vector<Weight>::const_iterator it = vit->begin(); it != vit->end(); ++it)
135 sum += it->mWeight;
136 for( std::vector<Weight>::iterator it = vit->begin(); it != vit->end(); ++it)
137 it->mWeight /= sum;
138 }
140 if (bChanged) {
141 // rebuild the vertex weight array for all bones
142 typedef std::vector< std::vector< aiVertexWeight > > WeightsPerBone;
143 WeightsPerBone boneWeights( pMesh->mNumBones);
144 for( unsigned int a = 0; a < vertexWeights.size(); a++)
145 {
146 const std::vector<Weight>& vw = vertexWeights[a];
147 for( std::vector<Weight>::const_iterator it = vw.begin(); it != vw.end(); ++it)
148 boneWeights[it->mBone].push_back( aiVertexWeight( a, it->mWeight));
149 }
151 // and finally copy the vertex weight list over to the mesh's bones
152 std::vector<bool> abNoNeed(pMesh->mNumBones,false);
153 bChanged = false;
155 for( unsigned int a = 0; a < pMesh->mNumBones; a++)
156 {
157 const std::vector<aiVertexWeight>& bw = boneWeights[a];
158 aiBone* bone = pMesh->mBones[a];
160 // ignore the bone if no vertex weights were removed there
162 // FIX (Aramis, 07|22|08)
163 // NO! we can't ignore it in this case ... it is possible that
164 // the number of weights did not change, but the weight values did.
166 // if( bw.size() == bone->mNumWeights)
167 // continue;
169 // FIX (Aramis, 07|21|08)
170 // It is possible that all weights of a bone have been removed.
171 // This would naturally cause an exception in &bw[0].
172 if ( bw.empty() )
173 {
174 abNoNeed[a] = bChanged = true;
175 continue;
176 }
178 // copy the weight list. should always be less weights than before, so we don't need a new allocation
179 ai_assert( bw.size() <= bone->mNumWeights);
180 bone->mNumWeights = (unsigned int) bw.size();
181 ::memcpy( bone->mWeights, &bw[0], bw.size() * sizeof( aiVertexWeight));
182 }
184 if (bChanged) {
185 // the number of new bones is smaller than before, so we can reuse the old array
186 aiBone** ppcCur = pMesh->mBones;aiBone** ppcSrc = ppcCur;
188 for (std::vector<bool>::const_iterator iter = abNoNeed.begin();iter != abNoNeed.end() ;++iter) {
189 if (*iter) {
190 delete *ppcSrc;
191 --pMesh->mNumBones;
192 }
193 else *ppcCur++ = *ppcSrc;
194 ++ppcSrc;
195 }
196 }
198 if (!DefaultLogger::isNullLogger()) {
199 char buffer[1024];
200 ::sprintf(buffer,"Removed %i weights. Input bones: %i. Output bones: %i",removed,old_bones,pMesh->mNumBones);
201 DefaultLogger::get()->info(buffer);
202 }
203 }
204 }