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