1 /*============================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center (DKFZ)
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
11 ============================================================================*/
13 #include "mitkSurfaceVtkWriter.h"
14 #include <vtkConfigure.h>
15 #include <vtkErrorCode.h>
16 #include <vtkLinearTransform.h>
17 #include <vtkPolyData.h>
18 #include <vtkTransformPolyDataFilter.h>
23 #include <sys/types.h>
25 template <class VTKWRITER>
26 mitk::SurfaceVtkWriter<VTKWRITER>::SurfaceVtkWriter() : m_WriterWriteHasReturnValue(false)
28 this->SetNumberOfRequiredInputs(1);
30 m_VtkWriter = vtkSmartPointer<VtkWriterType>::New();
32 // enable to write ascii-formatted-file
33 // m_VtkWriter->SetFileTypeToASCII();
35 SetDefaultExtension(); // and information about the Writer's Write() method
38 template <class VTKWRITER>
39 mitk::SurfaceVtkWriter<VTKWRITER>::~SurfaceVtkWriter()
43 template <class VTKWRITER>
44 void mitk::SurfaceVtkWriter<VTKWRITER>::SetDefaultExtension()
49 template <class VTKWRITER>
50 void mitk::SurfaceVtkWriter<VTKWRITER>::ExecuteWrite(VtkWriterType *vtkWriter)
52 if (vtkWriter->Write() == 0 || vtkWriter->GetErrorCode() != 0)
54 itkExceptionMacro(<< "Error during surface writing: "
55 << vtkErrorCode::GetStringFromErrorCode(vtkWriter->GetErrorCode()));
59 template <class VTKWRITER>
60 void mitk::SurfaceVtkWriter<VTKWRITER>::GenerateData()
64 itkWarningMacro(<< "Sorry, filename has not been set!");
68 mitk::Surface::Pointer input = const_cast<mitk::Surface *>(this->GetInput());
70 vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyData = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
71 vtkPolyData *polyData;
72 BaseGeometry *geometry;
74 unsigned int t, timesteps = input->GetTimeGeometry()->CountTimeSteps();
76 for (t = 0; t < timesteps; ++t)
78 // surfaces do not have to exist in all timeteps; therefor, only write valid surfaces
79 if (input->GetVtkPolyData(t) == nullptr)
82 std::ostringstream filename;
83 filename.imbue(::std::locale::classic());
84 geometry = input->GetGeometry(t);
87 if (input->GetTimeGeometry()->IsValidTimeStep(t))
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;
95 itkWarningMacro(<< "Error on write: TimeGeometry invalid of surface " << filename.str() << ".");
96 filename << m_FileName.c_str() << "_T" << t << m_Extension;
98 m_VtkWriter->SetFileName(filename.str().c_str());
101 m_VtkWriter->SetFileName(m_FileName.c_str());
103 transformPolyData->SetInputData(input->GetVtkPolyData(t));
104 transformPolyData->SetTransform(geometry->GetVtkTransform());
105 transformPolyData->UpdateWholeExtent();
106 polyData = transformPolyData->GetOutput();
108 m_VtkWriter->SetInputData(polyData);
110 ExecuteWrite(m_VtkWriter);
113 m_MimeType = "application/MITK.Surface";
116 template <class VTKWRITER>
117 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(mitk::Surface *surface)
119 this->ProcessObject::SetNthInput(0, surface);
122 template <class VTKWRITER>
123 const mitk::Surface *mitk::SurfaceVtkWriter<VTKWRITER>::GetInput()
125 if (this->GetNumberOfInputs() < 1)
131 return static_cast<const Surface *>(this->ProcessObject::GetInput(0));
135 template <class VTKWRITER>
136 bool mitk::SurfaceVtkWriter<VTKWRITER>::CanWriteDataType(DataNode *input)
140 BaseData *data = input->GetData();
143 Surface::Pointer surface = dynamic_cast<Surface *>(data);
144 if (surface.IsNotNull())
146 SetDefaultExtension();
154 template <class VTKWRITER>
155 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput(DataNode *input)
157 if (input && CanWriteDataType(input))
158 SetInput(dynamic_cast<Surface *>(input->GetData()));
161 template <class VTKWRITER>
162 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetWritenMIMEType()
167 template <class VTKWRITER>
168 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetFileExtension()