Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSurfaceVtkXmlIO.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 "mitkSurfaceVtkXmlIO.h"
18 
19 #include "mitkIOMimeTypes.h"
20 #include "mitkSurface.h"
21 
22 #include <vtkErrorCode.h>
23 #include <vtkSmartPointer.h>
24 #include <vtkXMLPolyDataReader.h>
25 #include <vtkXMLPolyDataWriter.h>
26 
27 namespace mitk
28 {
29  class VtkXMLPolyDataReader : public ::vtkXMLPolyDataReader
30  {
31  public:
32  static VtkXMLPolyDataReader *New() { return new VtkXMLPolyDataReader(); }
33  vtkTypeMacro(VtkXMLPolyDataReader, vtkXMLPolyDataReader)
34 
35  void SetStream(std::istream *is)
36  {
37  this->Stream = is;
38  }
39  std::istream *GetStream() const { return this->Stream; }
40  };
41 
42  class VtkXMLPolyDataWriter : public ::vtkXMLPolyDataWriter
43  {
44  public:
45  static VtkXMLPolyDataWriter *New() { return new VtkXMLPolyDataWriter(); }
46  vtkTypeMacro(VtkXMLPolyDataWriter, vtkXMLPolyDataWriter)
47 
48  void SetStream(std::ostream *os)
49  {
50  this->Stream = os;
51  }
52  std::ostream *GetStream() const { return this->Stream; }
53  };
54 
56  : SurfaceVtkIO(Surface::GetStaticNameOfClass(), IOMimeTypes::VTK_POLYDATA_MIMETYPE(), "VTK XML PolyData")
57  {
58  this->RegisterService();
59  }
60 
61  std::vector<itk::SmartPointer<BaseData>> SurfaceVtkXmlIO::Read()
62  {
64 
65  vtkSmartPointer<VtkXMLPolyDataReader> reader = vtkSmartPointer<VtkXMLPolyDataReader>::New();
66  if (this->GetInputStream())
67  {
68  reader->SetStream(this->GetInputStream());
69  }
70  else
71  {
72  reader->SetFileName(this->GetInputLocation().c_str());
73  }
74  reader->Update();
75 
76  if (reader->GetOutput() != NULL)
77  {
78  output->SetVtkPolyData(reader->GetOutput());
79  }
80  else
81  {
82  mitkThrow() << "vtkXMLPolyDataReader error: " << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
83  }
84 
85  std::vector<BaseData::Pointer> result;
86  result.push_back(output.GetPointer());
87  return result;
88  }
89 
91  {
93  return Unsupported;
94  if (this->GetInputStream() == NULL)
95  {
96  // check if the xml vtk reader can handle the file
97  vtkSmartPointer<VtkXMLPolyDataReader> xmlReader = vtkSmartPointer<VtkXMLPolyDataReader>::New();
98  if (xmlReader->CanReadFile(this->GetInputLocation().c_str()) != 0)
99  {
100  return Supported;
101  }
102  return Unsupported;
103  }
104  // in case of an input stream, VTK does not seem to have methods for
105  // validating it
106  return Supported;
107  }
108 
110  {
112 
113  const Surface *input = dynamic_cast<const Surface *>(this->GetInput());
114 
115  const unsigned int timesteps = input->GetTimeGeometry()->CountTimeSteps();
116  for (unsigned int t = 0; t < timesteps; ++t)
117  {
118  std::string fileName;
119  vtkSmartPointer<vtkPolyData> polyData = this->GetPolyData(t, fileName);
120  if (polyData.Get() == NULL)
121  {
122  mitkThrow() << "Cannot write empty surface";
123  }
124 
125  vtkSmartPointer<VtkXMLPolyDataWriter> writer = vtkSmartPointer<VtkXMLPolyDataWriter>::New();
126  writer->SetInputData(polyData);
127 
128  if (this->GetOutputStream())
129  {
130  if (input->GetTimeGeometry()->CountTimeSteps() > 1)
131  {
132  MITK_WARN << "Writing multiple time-steps to output streams is not supported. "
133  << "Only the first time-step will be written";
134  }
135  writer->SetStream(this->GetOutputStream());
136  }
137  else
138  {
139  writer->SetFileName(fileName.c_str());
140  }
141 
142  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
143  {
144  mitkThrow() << "Error during surface writing"
145  << (writer->GetErrorCode() ?
146  std::string(": ") + vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode()) :
147  std::string());
148  }
149 
150  if (this->GetOutputStream())
151  break;
152  }
153  }
154 
155  SurfaceVtkXmlIO *SurfaceVtkXmlIO::IOClone() const { return new SurfaceVtkXmlIO(*this); }
156 }
virtual std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
The IOMimeTypes class.
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
virtual std::istream * GetInputStream() const override
Get the input stream.
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
vtkSmartPointer< vtkPolyData > GetPolyData(unsigned int t, std::string &fileName)
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:52
#define MITK_WARN
Definition: mitkLogMacros.h:23
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
virtual void Write() override
Write the base data to the specified location or output stream.
#define mitkThrow()
virtual const BaseData * GetInput() const override
Get the input data set via SetInput().
static const char * GetStaticNameOfClass()
virtual std::ostream * GetOutputStream() const override
Get the output stream.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
virtual std::string GetInputLocation() const override
Get the current input location.
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.