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