Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 #include "mitkKinectController.h"
17 
18 #include <XnCppWrapper.h>
19 
20 namespace mitk
21 {
22 class KinectController::KinectControllerPrivate
23 {
24 public:
25  KinectControllerPrivate();
26  ~KinectControllerPrivate();
27 
28  bool ErrorText(unsigned int error);
29 //OpenNI related stuff
30  xn::Context m_Context;
31  xn::DepthGenerator m_DepthGenerator;
32  xn::ImageGenerator m_ImageGenerator;
33  xn::IRGenerator m_IRGenerator;
34 
35 
36  bool m_ConnectionCheck;
37 
38  bool m_UseIR;
39 
40  unsigned int m_CaptureWidth;
41  unsigned int m_CaptureHeight;
42 };
43 
44 KinectController::KinectControllerPrivate::KinectControllerPrivate():
45  m_Context(NULL),
46  m_DepthGenerator(NULL),
47  m_ImageGenerator(NULL),
48  m_IRGenerator(NULL),
49  m_ConnectionCheck(false),
50  m_UseIR(false),
51  m_CaptureWidth(640),
52  m_CaptureHeight(480)
53 {
54 }
55 
56 KinectController::KinectControllerPrivate::~KinectControllerPrivate()
57 {
58 }
59 
60 bool KinectController::KinectControllerPrivate::ErrorText(unsigned int error)
61 {
62  if(error != XN_STATUS_OK)
63  {
64  MITK_ERROR << "Kinect Camera Error " << xnGetStatusString(error);
65  mitkThrow() << "Kinect Camera Error " << xnGetStatusString(error);
66  return false;
67  }
68  else return true;
69 }
70 
71 KinectController::KinectController(): d(new KinectControllerPrivate)
72 {
73 }
74 
76 {
77  delete d;
78  }
79 
81  {
82  if (!d->m_ConnectionCheck)
83  {
84  // Initialize the OpenNI status
85  d->m_ConnectionCheck = d->ErrorText(d->m_Context.Init());
86  if (!d->m_ConnectionCheck) return false;
87  // Create a depth generator and set its resolution
88  XnMapOutputMode DepthMode;
89  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.Create(d->m_Context));
90  if (!d->m_ConnectionCheck) return false;
91  d->m_DepthGenerator.GetMapOutputMode(DepthMode);
92  DepthMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution();
93  DepthMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution();
94  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.SetMapOutputMode(DepthMode));
95  if (!d->m_ConnectionCheck) return false;
96 
97  if (d->m_UseIR)
98  {
99  // Create the IR generator and set its resolution
100  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.Create(d->m_Context));
101  if (!d->m_ConnectionCheck) return false;
102  XnMapOutputMode IRMode;
103  d->m_IRGenerator.GetMapOutputMode(IRMode);
104  IRMode.nXRes = XN_VGA_X_RES;
105  IRMode.nYRes = XN_VGA_Y_RES;
106  IRMode.nFPS = 30;
107  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.SetMapOutputMode(IRMode));
108  if (!d->m_ConnectionCheck) return false;
109  }
110  else
111  {
112  // Create an image generator and set its resolution
113  XnMapOutputMode ImageMode;
114  d->m_ConnectionCheck = d->ErrorText(d->m_ImageGenerator.Create(d->m_Context));
115  if (!d->m_ConnectionCheck) return false;
116  d->m_ImageGenerator.GetMapOutputMode(ImageMode);
117  ImageMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution();
118  ImageMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution();
119  d->m_ConnectionCheck = d->ErrorText(d->m_ImageGenerator.SetMapOutputMode(ImageMode));
120  if (!d->m_ConnectionCheck) return false;
121  }
122 
123  // Camera registration
124  if( d->m_DepthGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) )
125  {
126  if (!d->m_UseIR)
127  {
128  d->m_ConnectionCheck = d->ErrorText(d->m_DepthGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_ImageGenerator));
129  }
130  }
131  else
132  {
133  MITK_ERROR << "Alternative view point not supported by the depth generator...";
134  }
135  if(d->m_UseIR)
136  {
137  if( d->m_IRGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) )
138  {
139  d->m_ConnectionCheck = d->ErrorText(d->m_IRGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_DepthGenerator));
140  }
141  else
142  {
143  MITK_ERROR << "Alternative view point not supported by the depth generator...";
144  }
145  }
146 
147  // Start data generation
148  d->m_ConnectionCheck = d->ErrorText(d->m_Context.StartGeneratingAll());
149  if (!d->m_ConnectionCheck) return false;
150  }
151  return d->m_ConnectionCheck;
152  }
153 
155  {
156  d->m_ConnectionCheck = !d->ErrorText(d->m_Context.StopGeneratingAll());
157  return !d->m_ConnectionCheck;
158  }
159 
161  {
162  bool updateSuccessful = d->ErrorText(d->m_Context.WaitAndUpdateAll());
163  xn::DepthMetaData DepthMD;
164  d->m_DepthGenerator.GetMetaData(DepthMD);
165  d->m_CaptureWidth = DepthMD.XRes();
166  d->m_CaptureHeight = DepthMD.YRes();
167  return updateSuccessful;
168  }
169 
170  void KinectController::GetDistances(float* distances)
171  {
172  xn::DepthMetaData DepthMD;
173  d->m_DepthGenerator.GetMetaData(DepthMD);
174  const XnDepthPixel* DepthData = DepthMD.Data();
175 
176  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
177  {
178  distances[i] = static_cast<float>(DepthData[i]);
179  }
180  }
181 
182  void KinectController::GetRgb(unsigned char* rgb)
183  {
184  if (!d->m_UseIR)
185  {
186  xn::ImageMetaData ImageMD;
187  d->m_ImageGenerator.GetMetaData(ImageMD);
188  const XnRGB24Pixel* rgbPixelArray = ImageMD.RGB24Data();
189  for (int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
190  {
191  rgb[i*3] = rgbPixelArray[i].nRed;
192  rgb[i*3+1] = rgbPixelArray[i].nGreen;
193  rgb[i*3+2] = rgbPixelArray[i].nBlue;
194  }
195  }
196  }
197 
198  void KinectController::GetAllData(float* distances, float* amplitudes, unsigned char* rgb)
199  {
200  // get current distance data
201  xn::DepthMetaData DepthMD;
202  d->m_DepthGenerator.GetMetaData(DepthMD);
203  const XnDepthPixel* DepthData = DepthMD.Data();
204  // IR data
205  xn::IRMetaData IRData;
206  const XnIRPixel* IRPixelData;
207  // Image data
208  xn::ImageMetaData ImageMD;
209  const XnRGB24Pixel* rgbPixelArray;
210  if (d->m_UseIR)
211  {
212  d->m_IRGenerator.GetMetaData(IRData);
213  IRPixelData = IRData.Data();
214  }
215  else
216  {
217  // get current rgb data
218  d->m_ImageGenerator.GetMetaData(ImageMD);
219  rgbPixelArray = ImageMD.RGB24Data();
220  }
221 
222  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
223  {
224  distances[i] = static_cast<float>(DepthData[i]);
225  if (d->m_UseIR)
226  {
227  amplitudes[i] = static_cast<float>(IRPixelData[i]);
228  }
229  else
230  {
231  rgb[i*3] = rgbPixelArray[i].nRed;
232  rgb[i*3+1] = rgbPixelArray[i].nGreen;
233  rgb[i*3+2] = rgbPixelArray[i].nBlue;
234  }
235  }
236  }
237 
238  void KinectController::GetAmplitudes( float* amplitudes )
239  {
240  if (d->m_UseIR)
241  {
242  xn::IRMetaData IRData;
243  d->m_IRGenerator.GetMetaData(IRData);
244  const XnIRPixel* IRPixelData = IRData.Data();
245 
246  for (unsigned int i=0; i<d->m_CaptureWidth*d->m_CaptureHeight; i++)
247  {
248  amplitudes[i] = static_cast<float>(IRPixelData[i]);
249  }
250  }
251  }
252 
253  void KinectController::GetIntensities( float* intensities )
254  {
255  }
256 
258  {
259  return d->m_CaptureWidth;
260  }
261 
263  {
264  return d->m_CaptureHeight;
265  }
266 
268  {
269  return d->m_UseIR;
270  }
271  void KinectController::SetUseIR(bool useIR)
272  {
273  if (d->m_UseIR!=useIR)
274  {
275  d->m_UseIR = useIR;
276  this->Modified();
277  }
278  }
279 }
unsigned int GetCaptureHeight() const
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:24
unsigned int GetCaptureWidth() const
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
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