Medical Imaging Interaction Toolkit  2018.4.99-6ca56567
Medical Imaging Interaction Toolkit
mitkToFCameraDevice.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 "mitkToFCameraDevice.h"
13 #include <itksys/SystemTools.hxx>
14 
15 namespace mitk
16 {
17  ToFCameraDevice::ToFCameraDevice():m_BufferSize(1),m_MaxBufferSize(100),m_CurrentPos(-1),m_FreePos(0),
18  m_CaptureWidth(204),m_CaptureHeight(204),m_PixelNumber(41616),m_SourceDataSize(0),
19  m_ThreadID(0),m_CameraActive(false),m_CameraConnected(false),m_ImageSequence(0)
20  {
21  this->m_AmplitudeArray = nullptr;
22  this->m_IntensityArray = nullptr;
23  this->m_DistanceArray = nullptr;
25 
26  //By default, all devices have no further images (just a distance image)
27  //If a device provides more data (e.g. RGB, Intensity, Amplitde images,
28  //the property has to be true.
29  this->m_PropertyList->SetBoolProperty("HasRGBImage", false);
30  this->m_PropertyList->SetBoolProperty("HasIntensityImage", false);
31  this->m_PropertyList->SetBoolProperty("HasAmplitudeImage", false);
32 
33  this->m_MultiThreader = itk::MultiThreader::New();
34  this->m_ImageMutex = itk::FastMutexLock::New();
35  this->m_CameraActiveMutex = itk::FastMutexLock::New();
36 
37  this->m_RGBImageWidth = this->m_CaptureWidth;
38  this->m_RGBImageHeight = this->m_CaptureHeight;
40  }
41 
43  {
44  }
45 
46  void ToFCameraDevice::SetBoolProperty( const char* propertyKey, bool boolValue )
47  {
48  SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
49  }
50 
51  void ToFCameraDevice::SetIntProperty( const char* propertyKey, int intValue )
52  {
53  SetProperty(propertyKey, mitk::IntProperty::New(intValue));
54  }
55 
56  void ToFCameraDevice::SetFloatProperty( const char* propertyKey, float floatValue )
57  {
58  SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
59  }
60 
61  void ToFCameraDevice::SetStringProperty( const char* propertyKey, const char* stringValue )
62  {
63  SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
64  }
65 
66  void ToFCameraDevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
67  {
68  this->m_PropertyList->SetProperty(propertyKey, propertyValue);
69  }
70 
71  BaseProperty* ToFCameraDevice::GetProperty(const char *propertyKey)
72  {
73  return this->m_PropertyList->GetProperty(propertyKey);
74  }
75 
76  bool ToFCameraDevice::GetBoolProperty(const char *propertyKey, bool& boolValue)
77  {
78  mitk::BoolProperty::Pointer boolprop = dynamic_cast<mitk::BoolProperty*>(this->GetProperty(propertyKey));
79  if(boolprop.IsNull())
80  return false;
81 
82  boolValue = boolprop->GetValue();
83  return true;
84  }
85 
86  bool ToFCameraDevice::GetStringProperty(const char *propertyKey, std::string& string)
87  {
88  mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty*>(this->GetProperty(propertyKey));
89  if(stringProp.IsNull())
90  {
91  return false;
92  }
93  else
94  {
95  string = stringProp->GetValue();
96  return true;
97  }
98  }
99  bool ToFCameraDevice::GetIntProperty(const char *propertyKey, int& integer)
100  {
101  mitk::IntProperty::Pointer intProp = dynamic_cast<mitk::IntProperty*>(this->GetProperty(propertyKey));
102  if(intProp.IsNull())
103  {
104  return false;
105  }
106  else
107  {
108  integer = intProp->GetValue();
109  return true;
110  }
111  }
112 
114  {
115  if (m_IntensityArray)
116  {
117  delete [] m_IntensityArray;
118  }
119  if (m_DistanceArray)
120  {
121  delete [] m_DistanceArray;
122  }
123  if (m_AmplitudeArray)
124  {
125  delete [] m_AmplitudeArray;
126  }
127  }
128 
130  {
131  // free memory if it was already allocated
133  // allocate buffer
134  this->m_IntensityArray = new float[this->m_PixelNumber];
135  for(int i=0; i<this->m_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;}
136  this->m_DistanceArray = new float[this->m_PixelNumber];
137  for(int i=0; i<this->m_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;}
138  this->m_AmplitudeArray = new float[this->m_PixelNumber];
139  for(int i=0; i<this->m_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;}
140  }
141 
143  {
144  return this->m_RGBImageWidth;
145  }
146 
148  {
149  return this->m_RGBImageHeight;
150  }
151 
153  {
154  m_CameraActiveMutex->Lock();
155  m_CameraActive = false;
156  m_CameraActiveMutex->Unlock();
157  itksys::SystemTools::Delay(100);
158  if (m_MultiThreader.IsNotNull())
159  {
160  m_MultiThreader->TerminateThread(m_ThreadID);
161  }
162  // wait a little to make sure that the thread is terminated
163  itksys::SystemTools::Delay(100);
164  }
165 
167  {
168  m_CameraActiveMutex->Lock();
169  bool ok = m_CameraActive;
170  m_CameraActiveMutex->Unlock();
171  return ok;
172  }
173 
175  {
176  // Prepare connection, fail if this fails.
177  if (! this->OnConnectCamera()) return false;
178  return true;
179  }
180 
182  {
183  return m_CameraConnected;
184  }
185 }
virtual BaseProperty * GetProperty(const char *propertyKey)
get a BaseProperty from the property list
static Pointer New()
signed integer value
Definition: jsoncpp.h:348
bool GetBoolProperty(const char *propertyKey, bool &boolValue)
get a bool from the property list
int m_RGBPixelNumber
number of pixels in the range image (m_RGBImageWidth*m_RGBImageHeight)
bool GetStringProperty(const char *propertyKey, std::string &string)
get a string from the property list
float * m_IntensityArray
float array holding the intensity image
virtual bool IsCameraConnected()
returns true if the camera is connected
int m_PixelNumber
number of pixels in the range image (m_CaptureWidth*m_CaptureHeight)
DataCollection - Class to facilitate loading/accessing structured data.
PropertyList::Pointer m_PropertyList
a list of the corresponding properties
float * m_DistanceArray
float array holding the distance image
int m_ThreadID
ID of the started thread.
bool GetIntProperty(const char *propertyKey, int &integer)
get an int from the property list
static Pointer New()
itk::FastMutexLock::Pointer m_CameraActiveMutex
mutex for the cameraActive flag
itk::MultiThreader::Pointer m_MultiThreader
itk::MultiThreader used for thread handling
Abstract base class for properties.
virtual void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
set a BaseProperty property in the property list
int m_RGBImageHeight
height of the RGB image (y dimension)
virtual bool OnConnectCamera()=0
Opens a connection to the ToF camera. Has to be implemented in the specialized inherited classes...
bool m_CameraActive
flag indicating if the camera is currently active or not. Caution: thread safe access only! ...
bool m_CameraConnected
flag indicating if the camera is successfully connected or not. Caution: thread safe access only! ...
int m_CaptureWidth
width of the range image (x dimension)
virtual bool ConnectCamera()
ConnectCamera Internally calls OnConnectCamera() of the respective device implementation.
itk::FastMutexLock::Pointer m_ImageMutex
mutex for images provided by the range camera
virtual void AllocatePixelArrays()
method for allocating memory for pixel arrays m_IntensityArray, m_DistanceArray and m_AmplitudeArray ...
static Pointer New()
virtual void StopCamera()
stops the continuous updating of the camera
static Pointer New()
int m_CaptureHeight
height of the range image (y dimension)
UTF-8 string value.
Definition: jsoncpp.h:351
float * m_AmplitudeArray
float array holding the amplitude image
Property for strings.
virtual void CleanupPixelArrays()
method for cleanup memory allocated for pixel arrays m_IntensityArray, m_DistanceArray and m_Amplitud...
void SetIntProperty(const char *propertyKey, int intValue)
set an int property in the property list
void SetBoolProperty(const char *propertyKey, bool boolValue)
set a bool property in the property list
int m_RGBImageWidth
width of the RGB image (x dimension)
void SetFloatProperty(const char *propertyKey, float floatValue)
set a float property in the property list
static Pointer New()
virtual bool IsCameraActive()
returns true if the camera is connected and started
void SetStringProperty(const char *propertyKey, const char *stringValue)
set a string property in the property list