Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkGeometryDataReaderService.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 
17 // MITK
19 #include "mitkGeometry3DToXML.h"
20 #include "mitkIOMimeTypes.h"
22 
23 // STL
24 #include <mitkLocaleSwitch.h>
25 
26 #include <tinyxml.h>
27 
29  : AbstractFileReader(IOMimeTypes::GEOMETRY_DATA_MIMETYPE(), "MITK Geometry Data Reader")
30 {
32 }
33 
35 {
36 }
37 
38 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::GeometryDataReaderService::Read()
39 {
40  // Switch the current locale to "C"
41  LocaleSwitch localeSwitch("C");
42 
43  std::vector<itk::SmartPointer<BaseData>> result;
44 
45  InputStream stream(this);
46 
47  TiXmlDocument doc;
48  stream >> doc;
49  if (!doc.Error())
50  {
51  TiXmlHandle docHandle(&doc);
52 
53  for (TiXmlElement *geomDataElement = docHandle.FirstChildElement("GeometryData").ToElement();
54  geomDataElement != nullptr;
55  geomDataElement = geomDataElement->NextSiblingElement())
56  {
57  for (TiXmlElement *currentElement = geomDataElement->FirstChildElement(); currentElement != nullptr;
58  currentElement = currentElement->NextSiblingElement())
59  {
60  // different geometries could have been serialized from a GeometryData
61  // object:
62  std::string tagName = currentElement->Value();
63  if (tagName == "Geometry3D")
64  {
65  Geometry3D::Pointer restoredGeometry = Geometry3DToXML::FromXML(currentElement);
66  if (restoredGeometry.IsNotNull())
67  {
68  GeometryData::Pointer newGeometryData = GeometryData::New();
69  newGeometryData->SetGeometry(restoredGeometry);
70  result.push_back(newGeometryData.GetPointer());
71  }
72  else
73  {
74  MITK_ERROR << "Invalid <Geometry3D> tag encountered. Skipping.";
75  }
76  }
77  else if (tagName == "ProportionalTimeGeometry")
78  {
79  ProportionalTimeGeometry::Pointer restoredTimeGeometry =
81  if (restoredTimeGeometry.IsNotNull())
82  {
83  GeometryData::Pointer newGeometryData = GeometryData::New();
84  newGeometryData->SetTimeGeometry(restoredTimeGeometry);
85  result.push_back(newGeometryData.GetPointer());
86  }
87  else
88  {
89  MITK_ERROR << "Invalid <ProportionalTimeGeometry> tag encountered. Skipping.";
90  }
91  }
92  } // for child of <GeometryData>
93  } // for <GeometryData>
94  }
95  else
96  {
97  mitkThrow() << "Parsing error at line " << doc.ErrorRow() << ", col " << doc.ErrorCol() << ": " << doc.ErrorDesc();
98  }
99 
100  if (result.empty())
101  {
102  mitkThrow() << "Did not read a single GeometryData object from input.";
103  }
104 
105  return result;
106 }
107 
109  : mitk::AbstractFileReader(other)
110 {
111 }
112 
113 mitk::GeometryDataReaderService *mitk::GeometryDataReaderService::Clone() const
114 {
115  return new GeometryDataReaderService(*this);
116 }
The IOMimeTypes class.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Geometry3D::Pointer FromXML(TiXmlElement *node)
Create a Geometry3D from XML. Interprets only the format created by ToXML().
DataCollection - Class to facilitate loading/accessing structured data.
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Convenience class to temporarily change the current locale.
#define mitkThrow()
static Pointer New()
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
Base class for creating mitk::BaseData objects from files or streams.
static ProportionalTimeGeometry::Pointer FromXML(TiXmlElement *node)
Create a ProportionalTimeGeometry from XML. Interprets only the format created by ToXML()...