Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "mitkToFCameraDevice.h"
17 #include <itksys/SystemTools.hxx>
18 
19 namespace mitk
20 {
21  ToFCameraDevice::ToFCameraDevice():m_BufferSize(1),m_MaxBufferSize(100),m_CurrentPos(-1),m_FreePos(0),
22  m_CaptureWidth(204),m_CaptureHeight(204),m_PixelNumber(41616),m_SourceDataSize(0),
23  m_ThreadID(0),m_CameraActive(false),m_CameraConnected(false),m_ImageSequence(0)
24  {
25  this->m_AmplitudeArray = NULL;
26  this->m_IntensityArray = NULL;
27  this->m_DistanceArray = NULL;
29 
30  //By default, all devices have no further images (just a distance image)
31  //If a device provides more data (e.g. RGB, Intensity, Amplitde images,
32  //the property has to be true.
33  this->m_PropertyList->SetBoolProperty("HasRGBImage", false);
34  this->m_PropertyList->SetBoolProperty("HasIntensityImage", false);
35  this->m_PropertyList->SetBoolProperty("HasAmplitudeImage", false);
36 
40 
41  this->m_RGBImageWidth = this->m_CaptureWidth;
42  this->m_RGBImageHeight = this->m_CaptureHeight;
44  }
45 
47  {
48  }
49 
50  void ToFCameraDevice::SetBoolProperty( const char* propertyKey, bool boolValue )
51  {
52  SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
53  }
54 
55  void ToFCameraDevice::SetIntProperty( const char* propertyKey, int intValue )
56  {
57  SetProperty(propertyKey, mitk::IntProperty::New(intValue));
58  }
59 
60  void ToFCameraDevice::SetFloatProperty( const char* propertyKey, float floatValue )
61  {
62  SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
63  }
64 
65  void ToFCameraDevice::SetStringProperty( const char* propertyKey, const char* stringValue )
66  {
67  SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
68  }
69 
70  void ToFCameraDevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
71  {
72  this->m_PropertyList->SetProperty(propertyKey, propertyValue);
73  }
74 
75  BaseProperty* ToFCameraDevice::GetProperty(const char *propertyKey)
76  {
77  return this->m_PropertyList->GetProperty(propertyKey);
78  }
79 
80  bool ToFCameraDevice::GetBoolProperty(const char *propertyKey, bool& boolValue)
81  {
82  mitk::BoolProperty::Pointer boolprop = dynamic_cast<mitk::BoolProperty*>(this->GetProperty(propertyKey));
83  if(boolprop.IsNull())
84  return false;
85 
86  boolValue = boolprop->GetValue();
87  return true;
88  }
89 
90  bool ToFCameraDevice::GetStringProperty(const char *propertyKey, std::string& string)
91  {
92  mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty*>(this->GetProperty(propertyKey));
93  if(stringProp.IsNull())
94  {
95  return false;
96  }
97  else
98  {
99  string = stringProp->GetValue();
100  return true;
101  }
102  }
103  bool ToFCameraDevice::GetIntProperty(const char *propertyKey, int& integer)
104  {
105  mitk::IntProperty::Pointer intProp = dynamic_cast<mitk::IntProperty*>(this->GetProperty(propertyKey));
106  if(intProp.IsNull())
107  {
108  return false;
109  }
110  else
111  {
112  integer = intProp->GetValue();
113  return true;
114  }
115  }
116 
118  {
119  if (m_IntensityArray)
120  {
121  delete [] m_IntensityArray;
122  }
123  if (m_DistanceArray)
124  {
125  delete [] m_DistanceArray;
126  }
127  if (m_AmplitudeArray)
128  {
129  delete [] m_AmplitudeArray;
130  }
131  }
132 
134  {
135  // free memory if it was already allocated
137  // allocate buffer
138  this->m_IntensityArray = new float[this->m_PixelNumber];
139  for(int i=0; i<this->m_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;}
140  this->m_DistanceArray = new float[this->m_PixelNumber];
141  for(int i=0; i<this->m_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;}
142  this->m_AmplitudeArray = new float[this->m_PixelNumber];
143  for(int i=0; i<this->m_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;}
144  }
145 
147  {
148  return this->m_RGBImageWidth;
149  }
150 
152  {
153  return this->m_RGBImageHeight;
154  }
155 
157  {
158  m_CameraActiveMutex->Lock();
159  m_CameraActive = false;
160  m_CameraActiveMutex->Unlock();
161  itksys::SystemTools::Delay(100);
162  if (m_MultiThreader.IsNotNull())
163  {
164  m_MultiThreader->TerminateThread(m_ThreadID);
165  }
166  // wait a little to make sure that the thread is terminated
167  itksys::SystemTools::Delay(100);
168  }
169 
171  {
172  m_CameraActiveMutex->Lock();
173  bool ok = m_CameraActive;
174  m_CameraActiveMutex->Unlock();
175  return ok;
176  }
177 
179  {
180  // Prepare connection, fail if this fails.
181  if (! this->OnConnectCamera()) return false;
182  return true;
183  }
184 
186  {
187  return m_CameraConnected;
188  }
189 }
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
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.