remove old path and add few fixes

This commit is contained in:
Julien Valentin
2017-08-28 16:46:01 +02:00
parent 6d55d8d341
commit da1d2b67f7
5 changed files with 25 additions and 385 deletions

View File

@@ -1,5 +1,6 @@
/* -*-c++-*-
* Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
* Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
@@ -34,87 +35,6 @@ RigTransformSoftware::RigTransformSoftware(const RigTransformSoftware& rts,const
}
typedef std::vector<RigTransformSoftware::BonePtrWeight> BoneWeightList;
// 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;
}
};
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++)
{
bool result = SortByNameAndWeight()(b0[i], b1[i]);
if (result)
return true;
else if (SortByNameAndWeight()(b1[i], b0[i]))
return false;
}
return false;
}
};
bool RigTransformSoftware::init(RigGeometry& geom)
{
if (!geom.getSkeleton())
return false;
BoneMapVisitor mapVisitor;
geom.getSkeleton()->accept(mapVisitor);
BoneMap bm = mapVisitor.getBoneMap();
initVertexSetFromBones(bm, geom.getVertexInfluenceSet().getUniqVertexGroupList());
if (geom.getSourceGeometry())
geom.copyFrom(*geom.getSourceGeometry());
osg::Vec3Array* normalSrc = dynamic_cast<osg::Vec3Array*>(geom.getSourceGeometry()->getNormalArray());
osg::Vec3Array* positionSrc = dynamic_cast<osg::Vec3Array*>(geom.getSourceGeometry()->getVertexArray());
if(!(positionSrc) || positionSrc->empty() )
return false;
if(normalSrc&& normalSrc->size()!=positionSrc->size())
return false;
geom.setVertexArray(new osg::Vec3Array);
osg::Vec3Array* positionDst =new osg::Vec3Array;
geom.setVertexArray(positionDst);
*positionDst=*positionSrc;
positionDst->setDataVariance(osg::Object::DYNAMIC);
if(normalSrc){
osg::Vec3Array* normalDst =new osg::Vec3Array;
*normalDst=*normalSrc;
geom.setNormalArray(normalDst, osg::Array::BIND_PER_VERTEX);
normalDst->setDataVariance(osg::Object::DYNAMIC);
}
_needInit = false;
return true;
}
void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ){
///1 Create Index2Vec<BoneWeight>
@@ -172,29 +92,19 @@ void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const Rig
///2 Create inverse mapping Vec<BoneWeight>2Vec<Index> from previous built Index2Vec<BoneWeight>
///in order to minimize weighted matrices computation on update
typedef std::map<BoneWeightList, VertexGroup, SortByBoneWeightList> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
_uniqInfluenceSet2VertIDList.clear();
vertexID=0;
for (std::vector<BoneWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it,++vertexID)
{
BoneWeightList& bones = *it;
// sort the vector to have a consistent key
std::sort(bones.begin(), bones.end(), SortByNameAndWeight());
std::sort(bones.begin(), bones.end());
// we use the vector<BoneWeight> as key to differentiate group
UnifyBoneGroup::iterator result = unifyBuffer.find(bones);
if (result == unifyBuffer.end())
unifyBuffer[bones].getBoneWeights()=bones;
unifyBuffer[bones].getVertexes().push_back(vertexID);
VertexGroupSet::iterator result = _uniqInfluenceSet2VertIDList.find(bones);
if (result == _uniqInfluenceSet2VertIDList.end())
_uniqInfluenceSet2VertIDList[bones].getBoneWeights()=bones;
_uniqInfluenceSet2VertIDList[bones].getVertexes().push_back(vertexID);
}
if(_vertex2Bones.size()==unifyBuffer.size()) {
OSG_WARN << "RigTransformSoftware::build mapping is useless no duplicate VertexGroup : too much " <<_vertex2Bones.size()<<"=="<<unifyBuffer.size()<< std::endl;
}
_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;
}
@@ -290,48 +200,3 @@ void RigTransformSoftware::operator()(RigGeometry& geom)
}
}
///convert BoneWeight to BonePtrWeight using bonemap
void RigTransformSoftware::initVertexSetFromBones(const BoneMap& map, const VertexInfluenceSet::UniqVertexGroupList& vertexgroups)
{
_uniqInfluenceSet2VertIDList.clear();
int size = vertexgroups.size();
_uniqInfluenceSet2VertIDList.resize(size);
//for (VertexInfluenceSet::UniqVertexGroupList::const_iterator vgit=vertexgroups.begin(); vgit!=vertexgroups.end();vgit++)
for(int i = 0; i < size; i++)
{
const VertexInfluenceSet::VertexGroup& vg = vertexgroups[i];
int nbBones = vg.getBones().size();
BonePtrWeightList& boneList = _uniqInfluenceSet2VertIDList[i].getBoneWeights();
double sumOfWeight = 0;
for (int b = 0; b < nbBones; b++)
{
const std::string& bname = vg.getBones()[b].getBoneName();
float weight = vg.getBones()[b].getWeight();
BoneMap::const_iterator it = map.find(bname);
if (it == map.end() )
{
if (_invalidInfluence.find(bname) != _invalidInfluence.end()) {
_invalidInfluence[bname] = true;
OSG_WARN << "RigTransformSoftware Bone " << bname << " not found, skip the influence group " <<bname << std::endl;
}
continue;
}
Bone* bone = it->second.get();
boneList.push_back(BonePtrWeight(bone->getName(), weight, bone));
sumOfWeight += weight;
}
// if a bone referenced by a vertexinfluence is missed it can make the sum less than 1.0
// so we check it and renormalize the all weight bone
const double threshold = 1e-4;
if (!vg.getBones().empty() &&
(sumOfWeight < 1.0 - threshold || sumOfWeight > 1.0 + threshold))
{
for (int b = 0; b < (int)boneList.size(); b++)
boneList[b].setWeight(boneList[b].getWeight() / sumOfWeight);
}
_uniqInfluenceSet2VertIDList[i].getVertexes() = vg.getVertexes();
}
}