1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 #include "mitkSurfaceVtkWriter.h"
18 #include <vtkConfigure.h>
19 #include <vtkErrorCode.h>
20 #include <vtkLinearTransform.h>
21 #include <vtkPolyData.h>
22 #include <vtkTransformPolyDataFilter.h>
27 #include <sys/types.h>
29 template <class VTKWRITER>
30 mitk::SurfaceVtkWriter<VTKWRITER>::SurfaceVtkWriter() : m_WriterWriteHasReturnValue(false)
32 this->SetNumberOfRequiredInputs(1);
34 m_VtkWriter = vtkSmartPointer<VtkWriterType>::New();
36 // enable to write ascii-formatted-file
37 // m_VtkWriter->SetFileTypeToASCII();
39 SetDefaultExtension(); // and information about the Writer's Write() method
42 template <class VTKWRITER>
43 mitk::SurfaceVtkWriter<VTKWRITER>::~SurfaceVtkWriter()
47 template <class VTKWRITER>
48 void mitk::SurfaceVtkWriter<VTKWRITER>::SetDefaultExtension()
53 template <class VTKWRITER>
54 void mitk::SurfaceVtkWriter<VTKWRITER>::ExecuteWrite(VtkWriterType *vtkWriter)
56 if (vtkWriter->Write() == 0 || vtkWriter->GetErrorCode() != 0)
58 itkExceptionMacro(<< "Error during surface writing: "
59 << vtkErrorCode::GetStringFromErrorCode(vtkWriter->GetErrorCode()));
63 template <class VTKWRITER>
64 void mitk::SurfaceVtkWriter<VTKWRITER>::GenerateData()
68 itkWarningMacro(<< "Sorry, filename has not been set!");
72 mitk::Surface::Pointer input = const_cast<mitk::Surface *>(this->GetInput());
74 vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyData = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
75 vtkPolyData *polyData;
76 BaseGeometry *geometry;
78 unsigned int t, timesteps = input->GetTimeGeometry()->CountTimeSteps();
80 for (t = 0; t < timesteps; ++t)
82 // surfaces do not have to exist in all timeteps; therefor, only write valid surfaces
83 if (input->GetVtkPolyData(t) == nullptr)
86 std::ostringstream filename;
87 filename.imbue(::std::locale::classic());
88 geometry = input->GetGeometry(t);
91 if (input->GetTimeGeometry()->IsValidTimeStep(t))
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;
99 itkWarningMacro(<< "Error on write: TimeGeometry invalid of surface " << filename.str() << ".");
100 filename << m_FileName.c_str() << "_T" << t << m_Extension;
102 m_VtkWriter->SetFileName(filename.str().c_str());
105 m_VtkWriter->SetFileName(m_FileName.c_str());
107 transformPolyData->SetInputData(input->GetVtkPolyData(t));
108 transformPolyData->SetTransform(geometry->GetVtkTransform());
109 transformPolyData->UpdateWholeExtent();
110 polyData = transformPolyData->GetOutput();
112 m_VtkWriter->SetInputData(polyData);
114 ExecuteWrite(m_VtkWriter);
117 m_MimeType = "application/MITK.Surface";
120 template <class VTKWRITER>
121 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(mitk::Surface *surface)
123 this->ProcessObject::SetNthInput(0, surface);
126 template <class VTKWRITER>
127 const mitk::Surface *mitk::SurfaceVtkWriter<VTKWRITER>::GetInput()
129 if (this->GetNumberOfInputs() < 1)
135 return static_cast<const Surface *>(this->ProcessObject::GetInput(0));
139 template <class VTKWRITER>
140 bool mitk::SurfaceVtkWriter<VTKWRITER>::CanWriteDataType(DataNode *input)
144 BaseData *data = input->GetData();
147 Surface::Pointer surface = dynamic_cast<Surface *>(data);
148 if (surface.IsNotNull())
150 SetDefaultExtension();
158 template <class VTKWRITER>
159 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(DataNode *input)
161 if (input && CanWriteDataType(input))
162 SetInput(dynamic_cast<Surface *>(input->GetData()));
165 template <class VTKWRITER>
166 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetWritenMIMEType()
171 template <class VTKWRITER>
172 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetFileExtension()