summaryrefslogtreecommitdiffstats
path: root/dev/a4-dance/skeleton.cc
blob: f7d8c45e96d457592f906e5e20e7c8e814416450 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "skeleton.h"
#include "amc_util.h"


Skeleton::Skeleton() : using_degrees_(false) {
}

Skeleton::~Skeleton() {
    
}

int Skeleton::num_root_bones() const {
    return (int)root_bones_.size();
}

std::string Skeleton::root_bone(int i) const {
    return root_bones_[i];
}

int Skeleton::num_children(const std::string &bone_name) const {
    if (children_.find(bone_name) == children_.end()) {
        return 0;
    }
    else {
        return (int)children_.find(bone_name)->second.size();
    }
}

std::string Skeleton::child_bone(const std::string &bone_name, int i) const {
    return children_.find(bone_name)->second[i];
}

bool Skeleton::rx_dof(const std::string &bone_name) const {
    return rx_dof_.find(bone_name)->second;
}

Vector2 Skeleton::rx_limits(const std::string &bone_name) const {
    return rx_limits_.find(bone_name)->second;
}

bool Skeleton::ry_dof(const std::string &bone_name) const {
    return ry_dof_.find(bone_name)->second;
}

Vector2 Skeleton::ry_limits(const std::string &bone_name) const {
    return ry_limits_.find(bone_name)->second;
}

bool Skeleton::rz_dof(const std::string &bone_name) const {
    return rz_dof_.find(bone_name)->second;
}

Vector2 Skeleton::rz_limits(const std::string &bone_name) const {
    return rz_limits_.find(bone_name)->second;
}

int Skeleton::degrees_of_freedom(const std::string &bone_name) const {
    return (int)rx_dof(bone_name) + (int)ry_dof(bone_name) + (int)rz_dof(bone_name);
}


Vector3 Skeleton::BoneDirectionAndLength(const std::string &bone_name) const {
    return lengths_.find(bone_name)->second * directions_.find(bone_name)->second;
}

Matrix4 Skeleton::BoneSpaceToRotAxesSpace(const std::string &bone_name) const {
    return bone_to_rot_axes_.find(bone_name)->second;
}

Matrix4 Skeleton::RotAxesSpaceToBoneSpace(const std::string &bone_name) const {
    return rot_axes_to_bone_.find(bone_name)->second;
}

Matrix4 Skeleton::BoneSpaceToChildrenSpace(const std::string &bone_name) const {
    return bone_to_children_.find(bone_name)->second;
}


const bool ABORT_ON_ERROR=true;
void assume(bool b) {
  if (!b && ABORT_ON_ERROR) {
    std::abort();
  }
}


void Skeleton::LoadFromASF(const std::string &asf_filename) {
    std::ifstream in(asf_filename.c_str());
    SimpleParser parser(&in);
    
    while (parser.Good()) {
        if (parser.Expect("#")) {
            //std::cerr << "Ignoring comment line" << std::endl;
            parser.SwallowLine();
        }
        
        else if (parser.Expect(":version")) {
            parser.SwallowLine();
        }
        
        else if (parser.Expect(":name")) {
            //std::cerr << "Swallowing name" << std::endl;
            parser.SwallowLine();
        }
        
        else if (parser.Expect(":units")) {
            //std::cerr << "Reading units" << std::endl;
            ParseUnits(&parser);
        }
        
        else if (parser.Expect(":documentation")) {
            //std::cerr << "Reading documentation" << std::endl;
            while (parser.Good() && !parser.Peek(":")) {
                parser.SwallowLine();
            }
        }
        
        else if (parser.Expect(":root")) {
            //std::cerr << "Reading root" << std::endl;
            ParseRoot(&parser);
        }
        
        else if (parser.Expect(":bonedata")) {
            //std::cerr << "Reading bonedata" << std::endl;
            ParseBonedata(&parser);
        }
        
        else if (parser.Expect(":hierarchy")) {
            //std::cerr << "Reading hierarchy" << std::endl;
            ParseHierarchy(&parser);
        }
        
        else {
            std::string tok;
            parser.ReadToken(tok);
            if (!parser.Good()) {
                break;
            }
            std::cerr << "Encountered unknown token" << std::endl;
            std::cerr << "'" << tok << "'" << std::endl;
            std::abort();
        }
    } // end while (looping over file)
}


void Skeleton::ParseUnits(SimpleParser *parser) {
    bool cont;
    do {
        cont = false;
        
        if (parser->Expect("mass")) {
            float trash;
            parser->ReadFloat(trash);
            cont = true;
        }
        
        if (parser->Expect("length")) {
            float trash;
            parser->ReadFloat(trash);
            cont = true;
        }
        
        if (parser->Expect("angle")) {
            std::string token;
            parser->ReadToken(token);
            if (token == "deg") {
                using_degrees_ = true;
            }
        }
    } while (cont);
}

void Skeleton::ParseRoot(SimpleParser *parser) {
    bool cont;
    do {
        cont = false;
        
        if (parser->Expect("order")) {
            cont = true;
            if (!parser->Expect("TX TY TZ RX RY RZ")) {
                std::cerr << "'order' not in order expected" << std::endl;
                std::abort();
            }
        }
        
        if (parser->Expect("axis")) {
            cont = true;
            if (!parser->Expect("XYZ")) {
                std::cerr << "'axis' not in order expected" << std::endl;
                std::abort();
            }
        }
        
        if (parser->Expect("position")) {
            cont = true;
            parser->ReadFloat(root_position_[0]);
            parser->ReadFloat(root_position_[1]);
            parser->ReadFloat(root_position_[2]);
            root_position_ = amc2meter(root_position_);
        }
        
        if (parser->Expect("orientation")) {
            cont = true;
            parser->ReadFloat(root_orientation_[0]);
            parser->ReadFloat(root_orientation_[1]);
            parser->ReadFloat(root_orientation_[2]);
        }
        
    } while (cont);
}


void Skeleton::ParseBonedata(SimpleParser *parser) {
    while (parser->Expect("begin")) {
        int id = -1;
        std::string name;
        Vector3 direction;
        float length = 0.0;
        Matrix4 bone_to_rot_axes;
        Matrix4 rot_axes_to_bone;
        bool rx_dof = false;
        bool ry_dof = false;
        bool rz_dof = false;
        Vector2 rx_limits;
        Vector2 ry_limits;
        Vector2 rz_limits;
        
        while (!parser->Expect("end")) {
            
            if (parser->Expect("id")) {
                parser->ReadInt(id);
            }
            
            if (parser->Expect("name")) {
                parser->ReadToken(name);
            }
            
            if (parser->Expect("direction")) {
                parser->ReadFloat(direction[0]);
                parser->ReadFloat(direction[1]);
                parser->ReadFloat(direction[2]);
            }
            
            if (parser->Expect("length")) {
                parser->ReadFloat(length);
                length = amc2meter(length);
            }
            
            if (parser->Expect("axis")) {
                float ax, ay, az;
                std::string axis_type;
                parser->ReadFloat(ax);
                parser->ReadFloat(ay);
                parser->ReadFloat(az);
                if (using_degrees_) {
                    ax = GfxMath::ToRadians(ax);
                    ay = GfxMath::ToRadians(ay);
                    az = GfxMath::ToRadians(az);
                }
                parser->ReadToken(axis_type);
                if (axis_type == "XYZ") {
                    rot_axes_to_bone = Matrix4::RotationZ(az) *
                        Matrix4::RotationY(ay) *
                        Matrix4::RotationX(ax);
                    bone_to_rot_axes = rot_axes_to_bone.Inverse();
                }
                else {
                    std::cerr << "'axis type' not in order expected" << std::endl;
                    std::abort();
                }
            }
            
            if (parser->Expect("dof")) {
                rx_dof = parser->Expect("rx");
                ry_dof = parser->Expect("ry");
                rz_dof = parser->Expect("rz");
            }
            
            
            if (parser->Expect("limits")) {
                int ndof = (int)rx_dof + (int)ry_dof + (int)rz_dof;
                for (int dof=0; dof < ndof; dof++) {
                    assume(parser->Expect("("));
                    float min, max;
                    parser->ReadFloat(min);
                    parser->ReadFloat(max);
                    assume(parser->Expect(")"));
                    
                    if (dof == 0) {
                        if (rx_dof)      rx_limits = Vector2(min, max);
                        else if (ry_dof) ry_limits = Vector2(min, max);
                        else if (rz_dof) rz_limits = Vector2(min, max);
                        else abort();
                    }
                    else if (dof == 1) {
                        if (rx_dof && ry_dof)      ry_limits = Vector2(min, max);
                        else if (rx_dof && rz_dof) rz_limits = Vector2(min, max);
                        else if (ry_dof && rz_dof) rz_limits = Vector2(min, max);
                        else abort();
                    }
                    else if (dof == 2) {
                        if (rx_dof && ry_dof && rz_dof) rz_limits = Vector2(min, max);
                        else abort();
                    }
                    else {
                        std::cerr << "bad dof limits" << std::endl;
                        abort();
                    }
                }
            }
        } // read "end" token
        
        // save the bone to the skeleton data structure
        ids_[name] = id;
        directions_[name] = direction;
        lengths_[name] = length;
        rx_dof_[name] = rx_dof;
        ry_dof_[name] = ry_dof;
        rz_dof_[name] = rz_dof;
        rx_limits_[name] = rx_limits;
        ry_limits_[name] = ry_limits;
        rz_limits_[name] = rz_limits;
        bone_to_rot_axes_[name] = bone_to_rot_axes;
        rot_axes_to_bone_[name] = rot_axes_to_bone;
        bone_to_children_[name] = Matrix4::Translation(length * direction);
    }
}

void Skeleton::ParseHierarchy(SimpleParser *parser) {
    if (!parser->Expect("begin")) {
        std::cerr << "Reading hierarchy, expected 'begin', not found" << std::endl;
        std::abort();
    }
    while (!parser->Expect("end")) {
        std::string line;
        std::string parent;
        parser->ReadToken(parent);
        parser->ReadLine(line);
        
        std::stringstream ss(line);
        std::string child;
        ss >> child;
        while (ss) {
            if (parent == "root") {
                root_bones_.push_back(child);
            }
            else {
                children_[parent].push_back(child);
            }
            ss >> child;
        }
    }
}