forked from siana/SingularityViewer
/
llbvhloader.cpp
1579 lines (1379 loc) · 46.8 KB
/
llbvhloader.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file llbvhloader.cpp
* @brief Translates a BVH files to LindenLabAnimation format.
*
* $LicenseInfo:firstyear=2004&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llbvhloader.h"
#include <boost/tokenizer.hpp>
#include "lldatapacker.h"
#include "lldir.h"
#include "llkeyframemotion.h"
#include "llquantize.h"
#include "llstl.h"
#define INCHES_TO_METERS 0.02540005f
const F32 POSITION_KEYFRAME_THRESHOLD_SQUARED = 0.03f * 0.03f;
const F32 ROTATION_KEYFRAME_THRESHOLD = 0.01f;
const F32 POSITION_MOTION_THRESHOLD_SQUARED = 0.001f * 0.001f;
const F32 ROTATION_MOTION_THRESHOLD = 0.001f;
char gInFile[1024]; /* Flawfinder: ignore */
char gOutFile[1024]; /* Flawfinder: ignore */
/*
//------------------------------------------------------------------------
// Status Codes
//------------------------------------------------------------------------
const char *LLBVHLoader::ST_OK = "Ok";
const char *LLBVHLoader::ST_EOF = "Premature end of file.";
const char *LLBVHLoader::ST_NO_CONSTRAINT = "Can't read constraint definition.";
const char *LLBVHLoader::ST_NO_FILE = "Can't open BVH file.";
const char *LLBVHLoader::ST_NO_HIER = "Invalid HIERARCHY header.";
const char *LLBVHLoader::ST_NO_JOINT = "Can't find ROOT or JOINT.";
const char *LLBVHLoader::ST_NO_NAME = "Can't get JOINT name.";
const char *LLBVHLoader::ST_NO_OFFSET = "Can't find OFFSET.";
const char *LLBVHLoader::ST_NO_CHANNELS = "Can't find CHANNELS.";
const char *LLBVHLoader::ST_NO_ROTATION = "Can't get rotation order.";
const char *LLBVHLoader::ST_NO_AXIS = "Can't get rotation axis.";
const char *LLBVHLoader::ST_NO_MOTION = "Can't find MOTION.";
const char *LLBVHLoader::ST_NO_FRAMES = "Can't get number of frames.";
const char *LLBVHLoader::ST_NO_FRAME_TIME = "Can't get frame time.";
const char *LLBVHLoader::ST_NO_POS = "Can't get position values.";
const char *LLBVHLoader::ST_NO_ROT = "Can't get rotation values.";
const char *LLBVHLoader::ST_NO_XLT_FILE = "Can't open translation file.";
const char *LLBVHLoader::ST_NO_XLT_HEADER = "Can't read translation header.";
const char *LLBVHLoader::ST_NO_XLT_NAME = "Can't read translation names.";
const char *LLBVHLoader::ST_NO_XLT_IGNORE = "Can't read translation ignore value.";
const char *LLBVHLoader::ST_NO_XLT_RELATIVE = "Can't read translation relative value.";
const char *LLBVHLoader::ST_NO_XLT_OUTNAME = "Can't read translation outname value.";
const char *LLBVHLoader::ST_NO_XLT_MATRIX = "Can't read translation matrix.";
const char *LLBVHLoader::ST_NO_XLT_MERGECHILD = "Can't get mergechild name.";
const char *LLBVHLoader::ST_NO_XLT_MERGEPARENT = "Can't get mergeparent name.";
const char *LLBVHLoader::ST_NO_XLT_PRIORITY = "Can't get priority value.";
const char *LLBVHLoader::ST_NO_XLT_LOOP = "Can't get loop value.";
const char *LLBVHLoader::ST_NO_XLT_EASEIN = "Can't get easeIn values.";
const char *LLBVHLoader::ST_NO_XLT_EASEOUT = "Can't get easeOut values.";
const char *LLBVHLoader::ST_NO_XLT_HAND = "Can't get hand morph value.";
const char *LLBVHLoader::ST_NO_XLT_EMOTE = "Can't read emote name.";
const char *LLBVHLoader::ST_BAD_ROOT = "Illegal ROOT joint.";
*/
//------------------------------------------------------------------------
// find_next_whitespace()
//------------------------------------------------------------------------
const char *find_next_whitespace(const char *p)
{
while(*p && isspace(*p)) p++;
while(*p && !isspace(*p)) p++;
return p;
}
//------------------------------------------------------------------------
// bvhStringToOrder()
//
// XYZ order in BVH files must be passed to mayaQ() as ZYX.
// This function reverses the input string before passing it on
// to StringToOrder().
//------------------------------------------------------------------------
LLQuaternion::Order bvhStringToOrder( char *str )
{
char order[4]; /* Flawfinder: ignore */
order[0] = str[2];
order[1] = str[1];
order[2] = str[0];
order[3] = 0;
LLQuaternion::Order retVal = StringToOrder( order );
return retVal;
}
//-----------------------------------------------------------------------------
// LLBVHLoader()
//-----------------------------------------------------------------------------
/*
LLBVHLoader::LLBVHLoader(const char* buffer)
{
reset();
mStatus = loadTranslationTable("anim.ini");
if (mStatus == LLBVHLoader::ST_NO_XLT_FILE)
{
LL_WARNS() << "NOTE: No translation table found." << LL_ENDL;
return;
}
else
{
if (mStatus != LLBVHLoader::ST_OK)
{
LL_WARNS() << "ERROR: [line: " << getLineNumber() << "] " << mStatus << LL_ENDL;
return;
}
}
char error_text[128]; // Flawfinder: ignore
S32 error_line;
mStatus = loadBVHFile(buffer, error_text, error_line);
if (mStatus != LLBVHLoader::ST_OK)
{
LL_WARNS() << "ERROR: [line: " << getLineNumber() << "] " << mStatus << LL_ENDL;
return;
}
applyTranslations();
optimize();
mInitialized = TRUE;
}
*/
LLBVHLoader::LLBVHLoader(const char* buffer, ELoadStatus &loadStatus, S32 &errorLine)
{
reset();
errorLine = 0;
mStatus = loadTranslationTable("anim.ini");
loadStatus = mStatus;
LL_INFOS() << "Load Status 00 : " << loadStatus << LL_ENDL;
if (mStatus == E_ST_NO_XLT_FILE)
{
//LL_WARNS() << "NOTE: No translation table found." << LL_ENDL;
loadStatus = mStatus;
return;
}
else
{
if (mStatus != E_ST_OK)
{
//LL_WARNS() << "ERROR: [line: " << getLineNumber() << "] " << mStatus << LL_ENDL;
errorLine = getLineNumber();
loadStatus = mStatus;
return;
}
}
char error_text[128]; /* Flawfinder: ignore */
S32 error_line;
mStatus = loadBVHFile(buffer, error_text, error_line);
if (mStatus != E_ST_OK)
{
//LL_WARNS() << "ERROR: [line: " << getLineNumber() << "] " << mStatus << LL_ENDL;
loadStatus = mStatus;
errorLine = getLineNumber();
return;
}
applyTranslations();
optimize();
mInitialized = TRUE;
}
LLBVHLoader::~LLBVHLoader()
{
std::for_each(mJoints.begin(),mJoints.end(),DeletePointer());
}
//------------------------------------------------------------------------
// LLBVHLoader::loadTranslationTable()
//------------------------------------------------------------------------
ELoadStatus LLBVHLoader::loadTranslationTable(const char *fileName)
{
mLineNumber = 0;
mTranslations.clear();
mConstraints.clear();
//--------------------------------------------------------------------
// open file
//--------------------------------------------------------------------
std::string path = gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS,fileName);
llifstream infstream(path);
if (!infstream.is_open())
return E_ST_NO_XLT_FILE;
LL_INFOS() << "NOTE: Loading translation table: " << fileName << LL_ENDL;
//--------------------------------------------------------------------
// register file to be closed on function exit
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// load header
//--------------------------------------------------------------------
if ( ! getLine(infstream) )
return E_ST_EOF;
if ( strncmp(mLine, "Translations 1.0", 16) )
return E_ST_NO_XLT_HEADER;
//--------------------------------------------------------------------
// load data one line at a time
//--------------------------------------------------------------------
BOOL loadingGlobals = FALSE;
Translation *trans = NULL;
while ( getLine(infstream) )
{
//----------------------------------------------------------------
// check the 1st token on the line to determine if it's empty or a comment
//----------------------------------------------------------------
char token[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %127s", token) != 1 ) /* Flawfinder: ignore */
continue;
if (token[0] == '#')
continue;
//----------------------------------------------------------------
// check if a [jointName] or [GLOBALS] was specified.
//----------------------------------------------------------------
if (token[0] == '[')
{
char name[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " [%127[^]]", name) != 1 )
return E_ST_NO_XLT_NAME;
if (strcmp(name, "GLOBALS")==0)
{
loadingGlobals = TRUE;
continue;
}
else
{
loadingGlobals = FALSE;
Translation &newTrans = mTranslations[ name ];
trans = &newTrans;
continue;
}
}
//----------------------------------------------------------------
// check for optional emote
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "emote")==0)
{
char emote_str[1024]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %1023s", emote_str) != 1 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_EMOTE;
mEmoteName.assign( emote_str );
// LL_INFOS() << "NOTE: Emote: " << mEmoteName.c_str() << LL_ENDL;
continue;
}
//----------------------------------------------------------------
// check for global priority setting
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "priority")==0)
{
S32 priority;
if ( sscanf(mLine, " %*s = %d", &priority) != 1 )
return E_ST_NO_XLT_PRIORITY;
mPriority = priority;
// LL_INFOS() << "NOTE: Priority: " << mPriority << LL_ENDL;
continue;
}
//----------------------------------------------------------------
// check for global loop setting
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "loop")==0)
{
char trueFalse[128]; /* Flawfinder: ignore */
trueFalse[0] = '\0';
F32 loop_in = 0.f;
F32 loop_out = 1.f;
if ( sscanf(mLine, " %*s = %f %f", &loop_in, &loop_out) == 2 )
{
mLoop = TRUE;
}
else if ( sscanf(mLine, " %*s = %127s", trueFalse) == 1 ) /* Flawfinder: ignore */
{
mLoop = (LLStringUtil::compareInsensitive(trueFalse, "true")==0);
}
else
{
return E_ST_NO_XLT_LOOP;
}
mLoopInPoint = loop_in * mDuration;
mLoopOutPoint = loop_out * mDuration;
continue;
}
//----------------------------------------------------------------
// check for global easeIn setting
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "easein")==0)
{
F32 duration;
char type[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %f %127s", &duration, type) != 2 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_EASEIN;
mEaseIn = duration;
continue;
}
//----------------------------------------------------------------
// check for global easeOut setting
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "easeout")==0)
{
F32 duration;
char type[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %f %127s", &duration, type) != 2 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_EASEOUT;
mEaseOut = duration;
continue;
}
//----------------------------------------------------------------
// check for global handMorph setting
//----------------------------------------------------------------
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "hand")==0)
{
S32 handMorph;
if (sscanf(mLine, " %*s = %d", &handMorph) != 1)
return E_ST_NO_XLT_HAND;
mHand = handMorph;
continue;
}
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "constraint")==0)
{
Constraint constraint;
// try reading optional target direction
if(sscanf( /* Flawfinder: ignore */
mLine,
" %*s = %d %f %f %f %f %15s %f %f %f %15s %f %f %f %f %f %f",
&constraint.mChainLength,
&constraint.mEaseInStart,
&constraint.mEaseInStop,
&constraint.mEaseOutStart,
&constraint.mEaseOutStop,
constraint.mSourceJointName,
&constraint.mSourceOffset.mV[VX],
&constraint.mSourceOffset.mV[VY],
&constraint.mSourceOffset.mV[VZ],
constraint.mTargetJointName,
&constraint.mTargetOffset.mV[VX],
&constraint.mTargetOffset.mV[VY],
&constraint.mTargetOffset.mV[VZ],
&constraint.mTargetDir.mV[VX],
&constraint.mTargetDir.mV[VY],
&constraint.mTargetDir.mV[VZ]) != 16)
{
if(sscanf( /* Flawfinder: ignore */
mLine,
" %*s = %d %f %f %f %f %15s %f %f %f %15s %f %f %f",
&constraint.mChainLength,
&constraint.mEaseInStart,
&constraint.mEaseInStop,
&constraint.mEaseOutStart,
&constraint.mEaseOutStop,
constraint.mSourceJointName,
&constraint.mSourceOffset.mV[VX],
&constraint.mSourceOffset.mV[VY],
&constraint.mSourceOffset.mV[VZ],
constraint.mTargetJointName,
&constraint.mTargetOffset.mV[VX],
&constraint.mTargetOffset.mV[VY],
&constraint.mTargetOffset.mV[VZ]) != 13)
{
return E_ST_NO_CONSTRAINT;
}
}
else
{
// normalize direction
if (!constraint.mTargetDir.isExactlyZero())
{
constraint.mTargetDir.normVec();
}
}
constraint.mConstraintType = CONSTRAINT_TYPE_POINT;
mConstraints.push_back(constraint);
continue;
}
if (loadingGlobals && LLStringUtil::compareInsensitive(token, "planar_constraint")==0)
{
Constraint constraint;
// try reading optional target direction
if(sscanf( /* Flawfinder: ignore */
mLine,
" %*s = %d %f %f %f %f %15s %f %f %f %15s %f %f %f %f %f %f",
&constraint.mChainLength,
&constraint.mEaseInStart,
&constraint.mEaseInStop,
&constraint.mEaseOutStart,
&constraint.mEaseOutStop,
constraint.mSourceJointName,
&constraint.mSourceOffset.mV[VX],
&constraint.mSourceOffset.mV[VY],
&constraint.mSourceOffset.mV[VZ],
constraint.mTargetJointName,
&constraint.mTargetOffset.mV[VX],
&constraint.mTargetOffset.mV[VY],
&constraint.mTargetOffset.mV[VZ],
&constraint.mTargetDir.mV[VX],
&constraint.mTargetDir.mV[VY],
&constraint.mTargetDir.mV[VZ]) != 16)
{
if(sscanf( /* Flawfinder: ignore */
mLine,
" %*s = %d %f %f %f %f %15s %f %f %f %15s %f %f %f",
&constraint.mChainLength,
&constraint.mEaseInStart,
&constraint.mEaseInStop,
&constraint.mEaseOutStart,
&constraint.mEaseOutStop,
constraint.mSourceJointName,
&constraint.mSourceOffset.mV[VX],
&constraint.mSourceOffset.mV[VY],
&constraint.mSourceOffset.mV[VZ],
constraint.mTargetJointName,
&constraint.mTargetOffset.mV[VX],
&constraint.mTargetOffset.mV[VY],
&constraint.mTargetOffset.mV[VZ]) != 13)
{
return E_ST_NO_CONSTRAINT;
}
}
else
{
// normalize direction
if (!constraint.mTargetDir.isExactlyZero())
{
constraint.mTargetDir.normVec();
}
}
constraint.mConstraintType = CONSTRAINT_TYPE_PLANE;
mConstraints.push_back(constraint);
continue;
}
//----------------------------------------------------------------
// at this point there must be a valid trans pointer
//----------------------------------------------------------------
if ( ! trans )
return E_ST_NO_XLT_NAME;
//----------------------------------------------------------------
// check for ignore flag
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "ignore")==0 )
{
char trueFalse[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %127s", trueFalse) != 1 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_IGNORE;
trans->mIgnore = (LLStringUtil::compareInsensitive(trueFalse, "true")==0);
continue;
}
//----------------------------------------------------------------
// check for relativepos flag
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "relativepos")==0 )
{
F32 x, y, z;
char relpos[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %f %f %f", &x, &y, &z) == 3 )
{
trans->mRelativePosition.setVec( x, y, z );
}
else if ( sscanf(mLine, " %*s = %127s", relpos) == 1 ) /* Flawfinder: ignore */
{
if ( LLStringUtil::compareInsensitive(relpos, "firstkey")==0 )
{
trans->mRelativePositionKey = TRUE;
}
else
{
return E_ST_NO_XLT_RELATIVE;
}
}
else
{
return E_ST_NO_XLT_RELATIVE;
}
continue;
}
//----------------------------------------------------------------
// check for relativerot flag
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "relativerot")==0 )
{
//F32 x, y, z;
char relpos[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %127s", relpos) == 1 ) /* Flawfinder: ignore */
{
if ( LLStringUtil::compareInsensitive(relpos, "firstkey")==0 )
{
trans->mRelativeRotationKey = TRUE;
}
else
{
return E_ST_NO_XLT_RELATIVE;
}
}
else
{
return E_ST_NO_XLT_RELATIVE;
}
continue;
}
//----------------------------------------------------------------
// check for outname value
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "outname")==0 )
{
char outName[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %127s", outName) != 1 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_OUTNAME;
trans->mOutName = outName;
continue;
}
//----------------------------------------------------------------
// check for frame matrix value
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "frame")==0 )
{
LLMatrix3 fm;
if ( sscanf(mLine, " %*s = %f %f %f, %f %f %f, %f %f %f",
&fm.mMatrix[0][0], &fm.mMatrix[0][1], &fm.mMatrix[0][2],
&fm.mMatrix[1][0], &fm.mMatrix[1][1], &fm.mMatrix[1][2],
&fm.mMatrix[2][0], &fm.mMatrix[2][1], &fm.mMatrix[2][2] ) != 9 )
return E_ST_NO_XLT_MATRIX;
trans->mFrameMatrix = fm;
continue;
}
//----------------------------------------------------------------
// check for offset matrix value
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "offset")==0 )
{
LLMatrix3 om;
if ( sscanf(mLine, " %*s = %f %f %f, %f %f %f, %f %f %f",
&om.mMatrix[0][0], &om.mMatrix[0][1], &om.mMatrix[0][2],
&om.mMatrix[1][0], &om.mMatrix[1][1], &om.mMatrix[1][2],
&om.mMatrix[2][0], &om.mMatrix[2][1], &om.mMatrix[2][2] ) != 9 )
return E_ST_NO_XLT_MATRIX;
trans->mOffsetMatrix = om;
continue;
}
//----------------------------------------------------------------
// check for mergeparent value
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "mergeparent")==0 )
{
char mergeParentName[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %127s", mergeParentName) != 1 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_MERGEPARENT;
trans->mMergeParentName = mergeParentName;
continue;
}
//----------------------------------------------------------------
// check for mergechild value
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "mergechild")==0 )
{
char mergeChildName[128]; /* Flawfinder: ignore */
if ( sscanf(mLine, " %*s = %127s", mergeChildName) != 1 ) /* Flawfinder: ignore */
return E_ST_NO_XLT_MERGECHILD;
trans->mMergeChildName = mergeChildName;
continue;
}
//----------------------------------------------------------------
// check for per-joint priority
//----------------------------------------------------------------
if ( LLStringUtil::compareInsensitive(token, "priority")==0 )
{
S32 priority;
if ( sscanf(mLine, " %*s = %d", &priority) != 1 )
return E_ST_NO_XLT_PRIORITY;
trans->mPriorityModifier = priority;
continue;
}
}
infstream.close() ;
return E_ST_OK;
}
//------------------------------------------------------------------------
// LLBVHLoader::loadBVHFile()
//------------------------------------------------------------------------
ELoadStatus LLBVHLoader::loadBVHFile(const char *buffer, char* error_text, S32 &err_line)
{
std::string line;
err_line = 0;
error_text[127] = '\0';
std::string str(buffer);
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep("\r\n");
tokenizer tokens(str, sep);
tokenizer::iterator iter = tokens.begin();
mLineNumber = 0;
mJoints.clear();
std::vector<S32> parent_joints;
//--------------------------------------------------------------------
// consume hierarchy
//--------------------------------------------------------------------
if (iter == tokens.end())
return E_ST_EOF;
line = (*(iter++));
err_line++;
if ( !strstr(line.c_str(), "HIERARCHY") )
{
// LL_INFOS() << line << LL_ENDL;
return E_ST_NO_HIER;
}
//--------------------------------------------------------------------
// consume joints
//--------------------------------------------------------------------
while (TRUE)
{
//----------------------------------------------------------------
// get next line
//----------------------------------------------------------------
if (iter == tokens.end())
return E_ST_EOF;
line = (*(iter++));
err_line++;
//----------------------------------------------------------------
// consume }
//----------------------------------------------------------------
if ( strstr(line.c_str(), "}") )
{
if (parent_joints.size() > 0)
{
parent_joints.pop_back();
}
continue;
}
//----------------------------------------------------------------
// if MOTION, break out
//----------------------------------------------------------------
if ( strstr(line.c_str(), "MOTION") )
break;
//----------------------------------------------------------------
// it must be either ROOT or JOINT or EndSite
//----------------------------------------------------------------
if ( strstr(line.c_str(), "ROOT") )
{
}
else if ( strstr(line.c_str(), "JOINT") )
{
}
else if ( strstr(line.c_str(), "End Site") )
{
iter++; // {
iter++; // OFFSET
S32 depth = 0;
for (S32 j = (S32)parent_joints.size() - 1; j >= 0; j--)
{
Joint *joint = mJoints[parent_joints[j]];
if (depth > joint->mChildTreeMaxDepth)
{
joint->mChildTreeMaxDepth = depth;
}
depth++;
}
continue;
}
else
{
strncpy(error_text, line.c_str(), 127); /* Flawfinder: ignore */
return E_ST_NO_JOINT;
}
//----------------------------------------------------------------
// get the joint name
//----------------------------------------------------------------
char jointName[80]; /* Flawfinder: ignore */
if ( sscanf(line.c_str(), "%*s %79s", jointName) != 1 ) /* Flawfinder: ignore */
{
strncpy(error_text, line.c_str(), 127); /* Flawfinder: ignore */
return E_ST_NO_NAME;
}
//---------------------------------------------------------------
// we require the root joint be "hip" - DEV-26188
//---------------------------------------------------------------
const char* FORCED_ROOT_NAME = "hip";
if ( (mJoints.size() == 0 ) && ( !strstr(jointName, FORCED_ROOT_NAME) ) )
{
strncpy(error_text, line.c_str(), 127); /* Flawfinder: ignore */
return E_ST_BAD_ROOT;
}
//----------------------------------------------------------------
// add a set of keyframes for this joint
//----------------------------------------------------------------
mJoints.push_back( new Joint( jointName ) );
Joint *joint = mJoints.back();
S32 depth = 1;
for (S32 j = (S32)parent_joints.size() - 1; j >= 0; j--)
{
Joint *pjoint = mJoints[parent_joints[j]];
if (depth > pjoint->mChildTreeMaxDepth)
{
pjoint->mChildTreeMaxDepth = depth;
}
depth++;
}
//----------------------------------------------------------------
// get next line
//----------------------------------------------------------------
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
//----------------------------------------------------------------
// it must be {
//----------------------------------------------------------------
if ( !strstr(line.c_str(), "{") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_OFFSET;
}
else
{
parent_joints.push_back((S32)mJoints.size() - 1);
}
//----------------------------------------------------------------
// get next line
//----------------------------------------------------------------
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
//----------------------------------------------------------------
// it must be OFFSET
//----------------------------------------------------------------
if ( !strstr(line.c_str(), "OFFSET") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_OFFSET;
}
//----------------------------------------------------------------
// get next line
//----------------------------------------------------------------
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
//----------------------------------------------------------------
// it must be CHANNELS
//----------------------------------------------------------------
if ( !strstr(line.c_str(), "CHANNELS") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_CHANNELS;
}
//----------------------------------------------------------------
// get rotation order
//----------------------------------------------------------------
const char *p = line.c_str();
for (S32 i=0; i<3; i++)
{
p = strstr(p, "rotation");
if (!p)
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_ROTATION;
}
const char axis = *(p - 1);
if ((axis != 'X') && (axis != 'Y') && (axis != 'Z'))
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_AXIS;
}
joint->mOrder[i] = axis;
p++;
}
}
//--------------------------------------------------------------------
// consume motion
//--------------------------------------------------------------------
if ( !strstr(line.c_str(), "MOTION") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_MOTION;
}
//--------------------------------------------------------------------
// get number of frames
//--------------------------------------------------------------------
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
if ( !strstr(line.c_str(), "Frames:") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_FRAMES;
}
if ( sscanf(line.c_str(), "Frames: %d", &mNumFrames) != 1 )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_FRAMES;
}
//--------------------------------------------------------------------
// get frame time
//--------------------------------------------------------------------
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
if ( !strstr(line.c_str(), "Frame Time:") )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_FRAME_TIME;
}
if ( sscanf(line.c_str(), "Frame Time: %f", &mFrameTime) != 1 )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_FRAME_TIME;
}
mDuration = (F32)mNumFrames * mFrameTime;
if (!mLoop)
{
mLoopOutPoint = mDuration;
}
//--------------------------------------------------------------------
// load frames
//--------------------------------------------------------------------
for (S32 i=0; i<mNumFrames; i++)
{
// get next line
if (iter == tokens.end())
{
return E_ST_EOF;
}
line = (*(iter++));
err_line++;
// read and store values
const char *p = line.c_str();
for (U32 j=0; j<mJoints.size(); j++)
{
Joint *joint = mJoints[j];
joint->mKeys.push_back( Key() );
Key &key = joint->mKeys.back();
// get 3 pos values for root joint only
if (j==0)
{
if ( sscanf(p, "%f %f %f", key.mPos, key.mPos+1, key.mPos+2) != 3 )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_POS;
}
}
// skip to next 3 values in the line
p = find_next_whitespace(p);
if (!p)
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_ROT;
}
p = find_next_whitespace(++p);
if (!p)
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_ROT;
}
p = find_next_whitespace(++p);
if (!p)
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_ROT;
}
// get 3 rot values for joint
F32 rot[3];
if ( sscanf(p, " %f %f %f", rot, rot+1, rot+2) != 3 )
{
strncpy(error_text, line.c_str(), 127); /*Flawfinder: ignore*/
return E_ST_NO_ROT;