| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/VertexInfluence> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | #include <iostream> |
|---|
| 18 | #include <algorithm> |
|---|
| 19 | |
|---|
| 20 | using namespace osgAnimation; |
|---|
| 21 | |
|---|
| 22 | void VertexInfluenceSet::addVertexInfluence(const VertexInfluence& v) { _bone2Vertexes.push_back(v); } |
|---|
| 23 | const VertexInfluenceSet::VertexIndexToBoneWeightMap& VertexInfluenceSet::getVertexToBoneList() const { return _vertex2Bones;} |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | void VertexInfluenceSet::buildVertex2BoneList() |
|---|
| 27 | { |
|---|
| 28 | _vertex2Bones.clear(); |
|---|
| 29 | for (BoneToVertexList::const_iterator it = _bone2Vertexes.begin(); it != _bone2Vertexes.end(); ++it) |
|---|
| 30 | { |
|---|
| 31 | const VertexInfluence& vi = (*it); |
|---|
| 32 | int size = vi.size(); |
|---|
| 33 | for (int i = 0; i < size; i++) |
|---|
| 34 | { |
|---|
| 35 | VertexIndexWeight viw = vi[i]; |
|---|
| 36 | int index = viw.first; |
|---|
| 37 | float weight = viw.second; |
|---|
| 38 | if (vi.getName().empty()) |
|---|
| 39 | OSG_WARN << "VertexInfluenceSet::buildVertex2BoneList warning vertex " << index << " is not assigned to a bone" << std::endl; |
|---|
| 40 | _vertex2Bones[index].push_back(BoneWeight(vi.getName(), weight)); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | for (VertexIndexToBoneWeightMap::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it) |
|---|
| 46 | { |
|---|
| 47 | BoneWeightList& bones = it->second; |
|---|
| 48 | int size = bones.size(); |
|---|
| 49 | float sum = 0; |
|---|
| 50 | for (int i = 0; i < size; i++) |
|---|
| 51 | sum += bones[i].getWeight(); |
|---|
| 52 | if (sum < 1e-4) |
|---|
| 53 | { |
|---|
| 54 | OSG_WARN << "VertexInfluenceSet::buildVertex2BoneList warning the vertex " << it->first << " seems to have 0 weight, skip normalize for this vertex" << std::endl; |
|---|
| 55 | } |
|---|
| 56 | else |
|---|
| 57 | { |
|---|
| 58 | float mult = 1.0/sum; |
|---|
| 59 | for (int i = 0; i < size; i++) |
|---|
| 60 | bones[i].setWeight(bones[i].getWeight() * mult); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | struct SortByNameAndWeight : public std::less<VertexInfluenceSet::BoneWeight> |
|---|
| 69 | { |
|---|
| 70 | bool operator()(const VertexInfluenceSet::BoneWeight& b0, |
|---|
| 71 | const VertexInfluenceSet::BoneWeight& b1) const |
|---|
| 72 | { |
|---|
| 73 | if (b0.getBoneName() < b1.getBoneName()) |
|---|
| 74 | return true; |
|---|
| 75 | else if (b0.getBoneName() > b1.getBoneName()) |
|---|
| 76 | return false; |
|---|
| 77 | if (b0.getWeight() < b1.getWeight()) |
|---|
| 78 | return true; |
|---|
| 79 | return false; |
|---|
| 80 | } |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | struct SortByBoneWeightList : public std::less<VertexInfluenceSet::BoneWeightList> |
|---|
| 84 | { |
|---|
| 85 | bool operator()(const VertexInfluenceSet::BoneWeightList& b0, |
|---|
| 86 | const VertexInfluenceSet::BoneWeightList& b1) const |
|---|
| 87 | { |
|---|
| 88 | if (b0.size() < b1.size()) |
|---|
| 89 | return true; |
|---|
| 90 | else if (b0.size() > b1.size()) |
|---|
| 91 | return false; |
|---|
| 92 | |
|---|
| 93 | int size = b0.size(); |
|---|
| 94 | for (int i = 0; i < size; i++) |
|---|
| 95 | { |
|---|
| 96 | bool result = SortByNameAndWeight()(b0[i], b1[i]); |
|---|
| 97 | if (result) |
|---|
| 98 | return true; |
|---|
| 99 | else if (SortByNameAndWeight()(b1[i], b0[i])) |
|---|
| 100 | return false; |
|---|
| 101 | } |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | }; |
|---|
| 105 | |
|---|
| 106 | void VertexInfluenceSet::clear() |
|---|
| 107 | { |
|---|
| 108 | _bone2Vertexes.clear(); |
|---|
| 109 | _uniqVertexSetToBoneSet.clear(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void VertexInfluenceSet::buildUniqVertexSetToBoneSetList() |
|---|
| 113 | { |
|---|
| 114 | _uniqVertexSetToBoneSet.clear(); |
|---|
| 115 | |
|---|
| 116 | typedef std::map<BoneWeightList,UniqVertexSetToBoneSet, SortByBoneWeightList> UnifyBoneGroup; |
|---|
| 117 | UnifyBoneGroup unifyBuffer; |
|---|
| 118 | |
|---|
| 119 | for (VertexIndexToBoneWeightMap::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it) |
|---|
| 120 | { |
|---|
| 121 | BoneWeightList bones = it->second; |
|---|
| 122 | int vertexIndex = it->first; |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | std::sort(bones.begin(), bones.end(), SortByNameAndWeight()); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | UnifyBoneGroup::iterator result = unifyBuffer.find(bones); |
|---|
| 129 | if (result == unifyBuffer.end()) |
|---|
| 130 | unifyBuffer[bones].setBones(bones); |
|---|
| 131 | unifyBuffer[bones].getVertexes().push_back(vertexIndex); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | _uniqVertexSetToBoneSet.reserve(unifyBuffer.size()); |
|---|
| 135 | for (UnifyBoneGroup::iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it) |
|---|
| 136 | { |
|---|
| 137 | _uniqVertexSetToBoneSet.push_back(it->second); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|