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:
parent
0d02dfbbbd
commit
78dd81a8b4
@ -24,6 +24,8 @@
|
|||||||
|
|
||||||
namespace osgAnimation
|
namespace osgAnimation
|
||||||
{
|
{
|
||||||
|
class Skeleton;
|
||||||
|
|
||||||
// first is bonename, and second the weight
|
// first is bonename, and second the weight
|
||||||
typedef std::pair<std::string, float> BoneWeight;
|
typedef std::pair<std::string, float> BoneWeight;
|
||||||
// first is vertex index, and second the weight
|
// 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
|
/// compute the minimal VertexGroup Set in which vertices shares the same influence set
|
||||||
void computeMinimalVertexGroupList(std::vector<VertexGroup>&uniqVertexGroupList, unsigned int numvert)const;
|
void computeMinimalVertexGroupList(std::vector<VertexGroup>&uniqVertexGroupList, unsigned int numvert)const;
|
||||||
|
|
||||||
|
//Experimental removal of unexpressed bone from the skeleton
|
||||||
|
void removeUnexpressedBones(Skeleton &skel) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <osgAnimation/VertexInfluence>
|
#include <osgAnimation/VertexInfluence>
|
||||||
|
#include <osgAnimation/RigGeometry>
|
||||||
|
#include <osgAnimation/BoneMapVisitor>
|
||||||
#include <osg/Notify>
|
#include <osg/Notify>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#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;
|
typedef std::pair<float, std::vector<float*> > PerVertWeights;
|
||||||
std::vector<PerVertWeights > localstore;
|
std::vector<PerVertWeights > localstore;
|
||||||
localstore.resize(numvert);
|
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;
|
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;
|
VertexIndexWeight& inf=*curinf;
|
||||||
localstore[inf.first].first+=inf.second;
|
localstore[inf.first].first+=inf.second;
|
||||||
localstore[inf.first].second.push_back(&inf.second);
|
localstore[inf.first].second.push_back(&inf.second);
|
||||||
@ -46,7 +51,8 @@ void VertexInfluenceMap::normalize(unsigned int numvert) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned int vertid=0;
|
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;
|
PerVertWeights & weights=*itvert;
|
||||||
if(weights.first< 1e-4)
|
if(weights.first< 1e-4)
|
||||||
{
|
{
|
||||||
@ -62,41 +68,51 @@ void VertexInfluenceMap::normalize(unsigned int numvert) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
///remove weakest influences in order to fit targetted numbonepervertex
|
///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;
|
typedef std::set<BoneWeight, invweight_ordered > BoneWeightOrdered;
|
||||||
std::map<int,BoneWeightOrdered > tempVec2Bones;
|
std::map<int, BoneWeightOrdered > tempVec2Bones;
|
||||||
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
|
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
|
||||||
{
|
{
|
||||||
const std::string& bonename=mapit->first;
|
const std::string& bonename=mapit->first;
|
||||||
IndexWeightList &curvecinf=mapit->second;
|
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;
|
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;
|
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));
|
else if(inf.second>minweight)tempVec2Bones[inf.first].insert(BoneWeight(bonename, inf.second));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->clear();
|
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;
|
BoneWeightOrdered& bwset=mapit->second;
|
||||||
unsigned int newsize=numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
|
unsigned int newsize=numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
|
||||||
float sum=0;
|
float sum=0;
|
||||||
while(bwset.size()>newsize)bwset.erase(*bwset.rbegin());
|
while(bwset.size()>newsize)bwset.erase(*bwset.rbegin());
|
||||||
if(renormalize){
|
if(renormalize)
|
||||||
|
{
|
||||||
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
|
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
|
||||||
sum+=bwit->second;
|
sum+=bwit->second;
|
||||||
if(sum>1e-4){
|
if(sum>1e-4)
|
||||||
|
{
|
||||||
sum=1.0f/sum;
|
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];
|
VertexInfluence & inf= (*this)[bwit->first];
|
||||||
inf.push_back(VertexIndexWeight(mapit->first, bwit->second*sum));
|
inf.push_back(VertexIndexWeight(mapit->first, bwit->second*sum));
|
||||||
inf.setName(bwit->first);
|
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];
|
VertexInfluence & inf= (*this)[bwit->first];
|
||||||
inf.push_back(VertexIndexWeight(mapit->first,bwit->second));
|
inf.push_back(VertexIndexWeight(mapit->first,bwit->second));
|
||||||
inf.setName(bwit->first);
|
inf.setName(bwit->first);
|
||||||
@ -108,13 +124,14 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert
|
|||||||
|
|
||||||
void VertexInfluenceMap::computePerVertexInfluenceList(std::vector<BoneWeightList>& vertex2Bones,unsigned int numvert)const
|
void VertexInfluenceMap::computePerVertexInfluenceList(std::vector<BoneWeightList>& vertex2Bones,unsigned int numvert)const
|
||||||
{
|
{
|
||||||
vertex2Bones.resize(numvert);
|
vertex2Bones.resize(numvert);
|
||||||
for (osgAnimation::VertexInfluenceMap::const_iterator it = begin();
|
for (osgAnimation::VertexInfluenceMap::const_iterator it = begin();
|
||||||
it != end();
|
it != end();
|
||||||
++it)
|
++it)
|
||||||
{
|
{
|
||||||
const IndexWeightList& inflist = it->second;
|
const IndexWeightList& inflist = it->second;
|
||||||
if (it->first.empty()) {
|
if (it->first.empty())
|
||||||
|
{
|
||||||
OSG_WARN << "VertexInfluenceMap::computePerVertexInfluenceList contains unamed bone IndexWeightList" << std::endl;
|
OSG_WARN << "VertexInfluenceMap::computePerVertexInfluenceList contains unamed bone IndexWeightList" << std::endl;
|
||||||
}
|
}
|
||||||
for(IndexWeightList::const_iterator infit=inflist.begin(); infit!=inflist.end(); ++infit)
|
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)
|
if (b0.first < b1.first)
|
||||||
return true;
|
return true;
|
||||||
else if (b0.first> b1.first)
|
else if (b0.first > b1.first)
|
||||||
return false;
|
return false;
|
||||||
return (b0.second < b1.second);
|
return (b0.second < b1.second);
|
||||||
}
|
}
|
||||||
@ -182,7 +199,8 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
|
|||||||
unifyBuffer[boneweightlist].setBoneWeights(boneweightlist);
|
unifyBuffer[boneweightlist].setBoneWeights(boneweightlist);
|
||||||
unifyBuffer[boneweightlist].vertIDs().push_back(vertexID);
|
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;
|
OSG_WARN << "VertexInfluenceMap::computeMinimalVertexGroupList is useless no duplicate VertexGroup" << std::endl;
|
||||||
}
|
}
|
||||||
uniqVertexGroupList.reserve(unifyBuffer.size());
|
uniqVertexGroupList.reserve(unifyBuffer.size());
|
||||||
@ -191,3 +209,92 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
|
|||||||
uniqVertexGroupList.push_back(it->second);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user