Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPointSetReader.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 "mitkPointSetReader.h"
18 #include <fstream>
19 #include <iostream>
20 #include <mitkLocaleSwitch.h>
21 
23 {
24  m_Success = false;
25 }
26 
28 {
29 }
30 
32 {
33  // Switch the current locale to "C"
34  LocaleSwitch localeSwitch("C");
35 
36  m_Success = false;
37  if (m_FileName == "")
38  {
39  itkWarningMacro(<< "Sorry, filename has not been set!");
40  return;
41  }
42  if (!this->CanReadFile(m_FileName.c_str()))
43  {
44  itkWarningMacro(<< "Sorry, can't read file " << m_FileName << "!");
45  return;
46  }
47 
48  try
49  {
50  TiXmlDocument doc(m_FileName.c_str());
51  bool loadOkay = doc.LoadFile();
52  if (loadOkay)
53  {
54  TiXmlHandle docHandle(&doc);
55  unsigned int pointSetCounter(0);
56  for (TiXmlElement *currentPointSetElement =
57  docHandle.FirstChildElement("point_set_file").FirstChildElement("point_set").ToElement();
58  currentPointSetElement != nullptr;
59  currentPointSetElement = currentPointSetElement->NextSiblingElement())
60  {
62  if (currentPointSetElement->FirstChildElement("time_series") != nullptr)
63  {
64  for (TiXmlElement *currentTimeSeries = currentPointSetElement->FirstChildElement("time_series")->ToElement();
65  currentTimeSeries != nullptr;
66  currentTimeSeries = currentTimeSeries->NextSiblingElement())
67  {
68  unsigned int currentTimeStep(0);
69  TiXmlElement *currentTimeSeriesID = currentTimeSeries->FirstChildElement("time_series_id");
70 
71  currentTimeStep = atoi(currentTimeSeriesID->GetText());
72 
73  newPointSet = this->ReadPoint(newPointSet, currentTimeSeries, currentTimeStep);
74  }
75  }
76  else
77  {
78  newPointSet = this->ReadPoint(newPointSet, currentPointSetElement, 0);
79  }
80  this->SetNthOutput(pointSetCounter, newPointSet);
81  pointSetCounter++;
82  }
83  }
84  else
85  {
86  MITK_WARN << "XML parser error!";
87  }
88  }
89  catch (...)
90  {
91  MITK_ERROR << "Cannot read point set.";
92  m_Success = false;
93  }
94  m_Success = true;
95 }
96 
98  TiXmlElement *currentTimeSeries,
99  unsigned int currentTimeStep)
100 {
101  if (currentTimeSeries->FirstChildElement("point") != nullptr)
102  {
103  for (TiXmlElement *currentPoint = currentTimeSeries->FirstChildElement("point")->ToElement();
104  currentPoint != nullptr;
105  currentPoint = currentPoint->NextSiblingElement())
106  {
107  unsigned int id(0);
109  double x(0.0);
110  double y(0.0);
111  double z(0.0);
112 
113  id = atoi(currentPoint->FirstChildElement("id")->GetText());
114  if (currentPoint->FirstChildElement("specification") != nullptr)
115  {
116  spec = (mitk::PointSpecificationType)atoi(currentPoint->FirstChildElement("specification")->GetText());
117  }
118  x = atof(currentPoint->FirstChildElement("x")->GetText());
119  y = atof(currentPoint->FirstChildElement("y")->GetText());
120  z = atof(currentPoint->FirstChildElement("z")->GetText());
121 
122  mitk::Point3D point;
123  mitk::FillVector3D(point, x, y, z);
124  newPointSet->SetPoint(id, point, spec, currentTimeStep);
125  }
126  }
127  else
128  {
129  if (currentTimeStep != newPointSet->GetTimeSteps() + 1)
130  {
131  newPointSet->Expand(currentTimeStep + 1); // expand time step series with empty time step
132  }
133  }
134  return newPointSet;
135 }
136 
138 {
139 }
140 
142 {
143  std::ifstream in(name);
144  bool isGood = in.good();
145  in.close();
146  return isGood;
147 }
148 
150  const std::string filePrefix,
151  const std::string filePattern)
152 {
153  // First check the extension
154  if (filename == "")
155  {
156  // MITK_INFO<<"No filename specified."<<std::endl;
157  return false;
158  }
159 
160  // check if image is serie
161  if (filePattern != "" && filePrefix != "")
162  return false;
163 
164  bool extensionFound = false;
165  std::string::size_type MPSPos = filename.rfind(".mps");
166  if ((MPSPos != std::string::npos) && (MPSPos == filename.length() - 4))
167  {
168  extensionFound = true;
169  }
170 
171  MPSPos = filename.rfind(".MPS");
172  if ((MPSPos != std::string::npos) && (MPSPos == filename.length() - 4))
173  {
174  extensionFound = true;
175  }
176 
177  if (!extensionFound)
178  {
179  // MITK_INFO<<"The filename extension is not recognized."<<std::endl;
180  return false;
181  }
182 
183  return true;
184 }
185 
186 void mitk::PointSetReader::ResizeOutputs(const unsigned int &num)
187 {
188  unsigned int prevNum = this->GetNumberOfOutputs();
189  this->SetNumberOfIndexedOutputs(num);
190  for (unsigned int i = prevNum; i < num; ++i)
191  {
192  this->SetNthOutput(i, this->MakeOutput(i).GetPointer());
193  }
194 }
195 
197 {
198  return m_Success;
199 }
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:110
virtual void GenerateData() override
virtual void GenerateOutputInformation() override
#define MITK_WARN
Definition: mitkLogMacros.h:23
Convenience class to temporarily change the current locale.
static const std::string filename
virtual void ResizeOutputs(const unsigned int &num)
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
virtual mitk::PointSet::Pointer ReadPoint(mitk::PointSet::Pointer newPointSet, TiXmlElement *currentTimeSeries, unsigned int currentTimeStep)
PointSpecificationType
enumeration of the type a point can be
Definition: mitkPoint.h:30