JunkBox_Win_Lib 1.5.3
Loading...
Searching...
No Matches
NiJointsTool.cpp
Go to the documentation of this file.
1
2#include "tools++.h"
3#include "NiJointsTool.h"
4
5
6using namespace jbxl;
7using namespace jbxwl;
8
9
11// SDK(OpenNI, Kinect SDK)用 作業ジョイント番号.
12// 二つのSDKのジョイント名はこの名前に集約される.
13//
14
15// Default or No SDK
20
25
31
37
42
47
48int NI_SDK_L_FINGER = NI_L_FINGER; // Parameter of Finger
49int NI_SDK_R_FINGER = NI_R_FINGER; // Parameter of Finger
50int NI_SDK_FACE = NI_FACE; // Parameter of Expression
52
53
54
55
57// 回転の制約条件を設定する変数.
58// 具体的な内容はライブラリ外部で指定する.
59//
60
61RBound<double> RBound_NI_TORSO;
62RBound<double> RBound_NI_CHEST;
63RBound<double> RBound_NI_NECK;
64RBound<double> RBound_NI_HEAD;
65
66RBound<double> RBound_NI_L_COLLAR;
67RBound<double> RBound_NI_R_COLLAR;
68RBound<double> RBound_NI_L_SHLDR;
69RBound<double> RBound_NI_R_SHLDR;
70RBound<double> RBound_NI_L_ELBOW;
71RBound<double> RBound_NI_R_ELBOW;
72RBound<double> RBound_NI_L_WRIST;
73RBound<double> RBound_NI_R_WRIST;
74RBound<double> RBound_NI_L_HAND;
75RBound<double> RBound_NI_R_HAND;
76
77RBound<double> RBound_NI_L_HIP;
78RBound<double> RBound_NI_R_HIP;
79RBound<double> RBound_NI_L_KNEE;
80RBound<double> RBound_NI_R_KNEE;
81RBound<double> RBound_NI_L_ANKLE;
82RBound<double> RBound_NI_R_ANKLE;
83RBound<double> RBound_NI_L_FOOT;
84RBound<double> RBound_NI_R_FOOT;
85
86
87
88
90//
91
92static std::string _NiJointName[] = // NI_TOTAL_JOINT_NUM (37)
93{
94 // 0 1 2 3 4 5 6
95 "PELVIS", "WAIST", "TORSO", "CHEST", "NECK", "HEAD", "SKULL",
96 // 7 8 9 10
97 "L_EYE", "R_EYE", "L_BUST", "R_BUST",
98 // 11 12 13 14 15 16
99 "L_COLLAR", "L_SHLDR", "L_ELBOW", "L_WRIST", "L_HAND", "L_FNGRTIP",
100 // 17 18 19 20 21 22
101 "R_COLLAR", "R_SHLDR", "R_ELBOW", "R_WRIST", "R_HAND", "R_FNGRTIP",
102 // 23 24 25 26 27
103 "L_HIP", "L_KNEE", "L_ANKLE", "L_FOOT", "L_TOE",
104 // 28 29 30 31 32
105 "R_HIP", "R_KNEE", "R_ANKLE", "R_FOOT", "R_TOE",
106 // 33 34 35 36
107 "L_FINGER", "R_FINGER", "FACE", "AVATAR"
108};
109
110
111
112
114// 共通のジョイントの名前と番号
115//
116
117std::string jbxwl::NiJointName(int n)
118{
119 std::string str = "";
120
121 if (n>=0 && n<NI_TOTAL_JOINT_NUM) {
122 str = _NiJointName[n];
123 }
124
125 return str;
126}
127
128
129
130int jbxwl::NiJointNum(char* name)
131{
132 for (int i=0; i<NI_TOTAL_JOINT_NUM; i++) {
133 if (!strcmp(name, _NiJointName[i].c_str())) return i;
134 }
135 return -1;
136}
137
138
139
140
142// Library 固有のジョイント番号から共通のジョイント名の番号を得る
143//
144
145static int _OpenNI2JointNameNum[] = // NI_OPENNI_JOINT_NUM (25)
146{
147 0, 5, 4, 2, 1,
148 17, 18, 19, 20, 21, 22,
149 11, 12, 13, 14, 15, 16,
150 28, 29, 30, 31,
151 23, 24, 25, 26
152};
153
154//
155static int _OpenNI22JointNameNum[] = // NI_OPENNI2_JOINT_NUM (16)
156{
157 0, 5, 4,
158 18, 12, 19, 13, 21, 15,
159 2,
160 28, 23, 29, 24, 31, 26
161};
162
163//
164static int _Kinect2JointNameNum[] = // NI_KINECT_JOINT_NUM (20)
165{
166 0, 2, 4, 5,
167 12, 13, 14, 15,
168 18, 19, 20, 21,
169 23, 24, 25, 26,
170 28, 29, 30, 31
171};
172
173/*
174static int _Ni2JointNameNum[] = // NI_TOTAL_JOINT_NUM (37)
175{
176 0, 1, 2, 3, 4, 5, 6,
177 7, 8, 9, 10,
178 11, 12, 13, 14, 15, 16,
179 17, 18, 19, 20, 21, 22,
180 23, 24, 25, 26, 27,
181 28, 29, 30, 31, 32,
182 33, 34, 35, 36
183};
184*/
185
186
187
188std::string jbxwl::NiSDK2JointName(int j, NiSDK_Lib lib)
189{
190 std::string str = "";
191
192 if (lib==NiSDK_OpenNI2) {
193 int n = _OpenNI22JointNameNum[j];
194 if (n>=0) str = _NiJointName[n];
195 }
196 else if (lib==NiSDK_OpenNI) {
197 int n = _OpenNI2JointNameNum[j];
198 if (n>=0) str = _NiJointName[n];
199 }
200 else if (lib==NiSDK_Kinect) {
201 int n = _Kinect2JointNameNum[j];
202 if (n>=0) str = _NiJointName[n];
203 }
204 else if (lib==NiSDK_None) {
205 //str = _NiJointName[_Ni2JointNameNum[j]];
206 str = _NiJointName[j];
207 }
208
209 return str;
210}
211
212
213
214
216// Library 固有のジョイント番号のミラージョイントの番号を得る
217//
218
219static int _OpenNIMirrorJointNum[] = // NI_OPENNI_JOINT_NUM (25)
220{
221 0, 1, 2, 3, 4,
222 11, 12, 13, 14, 15, 16,
223 5, 6, 7, 8, 9, 10,
224 21, 22, 23, 24,
225 17, 18, 19, 20
226};
227
228//
229static int _OpenNI2MirrorJointNum[] = // NI_OPENNI2_JOINT_NUM (16)
230{
231 0, 1, 2,
232 4, 3, 6, 5, 8, 7,
233 9,
234 11, 10, 13, 12, 15, 14
235};
236
237//
238static int _KinectMirrorJointNum[] = // NI_KINECT_JOINT_NUM (20)
239{
240 0, 1, 2, 3,
241 8, 9, 10, 11,
242 4, 5, 6, 7,
243 16, 17, 18, 19,
244 12, 13, 14, 15
245};
246
247//
248static int _NiMirrorJointNum[] = // NI_TOTAL_JOINT_NUM (37)
249{
250 0, 1, 2, 3, 4, 5, 6,
251 8, 7, 10, 9,
252 17, 18, 19, 20, 21, 22,
253 11, 12, 13, 14, 15, 16,
254 28, 29, 30, 31, 32,
255 23, 24, 25, 26, 27,
256 34, 33, 35, 36
257};
258
259
260
262{
263 int ret = -1;
264 if (j<0) return ret;
265
266 if (lib==NiSDK_OpenNI2) {
267 ret = _OpenNI2MirrorJointNum[j];
268 }
269 else if (lib==NiSDK_OpenNI) {
270 ret = _OpenNIMirrorJointNum[j];
271 }
272 else if (lib==NiSDK_Kinect) {
273 ret = _KinectMirrorJointNum[j];
274 }
275 else if (lib==NiSDK_None) {
276 ret = _NiMirrorJointNum[j];
277 }
278
279 return ret;
280}
281
282
283
284
286// 共通ジョイント番号から,Library固有の座標用ジョイント番号を得る
287//
288
289// HAND->WRIST, FOOT->ANKLE
290static int _Ni2OpenNIPosJointNum[] = // NI_TOTAL_JOINT_NUM (37)
291{
292 0, -1, 3, -1, 2, 1, -1,
293 -1, -1, -1, -1,
294 -1, 12, 13, 15, -1, -1,
295 -1, 6, 7, 9, -1, -1,
296 21, 22, 24, -1, -1,
297 17, 18, 20, -1, -1,
298 -1, -1, -1, -1
299};
300
301// HAND->WRIST, FOOT->ANKLE
302static int _Ni2OpenNI2PosJointNum[] = // NI_TOTAL_JOINT_NUM (37)
303{
304 0, -1, 9, -1, 2, 1, -1,
305 -1, -1, -1, -1,
306 -1, 4, 6, 8, -1, -1,
307 -1, 3, 5, 7, -1, -1,
308 11, 13, 15, -1, -1,
309 10, 12, 14, -1, -1,
310 -1, -1, -1, -1
311};
312
313//
314static int _Ni2KinectPosJointNum[] = // NI_TOTAL_JOINT_NUM (37)
315{
316 0, -1, 1, -1, 2, 3, -1,
317 -1, -1, -1, -1,
318 -1, 4, 5, 6, 7, -1,
319 -1, 8, 9, 10, 11, -1,
320 12, 13, 14, 15, -1,
321 16, 17, 18, 19, -1,
322 -1, -1, -1, -1
323};
324
325
326//
327// 暫定的!!!!
328/*
329static int _Ni2ValidPosJointNum[] = // NI_TOTAL_JOINT_NUM (37)
330{
331 0, -1, 2, 3, 4, 5, -1,
332 7, 8, 9, 10,
333 11, 12, 13, 14, -1, -1,
334 17, 18, 19, 20, -1, -1,
335 23, 24, 25, 26, -1,
336 28, 29, 30, 31, -1,
337 33, 34, 35, 36
338};*/
339
340
341
343{
344 int ret = -1;
345
346 if (lib==NiSDK_OpenNI2) {
347 ret = _Ni2OpenNI2PosJointNum[j];
348 }
349 else if (lib==NiSDK_OpenNI) {
350 ret = _Ni2OpenNIPosJointNum[j];
351 }
352 else if (lib==NiSDK_Kinect) {
353 ret = _Ni2KinectPosJointNum[j];
354 }
355 else if (lib==NiSDK_None) {
356 //ret = _Ni2ValidPosJointNum[j];
357 ret = j;
358 }
359
360 return ret;
361}
362
363
364
365
367// 共通ジョイント番号から,Library固有の回転用ジョイント番号を得る
368
369static int _Ni2OpenNIRotJointNum[] = // NI_TOTAL_JOINT_NUM (37)
370{
371// 0, -1, 3, -1, 2, 1, -1,
372 0, -1, 3, -1, 2, -1, -1, // exclude HEAD
373 -1, -1, -1, -1,
374 -1, 12, 13, -1, -1, -1,
375 -1, 6, 7, -1, -1, -1,
376 21, 22, -1, -1, -1,
377 17, 18, -1, -1, -1,
378 -1, -1, -1, -1
379};
380
381//
382static int _Ni2OpenNI2RotJointNum[] = // NI_TOTAL_JOINT_NUM (37)
383{
384 0, -1, 9, -1, 2, -1, -1,
385 -1, -1, -1, -1,
386 -1, 4, 6, -1, -1, -1,
387 -1, 3, 5, -1, -1, -1,
388 11, 13, -1, -1, -1,
389 10, 12, -1, -1, -1,
390 -1, -1, -1, -1
391};
392
393//
394static int _Ni2KinectRotJointNum[] = // NI_TOTAL_JOINT_NUM (37)
395{
396 0, -1, 1, -1, 2, 3, -1,
397 -1, -1, -1, -1,
398 -1, 4, 5, 6, -1, -1,
399 -1, 8, 9, 10, -1, -1,
400 12, 13, 14, -1, -1,
401 16, 17, 18, -1, -1,
402 -1, -1, -1, -1
403};
404
405//
406static int _Ni2RotJointNum[] = // NI_TOTAL_JOINT_NUM (37)
407{
408 0, 1, 2, 3, 4, 5, -1,
409 7, 8, 9, 10,
410 11, 12, 13, 14, 15, -1,
411 17, 18, 19, 20, 21, -1,
412 23, 24, 25, 26, -1,
413 28, 29, 30, 31, -1,
414 33, 34, 35, 36
415};
416
417
418
420{
421 int ret = -1;
422
423 if (lib==NiSDK_OpenNI2) {
424 ret = _Ni2OpenNI2RotJointNum[j];
425 }
426 else if (lib==NiSDK_OpenNI) {
427 ret = _Ni2OpenNIRotJointNum[j];
428 }
429 else if (lib==NiSDK_Kinect) {
430 ret = _Ni2KinectRotJointNum[j];
431 }
432 else if (lib==NiSDK_None) {
433 ret = _Ni2RotJointNum[j];
434 }
435
436 return ret;
437}
438
439
440
441
443// 共通のジョイント名から Library 固有のジョイント番号を得る
444
445/*
446static int _JointNameNum2Ni[] = // NI_TOTAL_JOINT_NUM (37)
447{
448 0, 1, 2, 3, 4, 5, 6,
449 7, 8, 9, 10,
450 11, 12, 13, 14, 15, 16,
451 17, 18, 19, 20, 21, 22,
452 23, 24, 25, 26, 27,
453 28, 29, 30, 31, 32,
454 33, 34, 35, 36
455};
456*/
457
458//
460{
461 int ret = -1;
462
463 int j;
464 for (j=0; j<NI_TOTAL_JOINT_NUM; j++) {
465 if (!strcasecmp(jname, (char*)_NiJointName[j].c_str())) break;
466 }
467
468 if (j<NI_TOTAL_JOINT_NUM) {
469 ret = Ni2SDKJointNum(j, lib);
470 }
471 else if (j==NI_TOTAL_JOINT_NUM) {
472 //DEBUG_INFO("JointName2NiSDK: Unknown Joint Name = %s\n", jname);
473 }
474
475 return ret;
476}
477
478
479
480
482//
483
484// OpenNI
486{
487 NI_SDK_PELVIS = 0; //
488 NI_SDK_TORSO = 3; // XN_SKEL_TORSO
489 NI_SDK_NECK = 2; // XN_SKEL_NECK
490 NI_SDK_HEAD = 1; // XN_SKEL_HEAD
491
492 NI_SDK_L_EYE = -1;
493 NI_SDK_R_EYE = -1;
494 NI_SDK_L_BUST = -1;
495 NI_SDK_R_BUST = -1;
496
497 NI_SDK_L_SHLDR = 12; // XN_SKEL_RIGHT_SHOULDER
498 NI_SDK_L_ELBOW = 13; // XN_SKEL_RIGHT_ELBOW
499 NI_SDK_L_WRIST = 15; // XN_SKEL_RIGHT_HAND
500 NI_SDK_L_HAND = -1;
501 NI_SDK_L_FNGRTIP = -1;
502
503 NI_SDK_R_SHLDR = 6; // XN_SKEL_LEFT_SHOULDER
504 NI_SDK_R_ELBOW = 7; // XN_SKEL_LEFT_ELBOW
505 NI_SDK_R_WRIST = 9; // XN_SKEL_LEFT_HAND
506 NI_SDK_R_HAND = -1;
507 NI_SDK_R_FNGRTIP = -1;
508
509 NI_SDK_L_HIP = 21; // XN_SKEL_RIGHT_HIP
510 NI_SDK_L_KNEE = 22; // XN_SKEL_RIGHT_KNEE
511 NI_SDK_L_ANKLE = 24; // XN_SKEL_RIGHT_FOOT
512 NI_SDK_L_FOOT = -1;
513
514 NI_SDK_R_HIP = 17; // XN_SKEL_LEFT_HIP
515 NI_SDK_R_KNEE = 18; // XN_SKEL_LEFT_KNEE
516 NI_SDK_R_ANKLE = 20; // XN_SKEL_LEFT_FOOT
517 NI_SDK_R_FOOT = -1;
518
519 NI_SDK_L_FINGER = -1;
520 NI_SDK_R_FINGER = -1;
521 NI_SDK_FACE = -1;
522 NI_SDK_AVATAR = -1;
523
524 return;
525}
526
527
528
529// OpenNI2
531{
532 NI_SDK_PELVIS = 0; //
533 NI_SDK_TORSO = 9; // NITE_JOINT_TORSO + 1
534 NI_SDK_NECK = 2; // NITE_JOINT_NECK + 1
535 NI_SDK_HEAD = 1; // NITE_JOINT_HEAD + 1
536
537 NI_SDK_L_EYE = -1;
538 NI_SDK_R_EYE = -1;
539 NI_SDK_L_BUST = -1;
540 NI_SDK_R_BUST = -1;
541
542 NI_SDK_L_SHLDR = 4; // NITE_JOINT_RIGHT_SHOULDER + 1
543 NI_SDK_L_ELBOW = 6; // NITE_JOINT_RIGHT_ELBOW + 1
544 NI_SDK_L_WRIST = 8; // NITE_JOINT_RIGHT_HAND + 1
545 NI_SDK_L_HAND = -1;
546 NI_SDK_L_FNGRTIP = -1;
547
548 NI_SDK_R_SHLDR = 3; // NITE_JOINT_LEFT_SHOULDER + 1
549 NI_SDK_R_ELBOW = 5; // NITE_JOINT_LEFT_ELBOW + 1
550 NI_SDK_R_WRIST = 7; // NITE_JOINT_LEFT_HAND + 1
551 NI_SDK_R_HAND = -1;
552 NI_SDK_R_FNGRTIP = -1;
553
554 NI_SDK_L_HIP = 11; // NITE_JOINT_RIGHT_HIP + 1
555 NI_SDK_L_KNEE = 13; // NITE_JOINT_RIGHT_KNEE + 1
556 NI_SDK_L_ANKLE = 15; // NITE_JOINT_RIGHT_FOOT + 1
557 NI_SDK_L_FOOT = -1;
558
559 NI_SDK_R_HIP = 10; // NITE_JOINT_LEFT_HIP + 1
560 NI_SDK_R_KNEE = 12; // NITE_JOINT_LEFT_KNEE + 1
561 NI_SDK_R_ANKLE = 14; // NITE_JOINT_LEFT_FOOT + 1
562 NI_SDK_R_FOOT = -1;
563
564 NI_SDK_L_FINGER = -1;
565 NI_SDK_R_FINGER = -1;
566 NI_SDK_FACE = -1;
567 NI_SDK_AVATAR = -1;
568
569 return;
570}
571
572
573
574// Kinect SDK
576{
577 NI_SDK_PELVIS = 0; // NUI_SKELETON_POSITION_HIP_CENTER
578 NI_SDK_TORSO = 1; // SPINE は書き換えて使用
579 NI_SDK_NECK = 2; // NUI_SKELETON_POSITION_SHOULDER_CENTER
580 NI_SDK_HEAD = 3; // NUI_SKELETON_POSITION_HEAD
581
582 NI_SDK_L_EYE = -1;
583 NI_SDK_R_EYE = -1;
584 NI_SDK_L_BUST = -1;
585 NI_SDK_R_BUST = -1;
586
587 NI_SDK_L_SHLDR = 4; // NUI_SKELETON_POSITION_SHOULDER_LEFT
588 NI_SDK_L_ELBOW = 5; // NUI_SKELETON_POSITION_ELBOW_LEFT
589 NI_SDK_L_WRIST = 6; // NUI_SKELETON_POSITION_WRIST_LEFT
590 NI_SDK_L_HAND = 7; // NUI_SKELETON_POSITION_HAND_LEFT
591 NI_SDK_L_FNGRTIP = -1;
592
593 NI_SDK_R_SHLDR = 8; // NUI_SKELETON_POSITION_SHOULDER_RIGHT
594 NI_SDK_R_ELBOW = 9; // NUI_SKELETON_POSITION_ELBOW_RIGHT
595 NI_SDK_R_WRIST = 10; // NUI_SKELETON_POSITION_WRIST_RIGHT
596 NI_SDK_R_HAND = 11; // NUI_SKELETON_POSITION_HAND_RIGHT
597 NI_SDK_R_FNGRTIP = -1;
598
599 NI_SDK_L_HIP = 12; // NUI_SKELETON_POSITION_HIP_LEFT
600 NI_SDK_L_KNEE = 13; // NUI_SKELETON_POSITION_KNEE_LEFT
601 NI_SDK_L_ANKLE = 14; // NUI_SKELETON_POSITION_ANKLE_LEFT
602 NI_SDK_L_FOOT = 15; // NUI_SKELETON_POSITION_FOOT_LEFT
603
604 NI_SDK_R_HIP = 16; // NUI_SKELETON_POSITION_HIP_RIGHT
605 NI_SDK_R_KNEE = 17; // NUI_SKELETON_POSITION_KNEE_RIGHT
606 NI_SDK_R_ANKLE = 18; // NUI_SKELETON_POSITION_ANKLE_RIGHT
607 NI_SDK_R_FOOT = 19; // NUI_SKELETON_POSITION_FOOT_RIGHT
608
609 NI_SDK_L_FINGER = -1;
610 NI_SDK_R_FINGER = -1;
611 NI_SDK_FACE = -1;
612 NI_SDK_AVATAR = -1;
613
614 return;
615}
616
617
618
619
621// 共通ジョイントのクラス
622// ジョイントを抽象化するクラス
623
625{
627
628 posVect = NULL;
629 rotQuat = NULL;
630 jntAngl = NULL;
631
632 posRing = NULL;
633 rotRing = NULL;
634
636}
637
638
639
641{
642 //
643 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
644 //
645 niPosVect[j].init(-1.0);
646 niRotQuat[j].init(-1.0);
647 niJntAngl[j] = 0.0;
648 }
649
650 return;
651}
652
653
654
655void CNiJoints::connectJointsData(Vector<double>* vct, Quaternion<double>* qut,
656 Vector<int>* crd, double* agl, CRingBuffer* vrg, CRingBuffer* qrg)
657{
658 posVect = vct;
659 rotQuat = qut;
660 jntAngl = agl;
661 crdVect = crd;
662 posRing = vrg;
663 rotRing = qrg;
664}
665
666
667
669{
670 if (posVect!=NULL) {
671 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
672 int n = Ni2SDKPosJointNum(j, sdk_lib);
673 if (n>=0) {
674 niPosVect[j] = posVect[n];
675 }
676 }
677 }
678
679 if (rotQuat!=NULL) {
680 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
681 int n = Ni2SDKRotJointNum(j, sdk_lib);
682 if (n>=0) {
683 niRotQuat[j] = rotQuat[n];
684 }
685 }
686 }
687
688 if (jntAngl!=NULL) {
689 for (int j=0; j<NI_TOTAL_JOINT_NUM; j++) {
690 int n = Ni2SDKRotJointNum(j, sdk_lib);
691 if (n>=0) {
692 //if (mirror) n = NiSDKMirrorJointNum(n, sdk_lib);
693 niJntAngl[j] = jntAngl[n];
694 }
695 }
696 }
697
698 return;
699}
700
701
702
703
705// Frame Data
706
718NiFrameData* jbxwl::makeFramesData(int frame, int jnum, int* frame_num)
719{
720 if (jnum<0) return NULL; // 0 is OK for dummy
721
722 NiFrameData* frmdata = (NiFrameData*)malloc(frame*sizeof(NiFrameData));
723 if (frmdata==NULL) return NULL;
724 memset(frmdata, 0, frame*sizeof(NiFrameData));
725
726 for (int i=0; i<frame; i++) {
727 if (frame_num!=NULL) {
728 frmdata[i].frmn = frame_num[i];
729 frmdata[i].msec = frame_num[i]; // デフォルトはフレーム間 1ms
730 }
731 else {
732 frmdata[i].frmn = i;
733 frmdata[i].msec = i;
734 }
735 frmdata[i].jnum = jnum;
736
737 if (frmdata[i].jnum>0) {
738 frmdata[i].jdat = (NiJointData*)malloc(frmdata[i].jnum*sizeof(NiJointData));
739 if (frmdata[i].jdat==NULL) {
740 freeFramesData(frmdata, frame);
741 return NULL;
742 }
743 //
744 for (int j=0; j<frmdata[i].jnum; j++) {
745 frmdata[i].jdat[j].joint = -1;
746 frmdata[i].jdat[j].index = -1;
747 frmdata[i].jdat[j].vect.init(-1.0);
748 frmdata[i].jdat[j].quat.init(-1.0);
749 }
750 }
751 else {
752 frmdata[i].jdat = NULL; // for dummy
753 }
754 }
755
756 return frmdata;
757}
758
759
760
771/*
772NiFrameData* jbxwl::makeFramesData(int frame, int jnum)
773{
774 if (jnum<0) return NULL;
775
776 int* joint_num = (int*)malloc(frame*sizeof(int));
777 if (joint_num==NULL) return NULL;
778 for (int i=0; i<frame; i++) joint_num[i] = jnum;
779
780 NiFrameData* frmdata = makeVarFrameData(frame, joint_num, NULL);
781 ::free(joint_num);
782
783 return frmdata;
784}*/
785
786
787
788void jbxwl::freeFramesData(NiFrameData* frmdata, int frm_num)
789{
790 if (frmdata==NULL || frm_num<=0) return;
791
792 for (int i=0; i<frm_num; i++) {
793 if (frmdata[i].jdat!=NULL) {
794 ::free(frmdata[i].jdat);
795 frmdata[i].jdat = NULL;
796 }
797 }
798
799 ::free(frmdata);
800 return;
801}
802
803
804
805
807// 回転角の補正
808
809//
810// tmin が 0.0の場合は,補正は適用されない.
811//
813{
814 // Initialize
815 RBound_NI_TORSO.init();
816 RBound_NI_CHEST.init();
817 RBound_NI_NECK.init();
818 RBound_NI_HEAD.init();
819
820 RBound_NI_L_COLLAR.init();
821 RBound_NI_R_COLLAR.init();
822 RBound_NI_L_SHLDR.init();
823 RBound_NI_R_SHLDR.init();
824 RBound_NI_L_ELBOW.init();
825 RBound_NI_R_ELBOW.init();
826 RBound_NI_L_WRIST.init();
827 RBound_NI_R_WRIST.init();
828 RBound_NI_L_HAND.init();
829 RBound_NI_R_HAND.init();
830
831 RBound_NI_L_HIP.init();
832 RBound_NI_R_HIP.init();
833 RBound_NI_L_KNEE.init();
834 RBound_NI_R_KNEE.init();
835 RBound_NI_L_ANKLE.init();
836 RBound_NI_R_ANKLE.init();
837 RBound_NI_L_FOOT.init();
838 RBound_NI_R_FOOT.init();
839
840 return;
841}
842
843
844
845void jbxwl::NiCorrectJointsRotation(Quaternion<double>* quat, NiSDK_Lib lib)
846{
847 int joint;
848
849 // TORSO
850 joint = Ni2SDKRotJointNum(NI_TORSO, lib); // abdomen
851 if (joint>0) NiCorrectJointEuler(NI_TORSO, quat+joint, RBound_NI_TORSO);
852 // CHEST
853 joint = Ni2SDKRotJointNum(NI_CHEST, lib);
854 if (joint>0) NiCorrectJointEuler(NI_CHEST, quat+joint, RBound_NI_CHEST);
855 // NECK
856 joint = Ni2SDKRotJointNum(NI_NECK, lib);
857 if (joint>0) NiCorrectJointEuler(NI_NECK, quat+joint, RBound_NI_NECK);
858 // HEAD
859 joint = Ni2SDKRotJointNum(NI_HEAD, lib);
860 if (joint>0) NiCorrectJointEuler(NI_HEAD, quat+joint, RBound_NI_HEAD);
861
862 // L_COLLAR
863 joint = Ni2SDKRotJointNum(NI_L_COLLAR, lib);
864 if (joint>0) NiCorrectJointEuler(NI_L_COLLAR, quat+joint, RBound_NI_L_COLLAR);
865 // R_COLLAR
866 joint = Ni2SDKRotJointNum(NI_R_COLLAR, lib);
867 if (joint>0) NiCorrectJointEuler(NI_R_COLLAR, quat+joint, RBound_NI_R_COLLAR);
868 // L_SHLDR
869 joint = Ni2SDKRotJointNum(NI_L_SHLDR, lib);
870 if (joint>0) NiCorrectJointEuler(NI_L_SHLDR, quat+joint, RBound_NI_L_SHLDR);
871 // R_SHLDR
872 joint = Ni2SDKRotJointNum(NI_R_SHLDR, lib);
873 if (joint>0) NiCorrectJointEuler(NI_R_SHLDR, quat+joint, RBound_NI_R_SHLDR);
874 // L_ELBOW
875 joint = Ni2SDKRotJointNum(NI_L_ELBOW, lib);
876 if (joint>0) NiCorrectJointEuler(NI_L_ELBOW, quat+joint, RBound_NI_L_ELBOW);
877 // R_ELBOW
878 joint = Ni2SDKRotJointNum(NI_R_ELBOW, lib);
879 if (joint>0) NiCorrectJointEuler(NI_R_ELBOW, quat+joint, RBound_NI_R_ELBOW);
880 // L_WRIST
881 joint = Ni2SDKRotJointNum(NI_L_WRIST, lib);
882 if (joint>0) NiCorrectJointEuler(NI_L_WRIST, quat+joint, RBound_NI_L_WRIST);
883 // R_WRIST
884 joint = Ni2SDKRotJointNum(NI_R_WRIST, lib);
885 if (joint>0) NiCorrectJointEuler(NI_R_WRIST, quat+joint, RBound_NI_R_WRIST);
886 // L_HAND
887 joint = Ni2SDKRotJointNum(NI_L_HAND, lib);
888 if (joint>0) NiCorrectJointEuler(NI_L_HAND, quat+joint, RBound_NI_L_HAND);
889 // R_HAND
890 joint = Ni2SDKRotJointNum(NI_R_HAND, lib);
891 if (joint>0) NiCorrectJointEuler(NI_R_HAND, quat+joint, RBound_NI_R_HAND);
892
893 // L_HIP
894 joint = Ni2SDKRotJointNum(NI_L_HIP, lib);
895 if (joint>0) NiCorrectJointEuler(NI_L_HIP, quat+joint, RBound_NI_L_HIP);
896 // R_HIP
897 joint = Ni2SDKRotJointNum(NI_R_HIP, lib);
898 if (joint>0) NiCorrectJointEuler(NI_R_HIP, quat+joint, RBound_NI_R_HIP);
899 // L_KNEE
900 joint = Ni2SDKRotJointNum(NI_L_KNEE, lib);
901 if (joint>0) NiCorrectJointEuler(NI_L_KNEE, quat+joint, RBound_NI_L_KNEE);
902 // R_KNEE
903 joint = Ni2SDKRotJointNum(NI_R_KNEE, lib);
904 if (joint>0) NiCorrectJointEuler(NI_R_KNEE, quat+joint, RBound_NI_R_KNEE);
905 // L_ANKLE
906 joint = Ni2SDKRotJointNum(NI_L_ANKLE, lib);
907 if (joint>0) NiCorrectJointEuler(NI_L_ANKLE, quat+joint, RBound_NI_L_ANKLE);
908 // R_ANKLE
909 joint = Ni2SDKRotJointNum(NI_R_ANKLE, lib);
910 if (joint>0) NiCorrectJointEuler(NI_R_ANKLE, quat+joint, RBound_NI_R_ANKLE);
911 // L_FOOT
912 joint = Ni2SDKRotJointNum(NI_L_FOOT, lib);
913 if (joint>0) NiCorrectJointEuler(NI_L_FOOT, quat+joint, RBound_NI_L_FOOT);
914 // R_FOOT
915 joint = Ni2SDKRotJointNum(NI_R_FOOT, lib);
916 if (joint>0) NiCorrectJointEuler(NI_R_FOOT, quat+joint, RBound_NI_R_FOOT);
917
918 return;
919}
920
921
922
923void jbxwl::NiCorrectJointEuler(int joint, Quaternion<double>* quat, RBound<double> rb)
924{
925 if (rb.tmin==0.0) return; // 修正しない
926
927 Vector<double> eul[2];
928
929 eul[0].set(0.0, 0.0, 0.0, 0.0, -1.0);
930 NiGetJointEuler(joint, quat, eul);
931 if (eul[0].c==-1.0) return; // オイラー角への変換に失敗
932
933 //
934 BOOL chgf = FALSE;
935 if (eul[0].x<rb.xmin) { eul[0].x = rb.xmin; chgf = TRUE;}
936 else if (eul[0].x>rb.xmax) { eul[0].x = rb.xmax; chgf = TRUE;}
937 if (eul[0].y<rb.ymin) { eul[0].y = rb.ymin; chgf = TRUE;}
938 else if (eul[0].y>rb.ymax) { eul[0].y = rb.ymax; chgf = TRUE;}
939 if (eul[0].z<rb.zmin) { eul[0].z = rb.zmin; chgf = TRUE;}
940 else if (eul[0].z>rb.zmax) { eul[0].z = rb.zmax; chgf = TRUE;}
941 if (!chgf) return;
942
943 //
944 chgf = FALSE;
945 if (eul[1].x<rb.xmin) { eul[1].x = rb.xmin; chgf = TRUE;}
946 else if (eul[1].x>rb.xmax) { eul[1].x = rb.xmax; chgf = TRUE;}
947 if (eul[1].y<rb.ymin) { eul[1].y = rb.ymin; chgf = TRUE;}
948 else if (eul[1].y>rb.ymax) { eul[1].y = rb.ymax; chgf = TRUE;}
949 if (eul[1].z<rb.zmin) { eul[1].z = rb.zmin; chgf = TRUE;}
950 else if (eul[1].z>rb.zmax) { eul[1].z = rb.zmax; chgf = TRUE;}
951 if (!chgf) return;
952
953 //
954 NiSetJointEuler(joint, eul, quat);
955
956 return;
957}
958
959
960
961void jbxwl::NiGetJointEuler(int joint, Quaternion<double>* quat, Vector<double>* vect)
962{
963 if (vect==NULL) return;
964
965 Vector<double> vt[2];
966 vt[0].set(0.0, 0.0, 0.0, 0.0, -1.0);
967
968 if (joint==NI_L_ANKLE || joint==NI_R_ANKLE) {
969 Quaternion2ExtEulerXZY(*quat, vt);
970 if (vt[0].c!=-1.0) {
971 vect[0].set(vt[0].element1(), vt[0].element3(), vt[0].element2());
972 vect[1].set(vt[1].element1(), vt[1].element3(), vt[1].element2());
973 }
974 }
975 else if (joint==NI_L_COLLAR || joint==NI_R_COLLAR) {
976 Quaternion2ExtEulerYXZ(*quat, vt);
977 if (vt[0].c!=-1.0) {
978 vect[0].set(vt[0].element2(), vt[0].element1(), vt[0].element3());
979 vect[1].set(vt[1].element2(), vt[1].element1(), vt[1].element3());
980 }
981 }
982 else if (joint==NI_L_SHLDR || joint==NI_R_SHLDR) {
983 Quaternion2ExtEulerYZX(*quat, vt);
984 if (vt[0].c!=-1.0) {
985 vect[0].set(vt[0].element3(), vt[0].element1(), vt[0].element2());
986 vect[1].set(vt[1].element3(), vt[1].element1(), vt[1].element2());
987 }
988 }
989 else if (joint==NI_L_ELBOW || joint==NI_R_ELBOW) {
990 Quaternion2ExtEulerYXZ(*quat, vt);
991 if (vt[0].c!=-1.0) {
992 vect[0].set(vt[0].element2(), vt[0].element1(), vt[0].element3());
993 vect[1].set(vt[1].element2(), vt[1].element1(), vt[1].element3());
994 }
995 }
996 else if (joint==NI_L_WRIST || joint==NI_R_WRIST) {
997 Quaternion2ExtEulerYZX(*quat, vt);
998 if (vt[0].c!=-1.0) {
999 vect[0].set(vt[0].element3(), vt[0].element1(), vt[0].element2());
1000 vect[1].set(vt[1].element3(), vt[1].element1(), vt[1].element2());
1001 }
1002 }
1003 else {
1004 Quaternion2ExtEulerZXY(*quat, vt);
1005 if (vt[0].c!=-1.0) {
1006 vect[0].set(vt[0].element2(), vt[0].element3(), vt[0].element1());
1007 vect[1].set(vt[1].element2(), vt[1].element3(), vt[1].element1());
1008 }
1009 }
1010
1011 return;
1012}
1013
1014
1015
1016void jbxwl::NiSetJointEuler(int joint, Vector<double>* vect, Quaternion<double>* quat)
1017{
1018 Quaternion<double> qut[2], rot[2];
1019 Vector<double> eul[2];
1020
1021 if (joint==NI_L_ANKLE || joint==NI_R_ANKLE) {
1022 eul[0].set(vect[0].x, vect[0].z, vect[0].y);
1023 eul[1].set(vect[1].x, vect[1].z, vect[1].y);
1024 qut[0].setExtEulerXZY(eul[0]);
1025 qut[1].setExtEulerXZY(eul[1]);
1026 }
1027 else if (joint==NI_L_COLLAR || joint==NI_R_COLLAR) {
1028 eul[0].set(vect[0].y, vect[0].x, vect[0].z);
1029 eul[1].set(vect[1].y, vect[1].x, vect[1].z);
1030 qut[0].setExtEulerYXZ(eul[0]);
1031 qut[1].setExtEulerYXZ(eul[1]);
1032 }
1033 else if (joint==NI_L_SHLDR || joint==NI_R_SHLDR) {
1034 eul[0].set(vect[0].y, vect[0].z, vect[0].x);
1035 eul[1].set(vect[1].y, vect[1].z, vect[1].x);
1036 qut[0].setExtEulerYZX(eul[0]);
1037 qut[1].setExtEulerYZX(eul[1]);
1038 }
1039 else if (joint==NI_L_ELBOW || joint==NI_R_ELBOW) {
1040 eul[0].set(vect[0].y, vect[0].x, vect[0].z);
1041 eul[1].set(vect[1].y, vect[1].x, vect[1].z);
1042 qut[0].setExtEulerYXZ(eul[0]);
1043 qut[1].setExtEulerYXZ(eul[1]);
1044 }
1045 else if (joint==NI_L_WRIST || joint==NI_R_WRIST) {
1046 eul[0].set(vect[0].y, vect[0].z, vect[0].x);
1047 eul[1].set(vect[1].y, vect[1].z, vect[1].x);
1048 qut[0].setExtEulerYZX(eul[0]);
1049 qut[1].setExtEulerYZX(eul[1]);
1050 }
1051 else {
1052 eul[0].set(vect[0].z, vect[0].x, vect[0].y);
1053 eul[1].set(vect[1].z, vect[1].x, vect[1].y);
1054 qut[0].setExtEulerZXY(eul[0]);
1055 qut[1].setExtEulerZXY(eul[1]);
1056 }
1057
1058 //
1059 rot[0] = qut[0]*~(*quat);
1060 rot[1] = qut[1]*~(*quat);
1061 //
1062 if (rot[0].getAngle()<=rot[1].getAngle()) { // 回転角の小さい方を選択
1063 *quat = qut[0];
1064 }
1065 else {
1066 *quat = qut[1];
1067 }
1068
1069 return;
1070}
1071
1072
1073
1074
1076// CBaseFrameTool Class
1077
1082
1083
1084
1086{
1087 DEBUG_INFO("DESTRUCTOR: CBaseFrameTool\n");
1088
1089 free_data();
1090}
1091
1092
1093
1095{
1097 if (jointsData!=NULL) ::free(jointsData);
1098
1099 if (posVect!=NULL) ::free(posVect);
1100 if (rotQuat!=NULL) ::free(rotQuat);
1101
1102 framesData = NULL;
1103 jointsData = NULL;
1104
1105 posVect = NULL;
1106 rotQuat = NULL;
1107
1108 clear_data();
1109}
1110
1111
1112
1114{
1115 frames_num = 0;
1116 joints_num = 0;
1117
1118 start_time = 0;
1119 stop_time = 0;
1120 exec_time = 0;
1121}
1122
1123
1124
1126{
1127 framesData = NULL;
1128 jointsData = NULL;
1129
1130 posVect = NULL;
1131 rotQuat = NULL;
1132
1133 clear_data();
1134}
1135
1136
1137
1139
1141{
1142 if (jointsData!=NULL) {
1143 for (int j=0; j<jnum; j++) {
1144 jointsData[j].index = -1;
1145 jointsData[j].joint = -1;
1146 jointsData[j].vect.init(-1.0);
1147 jointsData[j].quat.init(-1.0);
1148 }
1149 }
1150}
1151
1152
1153
1155{
1156 if (posVect!=NULL && rotQuat!=NULL) {
1157 for (int j=0; j<jnum; j++) {
1158 posVect[j].init(-1.0);
1159 rotQuat[j].init(-1.0);
1160 }
1161 }
1162}
int NI_SDK_R_KNEE
RBound< double > RBound_NI_R_ELBOW
int NI_SDK_R_HAND
static int _NiMirrorJointNum[]
RBound< double > RBound_NI_L_ANKLE
RBound< double > RBound_NI_L_FOOT
int NI_SDK_L_WRIST
int NI_SDK_L_HAND
RBound< double > RBound_NI_L_HIP
int NI_SDK_L_HIP
int NI_SDK_R_HIP
RBound< double > RBound_NI_NECK
int NI_SDK_R_SHLDR
int NI_SDK_R_EYE
static int _KinectMirrorJointNum[]
int NI_SDK_L_SHLDR
RBound< double > RBound_NI_L_HAND
static int _OpenNI22JointNameNum[]
int NI_SDK_R_ANKLE
int NI_SDK_HEAD
RBound< double > RBound_NI_L_WRIST
static int _OpenNIMirrorJointNum[]
static int _Ni2OpenNI2RotJointNum[]
int NI_SDK_L_FNGRTIP
int NI_SDK_R_FINGER
int NI_SDK_PELVIS
static int _Ni2OpenNIPosJointNum[]
int NI_SDK_NECK
RBound< double > RBound_NI_R_KNEE
static int _Ni2OpenNI2PosJointNum[]
static int _Ni2KinectPosJointNum[]
int NI_SDK_R_ELBOW
int NI_SDK_FACE
int NI_SDK_L_FOOT
static int _OpenNI2MirrorJointNum[]
RBound< double > RBound_NI_L_SHLDR
static int _Ni2KinectRotJointNum[]
int NI_SDK_L_EYE
int NI_SDK_L_KNEE
static int _Ni2RotJointNum[]
int NI_SDK_AVATAR
static int _OpenNI2JointNameNum[]
RBound< double > RBound_NI_L_KNEE
static std::string _NiJointName[]
int NI_SDK_R_FNGRTIP
RBound< double > RBound_NI_HEAD
int NI_SDK_L_ANKLE
int NI_SDK_R_FOOT
RBound< double > RBound_NI_R_COLLAR
int NI_SDK_TORSO
static int _Ni2OpenNIRotJointNum[]
RBound< double > RBound_NI_R_WRIST
int NI_SDK_R_BUST
int NI_SDK_L_FINGER
RBound< double > RBound_NI_CHEST
int NI_SDK_L_ELBOW
RBound< double > RBound_NI_R_HIP
RBound< double > RBound_NI_L_ELBOW
RBound< double > RBound_NI_TORSO
static int _Kinect2JointNameNum[]
RBound< double > RBound_NI_R_SHLDR
int NI_SDK_R_WRIST
RBound< double > RBound_NI_R_FOOT
RBound< double > RBound_NI_L_COLLAR
int NI_SDK_L_BUST
RBound< double > RBound_NI_R_HAND
RBound< double > RBound_NI_R_ANKLE
#define NI_R_ELBOW
#define NI_PELVIS
#define NI_AVATAR
#define NI_HEAD
#define NI_R_HIP
#define NI_R_KNEE
#define NI_L_FNGRTIP
#define NI_R_FOOT
#define NI_R_FINGER
#define NI_R_FNGRTIP
#define NI_TOTAL_JOINT_NUM
#define NI_FACE
#define NI_TORSO
#define NI_L_BUST
#define NI_L_SHLDR
#define NI_R_EYE
#define Ni2SDKJointNum(j, l)
#define NI_L_FOOT
#define NI_R_SHLDR
#define NI_R_WRIST
#define NI_L_HIP
#define NI_L_ANKLE
#define NI_L_WRIST
#define NI_R_COLLAR
#define NI_L_COLLAR
#define NI_CHEST
#define NI_R_ANKLE
#define NI_L_FINGER
#define NI_L_ELBOW
#define NI_L_HAND
#define NI_NECK
#define NI_L_KNEE
#define NI_R_HAND
#define NI_R_BUST
#define NI_L_EYE
NiJointData * jointsData
void clearVectorData(int jnum)
virtual ~CBaseFrameTool(void)
void clearJointsData(int jnum)
Vector< double > * posVect
unsigned int frames_num
NiFrameData * framesData
Quaternion< double > * rotQuat
void clearNiJointsData(void)
Quaternion< double > niRotQuat[NI_TOTAL_JOINT_NUM]
void copyJoints2NiJoints(BOOL mirror)
Vector< int > * crdVect
Vector< double > * posVect
void connectJointsData(Vector< double > *vct, Quaternion< double > *qut, Vector< int > *crd, double *agl, CRingBuffer *vrg, CRingBuffer *qrg)
Vector< double > niPosVect[NI_TOTAL_JOINT_NUM]
double niJntAngl[NI_TOTAL_JOINT_NUM]
CRingBuffer * rotRing
CRingBuffer * posRing
Quaternion< double > * rotQuat
NiFrameData * makeFramesData(int frame, int joint_num, int *frame_num)
std::string NiJointName(int n)
void NiInitRBoundJointsRotation(void)
void NiSetOpenNI2JointNums(void)
void NiSetJointEuler(int joint, Vector< double > *vect, Quaternion< double > *quat)
int JointName2NiSDK(char *jname, NiSDK_Lib lib)
void NiCorrectJointsRotation(Quaternion< double > *quat, NiSDK_Lib lib)
int NiSDKMirrorJointNum(int joint, NiSDK_Lib lib)
void NiGetJointEuler(int joint, Quaternion< double > *quat, Vector< double > *vect)
void NiCorrectJointEuler(int joint, Quaternion< double > *quat, RBound< double > rb)
int Ni2SDKPosJointNum(int joint, NiSDK_Lib lib)
void NiSetKinectJointNums(void)
void freeFramesData(NiFrameData *frmdata, int frm_num)
void NiSetOpenNIJointNums(void)
std::string NiSDK2JointName(int joint, NiSDK_Lib lib)
int Ni2SDKRotJointNum(int joint, NiSDK_Lib lib)
NiSDK_Lib
Definition NiToolWin.h:35
@ NiSDK_Kinect
Definition NiToolWin.h:38
@ NiSDK_OpenNI2
Definition NiToolWin.h:40
@ NiSDK_OpenNI
Definition NiToolWin.h:39
@ NiSDK_None
Definition NiToolWin.h:37
int NiJointNum(char *name)
NiJointData * jdat
Quaternion< double > quat
Vector< double > vect