Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkSurfaceVtkLegacyIO.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 "mitkSurfaceVtkLegacyIO.h"
18 
19 #include "mitkIOMimeTypes.h"
20 #include "mitkSurface.h"
21 
22 #include <vtkErrorCode.h>
23 #include <vtkPolyDataReader.h>
24 #include <vtkPolyDataWriter.h>
25 #include <vtkSmartPointer.h>
26 #include <vtkXMLPolyDataReader.h>
27 #include <vtkXMLPolyDataWriter.h>
28 
29 namespace mitk
30 {
32  : SurfaceVtkIO(Surface::GetStaticNameOfClass(), IOMimeTypes::VTK_POLYDATA_LEGACY_MIMETYPE(), "VTK Legacy PolyData")
33  {
34  Options defaultOptions;
35  defaultOptions["Save as binary file"] = false;
36  this->SetDefaultWriterOptions(defaultOptions);
37  this->RegisterService();
38  }
39 
40  std::vector<itk::SmartPointer<BaseData>> SurfaceVtkLegacyIO::Read()
41  {
43 
44  // The legay vtk reader cannot work with input streams
45  const std::string fileName = this->GetLocalFileName();
46  vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
47  reader->SetFileName(fileName.c_str());
48  reader->Update();
49 
50  if (reader->GetOutput() != NULL)
51  {
52  output->SetVtkPolyData(reader->GetOutput());
53  }
54  else
55  {
56  mitkThrow() << "vtkPolyDataReader error: " << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
57  }
58 
59  std::vector<BaseData::Pointer> result;
60  result.push_back(output.GetPointer());
61  return result;
62  }
63 
65  {
67  return Unsupported;
68  vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
69  reader->SetFileName(this->GetLocalFileName().c_str());
70  if (reader->IsFilePolyData())
71  {
72  if (std::strcmp(reader->GetHeader(), "vtk output") == 0)
73  {
74  return Supported;
75  }
76  else
77  return PartiallySupported;
78  }
79  return Unsupported;
80  }
81 
83  {
85 
86  const Surface *input = dynamic_cast<const Surface *>(this->GetInput());
87 
88  const unsigned int timesteps = input->GetTimeGeometry()->CountTimeSteps();
89  for (unsigned int t = 0; t < timesteps; ++t)
90  {
91  std::string fileName;
92  vtkSmartPointer<vtkPolyData> polyData = this->GetPolyData(t, fileName);
93  vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();
94  writer->SetInputData(polyData);
95  if (us::any_cast<bool>(GetWriterOption("Save as binary file")))
96  {
97  writer->SetFileTypeToBinary();
98  }
99  // The legacy vtk poly data writer cannot write to streams
100  LocalFile localFile(this);
101  writer->SetFileName(localFile.GetFileName().c_str());
102 
103  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
104  {
105  mitkThrow() << "Error during surface writing"
106  << (writer->GetErrorCode() ?
107  std::string(": ") + vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode()) :
108  std::string());
109  }
110 
111  if (this->GetOutputStream() && input->GetTimeGeometry()->CountTimeSteps() > 1)
112  {
113  MITK_WARN << "Writing multiple time-steps to output streams is not supported. "
114  << "Only the first time-step will be written";
115  break;
116  }
117  }
118  }
119 
120  SurfaceVtkLegacyIO *SurfaceVtkLegacyIO::IOClone() const { return new SurfaceVtkLegacyIO(*this); }
121 }
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
The IOMimeTypes class.
virtual std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
DataCollection - Class to facilitate loading/accessing structured data.
vtkSmartPointer< vtkPolyData > GetPolyData(unsigned int t, std::string &fileName)
std::map< std::string, us::Any > Options
Options for reading or writing data.
Definition: mitkIFileIO.h:73
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:52
std::string GetLocalFileName() const
Get a local file name for reading.
#define MITK_WARN
Definition: mitkLogMacros.h:23
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define mitkThrow()
virtual const BaseData * GetInput() const override
Get the input data set via SetInput().
static const char * GetStaticNameOfClass()
virtual void Write() override
Write the base data to the specified location or output stream.
A local file representation for streams.
virtual std::ostream * GetOutputStream() const override
Get the output stream.
us::Any GetWriterOption(const std::string &name) const
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
static Pointer New()
void SetDefaultWriterOptions(const Options &defaultOptions)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.