add void InfluenceMap::removeUnexpressedBones(Skeleton &skel) const;

a bit experimental but work well without further process on my test set
This commit is contained in:
Julien Valentin 2017-09-01 17:48:28 +02:00
parent 0d02dfbbbd
commit 78dd81a8b4
2 changed files with 132 additions and 20 deletions

View File

@ -24,6 +24,8 @@
namespace osgAnimation
{
class Skeleton;
// first is bonename, and second the weight
typedef std::pair<std::string, float> BoneWeight;
// first is vertex index, and second the weight
@ -77,6 +79,9 @@ namespace osgAnimation
/// compute the minimal VertexGroup Set in which vertices shares the same influence set
void computeMinimalVertexGroupList(std::vector<VertexGroup>&uniqVertexGroupList, unsigned int numvert)const;
//Experimental removal of unexpressed bone from the skeleton
void removeUnexpressedBones(Skeleton &skel) const;
};
}

View File

@ -14,6 +14,8 @@
*/
#include <osgAnimation/VertexInfluence>
#include <osgAnimation/RigGeometry>
#include <osgAnimation/BoneMapVisitor>
#include <osg/Notify>
#include <iostream>
#include <algorithm>
@ -31,14 +33,17 @@ struct invweight_ordered
}
};
void VertexInfluenceMap::normalize(unsigned int numvert) {
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) {
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
{
IndexWeightList &curvecinf=mapit->second;
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf) {
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf=*curinf;
localstore[inf.first].first+=inf.second;
localstore[inf.first].second.push_back(&inf.second);
@ -46,7 +51,8 @@ void VertexInfluenceMap::normalize(unsigned int numvert) {
}
}
unsigned int vertid=0;
for(std::vector<PerVertWeights >::iterator itvert=localstore.begin(); itvert!=localstore.end(); ++itvert, ++vertid) {
for(std::vector<PerVertWeights >::iterator itvert=localstore.begin(); itvert!=localstore.end(); ++itvert, ++vertid)
{
PerVertWeights & weights=*itvert;
if(weights.first< 1e-4)
{
@ -62,41 +68,51 @@ void VertexInfluenceMap::normalize(unsigned int numvert) {
}
///remove weakest influences in order to fit targetted numbonepervertex
void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervertex,float minweight, bool renormalize) {
void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervertex,float minweight, bool renormalize)
{
typedef std::set<BoneWeight,invweight_ordered > BoneWeightOrdered;
std::map<int,BoneWeightOrdered > tempVec2Bones;
typedef std::set<BoneWeight, invweight_ordered > BoneWeightOrdered;
std::map<int, BoneWeightOrdered > tempVec2Bones;
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
{
const std::string& bonename=mapit->first;
IndexWeightList &curvecinf=mapit->second;
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf) {
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf=*curinf;
if( bonename.empty()) {
if( bonename.empty())
{
OSG_WARN << "VertexInfluenceSet::cullInfluenceCountPerVertex warning vertex " << inf.first << " is not assigned to a bone" << std::endl;
}
else if(inf.second>minweight)tempVec2Bones[inf.first].insert(BoneWeight(bonename, inf.second));
}
}
this->clear();
for( std::map<int,BoneWeightOrdered >::iterator mapit=tempVec2Bones.begin(); mapit!=tempVec2Bones.end(); ++mapit) {
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){
if(renormalize)
{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
sum+=bwit->second;
if(sum>1e-4){
if(sum>1e-4)
{
sum=1.0f/sum;
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit) {
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
{
VertexInfluence & inf= (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first, bwit->second*sum));
inf.setName(bwit->first);
}
}
}else{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit) {
}
else
{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
{
VertexInfluence & inf= (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first,bwit->second));
inf.setName(bwit->first);
@ -114,7 +130,8 @@ void VertexInfluenceMap::computePerVertexInfluenceList(std::vector<BoneWeightLis
++it)
{
const IndexWeightList& inflist = it->second;
if (it->first.empty()) {
if (it->first.empty())
{
OSG_WARN << "VertexInfluenceMap::computePerVertexInfluenceList contains unamed bone IndexWeightList" << std::endl;
}
for(IndexWeightList::const_iterator infit=inflist.begin(); infit!=inflist.end(); ++infit)
@ -135,7 +152,7 @@ struct SortByNameAndWeight : public std::less<BoneWeight>
{
if (b0.first < b1.first)
return true;
else if (b0.first> b1.first)
else if (b0.first > b1.first)
return false;
return (b0.second < b1.second);
}
@ -182,7 +199,8 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
unifyBuffer[boneweightlist].setBoneWeights(boneweightlist);
unifyBuffer[boneweightlist].vertIDs().push_back(vertexID);
}
if(vertex2Bones.size()==unifyBuffer.size()) {
if(vertex2Bones.size()==unifyBuffer.size())
{
OSG_WARN << "VertexInfluenceMap::computeMinimalVertexGroupList is useless no duplicate VertexGroup" << std::endl;
}
uniqVertexGroupList.reserve(unifyBuffer.size());
@ -191,3 +209,92 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
uniqVertexGroupList.push_back(it->second);
}
}
//Expermental
typedef std::vector<osgAnimation::RigGeometry*> RigList;
class CollectRigVisitor : public osg::NodeVisitor
{
public:
META_NodeVisitor(osgAnimation, CollectRigVisitor)
CollectRigVisitor();
//void apply(osg::Node&);
void apply(osg::Geometry& node);
const RigList& getRigList() const;
protected:
RigList _map;
};
CollectRigVisitor::CollectRigVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
//void CollectRigVisitor::apply(osg::Node&) { return; }
void CollectRigVisitor::apply(osg::Geometry& node)
{
RigGeometry* bone = dynamic_cast<RigGeometry*>(&node);
if (bone)
{
_map.push_back( bone);
traverse(node);
}
Skeleton* skeleton = dynamic_cast<Skeleton*>(&node);
if (skeleton)
traverse(node);
}
const RigList& CollectRigVisitor::getRigList() const
{
return _map;
}
void VertexInfluenceMap::removeUnexpressedBones(Skeleton &skel) const
{
BoneMapVisitor mapVisitor;
skel.accept(mapVisitor);
CollectRigVisitor rigvis;
skel.accept(rigvis);
RigList rigs=rigvis.getRigList();
BoneMap boneMap = mapVisitor.getBoneMap();
Bone* child,*par;
for(BoneMap::iterator bmit=boneMap.begin(); bmit!=boneMap.end();)
{
if( this->find(bmit->first) == this->end())
{
bool isusless=true;
for(RigList::iterator rigit=rigs.begin(); rigit != rigs.end(); ++rigit)
{
if( ((*rigit)->getInfluenceMap()->find(bmit->first) !=(*rigit)->getInfluenceMap()->end()))
{
isusless=false;
break;
}
}
if(!isusless||!(par=bmit->second->getBoneParent()))
{
++bmit;
continue;
}
///Bone can be removed
Bone * bone2rm=bmit->second;
for(unsigned int numchild=0; numchild<bone2rm->getNumChildren(); numchild++)
{
if( (child = dynamic_cast<Bone*>(bone2rm->getChild(numchild))) )
{
par->addChild(child);
bone2rm->removeChild(child);
}
}
par->removeChild(bone2rm);
///rebuild bonemap after bone removal
skel.accept(mapVisitor);
boneMap = mapVisitor.getBoneMap();
bmit=boneMap.begin();
}
else ++bmit;
}
}