Medical Imaging Interaction Toolkit  2018.4.99-b585543d
Medical Imaging Interaction Toolkit
mitkUSIGTLDevice.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 <mitkUSIGTLDevice.h>
14 
15 mitk::USIGTLDevice::USIGTLDevice(std::string manufacturer, std::string model,
16  std::string host, int port, bool server)
17  : mitk::USDevice(manufacturer, model), m_Host(host), m_Port(port)
18 {
19  m_ControlInterfaceCustom = mitk::USVideoDeviceCustomControls::New(this);
20  if (server)
21  {
22  m_Device = mitk::IGTLServer::New(true);
23  }
24  else
25  {
26  m_Device = mitk::IGTLClient::New(true);
27  }
28  m_Device->SetPortNumber(m_Port);
29  m_Device->SetHostname(m_Host);
30  m_Device->SetName(manufacturer + " - " + model);
31 
32  m_TransformDeviceSource = mitk::IGTLTrackingDataDeviceSource::New();
33  m_TransformDeviceSource->SetIGTLDevice(m_Device);
34  m_TransformDeviceSource->RegisterAsMicroservice();
35 
36  m_DeviceSource = mitk::IGTL2DImageDeviceSource::New();
37  m_DeviceSource->SetIGTLDevice(m_Device);
38  m_DeviceSource->RegisterAsMicroservice();
39 
41  m_Filter->SetNumberOfExpectedOutputs(1);
42  m_Filter->ConnectTo(m_DeviceSource);
43 }
44 
45 std::string mitk::USIGTLDevice::GetDeviceClass() { return "IGTL Client"; }
46 
47 mitk::USImageSource::Pointer mitk::USIGTLDevice::GetUSImageSource()
48 {
49  return m_Filter.GetPointer();
50 }
51 
52 mitk::USAbstractControlInterface::Pointer mitk::USIGTLDevice::GetControlInterfaceCustom()
53 {
54  return m_ControlInterfaceCustom.GetPointer();
55 }
56 
58 {
59  m_DeviceSource->UnRegisterMicroservice();
60  m_TransformDeviceSource->UnRegisterMicroservice();
62 }
63 
64 std::vector<mitk::USProbe::Pointer> mitk::USIGTLDevice::GetAllProbes()
65 {
66  if (m_Probes.empty())
67  {
68  MITK_INFO << "No probes exist for this USVideDevice. Empty vector is returned";
69  }
70  return m_Probes;
71 }
72 
74 {
75  m_Probes.clear();
76 }
77 
78 mitk::USProbe::Pointer mitk::USIGTLDevice::GetCurrentProbe()
79 {
80  if (m_CurrentProbe.IsNotNull())
81  {
82  return m_CurrentProbe;
83  }
84  else
85  {
86  return nullptr;
87  }
88 }
89 
90 void mitk::USIGTLDevice::AddNewProbe(mitk::USProbe::Pointer probe)
91 {
92  m_Probes.push_back(probe);
93 }
94 
95 mitk::USProbe::Pointer mitk::USIGTLDevice::GetProbeByName(std::string name)
96 {
97  for (std::vector<mitk::USProbe::Pointer>::iterator it = m_Probes.begin(); it != m_Probes.end(); it++)
98  {
99  if (name.compare((*it)->GetName()) == 0)
100  return (*it);
101  }
102  MITK_INFO << "No probe with given name " << name << " was found.";
103  return nullptr; //no matching probe was found so 0 is returned
104 }
105 
107 {
108  for (std::vector<mitk::USProbe::Pointer>::iterator it = m_Probes.begin(); it != m_Probes.end(); it++)
109  {
110  if (name.compare((*it)->GetName()) == 0)
111  {
112  m_Probes.erase(it);
113  return;
114  }
115  }
116  MITK_INFO << "No Probe with given name " << name << " was found";
117 }
118 
120 {
121  if (m_Probes.size() == 0)
122  {
123  std::string name = "default";
124  mitk::USProbe::Pointer defaultProbe = mitk::USProbe::New(name);
125  m_Probes.push_back(defaultProbe);
126  }
127 
128  m_CurrentProbe = m_Probes.at(0);
129  MITK_INFO << "SetDefaultProbeAsCurrentProbe()";
130  this->ProbeChanged(m_CurrentProbe->GetName());
131 }
132 
133 void mitk::USIGTLDevice::SetCurrentProbe(std::string probename)
134 {
135  m_CurrentProbe = this->GetProbeByName(probename);
136  MITK_INFO << "SetCurrentProbe() " << probename;
137 }
138 
139 void mitk::USIGTLDevice::SetSpacing(double xSpacing, double ySpacing)
140 {
141  mitk::Vector3D spacing;
142  spacing[0] = xSpacing;
143  spacing[1] = ySpacing;
144  spacing[2] = 1;
145  MITK_INFO << "Spacing: " << spacing;
146 
147  if (m_CurrentProbe.IsNotNull())
148  {
149  m_CurrentProbe->SetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth(), spacing);
150  }
151  else
152  {
153  MITK_WARN << "Cannot set spacing. Current ultrasound probe not set.";
154  }
155 }
156 
158 
160 {
161  if (m_Device->GetState() == mitk::IGTLDevice::IGTLDeviceState::Running ||
162  m_Device->GetState() == mitk::IGTLDevice::IGTLDeviceState::Ready)
163  {
164  MITK_INFO << "Device is ready or running. So return true";
165  return true;
166  }
167  return m_Device->OpenConnection();
168 }
169 
171 {
172  return m_Device->CloseConnection();
173 }
174 
176 {
177  if (m_Device->GetState() == mitk::IGTLDevice::IGTLDeviceState::Running )
178  {
179  MITK_INFO << "Device is running. So return true";
180  return true;
181  }
182  return m_Device->StartCommunication();
183 }
184 
186 {
187  return m_Device->StopCommunication();
188 }
189 
191 {
192  Superclass::GenerateData();
193  if (m_ImageVector.size() == 0 || this->GetNumberOfIndexedOutputs() == 0)
194  {
195  return;
196  }
197 
198  m_ImageMutex->Lock();
199  auto& image = m_ImageVector[0];
200  if (image.IsNotNull() && image->IsInitialized() && m_CurrentProbe.IsNotNull())
201  {
202  //MITK_INFO << "Spacing CurrentProbe: " << m_CurrentProbe->GetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth());
203  image->GetGeometry()->SetSpacing(m_CurrentProbe->GetSpacingForGivenDepth(m_CurrentProbe->GetCurrentDepth()));
204  this->GetOutput(0)->SetGeometry(image->GetGeometry());
205  }
206  m_ImageMutex->Unlock();
207 }
bool OnActivation() override
Is called during the activation process. After this method is finished, the device should be generati...
void AddNewProbe(mitk::USProbe::Pointer probe) override
adds a new probe to the device
A device holds information about it&#39;s model, make and the connected probes. It is the common super cl...
Definition: mitkUSDevice.h:73
#define MITK_INFO
Definition: mitkLogMacros.h:18
bool OnConnection() override
Is called during the connection process. Override this method in a subclass to handle the actual conn...
void DeleteAllProbes() override
Cleans the std::vector containing all configured probes.
itk::SmartPointer< USAbstractControlInterface > GetControlInterfaceCustom() override
void SetSpacing(double xSpacing, double ySpacing) override
Sets the given spacing of the current depth of the current probe.
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 OnInitialization() override
Is called during the initialization process. Override this method in a subclass to handle the actual ...
void SetCurrentProbe(std::string probename) override
Sets the probe with the given name as current probe if the named probe exists.
USImageSource::Pointer GetUSImageSource() override
mitk::USProbe::Pointer GetCurrentProbe() override
Return current active probe for this USDevice Returns a pointer to the probe that is currently in use...
void ProbeChanged(std::string probename)
To be called when the used probe changed. Will update the service properties.
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...
#define MITK_WARN
Definition: mitkLogMacros.h:19
USIGTLDevice(std::string manufacturer, std::string model, std::string host, int port, bool server)
void UnregisterOnService()
Remove the IGTLDevice from the micro service.
mitk::Image::Pointer image
void UnregisterOnService()
Remove this device from the micro service.
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 RemoveProbeByName(std::string name) override
Removes the Probe with the given name.
std::vector< mitk::Image::Pointer > m_ImageVector
Definition: mitkUSDevice.h:386
static Pointer New()
std::string GetDeviceClass() override
Returns the Class of the Device. This Method must be reimplemented by every Inheriting Class...
OutputType * GetOutput()
Get the output data of this image source object.
bool OnDeactivation() override
Is called during the deactivation process. After a call to this method the device should still be con...
void SetDefaultProbeAsCurrentProbe() override
Sets the first existing probe or the default probe of the video device as the current probe of it...
itk::FastMutexLock::Pointer m_ImageMutex
mutex for images provided by the image source
Definition: mitkUSDevice.h:371
void GenerateData() override
Grabs the next frame from the Video input. This method is called internally, whenever Update() is inv...
bool OnDisconnection() override
Is called during the disconnection process. Override this method in a subclass to handle the actual d...