Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkKinectController.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #include "mitkKinectController.h"
13 
14 #include <XnCppWrapper.h>
15 
16 namespace mitk
17 {
18 class KinectController::KinectControllerPrivate
19 {
20 public:
21  KinectControllerPrivate();
22  ~KinectControllerPrivate();
23 
24  bool ErrorText(unsigned int error);
25 //OpenNI related stuff
26  xn::Context m_Context;
27  xn::DepthGenerator m_DepthGenerator;
28  xn::ImageGenerator m_ImageGenerator;
29  xn::IRGenerator m_IRGenerator;
30 
31 
32  bool m_ConnectionCheck;
33 
34  bool m_UseIR;
35 
36  unsigned int m_CaptureWidth;
37  unsigned int m_CaptureHeight;
38 };
39 
40 KinectController::KinectControllerPrivate::KinectControllerPrivate():
41  m_Context(nullptr),
42  m_DepthGenerator(nullptr),
43  m_ImageGenerator(nullptr),
44  m_IRGenerator(nullptr),
45  m_ConnectionCheck(false),
46  m_UseIR(false),
47  m_CaptureWidth(640),
48  m_CaptureHeight(480)
49 {
50 }
51 
52 KinectController::KinectControllerPrivate::~KinectControllerPrivate()
53 {
54 }
55 
56 bool KinectController::KinectControllerPrivate::ErrorText(unsigned int error)
57 {
58  if(error != XN_STATUS_OK)
59  {
60  MITK_ERROR << "Kinect Camera Error " << xnGetStatusString(error);
61  mitkThrow() << "Kinect Camera Error " << xnGetStatusString(error);
62  return false;
63  }
64  else return true;
65 }
66 
67 KinectController::KinectController(): d(new KinectControllerPrivate)
68 {
69 }
70 
72 {
73  delete d;
74  }
75 
77  {
78  if (!d->m_ConnectionCheck)
79  {
80  // Initialize the OpenNI status
81  d->m_ConnectionCheck = d->ErrorText(d->m_Context.Init());
82  if (!d->m_ConnectionCheck) return false;
83  // Create a depth generator and set its resolution
84  XnMapOutputMode DepthMode;
85  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.Create(d->m_Context));
86  if (!d->m_ConnectionCheck) return false;
87  d->m_DepthGenerator.GetMapOutputMode(DepthMode);
88  DepthMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution();
89  DepthMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution();
90  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.SetMapOutputMode(DepthMode));
91  if (!d->m_ConnectionCheck) return false;
92 
93  if (d->m_UseIR)
94  {
95  // Create the IR generator and set its resolution
96  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.Create(d->m_Context));
97  if (!d->m_ConnectionCheck) return false;
98  XnMapOutputMode IRMode;
99  d->m_IRGenerator.GetMapOutputMode(IRMode);
100  IRMode.nXRes = XN_VGA_X_RES;
101  IRMode.nYRes = XN_VGA_Y_RES;
102  IRMode.nFPS = 30;
103  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.SetMapOutputMode(IRMode));
104  if (!d->m_ConnectionCheck) return false;
105  }
106  else
107  {
108  // Create an image generator and set its resolution
109  XnMapOutputMode ImageMode;
110  d->m_ConnectionCheck = d->ErrorText(d->m_ImageGenerator.Create(d->m_Context));
111  if (!d->m_ConnectionCheck) return false;
112  d->m_ImageGenerator.GetMapOutputMode(ImageMode);
113  ImageMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution();
114  ImageMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution();
115  d->m_ConnectionCheck = d->ErrorText(d->m_ImageGenerator.SetMapOutputMode(ImageMode));
116  if (!d->m_ConnectionCheck) return false;
117  }
118 
119  // Camera registration
120  if( d->m_DepthGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) )
121  {
122  if (!d->m_UseIR)
123  {
124  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_ImageGenerator));
125  }
126  }
127  else
128  {
129  MITK_ERROR << "Alternative view point not supported by the depth generator...";
130  }
131  if(d->m_UseIR)
132  {
133  if( d->m_IRGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) )
134  {
135  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_DepthGenerator));
136  }
137  else
138  {
139  MITK_ERROR << "Alternative view point not supported by the depth generator...";
140  }
141  }
142 
143  // Start data generation
144  d->m_ConnectionCheck = d->ErrorText(d->m_Context.StartGeneratingAll());
145  if (!d->m_ConnectionCheck) return false;
146  }
147  return d->m_ConnectionCheck;
148  }
149 
151  {
152  d->m_ConnectionCheck = !d->ErrorText(d->m_Context.StopGeneratingAll());
153  return !d->m_ConnectionCheck;
154  }
155 
157  {
158  bool updateSuccessful = d->ErrorText(d->m_Context.WaitAndUpdateAll());
159  xn::DepthMetaData DepthMD;
160  d->m_DepthGenerator.GetMetaData(DepthMD);
161  d->m_CaptureWidth = DepthMD.XRes();
162  d->m_CaptureHeight = DepthMD.YRes();
163  return updateSuccessful;
164  }
165 
166  void KinectController::GetDistances(float* distances)
167  {
168  xn::DepthMetaData DepthMD;
169  d->m_DepthGenerator.GetMetaData(DepthMD);
170  const XnDepthPixel* DepthData = DepthMD.Data();
171 
172  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
173  {
174  distances[i] = static_cast<float>(DepthData[i]);
175  }
176  }
177 
178  void KinectController::GetRgb(unsigned char* rgb)
179  {
180  if (!d->m_UseIR)
181  {
182  xn::ImageMetaData ImageMD;
183  d->m_ImageGenerator.GetMetaData(ImageMD);
184  const XnRGB24Pixel* rgbPixelArray = ImageMD.RGB24Data();
185  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
186  {
187  rgb[i*3] = rgbPixelArray[i].nRed;
188  rgb[i*3+1] = rgbPixelArray[i].nGreen;
189  rgb[i*3+2] = rgbPixelArray[i].nBlue;
190  }
191  }
192  }
193 
194  void KinectController::GetAllData(float* distances, float* amplitudes, unsigned char* rgb)
195  {
196  // get current distance data
197  xn::DepthMetaData DepthMD;
198  d->m_DepthGenerator.GetMetaData(DepthMD);
199  const XnDepthPixel* DepthData = DepthMD.Data();
200  // IR data
201  xn::IRMetaData IRData;
202  const XnIRPixel* IRPixelData = nullptr;
203  // Image data
204  xn::ImageMetaData ImageMD;
205  const XnRGB24Pixel* rgbPixelArray = nullptr;
206  if (d->m_UseIR)
207  {
208  d->m_IRGenerator.GetMetaData(IRData);
209  IRPixelData = IRData.Data();
210  }
211  else
212  {
213  // get current rgb data
214  d->m_ImageGenerator.GetMetaData(ImageMD);
215  rgbPixelArray = ImageMD.RGB24Data();
216  }
217 
218  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
219  {
220  distances[i] = static_cast<float>(DepthData[i]);
221  if (d->m_UseIR)
222  {
223  amplitudes[i] = static_cast<float>(IRPixelData[i]);
224  }
225  else
226  {
227  rgb[i*3] = rgbPixelArray[i].nRed;
228  rgb[i*3+1] = rgbPixelArray[i].nGreen;
229  rgb[i*3+2] = rgbPixelArray[i].nBlue;
230  }
231  }
232  }
233 
234  void KinectController::GetAmplitudes( float* amplitudes )
235  {
236  if (d->m_UseIR)
237  {
238  xn::IRMetaData IRData;
239  d->m_IRGenerator.GetMetaData(IRData);
240  const XnIRPixel* IRPixelData = IRData.Data();
241 
242  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
243  {
244  amplitudes[i] = static_cast<float>(IRPixelData[i]);
245  }
246  }
247  }
248 
250  {
251  }
252 
254  {
255  return d->m_CaptureWidth;
256  }
257 
259  {
260  return d->m_CaptureHeight;
261  }
262 
264  {
265  return d->m_UseIR;
266  }
267  void KinectController::SetUseIR(bool useIR)
268  {
269  if (d->m_UseIR!=useIR)
270  {
271  d->m_UseIR = useIR;
272  this->Modified();
273  }
274  }
275 }
void GetIntensities(float *intensities)
virtual bool UpdateCamera()
updates the camera. The update function of the hardware interface is called only when new data is ava...
void GetRgb(unsigned char *rgb)
acquire new rgb data from the Kinect camera
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataCollection - Class to facilitate loading/accessing structured data.
void GetAllData(float *distances, float *amplitudes, unsigned char *rgb)
convenience method for faster access to distance and rgb data
unsigned int GetCaptureHeight() const
void GetDistances(float *distances)
acquire new distance data from the Kinect camera
#define mitkThrow()
virtual bool OpenCameraConnection()
opens a connection to the Kinect camera.
void GetAmplitudes(float *amplitudes)
virtual bool CloseCameraConnection()
closes the connection to the camera
unsigned int GetCaptureWidth() const