Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkUSVideoDevice.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 
13 #include "mitkUSVideoDevice.h"
15 
16 mitk::USVideoDevice::USVideoDevice(int videoDeviceNumber, std::string manufacturer, std::string model) : mitk::USDevice(manufacturer, model)
17 {
18  Init();
19  m_SourceIsFile = false;
20  m_DeviceID = videoDeviceNumber;
21  m_FilePath = "";
22 }
23 
24 mitk::USVideoDevice::USVideoDevice(std::string videoFilePath, std::string manufacturer, std::string model) : mitk::USDevice(manufacturer, model)
25 {
26  Init();
27  m_SourceIsFile = true;
28  m_FilePath = videoFilePath;
29 }
30 
31 mitk::USVideoDevice::USVideoDevice(int videoDeviceNumber, mitk::USImageMetadata::Pointer metadata) : mitk::USDevice(metadata)
32 {
33  Init();
34  m_SourceIsFile = false;
35  m_DeviceID = videoDeviceNumber;
36  m_FilePath = "";
37 }
38 
39 mitk::USVideoDevice::USVideoDevice(std::string videoFilePath, mitk::USImageMetadata::Pointer metadata) : mitk::USDevice(metadata)
40 {
41  Init();
42  m_SourceIsFile = true;
43  m_FilePath = videoFilePath;
44 }
45 
47 {
48  //m_Source->UnRegister();
49  m_Source = nullptr;
50 }
51 
53 {
55  m_ControlInterfaceCustom = mitk::USVideoDeviceCustomControls::New(this);
56  //this->SetNumberOfInputs(1);
57  this->SetNumberOfIndexedOutputs(1);
58 
59  // mitk::USImage::Pointer output = mitk::USImage::New();
60  // output->Initialize();
61  this->SetNthOutput(0, this->MakeOutput(0));
62 }
63 
65 {
67 }
68 
70 {
71  return "org.mitk.modules.us.USVideoDevice";
72 }
73 
74 mitk::USAbstractControlInterface::Pointer mitk::USVideoDevice::GetControlInterfaceCustom()
75 {
76  return m_ControlInterfaceCustom.GetPointer();
77 }
78 
80 {
81  // nothing to do at initialization of video device
82  return true;
83 }
84 
86 {
87  if (m_SourceIsFile){
88  m_Source->SetVideoFileInput(m_FilePath);
89  }
90  else {
91  m_Source->SetCameraInput(m_DeviceID);
92  }
93  //SetSourceCropArea();
94  return true;
95 }
96 
98 {
100 
101  m_Source->ReleaseInput();
102 
103  return true;
104 }
105 
107 {
108  // make sure that video device is ready before aquiring images
109  if (!m_Source->GetIsReady())
110  {
111  MITK_WARN("mitkUSDevice")("mitkUSVideoDevice") << "Could not activate us video device. Check if video grabber is configured correctly.";
112  return false;
113  }
114 
115  MITK_INFO << "Activated UsVideoDevice!";
116  return true;
117 }
118 
120 {
121  // happens automatically when m_Active is set to false
122  return true;
123 }
124 
126 {
127  Superclass::GenerateData();
128 
129  if( m_ImageVector.size() == 0 || this->GetNumberOfIndexedOutputs() == 0 )
130  {
131  return;
132  }
133 
134  m_ImageMutex->Lock();
135  auto& image = m_ImageVector[0];
136  if( image.IsNotNull() && image->IsInitialized() && m_CurrentProbe.IsNotNull() )
137  {
138  //MITK_INFO << "Spacing CurrentProbe: " << m_CurrentProbe->GetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth());
139  image->GetGeometry()->SetSpacing(m_CurrentProbe->GetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth()));
140  this->GetOutput(0)->SetGeometry(image->GetGeometry());
141  }
142  m_ImageMutex->Unlock();
143 }
144 
146 {
147  if (m_DeviceState == State_Activated) { this->Deactivate(); }
148  if (m_DeviceState == State_Connected) { this->Disconnect(); }
149 
151 }
152 
153 mitk::USImageSource::Pointer mitk::USVideoDevice::GetUSImageSource()
154 {
155  return m_Source.GetPointer();
156 }
157 
158 std::vector<mitk::USProbe::Pointer> mitk::USVideoDevice::GetAllProbes()
159 {
160  if (m_Probes.empty())
161  {
162  MITK_INFO << "No probes exist for this USVideDevice. Empty vector is returned";
163  }
164  return m_Probes;
165 }
166 
168 {
169  m_Probes.clear();
170 }
171 
172 mitk::USProbe::Pointer mitk::USVideoDevice::GetCurrentProbe()
173 {
174  if (m_CurrentProbe.IsNotNull())
175  {
176  return m_CurrentProbe;
177  }
178  else
179  {
180  return nullptr;
181  }
182 }
183 
184 mitk::USProbe::Pointer mitk::USVideoDevice::GetProbeByName(std::string name)
185 {
186  for (std::vector<mitk::USProbe::Pointer>::iterator it = m_Probes.begin(); it != m_Probes.end(); it++)
187  {
188  if (name.compare((*it)->GetName()) == 0)
189  return (*it);
190  }
191  MITK_INFO << "No probe with given name " << name << " was found.";
192  return nullptr; //no matching probe was found so 0 is returned
193 }
194 
196 {
197  for (std::vector<mitk::USProbe::Pointer>::iterator it = m_Probes.begin(); it != m_Probes.end(); it++)
198  {
199  if (name.compare((*it)->GetName()) == 0)
200  {
201  m_Probes.erase(it);
202  return;
203  }
204  }
205  MITK_INFO << "No Probe with given name " << name << " was found";
206 }
207 
208 void mitk::USVideoDevice::AddNewProbe(mitk::USProbe::Pointer probe)
209 {
210  m_Probes.push_back(probe);
211 }
212 
214 {
215  return m_SourceIsFile;
216 }
217 
219 {
220  if( m_Probes.size() == 0 )
221  {
222  std::string name = "default";
223  mitk::USProbe::Pointer defaultProbe = mitk::USProbe::New( name );
224  m_Probes.push_back( defaultProbe );
225  }
226 
227  m_CurrentProbe = m_Probes.at(0);
228  MITK_INFO << "SetDefaultProbeAsCurrentProbe()";
229  this->ProbeChanged( m_CurrentProbe->GetName() );
230 }
231 
232 void mitk::USVideoDevice::SetCurrentProbe(std::string probename)
233 {
234  m_CurrentProbe = this->GetProbeByName( probename );
235  MITK_INFO << "SetCurrentProbe() " << probename;
236 }
237 
238 void mitk::USVideoDevice::SetSpacing(double xSpacing, double ySpacing)
239 {
240  mitk::Vector3D spacing;
241  spacing[0] = xSpacing;
242  spacing[1] = ySpacing;
243  spacing[2] = 1;
244  MITK_INFO << "Spacing: " << spacing;
245 
246  if( m_CurrentProbe.IsNotNull() )
247  {
248  m_CurrentProbe->SetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth(), spacing);
249  }
250  else
251  {
252  MITK_WARN << "Cannot set spacing. Current ultrasound probe not set.";
253  }
254 }
itk::SmartPointer< USAbstractControlInterface > GetControlInterfaceCustom() override
A device holds information about it&#39;s model, make and the connected probes. It is the common super cl...
Definition: mitkUSDevice.h:73
bool GetIsSourceFile()
True, if this Device plays back a file, false if it recieves data from a device.
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
Make a DataObject of the correct type to used as the specified output.
#define MITK_INFO
Definition: mitkLogMacros.h:18
std::vector< mitk::USProbe::Pointer > GetAllProbes() override
Return all probes for this USVideoDevice or an empty vector it no probes were set Returns a std::vect...
void SetDefaultProbeAsCurrentProbe() override
Sets the first existing probe or the default probe of the video device as the current probe of it...
virtual void SetGeometry(BaseGeometry *aGeometry3D)
Set the BaseGeometry of the data, which will be referenced (not copied!). Assumes the data object has...
DataCollection - Class to facilitate loading/accessing structured data.
bool OnConnection() override
Is called during the connection process. Returns true if successful and false if unsuccessful. Additionally, you may throw an exception to clarify what went wrong.
itk::SmartPointer< USVideoDeviceCustomControls > m_ControlInterfaceCustom
custom control interface for us video device
bool OnInitialization() override
Is called during the initialization process. Returns true if successful and false if unsuccessful...
int m_DeviceID
The device id to connect to. Undefined, if m_SourceIsFile == true;.
bool OnDisconnection() override
Is called during the disconnection process. Returns true if successful and false if unsuccessful...
mitk::USProbe::Pointer GetProbeByName(std::string name) override
get the probe by its name Returns a pointer to the probe identified by the given name. If no probe of given name exists for this Device 0 is returned.
void ProbeChanged(std::string probename)
To be called when the used probe changed. Will update the service properties.
void SetCurrentProbe(std::string probename) override
Sets the probe with the given name as current probe if the named probe exists.
#define MITK_WARN
Definition: mitkLogMacros.h:19
mitk::USProbe::Pointer GetCurrentProbe() override
Return current active probe for this USVideoDevice Returns a pointer to the probe that is currently i...
void GenerateData() override
Grabs the next frame from the Video input. This method is called internally, whenever Update() is inv...
void RemoveProbeByName(std::string name) override
Removes the Probe with the given name.
bool m_SourceIsFile
True, if this source plays back a file, false if it recieves data from a device.
mitk::Image::Pointer image
mitk::USProbe::Pointer m_CurrentProbe
probe that is currently in use
void UnregisterOnService()
Remove this device from the micro service.
std::vector< mitk::Image::Pointer > m_ImageVector
Definition: mitkUSDevice.h:386
USVideoDevice(int videoDeviceNumber, std::string manufacturer, std::string model)
Creates a new device that will deliver USImages taken from a video device. under windows, try -1 for device number, which will grab the first available one (Open CV functionality)
static Pointer New()
void SetSpacing(double xSpacing, double ySpacing) override
Sets the given spacing of the current depth of the current probe.
void UnregisterOnService()
Remove this device from the micro service. This method is public for mitk::USVideoDevice, because this devices can be completly removed. This is not possible for API devices, which should be available while their sub module is loaded.
DeviceStates m_DeviceState
Definition: mitkUSDevice.h:408
void AddNewProbe(mitk::USProbe::Pointer probe) override
adds a new probe to the device
USImageSource::Pointer GetUSImageSource() override
bool OnActivation() override
Is called during the activation process. After this method is finsihed, the device should be generati...
std::string m_FilePath
The Filepath id to connect to. Undefined, if m_SourceIsFile == false;.
void Deactivate()
Deactivates this device. After the deactivation process, the device will no longer produce images...
std::vector< mitk::USProbe::Pointer > m_Probes
probes for this USVideoDevice
static Pointer New()
OutputType * GetOutput()
Get the output data of this image source object.
static std::string GetDeviceClassStatic()
std::string GetDeviceClass() override
itk::FastMutexLock::Pointer m_ImageMutex
mutex for images provided by the image source
Definition: mitkUSDevice.h:371
bool OnDeactivation() override
Is called during the deactivation process. After a call to this method the device should still be con...
bool Disconnect()
Works analogously to mitk::USDevice::Connect(). Don&#39;t override this Method, but onDisconnection inste...
void DeleteAllProbes() override
Cleans the std::vector containing all configured probes.
mitk::USImageVideoSource::Pointer m_Source
The image source that we use to aquire data.
void Init()
Initializes common properties for all constructors.