-
Notifications
You must be signed in to change notification settings - Fork 20
/
BvhParser.pde
500 lines (428 loc) · 11.7 KB
/
BvhParser.pde
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import processing.core.PApplet;
import processing.core.PMatrix3D;
import processing.core.PVector;
class BvhParser {
int frame = 0;
Boolean _motionLoop;
int _currentFrame = 0;
List<BvhLine> _lines;
int _currentLine;
BvhBone _currentBone;
BvhBone _rootBone;
List<List<Float>> _frames;
int _nbFrames;
float _frameTime;
List<BvhBone> _bones;
BvhParser()
{
_motionLoop = true;
}
/**
* if set to True motion will loop at end
*/
Boolean getMotionLoop()
{
return _motionLoop;
}
/**
* set Loop state
* @param value
*/
void setMotionLoop(Boolean value)
{
_motionLoop = value;
}
/**
* to string
* @return
*/
String toStr()
{
return _rootBone.structureToString();
}
/**
* get frame total
* @return
*/
int getNbFrames()
{
return _nbFrames;
}
/**
* get bones list
* @return
*/
List<BvhBone> getBones()
{
return _bones;
}
/**
* call before parse BVH
* create array instance
* and setloopstatus true
*/
void init()
{
_bones = new ArrayList<BvhBone>();
_motionLoop = true;
}
/**
* go to the frame at index
*/
void moveFrameTo(int __index)
{
if(!_motionLoop)
{
if(__index >= _nbFrames)
_currentFrame = _nbFrames-1;//last frame
}else{
while (__index >= _nbFrames)
__index -= _nbFrames;
_currentFrame = __index; //looped frame
}
_updateFrame();
}
/**
* go to millisecond of the BVH
* @param mills millisecond
* @param loopSec the default loopsec for
*/
void moveMsTo( int mills )
{
float frameTime = _frameTime * 1000;
int curFrame = int(mills / frameTime);
moveFrameTo( curFrame );
frame = curFrame;
}
/**
* update bone position and rotation
*/
void update()
{
update( getBones().get(0) );
}
void update(BvhBone bone )
{
PMatrix3D m = new PMatrix3D();
m.translate(bone.getXposition(), bone.getYposition(), bone.getZposition());
m.translate(bone.getOffsetX(), bone.getOffsetY(), bone.getOffsetZ());
m.rotateY(PApplet.radians(bone.getYrotation()));
m.rotateX(PApplet.radians(bone.getXrotation()));
m.rotateZ(PApplet.radians(bone.getZrotation()));
bone.global_matrix = m;
if (bone.getParent() != null && bone.getParent().global_matrix != null)
m.preApply(bone.getParent().global_matrix);
m.mult(new PVector(), bone.getAbsPosition());
if (bone.getChildren().size() > 0)
{
for (BvhBone child : bone.getChildren())
{
update(child);
}
}
else
{
m.translate(bone.getEndOffsetX(), bone.getEndOffsetY(), bone.getEndOffsetZ());
m.mult(new PVector(), bone.getAbsEndPosition());
}
}
void _updateFrame()
{
if (_currentFrame >= _frames.size()) return;
List<Float> frame = _frames.get(_currentFrame);
int count = 0;
for (float n : frame)
{
BvhBone bone = _getBoneInFrameAt(count);
String prop = _getBonePropInFrameAt(count);
if(bone != null) {
Method getterMethod;
try {
getterMethod = bone.getClass().getDeclaredMethod("set".concat(prop), new Class[]{float.class});
getterMethod.invoke(bone, n);
} catch (SecurityException e) {
e.printStackTrace();
System.err.println("ERROR WHILST GETTING FRAME - 1");
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.err.println("ERROR WHILST GETTING FRAME - 2");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println("ERROR WHILST GETTING FRAME - 3");
} catch (IllegalAccessException e) {
e.printStackTrace();
System.err.println("ERROR WHILST GETTING FRAME - 4");
} catch (InvocationTargetException e) {
e.printStackTrace();
System.err.println("ERROR WHILST GETTING FRAME - 5");
}
}
count++;
}
}
String _getBonePropInFrameAt(int n)
{
int c = 0;
for (BvhBone bone : _bones)
{
if (c + bone.getNbChannels() > n)
{
n -= c;
return bone.getChannels().get(n);
}else{
c += bone.getNbChannels();
}
}
return null;
}
BvhBone _getBoneInFrameAt( int n)
{
int c = 0;
for (BvhBone bone : _bones)
{
c += bone.getNbChannels();
if ( c > n )
return bone;
}
return null;
}
void parse(String[] srces)
{
String[] linesStr = srces;
// liste de BvhLines
_lines = new ArrayList<BvhLine>();
for ( String lineStr : linesStr)
_lines.add(new BvhLine(lineStr));
_currentLine = 1;
_rootBone = _parseBone();
// center locs
//_rootBone.offsetX = _rootBone.offsetY = _rootBone.offsetZ = 0;
_parseFrames();
}
void _parseFrames()
{
int currentLine = _currentLine;
for (; currentLine < _lines.size(); currentLine++)
if(_lines.get(currentLine).getLineType() == BvhLine.MOTION) break;
if ( _lines.size() > currentLine)
{
currentLine++; //Frames
_nbFrames = _lines.get(currentLine).getNbFrames();
currentLine++; //FrameTime
_frameTime = _lines.get(currentLine).getFrameTime();
currentLine++;
_frames = new ArrayList<List<Float>>();
for (; currentLine < _lines.size(); currentLine++)
{
_frames.add(_lines.get(currentLine).getFrames());
}
}
}
BvhBone _parseBone()
{
//_currentBone is Parent
BvhBone bone = new BvhBone( _currentBone );
_bones.add(bone);
bone.setName( _lines.get(_currentLine)._boneName ); //1
// +2 OFFSET
_currentLine++; // 2 {
_currentLine++; // 3 OFFSET
bone.setOffsetX( _lines.get(_currentLine).getOffsetX() );
bone.setOffsetY( _lines.get(_currentLine).getOffsetY() );
bone.setOffsetZ( _lines.get(_currentLine).getOffsetZ() );
// +3 CHANNELS
_currentLine++;
bone.setnbChannels( _lines.get(_currentLine).getNbChannels() );
bone.setChannels( _lines.get(_currentLine).getChannelsProps() );
// +4 JOINT or End Site or }
_currentLine++;
while(_currentLine < _lines.size())
{
String lineType = _lines.get(_currentLine).getLineType();
if ( BvhLine.BONE.equals( lineType ) ) //JOINT or ROOT
{
BvhBone child = _parseBone(); //generate new BvhBONE
child.setParent( bone );
bone.getChildren().add(child);
}
else if( BvhLine.END_SITE.equals( lineType ) )
{
_currentLine++; // {
_currentLine++; // OFFSET
bone.setEndOffsetX( _lines.get(_currentLine).getOffsetX() );
bone.setEndOffsetY( _lines.get(_currentLine).getOffsetY() );
bone.setEndOffsetZ( _lines.get(_currentLine).getOffsetZ() );
_currentLine++; //}
_currentLine++; //}
return bone;
}
else if( BvhLine.BRACE_CLOSED.equals( lineType ) )
{
return bone; //}
}
_currentLine++;
}
System.out.println("//Something strage");
return bone;
}
class BvhLine
{
static final String HIERARCHY = "HIERARCHY";
static final String BONE = "BONE";
static final String BRACE_OPEN = "BRACE_OPEN";
static final String BRACE_CLOSED = "BRACE_CLOSED";
static final String OFFSET = "OFFSET";
static final String CHANNELS = "CHANNELS";
static final String END_SITE = "END_SITE";
static final String MOTION = "MOTION";
static final String FRAMES = "FRAMES";
static final String FRAME_TIME = "FRAME_TIME";
static final String FRAME = "FRAME";
static final String BONE_TYPE_ROOT = "ROOT";
static final String BONE_TYPE_JOINT = "JOINT";
String _lineStr;
String _lineType;
String _boneType;
String _boneName;
float _offsetX;
float _offsetY;
float _offsetZ;
int _nbChannels;
List<String> _channelsProps;
int _nbFrames;
float _frameTime;
List<Float> _frames;
String toString()
{
return _lineStr;
}
void _parse(String __lineStr)
{
_lineStr = __lineStr;
_lineStr = _lineStr.trim();
_lineStr = _lineStr.replace("\t", "");
_lineStr = _lineStr.replace("\n", "");
_lineStr = _lineStr.replace("\r", "");
String[] words = _lineStr.split(" ");
_lineType = _parseLineType(words);
//
if ( HIERARCHY.equals(_lineType) )
{
return;
} else if ( BONE.equals(_lineType) ) {
_boneType = (words[0] == "ROOT") ? BONE_TYPE_ROOT : BONE_TYPE_JOINT;
_boneName = words[1];
return;
} else if ( OFFSET.equals(_lineType) ) {
_offsetX = Float.valueOf(words[1]);
_offsetY = Float.valueOf(words[2]);
_offsetZ = Float.valueOf(words[3]);
return;
} else if ( CHANNELS.equals(_lineType) ) {
_nbChannels = Integer.valueOf(words[1]);
_channelsProps = new ArrayList<String>();
for (int i = 0; i < _nbChannels; i++)
_channelsProps.add(words[i+2]);
return;
} else if (FRAMES.equals(_lineType) ) {
_nbFrames = Integer.valueOf(words[1]);
return;
} else if ( FRAME_TIME.equals(_lineType) ) {
_frameTime = Float.valueOf(words[2]);
return;
} else if ( FRAME.equals(_lineType) ) {
_frames = new ArrayList<Float>();
for (String word : words) _frames.add(Float.valueOf(word));
return;
} else if ( END_SITE.equals(_lineType) ||
BRACE_OPEN.equals(_lineType) ||
BRACE_CLOSED.equals(_lineType) ||
MOTION.equals(_lineType)) {
return;
}
}
String _parseLineType( String[] __words) {
//trace("'" + __words[0] + "' : " + __words[0].length);
if ( "HIERARCHY".equals(__words[ 0 ] ) )
return HIERARCHY;
if ( "ROOT".equals(__words[ 0 ] ) ||
"JOINT".equals(__words[ 0 ] ) )
return BONE;
if ( "{".equals(__words[ 0 ] ) )
return BRACE_OPEN;
if ( "}".equals(__words[ 0 ] ) )
return BRACE_CLOSED;
if ( "OFFSET".equals(__words[ 0 ] ) )
return OFFSET;
if ( "CHANNELS".equals(__words[ 0 ] ) )
return CHANNELS;
if ( "End".equals(__words[ 0 ] ) )
return END_SITE;
if ( "MOTION".equals(__words[ 0 ] ) )
return MOTION;
if ( "Frames:".equals(__words[ 0 ] ) )
return FRAMES;
if ( "Frame".equals(__words[ 0 ] ) )
return FRAME_TIME;
try {
Float.parseFloat(__words[0]); //check is Parsable
return FRAME;
} catch ( NumberFormatException e) {
e.printStackTrace();
}
return null;
}
BvhLine(String __lineStr)
{
_parse(__lineStr);
}
List<Float> getFrames()
{
return _frames;
}
float getFrameTime()
{
return _frameTime;
}
int getNbFrames()
{
return _nbFrames;
}
List<String> getChannelsProps()
{
return _channelsProps;
}
int getNbChannels()
{
return _nbChannels;
}
float getOffsetZ()
{
return _offsetZ;
}
float getOffsetY()
{
return _offsetY;
}
float getOffsetX()
{
return _offsetX;
}
String getBoneName()
{
return _boneName;
}
String getBoneType()
{
return _boneType;
}
String getLineType()
{
return _lineType;
}
}
}