Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPointSetWriter.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 "mitkPointSetWriter.h"
18 #include <fstream>
19 #include <iostream>
20 #include <locale>
21 
22 //
23 // Initialization of the xml tags.
24 //
25 
26 const char *mitk::PointSetWriter::XML_POINT_SET_FILE = "point_set_file";
27 
28 const char *mitk::PointSetWriter::XML_FILE_VERSION = "file_version";
29 
30 const char *mitk::PointSetWriter::XML_POINT_SET = "point_set";
31 
32 const char *mitk::PointSetWriter::XML_TIME_SERIES = "time_series";
33 
34 const char *mitk::PointSetWriter::XML_TIME_SERIES_ID = "time_series_id";
35 
36 const char *mitk::PointSetWriter::XML_POINT = "point";
37 
38 const char *mitk::PointSetWriter::XML_ID = "id";
39 
40 const char *mitk::PointSetWriter::XML_SPEC = "specification";
41 
42 const char *mitk::PointSetWriter::XML_X = "x";
43 
44 const char *mitk::PointSetWriter::XML_Y = "y";
45 
46 const char *mitk::PointSetWriter::XML_Z = "z";
47 
48 const char *mitk::PointSetWriter::VERSION_STRING = "0.1";
49 
50 mitk::PointSetWriter::PointSetWriter() : m_FileName(""), m_FilePrefix(""), m_FilePattern("")
51 {
52  this->SetNumberOfRequiredInputs(1);
53  this->SetNumberOfIndexedOutputs(1);
54  this->SetNthOutput(0, mitk::PointSet::New().GetPointer());
55  m_Indent = 2;
56  m_IndentDepth = 0;
57  m_Success = false;
58 }
59 
61 {
62 }
63 
65 {
66  m_Success = false;
67  m_IndentDepth = 0;
68 
69  //
70  // Opening the file to write to
71  //
72  if (m_FileName == "")
73  {
74  itkWarningMacro(<< "Sorry, filename has not been set!");
75  return;
76  }
77  std::ofstream out(m_FileName.c_str());
78  if (!out.good())
79  {
80  itkExceptionMacro(<< "File " << m_FileName << " could not be opened!");
81  itkWarningMacro(<< "Sorry, file " << m_FileName << " could not be opened!");
82  out.close();
83  return;
84  }
85 
86  std::locale previousLocale(out.getloc());
87  std::locale I("C");
88  out.imbue(I);
89 
90  //
91  // Here the actual xml writing begins
92  //
93  WriteXMLHeader(out);
94  WriteStartElement(XML_POINT_SET_FILE, out);
95  WriteStartElement(XML_FILE_VERSION, out);
96  WriteCharacterData(VERSION_STRING, out);
97  WriteEndElement(XML_FILE_VERSION, out, false);
98 
99  //
100  // for each input object write its xml representation to
101  // the stream
102  //
103  for (unsigned int i = 0; i < this->GetNumberOfInputs(); ++i)
104  {
105  InputType::Pointer pointSet = this->GetInput(i);
106  assert(pointSet.IsNotNull());
107  WriteXML(pointSet.GetPointer(), out);
108  }
109 
110  WriteEndElement(XML_POINT_SET_FILE, out);
111  out.imbue(previousLocale);
112  if (!out.good()) // some error during output
113  {
114  out.close();
115  throw std::ios_base::failure("Some error during point set writing.");
116  }
117 
118  out.close();
119  m_Success = true;
120  m_MimeType = "application/MITK.PointSet";
121 }
122 
123 void mitk::PointSetWriter::WriteXML(mitk::PointSet *pointSet, std::ofstream &out)
124 {
125  WriteStartElement(XML_POINT_SET, out);
126  unsigned int timecount = pointSet->GetTimeSteps();
127 
128  for (unsigned int i = 0; i < timecount; i++)
129  {
130  WriteStartElement(XML_TIME_SERIES, out);
131 
132  WriteStartElement(XML_TIME_SERIES_ID, out);
133  WriteCharacterData(ConvertToString(i).c_str(), out);
134  WriteEndElement(XML_TIME_SERIES_ID, out, false);
135 
136  mitk::PointSet::PointsContainer *pointsContainer = pointSet->GetPointSet(i)->GetPoints();
137  mitk::PointSet::PointsContainer::Iterator it;
138 
139  for (it = pointsContainer->Begin(); it != pointsContainer->End(); ++it)
140  {
141  WriteStartElement(XML_POINT, out);
142 
143  WriteStartElement(XML_ID, out);
144  WriteCharacterData(ConvertToString(it->Index()).c_str(), out);
145  WriteEndElement(XML_ID, out, false);
146 
147  mitk::PointSet::PointType point = it->Value();
148 
149  WriteStartElement(XML_SPEC, out);
150  WriteCharacterData(ConvertToString(pointSet->GetSpecificationTypeInfo(it->Index(), i)).c_str(), out);
151  WriteEndElement(XML_SPEC, out, false);
152 
153  WriteStartElement(XML_X, out);
154  WriteCharacterData(ConvertToString(point[0]).c_str(), out);
155  WriteEndElement(XML_X, out, false);
156 
157  WriteStartElement(XML_Y, out);
158  WriteCharacterData(ConvertToString(point[1]).c_str(), out);
159  WriteEndElement(XML_Y, out, false);
160 
161  WriteStartElement(XML_Z, out);
162  WriteCharacterData(ConvertToString(point[2]).c_str(), out);
163  WriteEndElement(XML_Z, out, false);
164 
165  WriteEndElement(XML_POINT, out);
166  }
167  WriteEndElement(XML_TIME_SERIES, out);
168  }
169 
170  WriteEndElement(XML_POINT_SET, out);
171 }
172 
173 void mitk::PointSetWriter::ResizeInputs(const unsigned int &num)
174 {
175  unsigned int prevNum = this->GetNumberOfInputs();
176  this->SetNumberOfIndexedInputs(num);
177  for (unsigned int i = prevNum; i < num; ++i)
178  {
179  this->SetNthInput(i, mitk::PointSet::New().GetPointer());
180  }
181 }
182 
184 {
185  this->ProcessObject::SetNthInput(0, pointSet);
186 }
187 
188 void mitk::PointSetWriter::SetInput(const unsigned int &id, InputType *pointSet)
189 {
190  if (id >= this->GetNumberOfInputs())
191  this->ResizeInputs(id + 1);
192  this->ProcessObject::SetNthInput(id, pointSet);
193 }
194 
196 {
197  if (this->GetNumberOfInputs() < 1)
198  {
199  return nullptr;
200  }
201  else
202  {
203  return dynamic_cast<InputType *>(this->GetInput(0));
204  }
205 }
206 
208 {
209  return dynamic_cast<InputType *>(this->ProcessObject::GetInput(num));
210 }
211 
212 template <typename T>
214 {
215  std::ostringstream o;
216  std::locale I("C");
217  o.imbue(I);
218 
219  if (o << value)
220  {
221  return o.str();
222  }
223  else
224  return "conversion error";
225 }
226 
227 void mitk::PointSetWriter::WriteXMLHeader(std::ofstream &file)
228 {
229  file << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
230 }
231 
232 void mitk::PointSetWriter::WriteStartElement(const char *const tag, std::ofstream &file)
233 {
234  file << std::endl;
235  WriteIndent(file);
236  file << '<' << tag << '>';
237  m_IndentDepth++;
238 }
239 
240 void mitk::PointSetWriter::WriteEndElement(const char *const tag, std::ofstream &file, const bool &indent)
241 {
242  m_IndentDepth--;
243  if (indent)
244  {
245  file << std::endl;
246  WriteIndent(file);
247  }
248  file << '<' << '/' << tag << '>';
249 }
250 
251 void mitk::PointSetWriter::WriteCharacterData(const char *const data, std::ofstream &file)
252 {
253  file << data;
254 }
255 
256 void mitk::PointSetWriter::WriteStartElement(std::string &tag, std::ofstream &file)
257 {
258  WriteStartElement(tag.c_str(), file);
259 }
260 
261 void mitk::PointSetWriter::WriteEndElement(std::string &tag, std::ofstream &file, const bool &indent)
262 {
263  WriteEndElement(tag.c_str(), file, indent);
264 }
265 
266 void mitk::PointSetWriter::WriteCharacterData(std::string &data, std::ofstream &file)
267 {
268  WriteCharacterData(data.c_str(), file);
269 }
270 
271 void mitk::PointSetWriter::WriteIndent(std::ofstream &file)
272 {
273  std::string spaces(m_IndentDepth * m_Indent, ' ');
274  file << spaces.c_str();
275 }
276 
277 bool mitk::PointSetWriter::GetSuccess() const
278 {
279  return m_Success;
280 }
281 
282 bool mitk::PointSetWriter::CanWriteDataType(DataNode *input)
283 {
284  if (input)
285  {
286  mitk::BaseData *data = input->GetData();
287  if (data)
288  {
289  mitk::PointSet::Pointer pointSet = dynamic_cast<mitk::PointSet *>(data);
290  if (pointSet.IsNotNull())
291  {
292  // this writer has no "SetDefaultExtension()" - function
293  m_Extension = ".mps";
294  return true;
295  }
296  }
297  }
298  return false;
299 }
300 
301 void mitk::PointSetWriter::SetInput(DataNode *input)
302 {
303  if (input && CanWriteDataType(input))
304  this->ProcessObject::SetNthInput(0, dynamic_cast<mitk::PointSet *>(input->GetData()));
305 }
306 
307 std::string mitk::PointSetWriter::GetWritenMIMEType()
308 {
309  return m_MimeType;
310 }
311 
312 std::vector<std::string> mitk::PointSetWriter::GetPossibleFileExtensions()
313 {
314  std::vector<std::string> possibleFileExtensions;
315  possibleFileExtensions.push_back(".mps");
316  return possibleFileExtensions;
317 }
318 
319 std::string mitk::PointSetWriter::GetSupportedBaseData() const
320 {
321  return PointSet::GetStaticNameOfClass();
322 }
323 
324 std::string mitk::PointSetWriter::GetFileExtension()
325 {
326  return m_Extension;
327 }
void SetInput(InputType *input)
static const char * XML_POINT
static Pointer New()
virtual PointSpecificationType GetSpecificationTypeInfo(int position, int t) const
to get the type of the point at the position and the moment
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
Definition: mitkBaseData.h:346
std::string ConvertToString(T value)
static const char * XML_POINT_SET_FILE
static const char * XML_ID
static const char * XML_POINT_SET
static const char * XML_TIME_SERIES
void WriteStartElement(const char *const tag, std::ofstream &file)
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:79
virtual DataType::Pointer GetPointSet(int t=0) const
returns the pointset
void WriteXML(mitk::PointSet *pointSet, std::ofstream &out)
DataType::PointsContainer PointsContainer
Definition: mitkPointSet.h:136
static const char * XML_TIME_SERIES_ID
static const char * XML_Y
void WriteXMLHeader(std::ofstream &file)
static const char * XML_FILE_VERSION
static const char * XML_Z
static const char * XML_SPEC
static const char * VERSION_STRING
static const char * XML_X
virtual void ResizeInputs(const unsigned int &num)
virtual void GenerateData() override