-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBVH_Parsing.cpp
349 lines (308 loc) · 9 KB
/
BVH_Parsing.cpp
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
/**
*@brief bvh_parser : parse bvh data
dataTransform : transform bvh data(joint rotation angles,offset) to 3-dimensional points,
3D points can be used to draw the motion
recommend you rewrite the code with one-dimensional array
*@author [email protected]
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
/////////////////////////////////////////////////////////////////
/**
*stack ,C++, zero-based
*clear() push() top() empty() pop() size()
*free()
*/
typedef int T;
struct myStack{
int curr,size_limit;
T *array;
myStack(int s){/*input stack size*/
curr = -1; size_limit = s;
array = new T[s];
}
void clear(){curr = -1;}/*empty stack*/
void free(){ if(array != 0) delete [] array; array = 0; clear();}/*free stack*/
T top(){
if( curr > -1 && curr < size_limit ){return array[curr];}
else return -1;}
bool push(T v){
if(curr + 1 < size_limit){ array[++curr] = v; return true;}
return false;}
bool empty(){
if(curr < 0)return true;
else return false;}
void pop(){curr--;}
int size(){return curr + 1;}
~myStack()
{
assert(array == 0);/*release memory explicitly*/
}
};
//////////////////////////////////////////////////////////////////////////
///define
#define mymin(a,b) (a>b?b:a)
#define mymax(a,b) (a>b?a:b)
#define MAX_JOINT 100
#define MAX_FRAME 1000
#define MAX_COL (3 + MAX_JOINT*3)
///global varibles
struct joint
{
int id;//one-based,[1,MAX_JOINT]
int channel;//channel {3,6}
int channels[6];//{0,1,2} - {X,Y,Z}
char joint_name[10];
double offset[3];//offset value
}joint_hiry[ MAX_JOINT + 1 ];
//bvh parser
int joint_num;//joint count
int joint_end;//END SITE count
int channels_num;//total channels
int frame_num;//frame count
double frame_time;//time per frame
int parent_of[MAX_JOINT+1];//parent of joint, parent_of[j] = -1 denote j is illegal, = 0 denote j is root joint
bool is_end[MAX_JOINT + 1];//mark end site joint
double mat[ MAX_FRAME ][ MAX_COL ];//original MOTION data
void bvh_parser(const char *bvh_dir)
{
FILE *bvh = fopen(bvh_dir,"r");
if(bvh == NULL)
{
printf("[open file error] %s\n",bvh_dir);
return;
}
//declarition
char buffer[256];
myStack my(MAX_JOINT+1);
//initialization
joint_num = 0;
joint_end = 0;
channels_num = 0;
memset(parent_of,-1,sizeof(parent_of));
memset(is_end,0,sizeof(is_end));
parent_of[1] = 0;
//hierarchy construct
while( fscanf(bvh,"%s",buffer) != EOF )
{
if( strcmp(buffer,"{") == 0 )
{
my.push(joint_num);
}
else if( strcmp(buffer,"}") == 0 )
{
int c = my.top(); my.pop();
if(c == 1)
{
//back to root
break;
}
parent_of[ c ] = my.top();
}
else if( strcmp(buffer,"OFFSET") == 0 )
{
fscanf(bvh,"%lf%lf%lf",&joint_hiry[joint_num].offset[0],\
&joint_hiry[joint_num].offset[1],\
&joint_hiry[joint_num].offset[2]);
}
else if( strcmp(buffer,"CHANNELS") == 0)
{
fscanf(bvh,"%d",&joint_hiry[joint_num].channel);
channels_num += joint_hiry[joint_num].channel;
for(int i = 0;i < joint_hiry[joint_num].channel; i++)
{
fscanf(bvh,"%s",buffer);
//x = 0, y = 1, z = 2
//position rotation | rotation
if( buffer[0] == 'X')
{
joint_hiry[joint_num].channels[i] = 0;
}
else if( buffer[0] == 'Y' )
{
joint_hiry[joint_num].channels[i] = 1;
}
else if( buffer[0] == 'Z')
{
joint_hiry[joint_num].channels[i] = 2;
}
}
}
else if( strcmp(buffer,"JOINT") == 0 || strcmp(buffer,"ROOT") == 0 || strcmp(buffer,"End") == 0)
{
fscanf(bvh,"%s",joint_hiry[++joint_num].joint_name);
if(buffer[0] == 'E')
{
is_end[joint_num] = true;
joint_end++;
}
}
}
my.free();
//end of construction
fscanf(bvh,"%s",buffer);fscanf(bvh,"%s",buffer);
fscanf(bvh,"%d",&frame_num);
fscanf(bvh,"%s",buffer);fscanf(bvh,"%s",buffer);
fscanf(bvh,"%lf",&frame_time);
int col = 6 + (joint_num - joint_end - 1)*3;
assert(col == channels_num);
assert(frame_num < MAX_FRAME);
assert(joint_num < MAX_JOINT);
for(int i = 0;i < frame_num;i++)
{
for(int j = 0;j < channels_num;j++)
{
fscanf(bvh,"%lf",&mat[i][j]);
}
}
fclose( bvh ); bvh = 0;
}
///define
#define PI 3.14159265358979323846
///
const int DOUBLE_SIZE = sizeof(double);
const double unitM[4][4] = { {1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1} };//unit matrix
double M[ MAX_JOINT + 1][4][4];//transformation matrix
double Tmat[ MAX_FRAME ][MAX_JOINT+1][ 3 ];//transformed data(3D points)
/**
*matrix multiplication
* a[4][4] X b[4][4] = rs[4][4]
* a[4][4] X b[4][1] = rs[4][1]
*rs will be overwrite
*/
void matrixMulti444(double rs[][4],double a[][4],double b[][4])
{
double temp[4][4]; memset(temp,0,sizeof(temp));
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
for(int k = 0;k < 4;k++)
{
temp[i][j] += a[i][k]*b[k][j];
}
}
}
memcpy(rs,temp,sizeof(temp));
}
void matrixMulti441(double rs[],double a[][4],double b[])
{
double temp[4]; memset(temp,0,sizeof(temp));
for(int i = 0;i < 4;i++)
{
for(int k = 0;k < 4;k++)
{
temp[i] += a[i][k]*b[k];
}
}
memcpy(rs,temp,sizeof(temp));
}
void getTransMatrix(double rs[][4],int f,int j,int end_num)
{
///T*R
//rs = T
double T[4][4] = {
{1,0,0,joint_hiry[j].offset[0]},
{0,1,0,joint_hiry[j].offset[1]},
{0,0,1,joint_hiry[j].offset[2]},
{0,0,0,1}};
memcpy(rs,T,sizeof(T));
//rs = rs*R
int i = 0;
if(joint_hiry[j].channel == 6)
{
//root joint
for(;i < 3;i++)
{
//position
if(joint_hiry[j].channels[i] == 0)
{
//Xposition
double t[4][4] = {
{1,0,0,mat[f][i]},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
else if(joint_hiry[j].channels[i] == 1)
{
//Yposition
double t[4][4] = {
{1,0,0,0},
{0,1,0,mat[f][i]},
{0,0,1,0},
{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
else{
//Zposition
double t[4][4] = {
{1,0,0,0},
{0,1,0,0},
{0,0,1,mat[f][i]},
{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
}
}
int off_col = (j-end_num)*3;
for(int k = 0;k < 3;i++,k++)
{
//rotation
double x = mat[f][ off_col + k]*PI/180;
if(joint_hiry[j].channels[i] == 0)
{
//Xrotation
double t[4][4] = { {1,0,0,0},{0,cos(x),-sin(x),0},{0,sin(x),cos(x),0},{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
else if(joint_hiry[j].channels[i] == 1)
{
//Yrotation
double t[4][4] = { {cos(x),0,sin(x),0},{0,1,0,0},{-sin(x),0,cos(x),0},{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
else{
//Zrotation
double t[4][4] = { {cos(x),-sin(x),0,0},{sin(x),cos(x),0,0},{0,0,1,0},{0,0,0,1} };
matrixMulti444(rs,rs,t);
}
}
}
void dataTransform()
{
memcpy(M[0],unitM,sizeof(unitM));
for(int i = 0;i < frame_num;i++)
{
int end_num = 0;
for(int j = 1;j <= joint_num;j++)
{
if(is_end[j] == false)
{
//M[parent_of[j]]*R(channels[0])*R()...
double trans[4][4];
getTransMatrix(trans,i,j,end_num);
matrixMulti444(M[j], M[parent_of[j]], trans);
}
else end_num++;
//S[4][1] = {0,0,0,1}
//E[4][1] = {offset[0],offset[1],offset[2],1};
//E = M[parent_of[j]]*E S = M[parent_of[j]]*S
if(j == 1)
{
double S[4] = {0,0,0,1};
matrixMulti441(S,M[1],S);
memcpy(Tmat[i][0],S,3*DOUBLE_SIZE);
}
else{
double E[4] = {joint_hiry[j].offset[0],joint_hiry[j].offset[1],joint_hiry[j].offset[2],1};
matrixMulti441(E,M[ parent_of[j] ],E);
memcpy(Tmat[i][j-1],E,3*DOUBLE_SIZE);
}
}
}
}
int main(){return 0;}