Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkUSDeviceReaderXML.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 // MITK
15 #include "mitkUSDeviceReaderXML.h"
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 <tinyxml.h>
25 
27  mitk::IGTMimeTypes::USDEVICEINFORMATIONXML_MIMETYPE(),
28  "MITK USDevice Reader (XML)"), m_Filename("")
29 {
31 }
32 
34 {
35 }
36 
38 {
39  return m_DeviceConfig;
40 }
41 
43 {
44 }
45 
47 {
48  return new USDeviceReaderXML(*this);
49 }
50 
51 
52 
53 
54 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::USDeviceReaderXML::Read()
55 {
56  MITK_WARN << "This method is not implemented. \
57  Please use the method ReadUltrasoundDeviceConfiguration() instead.";
58  std::vector<mitk::BaseData::Pointer> result;
59  return result;
60 }
61 
63 {
64  MITK_INFO << "Try to start reading xml device configuration...";
65  if (m_Filename == "")
66  {
67  MITK_WARN << "Cannot read file - empty filename!";
68  return false;
69  }
70 
71  TiXmlDocument document(m_Filename);
72  if (!document.LoadFile())
73  {
74  MITK_ERROR << "Error when opening and reading file :" << m_Filename;
75  return false;
76  }
77 
78  TiXmlHandle documentHandle(&document);
79  TiXmlElement* ultrasoundDeviceTag = documentHandle.FirstChildElement(TAG_ULTRASOUNDDEVICE).ToElement();
80  if (ultrasoundDeviceTag == nullptr)
81  {
82  MITK_ERROR << "Error parsing the file :" << m_Filename << std::endl << "Wrong xml format structure.";
83  return false;
84  }
85 
86  //Extract attribute information of the ULTRASOUNDDEVICE-Tag:
87  this->ExtractAttributeInformationOfUltrasoundDeviceTag(ultrasoundDeviceTag);
88 
89  TiXmlElement* generalSettingsTag = documentHandle.FirstChildElement(TAG_ULTRASOUNDDEVICE).FirstChildElement(TAG_GENERALSETTINGS).ToElement();
90  if (generalSettingsTag == nullptr)
91  {
92  MITK_ERROR << "Error parsing the GENERALSETTINGS-Tag in the file :" << m_Filename;
93  return false;
94  }
95 
96  //Extract attribute information of the GENERALSETTINGS-Tag:
97  this->ExtractAttributeInformationOfGeneralSettingsTag(generalSettingsTag);
98 
99  TiXmlElement* probesTag = documentHandle.FirstChildElement(TAG_ULTRASOUNDDEVICE).FirstChildElement(TAG_PROBES).ToElement();
100  if (probesTag == nullptr)
101  {
102  MITK_ERROR << "Error: PROBES-Tag was not found in the file :" << m_Filename << "Therefore, creating default probe.";
103  //Create default ultrasound probe:
104  mitk::USProbe::Pointer ultrasoundProbeDefault = mitk::USProbe::New();
105  ultrasoundProbeDefault->SetName("default");
106  ultrasoundProbeDefault->SetDepth(0);
107  m_DeviceConfig.probes.push_back(ultrasoundProbeDefault);
108  return true;
109  }
110 
111  //Extract all saved and configured probes of the USDevice:
112  for (TiXmlElement* probeTag = probesTag->FirstChildElement(TAG_PROBE);
113  probeTag != nullptr; probeTag = probeTag->NextSiblingElement())
114  {
115  this->ExtractProbe(probeTag);
116  }
117  return true;
118 }
119 
120 void mitk::USDeviceReaderXML::SetFilename(std::string filename)
121 {
122  m_Filename = filename;
123 }
124 
126 {
127  ultrasoundTag->QueryDoubleAttribute(ATTR_FILEVERS, &m_DeviceConfig.fileversion);
128  ultrasoundTag->QueryStringAttribute(ATTR_TYPE, &m_DeviceConfig.deviceType);
129  ultrasoundTag->QueryStringAttribute(ATTR_NAME, &m_DeviceConfig.deviceName);
130  ultrasoundTag->QueryStringAttribute(ATTR_MANUFACTURER, &m_DeviceConfig.manufacturer);
131  ultrasoundTag->QueryStringAttribute(ATTR_MODEL, &m_DeviceConfig.model);
132  ultrasoundTag->QueryStringAttribute(ATTR_COMMENT, &m_DeviceConfig.comment);
133  ultrasoundTag->QueryIntAttribute(ATTR_IMAGESTREAMS, &m_DeviceConfig.numberOfImageStreams);
134  ultrasoundTag->QueryStringAttribute(ATTR_HOST, &m_DeviceConfig.host);
135  ultrasoundTag->QueryIntAttribute(ATTR_PORT, &m_DeviceConfig.port);
136  ultrasoundTag->QueryBoolAttribute(ATTR_SERVER, &m_DeviceConfig.server);
137 }
138 
140 {
141  generalSettingsTag->QueryBoolAttribute(ATTR_GREYSCALE, &m_DeviceConfig.useGreyscale);
142  generalSettingsTag->QueryBoolAttribute(ATTR_RESOLUTIONOVERRIDE, &m_DeviceConfig.useResolutionOverride);
143  generalSettingsTag->QueryIntAttribute(ATTR_RESOLUTIONHEIGHT, &m_DeviceConfig.resolutionHeight);
144  generalSettingsTag->QueryIntAttribute(ATTR_RESOLUTIONWIDTH, &m_DeviceConfig.resolutionWidth);
145  generalSettingsTag->QueryIntAttribute(ATTR_SOURCEID, &m_DeviceConfig.sourceID);
146  generalSettingsTag->QueryStringAttribute(ATTR_FILEPATH, &m_DeviceConfig.filepathVideoSource);
147  generalSettingsTag->QueryIntAttribute(ATTR_OPENCVPORT, &m_DeviceConfig.opencvPort);
148 }
149 
150 void mitk::USDeviceReaderXML::ExtractProbe(TiXmlElement *probeTag)
151 {
152  mitk::USProbe::Pointer ultrasoundProbe = mitk::USProbe::New();
153  std::string probeName;
154  probeTag->QueryStringAttribute(ATTR_NAME, &probeName);
155  ultrasoundProbe->SetName(probeName);
156 
157  TiXmlElement* depthsTag = probeTag->FirstChildElement(TAG_DEPTHS);
158  if (depthsTag != nullptr)
159  {
160  for (TiXmlElement* depthTag = depthsTag->FirstChildElement(TAG_DEPTH);
161  depthTag != nullptr; depthTag = depthTag->NextSiblingElement())
162  {
163  int depth = 0;
164  mitk::Vector3D spacing;
165  spacing[0] = 1;
166  spacing[1] = 1;
167  spacing[2] = 1;
168 
169  depthTag->QueryIntAttribute(ATTR_DEPTH, &depth);
170 
171  TiXmlElement* spacingTag = depthTag->FirstChildElement(TAG_SPACING);
172  if (spacingTag != nullptr)
173  {
174  spacingTag->QueryDoubleAttribute(ATTR_X, &spacing[0]);
175  spacingTag->QueryDoubleAttribute(ATTR_Y, &spacing[1]);
176  }
177 
178  ultrasoundProbe->SetDepthAndSpacing(depth, spacing);
179  }
180  }
181  else
182  {
183  MITK_ERROR << "Error: DEPTHS-Tag was not found in the file :" << m_Filename
184  << "Therefore, creating default depth [0] and spacing [1,1,1] for the probe.";
185  ultrasoundProbe->SetDepth(0);
186  }
187 
188  unsigned int croppingTop = 0;
189  unsigned int croppingBottom = 0;
190  unsigned int croppingLeft = 0;
191  unsigned int croppingRight = 0;
192 
193  TiXmlElement* croppingTag = probeTag->FirstChildElement(TAG_CROPPING);
194  if (croppingTag != nullptr)
195  {
196  croppingTag->QueryUnsignedAttribute(ATTR_TOP, &croppingTop);
197  croppingTag->QueryUnsignedAttribute(ATTR_BOTTOM, &croppingBottom);
198  croppingTag->QueryUnsignedAttribute(ATTR_LEFT, &croppingLeft);
199  croppingTag->QueryUnsignedAttribute(ATTR_RIGHT, &croppingRight);
200  }
201 
202  ultrasoundProbe->SetProbeCropping(croppingTop, croppingBottom, croppingLeft, croppingRight);
203  m_DeviceConfig.probes.push_back(ultrasoundProbe);
204 }
static const char * TAG_CROPPING
static const char * TAG_DEPTH
#define MITK_INFO
Definition: mitkLogMacros.h:18
static const char * ATTR_OPENCVPORT
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static const char * ATTR_SERVER
mitk::USDeviceReaderXML * Clone() const override
static const char * TAG_PROBE
USDeviceConfigData & GetUSDeviceConfigData()
static const char * ATTR_RIGHT
DataCollection - Class to facilitate loading/accessing structured data.
static const char * ATTR_GREYSCALE
static const char * ATTR_RESOLUTIONWIDTH
std::vector< mitk::USProbe::Pointer > probes
static const char * ATTR_TOP
static const char * ATTR_DEPTH
static const char * ATTR_Y
std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define MITK_WARN
Definition: mitkLogMacros.h:19
void ExtractAttributeInformationOfGeneralSettingsTag(TiXmlElement *element)
Extracts all stored attribute information of the GENERALSETTINGS-Tag.
static const char * ATTR_LEFT
static const char * ATTR_COMMENT
void ExtractProbe(TiXmlElement *element)
Extracts all stored information of a single ultrasound probe.
static const char * ATTR_X
static const char * ATTR_TYPE
void SetFilename(std::string filename)
static const char * ATTR_IMAGESTREAMS
static const char * ATTR_PORT
void ExtractAttributeInformationOfUltrasoundDeviceTag(TiXmlElement *element)
Extracts all stored attribute information of the ULTRASOUNDDEVICE-Tag.
static Pointer New()
static const char * ATTR_FILEVERS
Base class for creating mitk::BaseData objects from files or streams.
static const char * ATTR_FILEPATH
static const char * ATTR_RESOLUTIONHEIGHT
static const char * ATTR_MODEL
static const char * ATTR_BOTTOM
static const char * TAG_ULTRASOUNDDEVICE
static const char * TAG_SPACING
static const char * ATTR_NAME
static const char * ATTR_HOST
static const char * TAG_GENERALSETTINGS
static const char * ATTR_RESOLUTIONOVERRIDE
static const char * ATTR_MANUFACTURER
static const char * TAG_PROBES
static const char * TAG_DEPTHS
static const char * ATTR_SOURCEID