00001 //============================================================================= 00002 // This file is part of VTKEdge. See vtkedge.org for more information. 00003 // 00004 // Copyright (c) 2010 Kitware, Inc. 00005 // 00006 // VTKEdge may be used under the terms of the BSD License 00007 // Please see the file Copyright.txt in the root directory of 00008 // VTKEdge for further information. 00009 // 00010 // Alternatively, you may see: 00011 // 00012 // http://www.vtkedge.org/vtkedge/project/license.html 00013 // 00014 // 00015 // For custom extensions, consulting services, or training for 00016 // this or any other Kitware supported open source project, please 00017 // contact Kitware at sales@kitware.com. 00018 // 00019 // 00020 //============================================================================= 00021 00022 00023 #ifndef _vtkKWEITKFilterModule_h 00024 #define _vtkKWEITKFilterModule_h 00025 00026 #include "VTKEdgeConfigure.h" // Needed for export symbols 00027 #include "itkVTKImageToImageFilter.h" 00028 #include "itkImageToVTKImageFilter.h" 00029 #include "vtkKWEITKFilterModuleBase.h" 00030 #include "itkImage.h" 00031 #include "vtkImageData.h" 00032 00033 #include <string> 00034 #include <iostream> 00035 00036 namespace vtkitk 00037 { 00038 00039 template <class TFilterType > 00040 class FilterModule : public FilterModuleBase 00041 { 00042 00043 public: 00044 00045 // Instantiate the image types 00046 typedef TFilterType FilterType; 00047 typedef typename FilterType::InputImageType InputImageType; 00048 typedef typename FilterType::OutputImageType OutputImageType; 00049 typedef typename InputImageType::PixelType InputPixelType; 00050 typedef typename OutputImageType::PixelType OutputPixelType; 00051 00052 itkStaticConstMacro( Dimension, unsigned int, 00053 itk::GetImageDimension< InputImageType >::ImageDimension ); 00054 00055 typedef itk::VTKImageToImageFilter< InputImageType > 00056 ImportFilterType; 00057 typedef itk::ImageToVTKImageFilter< OutputImageType > 00058 ExportFilterType; 00059 00060 typedef typename InputImageType::SizeType SizeType; 00061 typedef typename InputImageType::IndexType IndexType; 00062 typedef typename InputImageType::RegionType RegionType; 00063 00065 FilterModule() 00066 { 00067 this->m_ImportFilter = ImportFilterType::New(); 00068 this->m_ExportFilter = ExportFilterType::New(); 00069 this->m_Filter = FilterType::New(); 00070 //this->m_Filter->ReleaseDataFlagOn(); 00071 00072 // Set the Observer for updating progress in the GUI 00073 this->m_Filter->AddObserver( itk::ProgressEvent(), this->GetCommandObserver() ); 00074 this->m_Filter->AddObserver( itk::StartEvent(), this->GetCommandObserver() ); 00075 this->m_Filter->AddObserver( itk::EndEvent(), this->GetCommandObserver() ); 00076 this->m_LetITKAllocateOutputMemory = false; 00077 } 00078 00080 virtual ~FilterModule() 00081 { 00082 } 00083 00085 void SetInput(vtkImageData * image) 00086 { 00087 this->m_ImportFilter->SetInput(image); 00088 } 00089 00091 virtual vtkImageData* GetInput() 00092 { 00093 return this->m_ImportFilter->GetInput(); 00094 } 00095 00097 FilterType * GetFilter() 00098 { 00099 return this->m_Filter.GetPointer(); 00100 } 00101 00102 void SetLetITKAllocateOutputMemory(bool s) 00103 { 00104 this->m_LetITKAllocateOutputMemory = s; 00105 } 00106 00107 // FIXME: We are so busted when doing this for multi-component data. 00108 // It won't even frickin compile. 00109 void SetOutputBuffer( void * buffer ) 00110 { 00111 this->m_LetITKAllocateOutputMemory = false; 00112 RegionType region = this->m_Filter->GetOutput()->GetLargestPossibleRegion(); 00113 const unsigned long nPixels = region.GetNumberOfPixels(); 00114 this->m_Filter->GetOutput()->SetImportPointer( 00115 static_cast< OutputPixelType * >(buffer), nPixels ); 00116 this->m_Filter->GetOutput()->Allocate( ); // TODO Check if necessary 00117 } 00118 00120 virtual void Update() 00121 { 00122 00123 // Progress reporting 00124 this->InitializeProgressValue(); 00125 this->SetCurrentFilterProgressWeight( 1.0 ); 00126 00127 // Update the filter 00128 try 00129 { 00130 this->m_ImportFilter->Update(); 00131 } 00132 catch( itk::ProcessAborted & e) 00133 { 00134 std::cerr << "ITK error " << e.what() << std::endl; 00135 } 00136 00137 // Update the filter 00138 try 00139 { 00140 this->m_Filter->SetInput( this->m_ImportFilter->GetOutput() ); 00141 this->m_Filter->Update(); 00142 } 00143 catch( itk::ProcessAborted & e) 00144 { 00145 std::cerr << "ITK error " << e.what() << std::endl; 00146 } 00147 00148 // Update the exporter 00149 try 00150 { 00151 this->m_ExportFilter->SetInput( this->m_Filter->GetOutput() ); 00152 this->m_ExportFilter->Update(); 00153 } 00154 catch( itk::ProcessAborted & e) 00155 { 00156 std::cerr << "ITK error " << e.what() << std::endl; 00157 } 00158 } 00159 00160 vtkImageData * GetOutput() 00161 { 00162 return this->m_ExportFilter->GetOutput(); 00163 } 00164 00165 virtual void SetRequestedExtent( int extent[6] ) 00166 { 00167 SizeType size; 00168 size[0] = extent[1] - extent[0] + 1; 00169 size[1] = extent[3] - extent[2] + 1; 00170 size[2] = extent[5] - extent[4] + 1; 00171 IndexType index; 00172 index[0] = extent[0]; 00173 index[1] = extent[2]; 00174 index[2] = extent[4]; 00175 RegionType region(index,size); 00176 00177 this->m_Filter->GetOutput()->SetRegions( region ); 00178 this->m_ExportFilter->GetOutput()->SetUpdateExtent(extent); 00179 } 00180 00181 protected: 00182 typename ImportFilterType::Pointer m_ImportFilter; 00183 typename FilterType::Pointer m_Filter; 00184 bool m_LetITKAllocateOutputMemory; 00185 typename ExportFilterType::Pointer m_ExportFilter; 00186 }; 00187 00188 } // end namespace vtkitk 00189 00190 #endif
1.7.1