Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkSurfaceVtkWriter.h"
00019 #include <vtkConfigure.h>
00020 #include <vtkPolyData.h>
00021 #include <vtkLinearTransform.h>
00022 #include <vtkTransformPolyDataFilter.h>
00023 #include <vtkErrorCode.h>
00024
00025 #include <sstream>
00026 #include <sys/stat.h>
00027 #include <sys/types.h>
00028 #include <stdio.h>
00029
00030 template <class VTKWRITER>
00031 mitk::SurfaceVtkWriter<VTKWRITER>::SurfaceVtkWriter()
00032 : m_WriterWriteHasReturnValue( false )
00033 {
00034 this->SetNumberOfRequiredInputs( 1 );
00035
00036 m_VtkWriter = vtkSmartPointer<VtkWriterType>::New();
00037
00038
00039
00040
00041 SetDefaultExtension();
00042 }
00043
00044 template <class VTKWRITER>
00045 mitk::SurfaceVtkWriter<VTKWRITER>::~SurfaceVtkWriter()
00046 {
00047 }
00048
00049 template <class VTKWRITER>
00050 void mitk::SurfaceVtkWriter<VTKWRITER>::SetDefaultExtension()
00051 {
00052 m_Extension = ".vtk";
00053 }
00054
00055 template<class VTKWRITER>
00056 void mitk::SurfaceVtkWriter<VTKWRITER>::ExecuteWrite( VtkWriterType* vtkWriter )
00057 {
00058 if ( vtkWriter->Write() == 0 || vtkWriter->GetErrorCode() != 0 )
00059 {
00060 itkExceptionMacro(<<"Error during surface writing: " << vtkErrorCode::GetStringFromErrorCode(vtkWriter->GetErrorCode()) );
00061 }
00062 }
00063
00064 template <class VTKWRITER>
00065 void mitk::SurfaceVtkWriter<VTKWRITER>::GenerateData()
00066 {
00067 if ( m_FileName == "" )
00068 {
00069 itkWarningMacro( << "Sorry, filename has not been set!" );
00070 return ;
00071 }
00072
00073 mitk::Surface::Pointer input = const_cast<mitk::Surface*>(this->GetInput());
00074
00075 vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyData = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
00076 vtkPolyData * polyData;
00077 Geometry3D* geometry;
00078
00079 unsigned int t, timesteps = input->GetTimeSlicedGeometry()->GetTimeSteps();
00080
00081 for(t = 0; t < timesteps; ++t)
00082 {
00083
00084 if( input->GetVtkPolyData(t) == NULL ) continue;
00085
00086 ::itk::OStringStream filename;
00087 geometry = input->GetGeometry(t);
00088 if ( timesteps > 1 )
00089 {
00090 if(input->GetTimeSlicedGeometry()->IsValidTime(t))
00091 {
00092 const TimeBounds& timebounds = geometry->GetTimeBounds();
00093 filename << m_FileName.c_str() << "_S" << std::setprecision(0) << timebounds[0] << "_E" << std::setprecision(0) << timebounds[1] << "_T" << t << m_Extension;
00094 }
00095 else
00096 {
00097 itkWarningMacro(<<"Error on write: TimeSlicedGeometry invalid of surface " << filename << ".");
00098 filename << m_FileName.c_str() << "_T" << t << m_Extension;
00099 }
00100 m_VtkWriter->SetFileName(filename.str().c_str());
00101 }
00102 else
00103 m_VtkWriter->SetFileName(m_FileName.c_str());
00104
00105 geometry->TransferItkToVtkTransform();
00106 transformPolyData->SetInput(input->GetVtkPolyData(t));
00107 transformPolyData->SetTransform(geometry->GetVtkTransform());
00108 transformPolyData->UpdateWholeExtent();
00109 polyData = transformPolyData->GetOutput();
00110
00111 m_VtkWriter->SetInput(polyData);
00112
00113 ExecuteWrite( m_VtkWriter );
00114 }
00115
00116 m_MimeType = "application/MITK.Surface";
00117 }
00118
00119 template <class VTKWRITER>
00120 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput( mitk::Surface* surface )
00121 {
00122 this->ProcessObject::SetNthInput( 0, surface );
00123 }
00124
00125 template <class VTKWRITER>
00126 const mitk::Surface* mitk::SurfaceVtkWriter<VTKWRITER>::GetInput()
00127 {
00128 if ( this->GetNumberOfInputs() < 1 )
00129 {
00130 return NULL;
00131 }
00132 else
00133 {
00134 return static_cast< const Surface * >( this->ProcessObject::GetInput( 0 ) );
00135 }
00136 }
00137
00138 template <class VTKWRITER>
00139 bool mitk::SurfaceVtkWriter<VTKWRITER>::CanWriteDataType( DataNode* input )
00140 {
00141 if ( input )
00142 {
00143 BaseData* data = input->GetData();
00144 if ( data )
00145 {
00146 Surface::Pointer surface = dynamic_cast<Surface*>( data );
00147 if( surface.IsNotNull() )
00148 {
00149 SetDefaultExtension();
00150 return true;
00151 }
00152 }
00153 }
00154 return false;
00155 }
00156
00157 template <class VTKWRITER>
00158 void mitk::SurfaceVtkWriter<VTKWRITER>::SetInput( DataNode* input )
00159 {
00160 if( input && CanWriteDataType( input ) )
00161 SetInput( dynamic_cast<Surface*>( input->GetData() ) );
00162 }
00163
00164 template <class VTKWRITER>
00165 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetWritenMIMEType()
00166 {
00167 return m_MimeType;
00168 }
00169
00170 template <class VTKWRITER>
00171 std::string mitk::SurfaceVtkWriter<VTKWRITER>::GetFileExtension()
00172 {
00173 return m_Extension;
00174 }