Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUSDevicePersistence.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 
18 
19 //Microservices
20 #include <usServiceReference.h>
21 #include <usModuleContext.h>
22 #include <usGetModuleContext.h>
23 #include <usModuleContext.h>
24 
25 #include <iostream>
26 
27 mitk::USDevicePersistence::USDevicePersistence() : m_devices("MITK US", "Device Settings")
28 {
29 }
30 
32 {
33  us::ModuleContext* thisContext = us::GetModuleContext();
34 
35  std::vector<us::ServiceReference<USDevice> > services = thisContext->GetServiceReferences<USDevice>();
36  MITK_INFO << "Trying to save " << services.size() << " US devices.";
37  int numberOfSavedDevices = 0;
38  for (std::vector<us::ServiceReference<USDevice> >::iterator it = services.begin(); it != services.end(); ++it)
39  {
40  mitk::USDevice::Pointer currentDevice = thisContext->GetService(*it);
41  //check if it is a USVideoDevice
42  if (currentDevice->GetDeviceClass() == "org.mitk.modules.us.USVideoDevice")
43  {
44  mitk::USVideoDevice::Pointer currentVideoDevice = dynamic_cast<mitk::USVideoDevice*>(currentDevice.GetPointer());
45  QString identifier = "device" + QString::number(numberOfSavedDevices);
46  m_devices.setValue(identifier, USVideoDeviceToString(currentVideoDevice));
47  numberOfSavedDevices++;
48  }
49 
50  else
51  {
52  MITK_WARN << "Saving of US devices of the type " << currentDevice->GetDeviceClass() << " is not supported at the moment. Skipping device.";
53  }
54  }
55  m_devices.setValue("numberOfSavedDevices", numberOfSavedDevices);
56  MITK_INFO << "Successfully saved " << numberOfSavedDevices << " US devices.";
57 }
58 
59 std::vector<mitk::USDevice::Pointer> mitk::USDevicePersistence::RestoreLastDevices()
60 {
61  std::vector<mitk::USDevice::Pointer> devices;
62 
63  int numberOfSavedDevices = m_devices.value("numberOfSavedDevices").toInt();
64 
65  for (int i = 0; i < numberOfSavedDevices; i++)
66  {
67  // Try each device. If an exception occurs: Ignore device and notify user
68  try
69  {
70  QString currentString = m_devices.value("device" + QString::number(i)).toString();
71  mitk::USDevice::Pointer currentDevice = dynamic_cast<mitk::USDevice*>(StringToUSVideoDevice(currentString).GetPointer());
72  //currentDevice->Initialize();
73  devices.push_back(currentDevice.GetPointer());
74  }
75  catch (...)
76  {
77  MITK_ERROR << "Error occured while loading a USVideoDevice from persistence. Device assumed corrupt, will be deleted.";
78  //QMessageBox::warning(NULL, "Could not load device" ,"A stored ultrasound device is corrupted and could not be loaded. The device will be deleted.");
79  }
80  }
81 
82  MITK_INFO << "Restoring " << numberOfSavedDevices << " US devices.";
83 
84  return devices;
85 }
86 
88 {
89  QString manufacturer = d->GetManufacturer().c_str();
90  QString model = d->GetName().c_str();
91  QString comment = d->GetComment().c_str();
92  int source = d->GetDeviceID();
93  std::string file = d->GetFilePath();
94  if (!d->GetIsSourceFile()) file = "none"; //if GetIsSourceFile is true, the device plays back a file
95 
96  mitk::USImageVideoSource::Pointer imageSource = dynamic_cast<mitk::USImageVideoSource*>(d->GetUSImageSource().GetPointer());
97  if (!imageSource)
98  {
99  MITK_ERROR << "There is no USImageVideoSource at the current device.";
100  mitkThrow() << "There is no USImageVideoSource at the current device.";
101  }
102 
103  int greyscale = imageSource->GetIsGreyscale();
104  int resOverride = imageSource->GetResolutionOverride();
105  int resWidth = imageSource->GetResolutionOverrideWidth();
106  int resHight = imageSource->GetResolutionOverrideHeight();
107 
108  mitk::USImageVideoSource::USImageRoi roi = imageSource->GetRegionOfInterest();
109 
110  QString probes = ""; //ACV$100%1%1%0$120%2%2%0$140%2%2%5!BDW$90%1%1%2$100%1%1%8!CSV$50%1%2%3$60%2%2%5
111 
112  char probesSeperator = '!';
113  std::vector<mitk::USProbe::Pointer> allProbesOfDevice = d->GetAllProbes();
114  if (allProbesOfDevice.size() > 0)
115  {
116  for (std::vector<mitk::USProbe::Pointer>::iterator it = allProbesOfDevice.begin(); it != allProbesOfDevice.end(); it++)
117  {
118  if (it == allProbesOfDevice.begin())
119  { // if it is the first element there is no need for the probes seperator
120  probes = probes + USProbeToString(*it);
121  }
122  else
123  {
124  probes = probes + probesSeperator + USProbeToString(*it);
125  }
126  }
127  }
128 
129  char seperator = '|';
130 
131  QString returnValue = manufacturer + seperator
132  + model + seperator
133  + comment + seperator
134  + QString::number(source) + seperator
135  + file.c_str() + seperator
136  + QString::number(greyscale) + seperator
137  + QString::number(resOverride) + seperator
138  + QString::number(resWidth) + seperator
139  + QString::number(resHight) + seperator
140  + QString::number(roi.topLeftX) + seperator
141  + QString::number(roi.topLeftY) + seperator
142  + QString::number(roi.bottomRightX) + seperator
143  + QString::number(roi.bottomRightY) + seperator
144  + probes
145  ;
146 
147  MITK_INFO << "Output String: " << returnValue.toStdString();
148  return returnValue;
149 }
150 
152 {
153  QString probe = p->GetName().c_str();
154  char depthSeperator = '$';
155  char spacingSeperator = '%';
156  std::map<int, mitk::Vector3D> depthsAndSpacing = p->GetDepthsAndSpacing();
157  if (depthsAndSpacing.size() > 0)
158  {
159  for (std::map<int, mitk::Vector3D>::iterator it = depthsAndSpacing.begin(); it != depthsAndSpacing.end(); it++){
160  probe = probe + depthSeperator + QString::number(it->first) + spacingSeperator + QString::number(it->second[0])
161  + spacingSeperator + QString::number(it->second[1]) + spacingSeperator + QString::number(it->second[2]);
162  }
163  }
164  return probe;
165 }
166 
168 {
169  MITK_INFO << "Input String: " << s.toStdString();
170  std::vector<std::string> data;
171  std::string seperators = "|";
172  std::string text = s.toStdString();
173  split(text, seperators, data);
174  if (data.size() != 14)
175  {
176  MITK_ERROR << "Cannot parse US device! (Size: " << data.size() << ")";
177  return mitk::USVideoDevice::New("INVALID", "INVALID", "INVALID");
178  }
179 
180  std::string manufacturer = data.at(0);
181  std::string model = data.at(1);
182  std::string comment = data.at(2);
183  int source = (QString(data.at(3).c_str())).toInt();
184  std::string file = data.at(4);
185  bool greyscale = (QString(data.at(5).c_str())).toInt();
186  bool resOverride = (QString(data.at(6).c_str())).toInt();
187  int resWidth = (QString(data.at(7).c_str())).toInt();
188  int resHight = (QString(data.at(8).c_str())).toInt();
190  cropArea.topLeftX = (QString(data.at(9).c_str())).toInt();
191  cropArea.topLeftY = (QString(data.at(10).c_str())).toInt();
192  cropArea.bottomRightX = (QString(data.at(11).c_str())).toInt();
193  cropArea.bottomRightY = (QString(data.at(12).c_str())).toInt();
194 
195  // Create Device
196  mitk::USVideoDevice::Pointer returnValue;
197  if (file == "none")
198  {
199  returnValue = mitk::USVideoDevice::New(source, manufacturer, model);
200  returnValue->SetComment(comment);
201  }
202  else
203  {
204  returnValue = mitk::USVideoDevice::New(file, manufacturer, model);
205  returnValue->SetComment(comment);
206  }
207 
209  dynamic_cast<mitk::USImageVideoSource*>(returnValue->GetUSImageSource().GetPointer());
210  if (!imageSource)
211  {
212  MITK_ERROR << "There is no USImageVideoSource at the current device.";
213  mitkThrow() << "There is no USImageVideoSource at the current device.";
214  }
215 
216  // Set Video Options
217  imageSource->SetColorOutput(!greyscale);
218 
219  // If Resolution override is activated, apply it
220  if (resOverride)
221  {
222  imageSource->OverrideResolution(resWidth, resHight);
223  imageSource->SetResolutionOverride(true);
224  }
225 
226  // Set Crop Area
227  imageSource->SetRegionOfInterest(cropArea);
228  std::string probes = data.at(13);
229  std::string probesSeperator = "!";
230  std::vector<std::string> probesVector;
231  split(probes, probesSeperator, probesVector);
232  for (std::vector<std::string>::iterator it = probesVector.begin(); it != probesVector.end(); it++)
233  {
234  mitk::USProbe::Pointer probe = StringToUSProbe(*it);
235  returnValue->AddNewProbe(probe);
236  }
237 
238  return returnValue;
239 }
240 
242 {
244  std::string spacingSeperator = "%";
245  std::string depthSeperator = "$";
246  std::vector<std::string> depthsWithSpacings;
247  split(s, depthSeperator, depthsWithSpacings);
248 
249  for (std::vector<std::string>::iterator it = depthsWithSpacings.begin(); it != depthsWithSpacings.end(); it++)
250  {
251  if (it == depthsWithSpacings.begin()) //first element is the name of the probe
252  {
253  probe->SetName(*it);
254  }
255  else //other elements are the scanning depths of the probe and the spacing
256  {
257  std::vector<std::string> spacings;
258  split(*it, spacingSeperator, spacings);
259  mitk::Vector3D spacing;
260  double x;
261  double y;
262  double z;
263  int depth;
264  try
265  {
266  x = spacingToDouble(spacings.at(1));
267  y = spacingToDouble(spacings.at(2));
268  z = spacingToDouble(spacings.at(3));
269  }
270  catch (const mitk::Exception& e)
271  {
272  MITK_ERROR << e.GetDescription() << "Spacing of " << probe->GetName() << " at depth " << spacings.at(0) << " will be set to default value 1,1,0.";
273  x = 1;
274  y = 1;
275  z = 1;
276  }
277  spacing[0] = x;
278  spacing[1] = y;
279  spacing[2] = z;
280 
281  try
282  {
283  depth = depthToInt(spacings.at(0));
284  }
285  catch (const mitk::Exception& e)
286  {
287  MITK_ERROR << probe->GetName() << ": " << e.GetDescription();
288  continue;
289  }
290  probe->SetDepthAndSpacing(depth, spacing);
291  }
292  }
293  return probe;
294 }
295 
296 void mitk::USDevicePersistence::split(std::string& text, std::string& separators, std::vector<std::string>& words)
297 {
298  int n = text.length();
299  int start, stop;
300 
301  start = text.find_first_not_of(separators);
302  while ((start >= 0) && (start < n))
303  {
304  stop = text.find_first_of(separators, start);
305  if ((stop < 0) || (stop > n)) stop = n;
306  words.push_back(text.substr(start, stop - start));
307  start = text.find_first_not_of(separators, stop + 1);
308  }
309 }
310 
312 {
313  std::istringstream i(s);
314  double x;
315  if (!(i >> x))
316  {
317  //something went wrong because the string contains characters which can not be convertet into double
318  mitkThrow() << "An error occured while trying to recover the spacing.";
319  }
320  return x;
321 }
322 
324 {
325  std::istringstream i(s);
326  int x;
327  if (!(i >> x))
328  {
329  //something went wrong because the string contains characters which can not be convertet into int
330  mitkThrow() << "An error occured while trying to recover the scanning depth. " << s << " is not a valid scanning depth. ";
331  }
332  return x;
333 }
mitk::USProbe::Pointer StringToUSProbe(std::string s)
QString USVideoDeviceToString(mitk::USVideoDevice::Pointer d)
A device holds information about it's model, make and the connected probes. It is the common super cl...
Definition: mitkUSDevice.h:77
std::vector< mitk::USDevice::Pointer > RestoreLastDevices()
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
Method for creation through the object factory.
This class can be pointed to a video file or a videodevice and delivers USImages. ...
QString USProbeToString(mitk::USProbe::Pointer p)
void split(std::string &text, std::string &separators, std::vector< std::string > &words)
Defines a region of interest by top left and bottom right corner.
#define MITK_WARN
Definition: mitkLogMacros.h:23
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkThrow()
mitk::USVideoDevice::Pointer StringToUSVideoDevice(QString s)
static Pointer New()
double spacingToDouble(std::string s)
A mitk::USVideoDevice is the common class for video only devices. They capture video input either fro...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)