-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathMostInfluencedGeometryByBone
More file actions
229 lines (182 loc) · 7.76 KB
/
Copy pathMostInfluencedGeometryByBone
File metadata and controls
229 lines (182 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* -*-c++-*- OpenSceneGraph - Copyright (C) Sketchfab
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial
* applications, as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#ifndef MOST_INFLUENCED_GEOMETRY_BY_BONE_H
#define MOST_INFLUENCED_GEOMETRY_BY_BONE_H
#include <algorithm>
#include <osg/NodeVisitor>
#include <osg/Geometry>
#include <osg/Array>
#include <osgAnimation/RigGeometry>
#include <osgAnimation/Bone>
#include "StatLogger"
class InfluenceAttribute;
//{
// "Bone001": {
// Geom1: {
// numVertexInfluenced: (int),
// gloabalWeight: (float)
// },
// Geom2: {
// numVertexInfluenced: (int),
// gloabalWeight: (float)
// },
// ...
// },
// "Bone002": {
// Geom1: {
// numVertexInfluenced: (int),
// gloabalWeight: (float)
// },
// Geom4: {
// numVertexInfluenced: (int),
// gloabalWeight: (float)
// },
// ...
// },
// ...
//}
//
//Here we store influences by bone, we will sort it and take the biggest one
typedef std::map< osgAnimation::Bone*, std::map< osgAnimation::RigGeometry*, InfluenceAttribute > > RigGeometryInfluenceByBoneMap;
typedef std::map< osgAnimation::RigGeometry*, InfluenceAttribute > BoneInfluenceMap;
typedef std::pair< osgAnimation::RigGeometry*, InfluenceAttribute > BoneInfluence;
typedef std::vector< BoneInfluence > BoneInfluences;
typedef std::set< osgAnimation::RigGeometry* > RigGeometrySet;
typedef std::set< osgAnimation::Bone* > BoneSet;
//Here we simply collect all bones and all rigGeometries
class CollectBonesAndRigGeometriesVisitor: public osg::NodeVisitor {
public:
CollectBonesAndRigGeometriesVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{}
void apply(osg::Geometry &geometry) {
osgAnimation::RigGeometry *rigGeometry = dynamic_cast<osgAnimation::RigGeometry*>(&geometry);
if(rigGeometry) {
_rigGeometrySet.insert(rigGeometry);
}
traverse(geometry);
}
void apply(osg::MatrixTransform &node) {
osgAnimation::Bone *bone = dynamic_cast<osgAnimation::Bone*>(&node);
if(bone) {
_boneSet.insert(bone);
}
traverse(node);
}
RigGeometrySet& getRigGeometrySet() {
return _rigGeometrySet;
}
BoneSet& getBoneSet() {
return _boneSet;
}
protected:
RigGeometrySet _rigGeometrySet;
BoneSet _boneSet;
};
//Store and compute influence attributes i.e number of influenced vertex and accumulate weight
class InfluenceAttribute {
public:
InfluenceAttribute():
_accumulatedWeight(0),
_weightCount(0)
{}
void addWeight(float weight) {
_accumulatedWeight += weight;
_weightCount++;
}
unsigned int getNumInfluencedVertex() {
return _weightCount;
}
unsigned int getNumInfluencedVertex() const {
return _weightCount;
}
float getNormalizedWeight() const {
if(_weightCount == 0) return 0;
return _accumulatedWeight / _weightCount;
}
protected:
float _accumulatedWeight;
unsigned int _weightCount;
};
typedef std::pair< std::string, osgAnimation::Bone* > StringBonePair;
typedef std::pair< osgAnimation::RigGeometry*, unsigned int > RigGeometryIntPair;
class BoneNameBoneMap : public std::map<std::string, osgAnimation::Bone*> {
public:
BoneNameBoneMap(const BoneSet& bones) {
for(BoneSet::const_iterator bone = bones.begin(); bone != bones.end(); ++bone) {
insert(StringBonePair((*bone)->getName(), *bone));
}
}
};
class RigGeometryIndexMap : public std::map<osgAnimation::RigGeometry*, unsigned int> {
public:
RigGeometryIndexMap(const RigGeometrySet& rigGeometrySet) {
unsigned int index = 0;
for(RigGeometrySet::const_iterator rigGeometry = rigGeometrySet.begin(); rigGeometry != rigGeometrySet.end(); ++rigGeometry, ++index) {
insert(RigGeometryIntPair(*rigGeometry, index));
}
}
};
class ComputeMostInfluencedGeometryByBone {
public:
ComputeMostInfluencedGeometryByBone(RigGeometrySet &rigGeometrySet, BoneSet &boneSet):
_rigGeometrySet(rigGeometrySet),
_boneSet(boneSet),
_logger("ComputeMostInfluencedGeometryByBone::compute(...)")
{}
void compute() {
RigGeometryIndexMap rigGeometryIndexMap(_rigGeometrySet);
RigGeometryInfluenceByBoneMap ribbm;
computeInfluences(_boneSet, _rigGeometrySet, ribbm);
for(RigGeometryInfluenceByBoneMap::iterator boneInfluencePair = ribbm.begin(); boneInfluencePair != ribbm.end(); ++boneInfluencePair) {
osg::ref_ptr<osgAnimation::Bone> bone = boneInfluencePair->first;
BoneInfluenceMap boneInfluenceMap = boneInfluencePair->second;
BoneInfluences influences(boneInfluenceMap.begin(), boneInfluenceMap.end());
std::sort(influences.begin(), influences.end(), sort_influences());
bone->setUserValue("rigIndex", rigGeometryIndexMap [ influences.front().first ]);
}
RigGeometrySet &rigGeometrySet(_rigGeometrySet);
for(RigGeometrySet::iterator rigGeometry = rigGeometrySet.begin(); rigGeometry != rigGeometrySet.end(); ++rigGeometry) {
(*rigGeometry)->setUserValue("rigIndex", rigGeometryIndexMap[ *rigGeometry ]);
}
}
protected:
void computeInfluences(const BoneSet& bones, const RigGeometrySet& rigGeometries, RigGeometryInfluenceByBoneMap& rigGeometryInfluenceByBoneMap) {
BoneNameBoneMap boneMap(bones);
for(RigGeometrySet::const_iterator rigGeometry = rigGeometries.begin(); rigGeometry != rigGeometries.end(); ++rigGeometry) {
osg::ref_ptr<osgAnimation::VertexInfluenceMap> vertexInfluenceMap = (*rigGeometry)->getInfluenceMap();
for(osgAnimation::VertexInfluenceMap::iterator vertexInfluencePair = vertexInfluenceMap->begin(); vertexInfluencePair != vertexInfluenceMap->end(); ++vertexInfluencePair) {
BoneNameBoneMap::iterator bone_it = boneMap.find(vertexInfluencePair->first);
if(bone_it == boneMap.end()) continue;
osg::ref_ptr<osgAnimation::Bone> bone = bone_it->second;
const osgAnimation::VertexInfluence& vertexInfluence = (*vertexInfluencePair).second;
for(osgAnimation::VertexInfluence::const_iterator vertexIndexWeight = vertexInfluence.begin(); vertexIndexWeight != vertexInfluence.end(); ++vertexIndexWeight) {
rigGeometryInfluenceByBoneMap[bone.get()][*rigGeometry].addWeight((*vertexIndexWeight).second);
}
}
}
}
struct sort_influences {
//We sort influences by number of influenced vertex first and then by normalized weight (number_of_vertex_influence / accumulated_weight)
//i.e we choose to keep geometries with many vertex instead of geometries with high normalized weight, it makes more sense for geometry
//selection via bone influence box @see AABBonBoneVisitor class
bool operator()(const BoneInfluence &a, const BoneInfluence &b) {
return (a.second.getNumInfluencedVertex() > b.second.getNumInfluencedVertex()) ||
(a.second.getNumInfluencedVertex() == b.second.getNumInfluencedVertex() && \
a.second.getNormalizedWeight() > b.second.getNormalizedWeight());
}
};
RigGeometrySet &_rigGeometrySet;
BoneSet &_boneSet;
StatLogger _logger;
};
#endif // MOST_INFLUENCED_GEOMETRY_BY_BONE_H