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