remove default order for BoneWeight and restore old sort func (behaviors differs)

This commit is contained in:
Julien Valentin 2017-08-28 18:02:52 +02:00
parent ad550acc60
commit 28bb88a038
3 changed files with 57 additions and 17 deletions

View File

@ -127,9 +127,9 @@ namespace osgAnimation
template <class V> void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst) template <class V> void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
{ {
// the result of matrix mult should be cached to be used for vertexes transform and normal transform and maybe other computation // the result of matrix mult should be cached to be used for vertexes transform and normal transform and maybe other computation
for(VertexGroupSet::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg) for(VertexGroupList::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg)
{ {
VertexGroup& uniq = itvg->second; VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet(); uniq.computeMatrixForVertexSet();
osg::Matrix matrix = transform * uniq.getMatrix() * invTransform; osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
@ -145,9 +145,9 @@ namespace osgAnimation
template <class V> void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst) template <class V> void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
{ {
for(VertexGroupSet::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg) for(VertexGroupList::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg)
{ {
VertexGroup& uniq = itvg->second; VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet(); uniq.computeMatrixForVertexSet();
osg::Matrix matrix = transform * uniq.getMatrix() * invTransform; osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
@ -165,10 +165,9 @@ namespace osgAnimation
std::map<std::string,bool> _invalidInfluence; std::map<std::string,bool> _invalidInfluence;
typedef std::vector<BonePtrWeight> BoneWeightList; typedef std::vector<VertexGroup> VertexGroupList;
typedef std::map<BoneWeightList, VertexGroup> VertexGroupSet;
VertexGroupSet _uniqInfluenceSet2VertIDList; VertexGroupList _uniqInfluenceSet2VertIDList;
void buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ); void buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig );
}; };

View File

@ -32,11 +32,6 @@ namespace osgAnimation
inline void setBoneName(const std::string&s){first=s;} inline void setBoneName(const std::string&s){first=s;}
inline const float &getWeight()const{return second;} inline const float &getWeight()const{return second;}
inline void setWeight(float i){second=i;} inline void setWeight(float i){second=i;}
///default invweight ordered
bool operator <(const BoneWeight&o)const{
if(getWeight()>o.getWeight()) return true;
if(getWeight()<o.getWeight()) return false;
return getBoneName()<o.getBoneName();}
}; };
// first is vertex index, and second the weight // first is vertex index, and second the weight
struct IndexWeight: public std::pair<unsigned int, float> struct IndexWeight: public std::pair<unsigned int, float>

View File

@ -35,6 +35,46 @@ RigTransformSoftware::RigTransformSoftware(const RigTransformSoftware& rts,const
} }
// sort by name and weight
struct SortByNameAndWeight : public std::less<RigTransformSoftware::BonePtrWeight>
{
bool operator()(const RigTransformSoftware::BonePtrWeight& b0,
const RigTransformSoftware::BonePtrWeight& b1) const
{
if (b0.getBoneName() < b1.getBoneName())
return true;
else if (b0.getBoneName() > b1.getBoneName())
return false;
if (b0.getWeight() < b1.getWeight())
return true;
return false;
}
};
typedef std::vector<RigTransformSoftware::BonePtrWeight> BoneWeightList;
struct SortByBoneWeightList : public std::less<BoneWeightList>
{
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 RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ){ void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ){
///1 Create Index2Vec<BoneWeight> ///1 Create Index2Vec<BoneWeight>
@ -93,18 +133,24 @@ void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const Rig
///2 Create inverse mapping Vec<BoneWeight>2Vec<Index> from previous built Index2Vec<BoneWeight> ///2 Create inverse mapping Vec<BoneWeight>2Vec<Index> from previous built Index2Vec<BoneWeight>
///in order to minimize weighted matrices computation on update ///in order to minimize weighted matrices computation on update
_uniqInfluenceSet2VertIDList.clear(); _uniqInfluenceSet2VertIDList.clear();
typedef std::map<BoneWeightList, VertexGroup, SortByBoneWeightList> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
vertexID=0; vertexID=0;
for (std::vector<BoneWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it,++vertexID) for (std::vector<BoneWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it,++vertexID)
{ {
BoneWeightList& bones = *it; BoneWeightList& bones = *it;
// sort the vector to have a consistent key // sort the vector to have a consistent key
std::sort(bones.begin(), bones.end()); std::sort(bones.begin(), bones.end(), SortByNameAndWeight() );
// we use the vector<BoneWeight> as key to differentiate group // we use the vector<BoneWeight> as key to differentiate group
VertexGroupSet::iterator result = _uniqInfluenceSet2VertIDList.find(bones); UnifyBoneGroup::iterator result = unifyBuffer.find(bones);
if (result == _uniqInfluenceSet2VertIDList.end()) if (result == unifyBuffer.end())
_uniqInfluenceSet2VertIDList[bones].getBoneWeights()=bones; unifyBuffer[bones].getBoneWeights()=bones;
_uniqInfluenceSet2VertIDList[bones].getVertexes().push_back(vertexID); unifyBuffer[bones].getVertexes().push_back(vertexID);
} }
_uniqInfluenceSet2VertIDList.reserve(unifyBuffer.size());
for (UnifyBoneGroup::iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it)
_uniqInfluenceSet2VertIDList.push_back(it->second);
OSG_DEBUG << "uniq groups " << _uniqInfluenceSet2VertIDList.size() << " for " << rig.getName() << std::endl; OSG_DEBUG << "uniq groups " << _uniqInfluenceSet2VertIDList.size() << " for " << rig.getName() << std::endl;
} }