Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkContourModelReader.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 #include "mitkContourModelReader.h"
14 #include <fstream>
15 #include <iostream>
16 #include <mitkCustomMimeType.h>
17 #include <mitkLocaleSwitch.h>
18 
20 {
21 }
22 
24 {
25  std::string category = "Contour File";
26  mitk::CustomMimeType customMimeType;
27  customMimeType.SetCategory(category);
28  customMimeType.AddExtension("cnt");
29 
30  this->SetDescription(category);
31  this->SetMimeType(customMimeType);
32 
33  m_ServiceReg = this->RegisterService();
34 }
35 
37 {
38 }
39 
40 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::ContourModelReader::Read()
41 {
42  std::vector<itk::SmartPointer<mitk::BaseData>> result;
43  std::string location = GetInputLocation();
44 
45  // Switch the current locale to "C"
46  LocaleSwitch localeSwitch("C");
47 
48  try
49  {
50  TiXmlDocument doc(location.c_str());
51  bool loadOkay = doc.LoadFile();
52  if (loadOkay)
53  {
54  TiXmlHandle docHandle(&doc);
55 
56  /*++++ handle n contourModels within data tags ++++*/
57  for (TiXmlElement *currentContourElement = docHandle.FirstChildElement("contourModel").ToElement();
58  currentContourElement != nullptr;
59  currentContourElement = currentContourElement->NextSiblingElement())
60  {
62  if (currentContourElement->FirstChildElement("data")->FirstChildElement("timestep") != nullptr)
63  {
64  // handle geometry information
65  // TiXmlElement* currentGeometryInfo =
66  // currentContourElement->FirstChildElement("head")->FirstChildElement("geometryInformation")->ToElement();
68 
69  /*++++ handle n timesteps within timestep tags ++++*/
70  for (TiXmlElement *currentTimeSeries =
71  currentContourElement->FirstChildElement("data")->FirstChildElement("timestep")->ToElement();
72  currentTimeSeries != nullptr;
73  currentTimeSeries = currentTimeSeries->NextSiblingElement())
74  {
75  unsigned int currentTimeStep(0);
76 
77  currentTimeStep = atoi(currentTimeSeries->Attribute("n"));
78 
79  this->ReadPoints(newContourModel, currentTimeSeries, currentTimeStep);
80 
81  int isClosed;
82  currentTimeSeries->QueryIntAttribute("isClosed", &isClosed);
83  if (isClosed)
84  {
85  newContourModel->Close(currentTimeStep);
86  }
87  }
88  /*++++ END handle n timesteps within timestep tags ++++*/
89  }
90  else
91  {
92  // this should not happen
93  MITK_WARN << "wrong file format!";
94  // newContourModel = this->ReadPoint(newContourModel, currentContourElement, 0);
95  }
96  newContourModel->UpdateOutputInformation();
97  result.push_back(dynamic_cast<mitk::BaseData *>(newContourModel.GetPointer()));
98  }
99  /*++++ END handle n contourModels within data tags ++++*/
100  }
101  else
102  {
103  MITK_WARN << "XML parser error!";
104  }
105  }
106  catch (...)
107  {
108  MITK_ERROR << "Cannot read contourModel.";
109  }
110 
111  return result;
112 }
113 
114 mitk::ContourModelReader *mitk::ContourModelReader::Clone() const
115 {
116  return new ContourModelReader(*this);
117 }
118 
120  TiXmlElement *currentTimeSeries,
121  unsigned int currentTimeStep)
122 {
123  // check if the timesteps in contourModel have to be expanded
124  if (currentTimeStep != newContourModel->GetTimeSteps())
125  {
126  newContourModel->Expand(currentTimeStep + 1);
127  }
128 
129  // read all points within controlPoints tag
130  if (currentTimeSeries->FirstChildElement("controlPoints")->FirstChildElement("point") != nullptr)
131  {
132  for (TiXmlElement *currentPoint =
133  currentTimeSeries->FirstChildElement("controlPoints")->FirstChildElement("point")->ToElement();
134  currentPoint != nullptr;
135  currentPoint = currentPoint->NextSiblingElement())
136  {
137  double x(0.0);
138  double y(0.0);
139  double z(0.0);
140 
141  x = atof(currentPoint->FirstChildElement("x")->GetText());
142  y = atof(currentPoint->FirstChildElement("y")->GetText());
143  z = atof(currentPoint->FirstChildElement("z")->GetText());
144 
145  int isActivePoint;
146  currentPoint->QueryIntAttribute("isActive", &isActivePoint);
147 
148  mitk::Point3D point;
149  mitk::FillVector3D(point, x, y, z);
150  newContourModel->AddVertex(point, isActivePoint, currentTimeStep);
151  }
152  }
153  else
154  {
155  // nothing to read
156  }
157 }
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define MITK_WARN
Definition: mitkLogMacros.h:19
Convenience class to temporarily change the current locale.
virtual void ReadPoints(mitk::ContourModel::Pointer newContourModel, TiXmlElement *currentTimeSeries, unsigned int currentTimeStep)
void SetDescription(const std::string &description)
void AddExtension(const std::string &extension)
void SetCategory(const std::string &category)
Base class for creating mitk::BaseData objects from files or streams.
static Pointer New()
std::string GetInputLocation() const override
Get the current input location.