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