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