JunkBox_Win_Lib 1.5.3
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NiFileTool.cpp
Go to the documentation of this file.
1
2#include "WinBaseLib.h"
3#include "NiFileTool.h"
4#include "MFCio.h"
5#include "MessageBoxDLG.h"
6
7
8using namespace jbxl;
9using namespace jbxwl;
10
11
12
14{
15 DEBUG_INFO("DESTRUCTOR: CNiFileTool\n");
16
17 free_data();
18}
19
20
21
23//
24
26{
27 clear_data();
28
29 if (JTXT_Tool!=NULL) delete(JTXT_Tool);
30 if (VMD_Tool!=NULL) delete(VMD_Tool);
31 if (BVH_Tool!=NULL) delete(BVH_Tool);
32
33 JTXT_Tool = NULL;
34 VMD_Tool = NULL;
35 BVH_Tool = NULL;
36}
37
38
39
41{
42 close_wfile();
45
48
49 frames_data = NULL;
50 frame_start = 0;
51 frames_num = 0;
52 frame_ptr = 0;
53
54 frame_timer = 0;
55 lap_timer = 0;
56 time_scale = 1.0;
57
58 playback_time = 0.0;
59 playback_fps = 30;
60}
61
62
63
65{
66 m_enable_wfile = FALSE;
67 m_wfp = NULL;
68 m_rfp = NULL;
70
71 frames_data = NULL;
72 frame_start = 0;
73 frames_num = 0;
74 frame_ptr = 0;
75
76 frame_timer = 0;
77 lap_timer = 0;
78 time_scale = 1.0;
79
80 playback_time = 0.0;
81 playback_fps = 30;
82
85
86 JTXT_Tool = NULL;
87 VMD_Tool = NULL;
88 BVH_Tool = NULL;
89}
90
91
92
94{
95 m_lib = lib;
96
97 //
98 JTXT_Tool = new CNiJTextTool();
99 VMD_Tool = new CNiVMDTool();
100 BVH_Tool = new CNiBVHTool();
101
102 frameTool = NULL;
103}
104
105
106
107BOOL CNiFileTool::open_wfile(LPCTSTR fname, BOOL force)
108{
109 if (m_enable_wfile) return TRUE;
110 if (m_wfp!=NULL) return FALSE;
111
112 if (!force) {
113 if (file_exist_t(fname)) {
114 int ret = MessageBoxDLG(IDS_STR_WARN, IDS_STR_ASK_OVERWRITE, MB_YESNO, NULL);
115 if (ret==IDNO) return FALSE;
116 }
117 }
118
119 //
120 m_wfp = tfopen(fname, _T("wb"));
121 if (m_wfp==NULL) return FALSE;
122
123 m_enable_wfile = TRUE;
124 return TRUE;
125}
126
127
128
130{
131 if (fp==NULL) fp = m_wfp;
132
134}
135
136
137
138BOOL CNiFileTool::open_rfile(LPCTSTR fname)
139{
140 if (m_rfp!=NULL) return FALSE;
141
142 m_rfp = tfopen(fname, _T("rb"));
143 if (m_rfp==NULL) {
144 return FALSE;
145 }
146
147 return TRUE;
148}
149
150
151
153{
154 if (m_wfp!=NULL) {
155 fclose(m_wfp);
156 m_wfp = NULL;
157 m_enable_wfile = FALSE;
158 }
159}
160
161
162
164{
165 if (m_rfp!=NULL) {
166 fclose(m_rfp);
167 m_rfp = NULL;
168 }
169}
170
171
172
173
175// JointsText I/O
176
178{
179 if (fname==NULL) return -1;
181 if (JTXT_Tool==NULL) JTXT_Tool = new CNiJTextTool();
182
183 BOOL ret = open_rfile(fname);
184 if (!ret) return -2;
185
186 ret = JTXT_Tool->readFile(m_rfp);
187 close_rfile();
188 if (!ret) return -3;
189
191 if (frames_data==NULL) return -4;
192
195
198
199 return frames_num;
200}
201
202
203
204BOOL CNiFileTool::writeJTextData(FILE* fp, Vector<double>* pos, Quaternion<double>* rot, double* agl, BOOL mirror, NiSDK_Lib lib)
205{
206 if (fp==NULL) return FALSE;
207
208 if (JTXT_Tool==NULL) JTXT_Tool = new CNiJTextTool();
209
210 if (lib==NiSDK_Default) lib = m_lib;
211 JTXT_Tool->setPosVect(pos, lib, mirror);
212 JTXT_Tool->setRotQuat(rot, lib, mirror);
213 JTXT_Tool->setJntAngl(agl, lib, mirror);
215
216 return TRUE;
217}
218
219
220
221// 一時データを JointsTextに変換(コピー)
222BOOL CNiFileTool::saveJTextfromFile(LPCTSTR tempf, LPCTSTR fname, BOOL force)
223{
224 if (!force) {
225 if (file_exist_t(fname)) {
226 int ret = MessageBoxDLG(IDS_STR_WARN, IDS_STR_ASK_OVERWRITE, MB_YESNO, NULL);
227 if (ret==IDNO) return FALSE;
228 }
229 }
230
231 return copyFileWithCounter(tempf, fname);
232}
233
234
235
236// 一時データの作成
237BOOL CNiFileTool::writeTempJText(Vector<double>* pos, Quaternion<double>* rot, double* agl, BOOL mirror, NiSDK_Lib lib)
238{
239 return writeJTextData(m_wfp, pos, rot, agl, mirror, lib);
240}
241
242
243
244
246// BVH I/O
247
248int CNiFileTool::readBVHFile(LPCTSTR fname)
249{
250 if (fname==NULL) return -1;
252 if (BVH_Tool==NULL) BVH_Tool = new CNiBVHTool();
253
254 BOOL ret = open_rfile(fname);
255 if (!ret) return -2;
256
257 ret = BVH_Tool->readFile(m_rfp);
258 close_rfile();
259 if (!ret) return -3;
260
262 if (frames_data==NULL) return -4;
263
266
269
270 return frames_num;
271}
272
273
274
275// 一時データを BVHに変換
276BOOL CNiFileTool::saveBVHfromFile(LPCTSTR tempf, LPCTSTR fname, BVHSaveParam param)
277{
278 int frn = readJTextFile(tempf);
279 if (frn<=0) return FALSE;
280
281 param.scale /= 0.0254f; // m -> inch
282 BOOL ret = writeBVHFile(fname, param);
283
284 return ret;
285}
286
287
288//
289BOOL CNiFileTool::writeBVHFile(LPCTSTR fname, BVHSaveParam param)
290{
291 if (frameTool==NULL) return FALSE;
292 if (frameTool->getFramesNumber()<=0) return FALSE;
293
294
295 //
296 // HIERARCHY
297 //
298 tTree* hrchy = NULL;
299 if (param.format==BVH_SAVE_FORMAT_QAV) {
300 hrchy = makeBVH_QAvHierarchy();
301 }
302 else if (param.format==BVH_SAVE_FORMAT_SL) {
303 hrchy = makeBVH_SLHierarchy();
304 }
305 else if (param.format==BVH_SAVE_FORMAT_NI) {
306 hrchy = makeBVH_NiHierarchy();
307 }
308 else {
309 return FALSE;
310 }
311 if (param.fps<10) param.fps = 10;
312 else if (param.fps>60) param.fps = 60;
313
314 double frmitm = 1000.0/param.fps; // msec
315 int frmnum = (int)(frameTool->getPlayBackTime()/frmitm) + 1;
316
317 //
318 CBVHTool* bvh_tool = new CBVHTool();
319
320 bvh_tool->setHierarchy(hrchy);
321 bvh_tool->frame_num = frmnum;
322 bvh_tool->frame_time = frmitm/1000.0; // sec
323 bvh_tool->state = 0;
324
325 if (bvh_tool->frame_time!=0.0) bvh_tool->framepsec = (int)(1.0/bvh_tool->frame_time);
326 else bvh_tool->framepsec = 30;
327
328 //
329 size_t len = sizeof(vector)*bvh_tool->joint_num;
330 bvh_tool->offset = (vector*)malloc(len);
331 if (bvh_tool->offset==NULL) {
332 delete(bvh_tool);
333 return FALSE;
334 }
335 memset(bvh_tool->offset, 0, len);
336
337 len = sizeof(double)*frmnum*bvh_tool->channels;
338 bvh_tool->motion = (double*)malloc(len);
339 if (bvh_tool->motion==NULL) {
340 delete(bvh_tool);
341 return FALSE;
342 }
343 memset(bvh_tool->motion, 0, len);
344
345 //
346 // MOTION
347 //
348 double r2d = 180.0/PI;
349 tList* namelist = setBVHJointName();
350
351 Vector<double> vt[NI_TOTAL_JOINT_NUM];
352 Quaternion<double> qt[NI_TOTAL_JOINT_NUM];
353
354 //
355 for (int f=0; f<frmnum; f++) {
356 //
357 int frm = f*bvh_tool->channels;
358 NiJointData* jdat = frameTool->getJointsData(f, param.fps);
359
360 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
361 vt[j].set(0.0, 0.0, 0.0, 0.0, -1.0);
362 qt[j].set(1.0, 0.0, 0.0, 0.0, 0.0, -1.0);
363 }
364 for (int j=0; j<frameTool->getJointsNumber(); j++) {
365 int n = jdat[j].joint;
366 if (n>=0 && n<NI_TOTAL_JOINT_NUM) {
367 vt[n] = jdat[j].vect;
368 qt[n] = jdat[j].quat;
369 }
370 }
371 //
372 if (param.recalc) Vector2Quaternion(vt, qt);
373
374 //
375 for (int j=0; j<bvh_tool->joint_num; j++) {
376 int chnum = bvh_tool->channel_num[j];
377 int index = bvh_tool->channel_idx[j];
378 char* tpchr = (char*)&(bvh_tool->flex_joint.buf[index*2]);
379
380 //
381 Vector<double> vect(0.0, 0.0, 0.0, 0.0, -1.0);
382 Vector<double> eulr(0.0, 0.0, 0.0, 0.0, -1.0);
383
384 int jnum = BVHJoint2NiJointNum(namelist, bvh_tool->joint_name[j]); // 共通ジョイント番号
385 if (jnum>=0 && jnum<NI_TOTAL_JOINT_NUM) {
386 vect = vt[jnum];
387 eulr = Quaternion2ExtEulerXYZ(qt[jnum]);
388 }
389
390 //
391 for (int k=0; k<chnum; k++) {
392 int midx = frm + index;
393 if (tpchr[k*2]=='P') {
394 if (tpchr[k*2+1]=='X') bvh_tool->motion[midx+k] = vect.y*param.scale;
395 else if (tpchr[k*2+1]=='Y') bvh_tool->motion[midx+k] = vect.z*param.scale;
396 else if (tpchr[k*2+1]=='Z') bvh_tool->motion[midx+k] = vect.x*param.scale;
397 }
398 else if (tpchr[k*2]=='R') { // "RYRXRZ" -> ZXY -> XYZ
399 if (tpchr[k*2+1]=='X') bvh_tool->motion[midx+k] = eulr.y*r2d;
400 else if (tpchr[k*2+1]=='Y') bvh_tool->motion[midx+k] = eulr.z*r2d;
401 else if (tpchr[k*2+1]=='Z') bvh_tool->motion[midx+k] = eulr.x*r2d;
402 }
403 }
404 }
405 }
406
407 //
408 // OFFSET
409 //
410 for (int j=0; j<bvh_tool->joint_num; j++) {
411 bvh_tool->offset[j].x = 0.0;
412 bvh_tool->offset[j].y = 0.0;
413 bvh_tool->offset[j].z = 0.0;
414 }
415
416 // BVH_SAVE_FORMAT_QAV
417 if (param.format==BVH_SAVE_FORMAT_QAV) {
418 for (int j=0; j<bvh_tool->joint_num; j++) {
419 bvh_tool->offset[j] = getQAvBVHOffset(j);
420 }
421 }
422
423 // 0フレームのジョイントの位置
424 else {
425 NiJointData* jdat = frameTool->getJointsData(0, param.fps);
426 //
427 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
428 vt[j].set(0.0, 0.0, 0.0, 0.0, -1.0);
429 qt[j].set(1.0, 0.0, 0.0, 0.0, 0.0, -1.0);
430 }
431 for (int j=0; j<frameTool->getJointsNumber(); j++) {
432 int n = jdat[j].joint;
433 if (n>=0 && n<NI_TOTAL_JOINT_NUM) {
434 vt[n] = jdat[j].vect;
435 qt[n] = jdat[j].quat;
436 }
437 }
438
439 //
440 for (int j=0; j<bvh_tool->joint_num; j++) {
441 int jnum = BVHJoint2NiJointNum(namelist, bvh_tool->joint_name[j]);
442 if (jnum>=0 && jnum<NI_TOTAL_JOINT_NUM) {
443 bvh_tool->offset[j].x = vt[jnum].y*param.scale;
444 bvh_tool->offset[j].y = vt[jnum].z*param.scale;
445 bvh_tool->offset[j].z = vt[jnum].x*param.scale;
446 }
447 }
448 }
449
450 //
451 // 書き出し
452 //
453 BOOL ret = FALSE;
454
455 if (param.divtm>0) {
456 char* fn = ::ts2mbs(fname);
457 ret = bvh_tool->writeMultiFile(fn, param.divtm, Max(param.divtm, 10));
458 ::free(fn);
459 }
460 else {
461 //
462 ret = open_wfile(fname, FALSE);
463 if (ret) {
464 ret = bvh_tool->printBVH(m_wfp);
465 close_wfile();
466 }
467 else {
468 DEBUG_ERROR("CNiFileTool::writeBVHFile(): ERROR: Already other file for write is opened!\n");
469 }
470 }
471
472 del_tList(&namelist);
473 delete(bvh_tool);
474
475 return ret;
476}
477
478
479
480
482// MMD VMD I/O
483
484int CNiFileTool::readVMDFile(LPCTSTR fname)
485{
486 if (fname==NULL) return -1;
488 if (VMD_Tool==NULL) VMD_Tool = new CNiVMDTool();
489
490 BOOL ret = open_rfile(fname);
491 if (!ret) return -2;
492
493 ret = VMD_Tool->readFile(m_rfp);
494 close_rfile();
495 if (!ret) return -3;
496
497 //
499 if (frames_data==NULL) return -4;
500
503
506
507 return frames_num;
508}
509
510
511
512// 一時データを VMDに変換
513BOOL CNiFileTool::saveVMDfromFile(LPCTSTR tempf, LPCTSTR fname)
514{
515 // ここに実装... しないと思う.面倒だから.
516 return FALSE;
517}
518
519
520
521
523// Frame
524
525void CNiFileTool::setFrame(int frame)
526{
527 frame_start = 0;
528 if (frames_num!=0) {
529 frame_start = frame%frames_num;
530 }
531
533}
534
535
536
537//
538// Start 位置の設定とフレームデータの準備
539//
541{
542 if (frames_num==0 || start<0 || start>=frames_num) return FALSE;
543
545 frame_timer = (int)(frames_data[start].msec*time_scale);
546 setFrame(start);
547
548 return TRUE;
549}
550
551
552
553//
554// 次のフレームデータの準備
555//
556BOOL CNiFileTool::nextFrame(BOOL next) // next==TRUE フレームを進める
557{
558 if (next) {
559 frame_ptr++;
560 if (frame_ptr>frames_num) {
561 // not reachable
562 DEBUG_INFO("CNiFileTool::nextFrame(): frame ptr = %d, frame num = %d\n", frame_ptr, frames_num);
563 return FALSE;
564 }
565 }
566
567 //
568 int msec = 0;
569 if (frame_ptr==frames_num) {
570 if (frames_num!=1) {
571 msec = (int)((2*frames_data[frame_ptr-1].msec - frames_data[frame_ptr-2].msec)*time_scale);
572 }
573 }
574 else {
575 msec = (int)(frames_data[frame_ptr].msec*time_scale);
576 }
577
578 //
579 unsigned short ctime;
581 lap_timer = ctime;
582
583 if (frame_timer>=msec) {
584 return TRUE;
585 }
586 else if (msec>frame_timer+5000) {
587 DEBUG_INFO("CNiFileTool::nextFrame(): next frame = %d, current frame = %d\n", msec, frame_timer);
588 return FALSE; // 5sec 進?
589 }
590
591 // Wait
592 do {
593 ::DisPatcher();
594 int slptm = Min(msec-frame_timer, 100);
595 if (slptm>0) ::Sleep(slptm);
597 lap_timer = ctime;
598 } while (msec>frame_timer);
599
600 return TRUE;
601}
602
603
604
605
607// Frame Data
608
610{
611 //
612 frames_data = NULL;
613 frames_num = 0;
614 frame_ptr = 0;
615
616 if (JTXT_Tool!=NULL) JTXT_Tool->clear_data();
617 if (BVH_Tool!=NULL) BVH_Tool->clear_data();
618 if (VMD_Tool!=NULL) VMD_Tool->clear_data();
619
620 frameTool = NULL;
621
622 return;
623}
624
625
626
627
629// joints
630
632{
633 for (int i=0; i<NI_TOTAL_JOINT_NUM; i++) {
634 rotQuat[i].init(-1.0);
635 posVect[i].init(-1.0);
636 }
637 currentPos.init(-1.0);
638}
639
640
641
642void CNiFileTool::getJointsDataSeq(BOOL mirror, int frame)
643{
644 if (frame>=0) setFrame(frame);
645
646 // 静的なデータ
647 NiJointData* joint_data = frames_data[frame_ptr].jdat;
648 int joint_num = frames_data[frame_ptr].jnum;
649
650 // joint_data が無効なら,動的にデータを計算(補間)して得る.
651 if (joint_data==NULL) {
652 if (frameTool!=NULL) {
654 }
655 }
656 if (joint_data==NULL) return;
657
658 //
659 for (int j=0; j<joint_num; j++) {
660 //
661 int n = joint_data[j].joint;
662 if (n>=0 && n<NI_TOTAL_JOINT_NUM) {
663 if (mirror) n = NiMirrorJointNum(n);
664 //
665 posVect[n] = joint_data[j].vect;
666 rotQuat[n] = joint_data[j].quat;
667 //
668 if (mirror) {
669 posVect[n].y = - posVect[n].y;
670 rotQuat[n].x = - rotQuat[n].x;
671 rotQuat[n].z = - rotQuat[n].z;
672 }
673 }
674 }
675 return;
676}
677
#define BVH_SAVE_FORMAT_SL
Definition NiBVHName.h:13
#define BVH_SAVE_FORMAT_QAV
Definition NiBVHName.h:12
#define BVH_SAVE_FORMAT_NI
Definition NiBVHName.h:14
#define NiMirrorJointNum(j)
#define NI_TOTAL_JOINT_NUM
virtual unsigned int getFramesNumber(void)
virtual NiFrameData * getFramesData(void)
virtual int getPlayBackFPS(void)
virtual int getPlayBackTime(void)
virtual NiJointData * getJointsData(int frmnum, int fps)
virtual int getJointsNumber(void)
virtual BOOL readFile(FILE *fp)
Definition NiBVHTool.cpp:58
void clear_data(void)
Definition NiBVHTool.cpp:45
virtual int getPlayBackFPS(void)
CBaseFrameTool * frameTool
Definition NiFileTool.h:71
void init(NiSDK_Lib lib)
virtual ~CNiFileTool(void)
void setFrame(int frame)
CNiVMDTool * VMD_Tool
Definition NiFileTool.h:67
int readBVHFile(LPCTSTR fname)
BOOL writeTempJText(Vector< double > *pos, Quaternion< double > *rot, double *agl, BOOL mrr, NiSDK_Lib lib=NiSDK_Default)
Vector< double > posVect[NI_TOTAL_JOINT_NUM]
Definition NiFileTool.h:112
void write_header(FILE *fp=NULL)
unsigned short lap_timer
Definition NiFileTool.h:59
BOOL saveBVHfromFile(LPCTSTR tempf, LPCTSTR fname, BVHSaveParam param)
void clearStartPosition(void)
Definition NiFileTool.h:103
BOOL startFrame(int start=0)
BOOL nextFrame(BOOL next=TRUE)
Quaternion< double > rotQuat[NI_TOTAL_JOINT_NUM]
Definition NiFileTool.h:113
int readVMDFile(LPCTSTR fname)
virtual void Vector2Quaternion(Vector< double > *vt, Quaternion< double > *qt)
Definition NiFileTool.h:100
BOOL writeJTextData(FILE *fp, Vector< double > *pos, Quaternion< double > *rot, double *agl, BOOL mrr, NiSDK_Lib lib=NiSDK_Default)
void clearJointsData(void)
BOOL open_wfile(LPCTSTR fname, BOOL force)
void clear_data(void)
void clearFrameData(void)
void free_data(void)
BOOL open_rfile(LPCTSTR fname)
CNiJTextTool * JTXT_Tool
Definition NiFileTool.h:66
void close_rfile(void)
void init_data(void)
CNiBVHTool * BVH_Tool
Definition NiFileTool.h:68
void close_wfile(void)
Vector< double > currentPos
Definition NiFileTool.h:110
void getJointsDataSeq(BOOL mirror, int frame=-1)
BOOL saveVMDfromFile(LPCTSTR tempf, LPCTSTR fname)
BOOL writeBVHFile(LPCTSTR fname, BVHSaveParam param)
BOOL saveJTextfromFile(LPCTSTR tempf, LPCTSTR fname, BOOL force)
int readJTextFile(LPCTSTR fname)
NiFrameData * frames_data
Definition NiFileTool.h:55
void writeCurrentData(FILE *fp, unsigned short msec)
virtual BOOL readFile(FILE *fp)
void setRotQuat(Quaternion< double > *rot, NiSDK_Lib lib, BOOL mirror)
void setPosVect(Vector< double > *pos, NiSDK_Lib lib, BOOL mirror)
void writeHeader(FILE *fp)
Definition NiJTextTool.h:34
void setJntAngl(double *agl, NiSDK_Lib lib, BOOL mirror)
virtual unsigned int getFramesNumber(void)
Definition NiVMDTool.h:121
virtual BOOL readFile(FILE *fp)
void clear_data(void)
virtual NiFrameData * getFramesData(void)
tTree * makeBVH_QAvHierarchy(void)
vector getQAvBVHOffset(int joint)
char * ts2mbs(LPCTSTR str)
Definition WinTools.h:57
unsigned short GetMsecondsLapTimer(unsigned short pm, unsigned short *nt=NULL)
Definition WinTools.cpp:799
int BVHJoint2NiJointNum(tList *lt, Buffer joint_name)
int copyFileWithCounter(LPCTSTR src, LPCTSTR dst)
Definition MFCio.cpp:24
tList * setBVHJointName(void)
Definition NiBVHName.cpp:11
CMessageBoxDLG * MessageBoxDLG(int ttl, int msg, CWnd *wnd=NULL)
tTree * makeBVH_SLHierarchy(void)
unsigned short GetMsecondsTimer(void)
Definition WinTools.cpp:778
BOOL file_exist_t(LPCTSTR fn)
Definition WinTools.cpp:258
NiSDK_Lib
Definition NiToolWin.h:35
@ NiSDK_Default
Definition NiToolWin.h:36
@ NiSDK_None
Definition NiToolWin.h:37
tTree * makeBVH_NiHierarchy(void)
#define IDS_STR_ASK_OVERWRITE
Definition resource.h:124
#define IDS_STR_WARN
Definition resource.h:105
NiJointData * jdat
Quaternion< double > quat
Vector< double > vect