JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
OpenNi2Device.cpp
Go to the documentation of this file.
1
5#include "OpenNi2Device.h"
6
7#ifdef ENABLE_OPENNI2
8
9
10using namespace jbxl;
11
12
13COpenNi2Device::COpenNi2Device(void)
14{
15 context = NULL;
16 recorder = NULL;
17
18 image = NULL;
19 depth = NULL;
20// audio = NULL;
21 user = NULL;
22
23 m_enu_dev = EnuDev_Unknown;
24 m_has_camera = TRUE;
25 m_err_mesg = make_Buffer(LMESG);
26
28 m_xsize = OPENNI_DEPTH_XSIZE;
29 m_ysize = OPENNI_DEPTH_YSIZE;
30 m_nfps = OPENNI_DEPTH_FPS;
31
32 //
33 m_has_image_data = TRUE;
34 m_has_depth_data = TRUE;
35// m_has_audio_data = TRUE;
36
37 // Video Mode
38 imageMode.setFps(m_nfps);
39 imageMode.setResolution(m_xsize, m_ysize);
40 imageMode.setPixelFormat(openni::PIXEL_FORMAT_RGB888);
41
42 depthMode.setFps(m_nfps);
43 depthMode.setResolution(m_xsize, m_ysize);
44 depthMode.setPixelFormat(openni::PIXEL_FORMAT_DEPTH_1_MM);
45}
46
47
48BOOL COpenNi2Device::init(BOOL use_image)
49{
50 m_stream_flg = 0x00;
51 m_stream_num = 0;
52 for (int s=0; s<OPENNI2_STREAM_DATA_NUM; s++) m_streams[s] = NULL;
53
54 m_enu_dev = EnuDev_None;
55
56 BOOL ret = create_Context();
57 if (ret && use_image) ret = create_Image();
58 if (ret) ret = create_Depth();
59 if (ret) {
60 const openni::DeviceInfo info = context->getDeviceInfo();
61 DEBUG_INFO("INFO: COpenNi2Device::init(): Device Name = %s", info.getName());
62 //
63 if (!strcasecmp(info.getName(), OPENNI2_DEVICE_PS1080)) m_enu_dev = EnuDev_PS1080;
64 else if (!strcasecmp(info.getName(), OPENNI2_DEVICE_KINECT)) m_enu_dev = EnuDev_Kinect;
65 else if (!strcasecmp(info.getName(), OPENNI2_DEVICE_ONIFILE)) m_enu_dev = EnuDev_OniFile;
66 }
67 return ret;
68}
69
70
71BOOL COpenNi2Device::init(char* fname, BOOL use_image)
72{
73 m_enu_dev = EnuDev_None;
74
75 if (fname==NULL) {
76 copy_s2Buffer("ERROR: COpenNi2Device::init(): File name is NULL.", &m_err_mesg);
77 return FALSE;
78 }
79
80 m_stream_flg = 0x00;
81 m_stream_num = 0;
82 for (int s=0; s<OPENNI2_STREAM_DATA_NUM; s++) m_streams[s] = NULL;
83
84 BOOL ret = create_Context(fname);
85 if (ret && use_image) ret = create_Image();
86 if (ret) ret = create_Depth();
87 if (ret) m_enu_dev = EnuDev_OniFile;
88
89 return ret;
90}
91
92
93void COpenNi2Device::free(void)
94{
95 DEBUG_LOG("LOG: COpenNi2Device::free(): deleting Recorder ...");
96 delete_Recorder();
97 //
98 DEBUG_LOG("LOG: COpenNi2Device::free(): deleting Image ...");
99 delete_Image();
100 DEBUG_LOG("LOG: COpenNi2Device::free(): deleting Depth ...");
101 delete_Depth();
102 DEBUG_LOG("LOG: COpenNi2Device::free(): deleting Context ...");
103 delete_Context();
104 DEBUG_LOG("LOG: COpenNi2Device::free(): deleted Generators.");
105 //
106 free_Buffer(&m_err_mesg);
107
108 DEBUG_LOG("LOG: COpenNi2Device::free(): END");
109}
110
111
112
114// Create/Delete Generators
115
116BOOL COpenNi2Device::create_Context(char* fname)
117{
118 if (context==NULL) {
119 context = new openni::Device();
120 if (context==NULL) {
121 DEBUG_ERR("ERROR: COpenNi2Device::create_Context(): context is NULL!!");
122 return FALSE;
123 }
124 }
125 else context->close();
126
127 const char* uri = fname;
128 if (uri==NULL) uri = openni::ANY_DEVICE;
129
130 openni::Status ret = context->open(uri);
131 if (ret!=openni::STATUS_OK) {
132 DEBUG_ERR("ERROR: COpenNi2Device::create_Context(): Device Open Error!! (%d)", ret);
133 deleteNull(context);
134 return FALSE;
135 }
136 //
137 //context->setDepthColorSyncEnabled(true);
138
139 DEBUG_INFO("INFO: COpenNi2Device::create_Context(): created Context.");
140 return TRUE;
141}
142
143
144BOOL COpenNi2Device::create_Recorder(char* fname, BOOL use_image)
145{
146 if (fname==NULL) {
147 copy_s2Buffer("ERROR: COpenNi2Device::create_Recorder(): File name is NULL.", &m_err_mesg);
148 return FALSE;
149 }
150 if (depth==NULL || (use_image && image==NULL)) {
151 copy_s2Buffer("ERROR: COpenNi2Device::create_Recorder(): Image or Depth Stream is NULL.", &m_err_mesg);
152 return FALSE;
153 }
154
155 if (recorder==NULL) {
156 recorder = new openni::Recorder();
157 if (recorder==NULL) {
158 DEBUG_ERR("ERROR: COpenNi2Device::create_Recorder(): recorder is NULL!!");
159 return FALSE;
160 }
161 }
162 else recorder->stop();
163
164 openni::Status ret = recorder->create(fname);
165 if (ret!=openni::STATUS_OK) {
166 deleteNull(recorder);
167 DEBUG_ERR("ERROR: COpenNi2Device::create_Recorder: File Open Error.");
168 copy_s2Buffer("ERROR: COpenNi2Device::create_Recorder: File Open Error.", &m_err_mesg);
169 return FALSE;
170 }
171
172 ret = recorder->attach(*depth, true);
173 if (ret==openni::STATUS_OK && use_image) ret = recorder->attach(*image, true);
174 if (ret!=openni::STATUS_OK) {
175 deleteNull(recorder);
176 DEBUG_ERR("ERROR: COpenNi2Device::create_Recorder: Stream Attach Error.");
177 copy_s2Buffer("ERROR: COpenNi2Device::create_Recorder: Stream Attach Error.", &m_err_mesg);
178 return FALSE;
179 }
180
181 DEBUG_INFO("INFO: COpenNi2Device::create_Recorder(): created Recorder.");
182 return TRUE;
183}
184
185
186BOOL COpenNi2Device::create_Image(void)
187{
188 if (!m_has_camera) return FALSE;
189
190 if (context==NULL) {
191 copy_s2Buffer("ERROR: COpenNi2Device::create_Image(): context is NULL.", &m_err_mesg);
192 return FALSE;
193 }
194
195 image = new openni::VideoStream();
196 if (image==NULL) return FALSE;
197
198 openni::Status ret = image->create(*context, openni::SENSOR_COLOR);
199 if (ret!=openni::STATUS_OK) {
200 deleteNull(image);
201 copy_s2Buffer("ERROR: COpenNi2Device::create_Image(): Craete Error.", &m_err_mesg);
202 return FALSE;
203 }
204
205 ret = image->start();
206 if (ret!=openni::STATUS_OK) {
207 deleteNull(image);
208 copy_s2Buffer("ERROR: COpenNi2Device::create_Image(): Start Error.", &m_err_mesg);
209 return FALSE;
210 }
211
212 image->setVideoMode(imageMode);
213
214 m_streams[OPENNI2_IMAGE_INDEX] = image;
215 m_stream_flg |= OPENNI2_IMAGE_FLAG;
216 m_stream_num++;
217
218 DEBUG_INFO("INFO: COpenNi2Device::create_Image(): created Image Stream.");
219 return TRUE;
220}
221
222
223BOOL COpenNi2Device::create_Depth(void)
224{
225 if (context==NULL) {
226 copy_s2Buffer("ERROR: COpenNi2Device::create_Depth(): context is NULL.", &m_err_mesg);
227 return FALSE;
228 }
229
230 depth = new openni::VideoStream();
231 if (depth==NULL) return FALSE;
232
233 openni::Status ret = depth->create(*context, openni::SENSOR_DEPTH);
234 if (ret!=openni::STATUS_OK) {
235 deleteNull(depth);
236 copy_s2Buffer("ERROR: COpenNi2Device::create_Depth(): Craete Error.", &m_err_mesg);
237 return FALSE;
238 }
239
240 ret = depth->start();
241 if (ret!=openni::STATUS_OK) {
242 deleteNull(depth);
243 copy_s2Buffer("ERROR: COpenNi2Device::create_Depth(): Start Error.", &m_err_mesg);
244 return FALSE;
245 }
246
247 depth->setVideoMode(depthMode);
248
249 m_streams[OPENNI2_DEPTH_INDEX] = depth;
250 m_stream_flg |= OPENNI2_DEPTH_FLAG;
251 m_stream_num++;
252
253 DEBUG_INFO("INFO: COpenNi2Device::create_Depth(): created Depth Stream.");
254 return TRUE;
255}
256
257
258BOOL COpenNi2Device::create_User(void)
259{
260 user = new nite::UserTracker();
261 if (user==NULL) return FALSE;
262
263 nite::Status ret = user->create(context); // context==NULL is OK
264 if (ret!=nite::STATUS_OK) {
265 deleteNull(user);
266 copy_s2Buffer("ERROR: COpenNi2Device::create_Depth(): Create Error", &m_err_mesg);
267 return FALSE;
268 }
269
270 DEBUG_INFO("INFO: COpenNi2Device::create_User(): created User Tracker.");
271 return TRUE;
272}
273
274
275
277// Delete Generators
278
279void COpenNi2Device::delete_Context(void)
280{
281 if (context==NULL || !context->isValid()) return;
282 //
283 context->close();
284 delete(context);
285 context = NULL;
286
287 return;
288}
289
290
291void COpenNi2Device::delete_Recorder(void)
292{
293 if (recorder==NULL || !recorder->isValid()) return;
294 //
295 int prev_state = m_state;
296 m_state = NI_STATE_SAVE_WORKING;
297 {
298 recorder->stop();
299 DEBUG_INFO("INFO: COpenNi2Device::delete_Recorder(): Recorder Stopped.");
300 delete(recorder);
301 recorder = NULL;
302 }
303 m_state = prev_state;
304
305 return;
306}
307
308
309void COpenNi2Device::delete_Image(void)
310{
311 m_stream_num--;
312 m_streams[OPENNI2_IMAGE_INDEX] = NULL;
313 m_stream_flg &= ~OPENNI2_IMAGE_FLAG;
314
315 if (image==NULL || !image->isValid()) return;
316
317 //
318 image->stop();
319
320 if (imageFrame.isValid()) imageFrame.release();
321 delete(image);
322 image = NULL;
323 return;
324}
325
326
327void COpenNi2Device::delete_Depth(void)
328{
329 m_stream_num--;
330 m_streams[OPENNI2_DEPTH_INDEX] = NULL;
331 m_stream_flg &= ~OPENNI2_DEPTH_FLAG;
332
333 if (depth==NULL || !depth->isValid()) return;
334
335 //
336 depth->stop();
337
338 if (depthFrame.isValid()) depthFrame.release();
339 delete(depth);
340 depth = NULL;
341 return;
342}
343
344
345void COpenNi2Device::delete_User(void)
346{
347 if (user==NULL || !user->isValid()) return;
348
349 //
350 if (userFrame.isValid()) userFrame.release();
351 delete(user);
352 user = NULL;
353
354 return;
355}
356
357
358
360// File
361
362BOOL COpenNi2Device::start_Recorde(char* file_name, BOOL use_image)
363{
364 DEBUG_INFO("INFO: COpenNi2Device::delete_Recorder(): Recorder Starting ...");
365
366 BOOL ret = FALSE;
367 unlink(file_name);
368
369 int prev_state = m_state;
370 m_state = NI_STATE_SAVE_WORKING;
371 {
373 ret = create_Recorder(file_name, use_image);
374 if (ret) {
375 openni::Status nc = recorder->start();
376 if (nc!=openni::STATUS_OK) {
377 copy_s2Buffer("ERROR: COpenNi2Device::start_Recorde: Start Error.", &m_err_mesg);
378 ret = FALSE;
379 }
380 }
381 }
382 m_state = prev_state;
383
384 if (ret) {
385 DEBUG_INFO("INFO: COpenNi2Device::delete_Recorder(): Recorder Started.");
386 }
387 else {
388 delete_Recorder();
389 unlink(file_name);
390 DEBUG_INFO("INFO: COpenNi2Device::delete_Recorder(): Recorder can not Start.");
391 }
392
393 return ret;
394}
395
396
397void COpenNi2Device::stop_Recorde(void)
398{
399 DEBUG_INFO("INFO: COpenNi2Device::delete_Recorder(): Recorder Stopping ...");
400 if (recorder!=NULL && recorder->isValid()) {
401 delete_Recorder();
402 }
403}
404
405
406
408//
409
410BOOL COpenNi2Device::setup_Tracking(int profile, double smooth)
411{
412 if (user!=NULL) {
413 nite::Status nc = user->setSkeletonSmoothingFactor(smooth);
414 if (nc==nite::STATUS_OK) return TRUE;
415 }
416
417 return FALSE;
418}
419
420
421
423// Set Data
424
425void COpenNi2Device::wait_StreamData(void)
426{
427// return;
428
429 openni::Status rc;
430 unsigned char flag = 0x00;
431 int stream_idx;
432
433 m_has_image_data = FALSE;
434 m_has_depth_data = FALSE;
435// m_has_audio_data = FALSE;
436
437 Loop {
438 rc = openni::OpenNI::waitForAnyStream(m_streams, m_stream_num, &stream_idx, 0);
439 if (rc==openni::STATUS_OK) {
440 if (stream_idx==OPENNI2_IMAGE_INDEX) {
441 if (image!=NULL) {
442 image->readFrame(&imageFrame);
443 flag |= OPENNI2_IMAGE_FLAG;
444 m_has_image_data = TRUE;
445 }
446 }
447 else if (stream_idx==OPENNI2_DEPTH_INDEX) {
448 if (depth!=NULL) {
449 depth->readFrame(&depthFrame);
450 flag |= OPENNI2_DEPTH_FLAG;
451 m_has_depth_data = TRUE;
452 }
453 }
454 /*
455 else if (stream_idx==OPENNI2_AUDIO_INDEX) {
456 if (audio!=NULL) {
457 audio->readFrame(&audioFrame);
458 flag |= OPENNI2_AUDIO_FLAG;
459 m_has_audio_data = TRUE;
460 }
461 }*/
462
463 if ((flag & m_stream_flg)==m_stream_flg) break;
464 }
465 }
466}
467
468
469BOOL COpenNi2Device::set_ImageData(void)
470{
471 if (image!=NULL) {
472// image->readFrame(&imageFrame);
473 if (imageFrame.isValid()) return TRUE;
474 }
475 return FALSE;
476}
477
478
479BOOL COpenNi2Device::set_DepthData(void)
480{
481 if (depth!=NULL) {
482 //depth->readFrame(&depthFrame);
483 if (depthFrame.isValid()) return TRUE;
484 }
485 return FALSE;
486}
487
488
489BOOL COpenNi2Device::set_SceneData(void)
490{
491 if (user!=NULL) {
492 user->readFrame(&userFrame);
493 if (userFrame.isValid()) return TRUE;
494 }
495
496 return FALSE;
497}
498
499
500
502// External Support Functions
503//
504
505// need ./OpenNI2/Drivers/*.dll
506//
507Buffer jbxl::initialize_OpenNI2(void)
508{
509 Buffer mesg = init_Buffer();
510
511 openni::Status rc = openni::OpenNI::initialize();
512 if (rc!=openni::STATUS_OK) {
513 mesg = make_Buffer(LMESG);
514 copy_s2Buffer("ERROR: jbxl::initializeOpenNI2(): Initialize OpenNI Error!!\n", &mesg);
515 cat_s2Buffer (openni::OpenNI::getExtendedError(), &mesg);
516 DEBUG_WARN("WARNING: %s", mesg.buf);
517 }
518 else DEBUG_INFO("INFO: jbxl::initialize_OpenNI2(): initialize OpenNI2");
519
520 return mesg;
521}
522
523
524void jbxl::shutdown_OpenNI2(void)
525{
526 openni::OpenNI::shutdown();
527 DEBUG_INFO("INFO: jbxl::shutdown_OpenNI2(): shutdown OpenNI2");
528}
529
530
531//
532Buffer jbxl::initialize_NiTE2(void)
533{
534 Buffer mesg = init_Buffer();
535
536 nite::Status nc = nite::NiTE::initialize();
537 if (nc!=nite::STATUS_OK) {
538 mesg = make_Buffer(LMESG);
539 copy_s2Buffer("ERROR: jbxl::initializeNiTE2(): Initialize NiTE Error!!\n", &mesg);
540 DEBUG_WARN("WARNING: %s", mesg.buf);
541 }
542 else DEBUG_INFO("INFO: jbxl::initialize_NiTE2(): initialize NiTE2");
543
544 return mesg;
545}
546
547
548void jbxl::shutdown_NiTE2(void)
549{
550 nite::NiTE::shutdown();
551 DEBUG_INFO("INFO: jbxl::shutdown_NiTE2(): shutdown NiTE2");
552}
553
554#endif // ifdef ENABLE_OPENNI2x
#define NI_STATE_SAVE_WORKING
Definition NiDevice.h:44
#define NI_WORKING_WAIT_TIME
Definition NiDevice.h:46
#define OPENNI_DEPTH_FPS
Definition NiDevice.h:35
#define OPENNI_DEPTH_YSIZE
Definition NiDevice.h:34
#define NI_STATE_DETECT_STOPPED
Definition NiDevice.h:40
@ EnuDev_Kinect
Definition NiDevice.h:21
@ EnuDev_Unknown
Definition NiDevice.h:18
@ EnuDev_OniFile
Definition NiDevice.h:22
@ EnuDev_None
Definition NiDevice.h:19
@ EnuDev_PS1080
Definition NiDevice.h:20
#define OPENNI_DEPTH_XSIZE
Definition NiDevice.h:33
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:71
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition buffer.cpp:47
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
Definition buffer.h:108
#define cat_s2Buffer(src, dst)
cat_b2Buffer()
Definition buffer.h:122
#define unlink
Definition common.h:55
#define Loop
Definition common.h:256
#define LMESG
Definition common.h:148
#define TRUE
Definition common.h:226
#define FALSE
Definition common.h:223
#define strcasecmp
Definition common.h:58
Definition Brep.h:29
void deleteNull(T &p)
Definition common++.h:39
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
#define DEBUG_INFO
デバッグ用出力関数.コンパイル時に削除可能
Definition tools.h:511
#define DEBUG_WARN
デバッグ用出力関数.コンパイル時に削除可能
Definition tools.h:512