add 2 method to VertexInfluenceMap:

normalize and cullInfluenceCountPerVertex
This commit is contained in:
Julien Valentin 2017-08-28 18:42:22 +02:00
parent 5123614f89
commit 350756e738
2 changed files with 88 additions and 0 deletions

View File

@ -65,6 +65,12 @@ namespace osgAnimation
std::map<std::string, BoneInfluenceList>(org), std::map<std::string, BoneInfluenceList>(org),
osg::Object(org, copyop) osg::Object(org, copyop)
{} {}
///normalize per vertex weights given numvert of the attached mesh
void normalize(unsigned int numvert);
///remove weakest influences in order to fit targetted numbonepervertex
void cullInfluenceCountPerVertex(unsigned int maxnumbonepervertex, float minweight=0, bool renormalize=true);
}; };
} }

View File

@ -20,3 +20,85 @@
using namespace osgAnimation; using namespace osgAnimation;
struct invweight_ordered
{
inline bool operator() (const BoneWeight& bw1, const BoneWeight& bw2)
{
if (bw1.getWeight() > bw2.getWeight())return true;
if (bw1.getWeight() < bw2.getWeight())return false;
return(bw1.getBoneName()<bw2.getBoneName());
}
};
void VertexInfluenceMap::normalize(unsigned int numvert) {
typedef std::pair<float, std::vector<float*> > PerVertWeights;
std::vector<PerVertWeights > localstore;
localstore.resize(numvert);
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit) {
BoneInfluenceList &curvecinf=mapit->second;
for(BoneInfluenceList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf) {
IndexWeight& inf=*curinf;
localstore[inf.first].first+=inf.second;
localstore[inf.first].second.push_back(&inf.second);
}
}
unsigned int vertid=0;
for(std::vector<PerVertWeights >::iterator itvert=localstore.begin(); itvert!=localstore.end(); ++itvert, ++vertid) {
PerVertWeights & weights=*itvert;
if(weights.first< 1e-4)
{
OSG_WARN << "VertexInfluenceMap::normalize warning the vertex " <<vertid << " seems to have 0 weight, skip normalize for this vertex" << std::endl;
}
else
{
float mult = 1.0/weights.first;
for (std::vector<float*>::iterator itf =weights.second.begin(); itf!=weights.second.end(); ++itf)
**itf*=mult;
}
}
}
///remove weakest influences in order to fit targetted numbonepervertex
void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervertex,float minweight, bool renormalize) {
typedef std::set<BoneWeight,invweight_ordered > BoneWeightOrdered;
std::map<int,BoneWeightOrdered > tempVec2Bones;
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit) {
BoneInfluenceList &curvecinf=mapit->second;
for(BoneInfluenceList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf) {
IndexWeight& inf=*curinf;
if( curvecinf.getBoneName().empty()) {
OSG_WARN << "VertexInfluenceSet::buildVertex2BoneList warning vertex " << inf.first << " is not assigned to a bone" << std::endl;
}
else if(inf.second>minweight)tempVec2Bones[inf.first].insert(BoneWeight(curvecinf.getBoneName(), inf.second));
}
}
this->clear();
for( std::map<int,BoneWeightOrdered >::iterator mapit=tempVec2Bones.begin(); mapit!=tempVec2Bones.end(); ++mapit) {
BoneWeightOrdered& bwset=mapit->second;
unsigned int newsize=numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
float sum=0;
while(bwset.size()>newsize)bwset.erase(*bwset.rbegin());
if(renormalize){
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
sum+=bwit->getWeight();
if(sum>1e-4){
sum=1.0f/sum;
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit) {
BoneInfluenceList & inf= (*this)[bwit->getBoneName()];
inf.setBoneName(bwit->getBoneName());
inf.push_back(IndexWeight(mapit->first, bwit->getWeight()*sum));
}
}
}else{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit) {
BoneInfluenceList & inf= (*this)[bwit->getBoneName()];
inf.setBoneName(bwit->getBoneName());
inf.push_back(IndexWeight(mapit->first,bwit->getWeight()));
}
}
}
}