Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
vtkPointSetXMLParser.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 "vtkPointSetXMLParser.h"
14 #include "mitkInteractionConst.h"
15 #include "mitkOperation.h"
16 #include "mitkPointOperation.h"
17 #include "mitkPointSetWriter.h"
18 #include "vtkObjectFactory.h"
19 
20 namespace mitk
21 {
22  vtkStandardNewMacro(vtkPointSetXMLParser);
23 }
24 
26 {
27 }
28 
30 {
31 }
32 
34 {
35  vtkXMLParser::InitializeParser();
36  std::istream *stream = this->GetStream();
37  if (!stream)
38  {
39  vtkErrorMacro("no stream available in XML file reader");
40  this->ParseError = 1;
41  return 0;
42  }
43  m_PreviousLocale = stream->getloc();
44  std::locale I("C");
45  stream->imbue(I);
46  return 1;
47 }
48 
50 {
51  std::istream *stream = this->GetStream();
52  if (!stream)
53  {
54  vtkErrorMacro("no stream available in XML file reader");
55  this->ParseError = 1;
56  return 0;
57  }
58  stream->imbue(m_PreviousLocale);
59  vtkXMLParser::CleanupParser();
60  return 1;
61 }
62 
63 void mitk::vtkPointSetXMLParser::StartElement(const char *name, const char ** /*atts */)
64 {
65  std::string currentElement = name;
66  //
67  // when a new point set begins in the file, create a new
68  // mitk::point set and store it in m_PointSetList
69  //
70  if (currentElement == mitk::PointSetWriter::XML_POINT_SET)
71  {
72  m_CurrentPointSet = PointSetType::New();
73  }
74  //
75  // when a new point begins, initialize it to zero.
76  //
77  else if (currentElement == mitk::PointSetWriter::XML_POINT)
78  {
79  m_CurrentPoint[0] = 0.0f;
80  m_CurrentPoint[1] = 0.0f;
81  m_CurrentPoint[2] = 0.0f;
82  m_CurId.clear();
83  m_CurXString.clear();
84  m_CurYString.clear();
85  m_CurZString.clear();
86  }
87 
88  //
89  // the current element is pushed on to the stack
90  // to be able to detect some errors in the xml file
91  //
92  m_ParseStack.push(currentElement);
93 }
94 
96 {
97  std::string currentElement = name;
98 
99  //
100  // make sure, that the current end element matches with the
101  // last start tag
102  //
103  if (m_ParseStack.top() != currentElement)
104  {
105  MITK_ERROR << "Top of parse stack ( " << m_ParseStack.top() << " ) is != currentEndElement ( " << currentElement
106  << " )!" << std::endl;
107  }
108  m_ParseStack.pop();
109 
110  //
111  // After a complete point set has been parsed, its
112  // output information is updated and it is inserted into the list
113  // of parsed point sets.
114  //
115  if (currentElement == mitk::PointSetWriter::XML_POINT_SET)
116  {
117  m_CurrentPointSet->UpdateOutputInformation();
118  m_PointSetList.push_back(m_CurrentPointSet);
119  }
120  //
121  // if we have finished parsing a point, insert it to the current
122  // point set.
123  //
124  else if (currentElement == mitk::PointSetWriter::XML_POINT)
125  {
126  m_CurrentPointId = ParsePointIdentifier(m_CurId);
127  m_CurrentPoint[0] = ParseScalarType(m_CurXString);
128  m_CurrentPoint[1] = ParseScalarType(m_CurYString);
129  m_CurrentPoint[2] = ParseScalarType(m_CurZString);
130 
131  mitk::PointOperation popInsert(mitk::OpINSERT, m_CurrentPoint, m_CurrentPointId);
132  mitk::PointOperation popDeactivate(mitk::OpDESELECTPOINT, m_CurrentPoint, m_CurrentPointId);
133  assert(m_CurrentPointSet.IsNotNull());
134  m_CurrentPointSet->ExecuteOperation(&popInsert);
135  m_CurrentPointSet->ExecuteOperation(&popDeactivate);
136  }
137 }
138 
139 void mitk::vtkPointSetXMLParser::CharacterDataHandler(const char *inData, int inLength)
140 {
141  std::string currentElement = m_ParseStack.top();
142  if (currentElement == mitk::PointSetWriter::XML_ID)
143  {
144  m_CurId.append(inData, inLength);
145  }
146  else if (currentElement == mitk::PointSetWriter::XML_X)
147  {
148  m_CurXString.append(inData, inLength);
149  }
150  else if (currentElement == mitk::PointSetWriter::XML_Y)
151  {
152  m_CurYString.append(inData, inLength);
153  }
154  else if (currentElement == mitk::PointSetWriter::XML_Z)
155  {
156  m_CurZString.append(inData, inLength);
157  }
158 }
159 
161 {
162  std::istringstream stm;
163  stm.str(data);
164  ScalarType number;
165  stm >> number;
166  return number;
167 }
168 
170 {
171  std::istringstream stm;
172  stm.str(data);
173  PointIdentifier pointID;
174  stm >> pointID;
175  return pointID;
176 }
177 
179 {
180  return m_PointSetList;
181 }
#define MITK_ERROR
Definition: mitkLogMacros.h:20
double ScalarType
static const char * XML_POINT
virtual PointIdentifier ParsePointIdentifier(const std::string &data)
DataCollection - Class to facilitate loading/accessing structured data.
Constants for most interaction classes, due to the generic StateMachines.
static const char * XML_ID
static const char * XML_POINT_SET
void StartElement(const char *name, const char **atts) override
virtual PointSetList GetParsedPointSets()
virtual mitk::ScalarType ParseScalarType(const std::string &data)
Operation that handles all actions on one Point.
vtkStandardNewMacro(AnatomicalStructureColorPresets)
static const char * XML_Y
PointSetType::DataType::PointIdentifier PointIdentifier
static const char * XML_Z
static const char * XML_X
std::list< PointSetType::Pointer > PointSetList
void CharacterDataHandler(const char *inData, int inLength) override
void EndElement(const char *name) override