-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBVH.cpp
More file actions
349 lines (302 loc) · 9.67 KB
/
Copy pathBVH.cpp
File metadata and controls
349 lines (302 loc) · 9.67 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
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
#include "BVH.h"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
BVH::BVH()
{
load_success = false;
}
BVH::BVH(const char* filename)
{
std::string filenamestr(filename);
load_success = false;
load(filename);
}
BVH::~BVH()
{
clear();
}
void BVH::clear()
{
nameToJoint.clear();
jointList.clear();
channels.clear();
load_success = false;
rootJoint = NULL;
}
void BVH::load(const std::string& bvh_file_name)
{
#define BUFFER_LENGTH 1024*4
std::ifstream file;
char line[BUFFER_LENGTH];
char* token;
char separater[] = " :,\t";
std::vector<JOINT*> joint_stack;
JOINT* joint = NULL;
JOINT* new_joint = NULL;
bool is_site = false;
double x, y ,z;
clear();
file.open(bvh_file_name, std::ios::in);
if (! file.is_open()) return;
while (! file.eof())
{
if (file.eof()) goto bvh_error;
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
if ( token == NULL ) continue;
if ( strcmp( token, "{" ) == 0 )
{
joint_stack.push_back( joint );
joint = new_joint;
continue;
}
if ( strcmp( token, "}" ) == 0 )
{
joint = joint_stack.back();
joint_stack.pop_back();
is_site = false;
continue;
}
if ( ( strcmp( token, "ROOT" ) == 0 ) ||
( strcmp( token, "JOINT" ) == 0 ) )
{
new_joint = new JOINT();
new_joint->parent = joint;
new_joint->is_site = false;
new_joint->offset.x = 0.0; new_joint->offset.y = 0.0; new_joint->offset.z = 0.0;
//add to joint collection
jointList.push_back( new_joint );
//add children
if ( joint )
joint->children.push_back( new_joint );
else
rootJoint = new_joint; //the root
token = strtok( NULL, "" );
while ( *token == ' ' ) token ++;
new_joint->name = std::string(token);
nameToJoint[ new_joint->name ] = new_joint;
continue;
}
if ( ( strcmp( token, "End" ) == 0 ) )
{
new_joint = new JOINT();
new_joint->parent = joint;
new_joint->name = "EndSite";
new_joint->is_site = true;
new_joint->channels.clear();
//add children
if ( joint )
joint->children.push_back( new_joint );
else
rootJoint = new_joint; //can an endsite be root? -cuizh
//add to joint collection
jointList.push_back( new_joint );
is_site = true;
continue;
}
if ( strcmp( token, "OFFSET" ) == 0 )
{
token = strtok( NULL, separater );
x = token ? atof( token ) : 0.0;
token = strtok( NULL, separater );
y = token ? atof( token ) : 0.0;
token = strtok( NULL, separater );
z = token ? atof( token ) : 0.0;
joint->offset.x = x;
joint->offset.y = y;
joint->offset.z = z;
continue;
}
if ( strcmp( token, "CHANNELS" ) == 0 )
{
token = strtok( NULL, separater );
joint->channels.resize( token ? atoi( token ) : 0 );
for ( uint i=0; i<joint->channels.size(); i++ )
{
CHANNEL* channel = new CHANNEL();
channel->index = channels.size();
channels.push_back(channel);
joint->channels[i] = channel;
token = strtok( NULL, separater );
if ( strcmp( token, "Xrotation" ) == 0 )
channel->type = X_ROTATION;
else if ( strcmp( token, "Yrotation" ) == 0 )
channel->type = Y_ROTATION;
else if ( strcmp( token, "Zrotation" ) == 0 )
channel->type = Z_ROTATION;
else if ( strcmp( token, "Xposition" ) == 0 )
channel->type = X_POSITION;
else if ( strcmp( token, "Yposition" ) == 0 )
channel->type = Y_POSITION;
else if ( strcmp( token, "Zposition" ) == 0 )
channel->type = Z_POSITION;
}
}
if ( strcmp( token, "MOTION" ) == 0 )
break;
}
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
if ( strcmp( token, "Frames" ) != 0 ) goto bvh_error;
token = strtok( NULL, separater );
if ( token == NULL ) goto bvh_error;
motionData.num_frames = atoi( token );
file.getline( line, BUFFER_LENGTH );
token = strtok( line, ":" );
if ( strcmp( token, "Frame Time" ) != 0 ) goto bvh_error;
token = strtok( NULL, separater );
if ( token == NULL ) goto bvh_error;
motionData.frame_time = atof( token );
motionData.num_motion_channels = channels.size();
motionData.data = new float[motionData.num_frames*motionData.num_motion_channels];
for (uint i=0; i<motionData.num_frames; i++)
{
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
for ( uint j=0; j<motionData.num_motion_channels; j++ )
{
if (token == NULL)
goto bvh_error;
motionData.data[i*motionData.num_motion_channels+j] = atof(token);
token = strtok( NULL, separater );
}
}
file.close();
load_success = true;
return;
bvh_error:
file.close();
}
JOINT* BVH::getJoint(std::string name)
{
std::map<std::string, JOINT*>::const_iterator i = nameToJoint.find(name);
JOINT* j = (i!=nameToJoint.end()) ? (*i).second:NULL;
if(j==NULL){
std::cout<<"JOINT <"<<name<<"> is not loaded!\n";
}
return j;
}
float* BVH::getMotionDataPtr(int frame_no)
{
frame_no%=motionData.num_frames;
return motionData.data+frame_no*motionData.num_motion_channels;
}
void BVH::matrixMoveJoint(JOINT* joint, float* mdata, float scale)
{
// translate identity matrix to this joint's offset parameters
joint->matrix = glm::translate(glm::mat4(1.0),
glm::vec3(joint->offset.x*scale,
joint->offset.y*scale,
joint->offset.z*scale));
// transform based on joint channels
// end site will have channels.size() == 0, won't be an issue
for(uint i = 0; i < joint->channels.size(); i++)
{
// extract value from motion data
CHANNEL *channel = joint->channels[i];
float value = mdata[channel->index];
switch(channel->type){
case X_POSITION:
joint->matrix = glm::translate(joint->matrix, glm::vec3(value*scale, 0, 0));
break;
case Y_POSITION:
joint->matrix = glm::translate(joint->matrix, glm::vec3(0, value*scale, 0));
break;
case Z_POSITION:
joint->matrix = glm::translate(joint->matrix, glm::vec3(0, 0, value*scale));
break;
case X_ROTATION:
joint->matrix = glm::rotate(joint->matrix, value, glm::vec3(1, 0, 0));
break;
case Y_ROTATION:
joint->matrix = glm::rotate(joint->matrix, value, glm::vec3(0, 1, 0));
break;
case Z_ROTATION:
joint->matrix = glm::rotate(joint->matrix, value, glm::vec3(0, 0, 1));
break;
}
}
// apply parent's local transfomation matrix to this joint's LTM (local tr. mtx. :)
// watch out for the order
if( joint->parent != NULL )
joint->matrix = joint->parent->matrix * joint->matrix;
// do the same to all children
for(std::vector<JOINT*>::iterator child = joint->children.begin(); child != joint->children.end(); ++child)
matrixMoveJoint(*child, mdata,scale);
}
void BVH::matrixMoveTo(unsigned frame, float scale)
{
// we calculate motion data's array start index for a frame
unsigned start_index = frame * motionData.num_motion_channels;
// recursively transform skeleton
matrixMoveJoint(rootJoint, getMotionDataPtr(frame), scale);
}
void BVH::quaternionMoveJoint(JOINT* joint, float* mdata, float scale)
{
// translate to the offset and set the rotation to identity
joint->transform.translation = glm::vec3(joint->offset.x * scale,
joint->offset.y * scale,
joint->offset.z * scale);
joint->transform.quaternion = glm::quat();
// --------------------------------------
// TODO: [Part 2b - Forward Kinematics]
// --------------------------------------
// maintain the translation and the quaternion in the joint->transform properly
//
// NOTE: calculating transformation using matrix and converting to quaternion
// will not be counted as the valid solution.
for (uint i = 0; i < joint->channels.size(); i++)
{
// extract value from motion data
CHANNEL *channel = joint->channels[i];
float value = mdata[channel->index];
float displacement = value * scale;
glm::vec3 x_ax(1, 0, 0);
glm::vec3 y_ax(0, 1, 0);
glm::vec3 z_ax(0, 0, 1);
switch(channel->type){
case X_POSITION:
joint->transform.translation += glm::vec3(displacement, 0, 0);
break;
case Y_POSITION:
joint->transform.translation += glm::vec3(0, displacement, 0);
break;
case Z_POSITION:
joint->transform.translation += glm::vec3(0, 0, displacement);
break;
case X_ROTATION:
{
joint->transform.quaternion = glm::rotate(joint->transform.quaternion, value, x_ax);
break;
}
case Y_ROTATION:
{
joint->transform.quaternion = glm::rotate(joint->transform.quaternion, value, y_ax);
break;
}
case Z_ROTATION:
{
joint->transform.quaternion = glm::rotate(joint->transform.quaternion, value,z_ax);
break;
}
}
}
// apply parent's transfomation matrix to this joint to make the transformation global
if (joint->parent != NULL) {
glm::vec3 rotated_vec = glm::rotate(joint->parent->transform.quaternion, joint->transform.translation);
joint->transform.translation = joint->parent->transform.translation + rotated_vec;
joint->transform.quaternion = joint->parent->transform.quaternion * joint->transform.quaternion;
}
// do the same to all children
for(std::vector<JOINT*>::iterator child = joint->children.begin(); child != joint->children.end(); ++child)
quaternionMoveJoint(*child, mdata, scale);
}
void BVH::quaternionMoveTo(unsigned frame, float scale)
{
// we calculate motion data's array start index for a frame
unsigned start_index = frame * motionData.num_motion_channels;
// recursively transform skeleton
quaternionMoveJoint(rootJoint, getMotionDataPtr(frame), scale);
}