MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkUSDeviceReaderXML.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
13// MITK
16#include <mitkIGTMimeTypes.h>
17#include <mitkLocaleSwitch.h>
18
19#include <mitkUSVideoDevice.h>
20
21// Third Party
22#include <itksys/SystemTools.hxx>
23#include <fstream>
24#include <tinyxml2.h>
25
26namespace
27{
28 std::string ReadStringAttribute(const tinyxml2::XMLElement* elem, const char* name)
29 {
30 const char* attrib = elem->Attribute(name);
31
32 return attrib != nullptr
33 ? attrib
34 : "";
35 }
36}
37
39 mitk::IGTMimeTypes::USDEVICEINFORMATIONXML_MIMETYPE(),
40 "MITK USDevice Reader (XML)"), m_Filename("")
41{
42 RegisterService();
43}
44
48
53
55{
56}
57
62
63
64
65
66std::vector<itk::SmartPointer<mitk::BaseData>> mitk::USDeviceReaderXML::DoRead()
67{
68 MITK_WARN << "This method is not implemented. \
69 Please use the method ReadUltrasoundDeviceConfiguration() instead.";
70 std::vector<mitk::BaseData::Pointer> result;
71 return result;
72}
73
75{
76 MITK_INFO << "Try to start reading xml device configuration...";
77 if (m_Filename == "")
78 {
79 MITK_WARN << "Cannot read file - empty filename!";
80 return false;
81 }
82
83 tinyxml2::XMLDocument document;
84 if (tinyxml2::XML_SUCCESS != document.LoadFile(m_Filename.c_str()))
85 {
86 MITK_ERROR << "Error when opening and reading file :" << m_Filename;
87 return false;
88 }
89
90 tinyxml2::XMLHandle documentHandle(&document);
91 auto* ultrasoundDeviceTag = documentHandle.FirstChildElement(USDeviceReaderWriterConstants::TAG_ULTRASOUNDDEVICE).ToElement();
92 if (ultrasoundDeviceTag == nullptr)
93 {
94 MITK_ERROR << "Error parsing the file :" << m_Filename << std::endl << "Wrong xml format structure.";
95 return false;
96 }
97
98 //Extract attribute information of the ULTRASOUNDDEVICE-Tag:
99 this->ExtractAttributeInformationOfUltrasoundDeviceTag(ultrasoundDeviceTag);
100
101 auto* generalSettingsTag = documentHandle.FirstChildElement(USDeviceReaderWriterConstants::TAG_ULTRASOUNDDEVICE).FirstChildElement(USDeviceReaderWriterConstants::TAG_GENERALSETTINGS).ToElement();
102 if (generalSettingsTag == nullptr)
103 {
104 MITK_ERROR << "Error parsing the GENERALSETTINGS-Tag in the file :" << m_Filename;
105 return false;
106 }
107
108 //Extract attribute information of the GENERALSETTINGS-Tag:
109 this->ExtractAttributeInformationOfGeneralSettingsTag(generalSettingsTag);
110
111 auto* probesTag = documentHandle.FirstChildElement(USDeviceReaderWriterConstants::TAG_ULTRASOUNDDEVICE).FirstChildElement(USDeviceReaderWriterConstants::TAG_PROBES).ToElement();
112 if (probesTag == nullptr)
113 {
114 MITK_ERROR << "Error: PROBES-Tag was not found in the file :" << m_Filename << "Therefore, creating default probe.";
115 //Create default ultrasound probe:
116 mitk::USProbe::Pointer ultrasoundProbeDefault = mitk::USProbe::New();
117 ultrasoundProbeDefault->SetName("default");
118 ultrasoundProbeDefault->SetDepth(0);
119 m_DeviceConfig.probes.push_back(ultrasoundProbeDefault);
120 return true;
121 }
122
123 //Extract all saved and configured probes of the USDevice:
124 for (auto* probeTag = probesTag->FirstChildElement(USDeviceReaderWriterConstants::TAG_PROBE);
125 probeTag != nullptr; probeTag = probeTag->NextSiblingElement())
126 {
127 this->ExtractProbe(probeTag);
128 }
129 return true;
130}
131
132void mitk::USDeviceReaderXML::SetFilename(std::string filename)
133{
134 m_Filename = filename;
135}
136
138{
139 ultrasoundTag->QueryDoubleAttribute(USDeviceReaderWriterConstants::ATTR_FILEVERS, &m_DeviceConfig.fileversion);
140 ultrasoundTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_IMAGESTREAMS, &m_DeviceConfig.numberOfImageStreams);
141 ultrasoundTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_PORT, &m_DeviceConfig.port);
142 ultrasoundTag->QueryBoolAttribute(USDeviceReaderWriterConstants::ATTR_SERVER, &m_DeviceConfig.server);
143
144 m_DeviceConfig.deviceType = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_TYPE);
145 m_DeviceConfig.deviceName = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_TYPE);
146 m_DeviceConfig.manufacturer = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_TYPE);
147 m_DeviceConfig.model = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_TYPE);
148 m_DeviceConfig.comment = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_TYPE);
149 m_DeviceConfig.host = ReadStringAttribute(ultrasoundTag, USDeviceReaderWriterConstants::ATTR_HOST);
150}
151
152void mitk::USDeviceReaderXML::ExtractAttributeInformationOfGeneralSettingsTag(const tinyxml2::XMLElement *generalSettingsTag)
153{
154 generalSettingsTag->QueryBoolAttribute(USDeviceReaderWriterConstants::ATTR_GREYSCALE, &m_DeviceConfig.useGreyscale);
155 generalSettingsTag->QueryBoolAttribute(USDeviceReaderWriterConstants::ATTR_RESOLUTIONOVERRIDE, &m_DeviceConfig.useResolutionOverride);
156 generalSettingsTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_RESOLUTIONHEIGHT, &m_DeviceConfig.resolutionHeight);
157 generalSettingsTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_RESOLUTIONWIDTH, &m_DeviceConfig.resolutionWidth);
158 generalSettingsTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_SOURCEID, &m_DeviceConfig.sourceID);
159 generalSettingsTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_OPENCVPORT, &m_DeviceConfig.opencvPort);
160
161 m_DeviceConfig.filepathVideoSource = ReadStringAttribute(generalSettingsTag, USDeviceReaderWriterConstants::ATTR_FILEPATH);
162}
163
164void mitk::USDeviceReaderXML::ExtractProbe(const tinyxml2::XMLElement *probeTag)
165{
166 mitk::USProbe::Pointer ultrasoundProbe = mitk::USProbe::New();
167 auto probeName = ReadStringAttribute(probeTag, USDeviceReaderWriterConstants::ATTR_NAME);
168 ultrasoundProbe->SetName(probeName);
169
170 auto* depthsTag = probeTag->FirstChildElement(USDeviceReaderWriterConstants::TAG_DEPTHS);
171 if (depthsTag != nullptr)
172 {
173 for (auto* depthTag = depthsTag->FirstChildElement(USDeviceReaderWriterConstants::TAG_DEPTH);
174 depthTag != nullptr; depthTag = depthTag->NextSiblingElement())
175 {
176 int depth = 0;
177 mitk::Vector3D spacing;
178 spacing[0] = 1;
179 spacing[1] = 1;
180 spacing[2] = 1;
181
182 depthTag->QueryIntAttribute(USDeviceReaderWriterConstants::ATTR_DEPTH, &depth);
183
184 auto* spacingTag = depthTag->FirstChildElement(USDeviceReaderWriterConstants::TAG_SPACING);
185 if (spacingTag != nullptr)
186 {
187 spacingTag->QueryDoubleAttribute(USDeviceReaderWriterConstants::ATTR_X, &spacing[0]);
188 spacingTag->QueryDoubleAttribute(USDeviceReaderWriterConstants::ATTR_Y, &spacing[1]);
189 }
190
191 ultrasoundProbe->SetDepthAndSpacing(depth, spacing);
192 }
193 }
194 else
195 {
196 MITK_ERROR << "Error: DEPTHS-Tag was not found in the file :" << m_Filename
197 << "Therefore, creating default depth [0] and spacing [1,1,1] for the probe.";
198 ultrasoundProbe->SetDepth(0);
199 }
200
201 unsigned int croppingTop = 0;
202 unsigned int croppingBottom = 0;
203 unsigned int croppingLeft = 0;
204 unsigned int croppingRight = 0;
205
206 auto* croppingTag = probeTag->FirstChildElement(USDeviceReaderWriterConstants::TAG_CROPPING);
207 if (croppingTag != nullptr)
208 {
209 croppingTag->QueryUnsignedAttribute(USDeviceReaderWriterConstants::ATTR_TOP, &croppingTop);
210 croppingTag->QueryUnsignedAttribute(USDeviceReaderWriterConstants::ATTR_BOTTOM, &croppingBottom);
211 croppingTag->QueryUnsignedAttribute(USDeviceReaderWriterConstants::ATTR_LEFT, &croppingLeft);
212 croppingTag->QueryUnsignedAttribute(USDeviceReaderWriterConstants::ATTR_RIGHT, &croppingRight);
213 }
214
215 ultrasoundProbe->SetProbeCropping(croppingTop, croppingBottom, croppingLeft, croppingRight);
216 m_DeviceConfig.probes.push_back(ultrasoundProbe);
217}
void ExtractAttributeInformationOfGeneralSettingsTag(const tinyxml2::XMLElement *element)
Extracts all stored attribute information of the GENERALSETTINGS-Tag.
void ExtractAttributeInformationOfUltrasoundDeviceTag(const tinyxml2::XMLElement *element)
Extracts all stored attribute information of the ULTRASOUNDDEVICE-Tag.
void SetFilename(std::string filename)
std::vector< itk::SmartPointer< BaseData > > DoRead() override
USDeviceConfigData & GetUSDeviceConfigData()
void ExtractProbe(const tinyxml2::XMLElement *element)
Extracts all stored information of a single ultrasound probe.
mitk::USDeviceReaderXML * Clone() const override
IGT Exceptions.