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