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