From b3402d9344f9fd877cbc27cdc43ce4d60ad082ad Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Tue, 29 Aug 2017 00:34:26 +0200 Subject: [PATCH] readd the 2 methods in InfluenceMap just in case --- include/osgAnimation/VertexInfluence | 4 ++ src/osgAnimation/VertexInfluence.cpp | 90 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/include/osgAnimation/VertexInfluence b/include/osgAnimation/VertexInfluence index 317a45e87..1b35b2c0f 100644 --- a/include/osgAnimation/VertexInfluence +++ b/include/osgAnimation/VertexInfluence @@ -81,6 +81,10 @@ namespace osgAnimation ///remove weakest influences in order to fit targetted numbonepervertex void cullInfluenceCountPerVertex(unsigned int maxnumbonepervertex, float minweight=0, bool renormalize=true); + //compute PerVertexInfluenceList + void computePerVertexInfluenceList(std::vector& vertex2Bones,unsigned int numvert)const; + //create the minimal VertexGroup set + void computeMinimalVertexGroupList(std::vector&uniqVertexGroupList,unsigned int numvert)const; }; } diff --git a/src/osgAnimation/VertexInfluence.cpp b/src/osgAnimation/VertexInfluence.cpp index 3038e8374..dabaca32f 100644 --- a/src/osgAnimation/VertexInfluence.cpp +++ b/src/osgAnimation/VertexInfluence.cpp @@ -102,3 +102,93 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert } } } + +void VertexInfluenceMap::computePerVertexInfluenceList(std::vector& vertex2Bones,unsigned int numvert)const +{ + vertex2Bones.resize(numvert); + for (osgAnimation::VertexInfluenceMap::const_iterator it = begin(); + it != end(); + ++it) + { + const BoneInfluenceList& inflist = it->second; + if (inflist.getBoneName().empty()) { + OSG_WARN << "RigTransformSoftware::VertexInfluenceMap contains unamed bone BoneInfluenceList" << std::endl; + } + for(BoneInfluenceList::const_iterator infit=inflist.begin(); infit!=inflist.end(); ++infit) + { + const IndexWeight &iw = *infit; + const unsigned int &index = iw.getIndex(); + float weight = iw.getWeight(); + + vertex2Bones[index].push_back(BoneWeight(inflist.getBoneName(), weight));; + } + } +} + +// sort by name and weight +struct SortByNameAndWeight : public std::less +{ + bool operator()(const BoneWeight& b0, + const BoneWeight& b1) const + { + if (b0.getBoneName() < b1.getBoneName()) + return true; + else if (b0.getBoneName() > b1.getBoneName()) + return false; + return (b0.getWeight() < b1.getWeight()); + } +}; + +struct SortByBoneWeightList : public std::less +{ + bool operator()(const BoneWeightList& b0, + const BoneWeightList& b1) const + { + if (b0.size() < b1.size()) + return true; + else if (b0.size() > b1.size()) + return false; + + int size = b0.size(); + for (int i = 0; i < size; i++) + { + if (SortByNameAndWeight()(b0[i], b1[i])) + return true; + else if (SortByNameAndWeight()(b1[i], b0[i])) + return false; + } + return false; + } +}; +void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector&uniqVertexGroupList,unsigned int numvert)const +{ + uniqVertexGroupList.clear(); + std::vector vertex2Bones; + computePerVertexInfluenceList(vertex2Bones,numvert); + typedef std::map UnifyBoneGroup; + UnifyBoneGroup unifyBuffer; + + unsigned int vertexID=0; + for (std::vector::iterator it = vertex2Bones.begin(); it != vertex2Bones.end(); ++it,++vertexID) + { + BoneWeightList &boneweightlist = *it;//->second; + //int vertexIndex = it->first; + + // sort the vector to have a consistent key + std::sort(boneweightlist.begin(), boneweightlist.end(), SortByNameAndWeight()); + + // we use the vector as key to differentiate group + UnifyBoneGroup::iterator result = unifyBuffer.find(boneweightlist); + if (result == unifyBuffer.end()) + unifyBuffer[boneweightlist].setBoneWeights(boneweightlist); + unifyBuffer[boneweightlist].vertIDs().push_back(vertexID); + } + if(vertex2Bones.size()==unifyBuffer.size()) { + OSG_WARN << "VertexInfluenceSet::buildmap is useless no duplicate VertexGroup" << std::endl; + } + uniqVertexGroupList.reserve(unifyBuffer.size()); + for (UnifyBoneGroup::iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it) + { + uniqVertexGroupList.push_back(it->second); + } +}