Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSurfaceVtkWriter.txx
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 "mitkSurfaceVtkWriter.h"
14 #include <vtkConfigure.h>
15 #include <vtkErrorCode.h>
16 #include <vtkLinearTransform.h>
17 #include <vtkPolyData.h>
18 #include <vtkTransformPolyDataFilter.h>
19 
20 #include <sstream>
21 #include <cstdio>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 template <class VTKWRITER>
26 mitk::SurfaceVtkWriter<VTKWRITER>::SurfaceVtkWriter() : m_WriterWriteHasReturnValue(false)
27 {
28  this->SetNumberOfRequiredInputs(1);
29 
30  m_VtkWriter = vtkSmartPointer<VtkWriterType>::New();
31 
32  // enable to write ascii-formatted-file
33  // m_VtkWriter->SetFileTypeToASCII();
34 
35  SetDefaultExtension(); // and information about the Writer's Write() method
36 }
37 
38 template <class VTKWRITER>
39 mitk::SurfaceVtkWriter<VTKWRITER>::~SurfaceVtkWriter()
40 {
41 }
42 
43 template <class VTKWRITER>
44 void mitk::SurfaceVtkWriter<VTKWRITER>::SetDefaultExtension()
45 {
46  m_Extension = ".vtk";
47 }
48 
49 template <class VTKWRITER>
50 void mitk::SurfaceVtkWriter<VTKWRITER>::ExecuteWrite(VtkWriterType *vtkWriter)
51 {
52  if (vtkWriter->Write() == 0 || vtkWriter->GetErrorCode() != 0)
53  {
54  itkExceptionMacro(<< "Error during surface writing: "
55  << vtkErrorCode::GetStringFromErrorCode(vtkWriter->GetErrorCode()));
56  }
57 }
58 
59 template <class VTKWRITER>
60 void mitk::SurfaceVtkWriter<VTKWRITER>::GenerateData()
61 {
62  if (m_FileName == "")
63  {
64  itkWarningMacro(<< "Sorry, filename has not been set!");
65  return;
66  }
67 
68  mitk::Surface::Pointer input = const_cast<mitk::Surface *>(this->GetInput());
69 
70  vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyData = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
71  vtkPolyData *polyData;
72  BaseGeometry *geometry;
73 
74  unsigned int t, timesteps = input->GetTimeGeometry()->CountTimeSteps();
75 
76  for (t = 0; t < timesteps; ++t)
77  {
78  // surfaces do not have to exist in all timeteps; therefor, only write valid surfaces
79  if (input->GetVtkPolyData(t) == nullptr)
80  continue;
81 
82  std::ostringstream filename;
83  filename.imbue(::std::locale::classic());
84  geometry = input->GetGeometry(t);
85  if (timesteps > 1)
86  {
87  if (input->GetTimeGeometry()->IsValidTimeStep(t))
88  {
89  const TimeBounds &timebounds = input->GetTimeGeometry()->GetTimeBounds(t);
90  filename << m_FileName.c_str() << "_S" << std::setprecision(0) << timebounds[0] << "_E" << std::setprecision(0)
91  << timebounds[1] << "_T" << t << m_Extension;
92  }
93  else
94  {
95  itkWarningMacro(<< "Error on write: TimeGeometry invalid of surface " << filename.str() << ".");
96  filename << m_FileName.c_str() << "_T" << t << m_Extension;
97  }
98  m_VtkWriter->SetFileName(filename.str().c_str());
99  }
100  else
101  m_VtkWriter->SetFileName(m_FileName.c_str());
102 
103  transformPolyData->SetInputData(input->GetVtkPolyData(t));
104  transformPolyData->SetTransform(geometry->GetVtkTransform());
105  transformPolyData->UpdateWholeExtent();
106  polyData = transformPolyData->GetOutput();
107 
108  m_VtkWriter->SetInputData(polyData);
109 
110  ExecuteWrite(m_VtkWriter);
111  }
112 
113  m_MimeType = "application/MITK.Surface";
114 }
115 
116 template <class VTKWRITER>
117 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(mitk::Surface *surface)
118 {
119  this->ProcessObject::SetNthInput(0, surface);
120 }
121 
122 template <class VTKWRITER>
123 const mitk::Surface *mitk::SurfaceVtkWriter<VTKWRITER>::GetInput()
124 {
125  if (this->GetNumberOfInputs() < 1)
126  {
127  return nullptr;
128  }
129  else
130  {
131  return static_cast<const Surface *>(this->ProcessObject::GetInput(0));
132  }
133 }
134 
135 template <class VTKWRITER>
136 bool mitk::SurfaceVtkWriter<VTKWRITER>::CanWriteDataType(DataNode *input)
137 {
138  if (input)
139  {
140  BaseData *data = input->GetData();
141  if (data)
142  {
143  Surface::Pointer surface = dynamic_cast<Surface *>(data);
144  if (surface.IsNotNull())
145  {
146  SetDefaultExtension();
147  return true;
148  }
149  }
150  }
151  return false;
152 }
153 
154 template <class VTKWRITER>
155 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(DataNode *input)
156 {
157  if (input && CanWriteDataType(input))
158  SetInput(dynamic_cast<Surface *>(input->GetData()));
159 }
160 
161 template <class VTKWRITER>
162 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetWritenMIMEType()
163 {
164  return m_MimeType;
165 }
166 
167 template <class VTKWRITER>
168 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetFileExtension()
169 {
170  return m_Extension;
171 }