1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkITKImageToImageFilter - Abstract base class for connecting ITK and VTK
// .SECTION Description
// vtkITKImageToImageFilter provides a foo
#ifndef __vtkITKImageToImageFilter_h
#define __vtkITKImageToImageFilter_h
#include "itkCommand.h"
#include "vtkCommand.h"
#include "itkProcessObject.h"
#include "vtkImageImport.h"
#include "vtkImageExport.h"
#include "vtkImageToImageFilter.h"
#include "vtkImageCast.h"
#include "vtkImageData.h"
#undef itkExceptionMacro
#define itkExceptionMacro(x) \
{ \
::itk::OStringStream message; \
message << "itk::ERROR: " << this->GetNameOfClass() \
<< "(" << this << "): "; \
std::cout << message.str().c_str() << std::endl; \
}
#undef itkGenericExceptionMacro
#define itkGenericExceptionMacro(x) \
{ \
::itk::OStringStream message; \
message << "itk::ERROR: " x; \
std::cout << message.str() << std::endl; \
}
class VTK_EXPORT vtkITKImageToImageFilter : public vtkImageToImageFilter
{
public:
static vtkITKImageToImageFilter *New()
{
return new vtkITKImageToImageFilter;
};
vtkTypeMacro(vtkITKImageToImageFilter,vtkImageToImageFilter);
void PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf ( os, indent );
this->vtkExporter->PrintSelf ( os, indent );
this->vtkImporter->PrintSelf ( os, indent );
};
// Description:
// This method considers the sub filters MTimes when computing this objects
// modified time.
unsigned long int GetMTime()
{
unsigned long int t1, t2;
t1 = this->Superclass::GetMTime();
t2 = this->vtkExporter->GetMTime();
if (t2 > t1)
{
t1 = t2;
}
t2 = this->vtkImporter->GetMTime();
if (t2 > t1)
{
t1 = t2;
}
return t1;
};
// Description:
// Pass modified message to itk filter
void Modified()
{
this->Superclass::Modified();
if (this->m_Process)
{
m_Process->Modified();
}
};
// Description:
// Pass DebugOn.
void DebugOn()
{
this->m_Process->DebugOn();
};
// Description:
// Pass DebugOff.
void DebugOff()
{
this->m_Process->DebugOff();
};
// Description:
// Pass SetNumberOfThreads.
void SetNumberOfThreads(int val)
{
this->m_Process->SetNumberOfThreads(val);
};
// Description:
// Pass SetNumberOfThreads.
int GetNumberOfThreads()
{
return this->m_Process->GetNumberOfThreads();
};
// Description:
// This method returns the cache to make a connection
// It justs feeds the request to the sub filter.
void SetOutput ( vtkImageData* d ) { this->vtkImporter->SetOutput ( d ); };
virtual vtkImageData *GetOutput() { return this->vtkImporter->GetOutput(); };
virtual vtkImageData *GetOutput(int idx)
{
return (vtkImageData *) this->vtkImporter->GetOutput(idx);
};
// Description:
// Set the Input of the filter.
virtual void SetInput(vtkImageData *Input)
{
this->vtkCast->SetInput(Input);
};
// Description:
// Return the input to the filter
virtual vtkDataObject* GetInput()
{
return (vtkDataObject::SafeDownCast( this->vtkCast->GetInput() ));
};
// Description: Override vtkSource's Update so that we can access
// this class's GetOutput(). vtkSource's GetOutput is not virtual.
virtual void Update()
{
if (this->GetOutput(0))
{
this->m_Process->Update();
this->GetOutput(0)->Update();
if ( this->GetOutput(0)->GetSource() )
{
// this->SetErrorCode( this->GetOutput(0)->GetSource()->GetErrorCode() );
}
}
}
//BTX
void HandleProgressEvent ()
{
if ( this->m_Process )
{
this->UpdateProgress ( m_Process->GetProgress() );
}
};
void HandleStartEvent ()
{
this->InvokeEvent(vtkCommand::StartEvent,NULL);
};
void HandleEndEvent ()
{
this->InvokeEvent(vtkCommand::EndEvent,NULL);
};
// ETX
protected:
// BTX
// Dummy ExecuteData
void ExecuteData (vtkDataObject *)
{
vtkWarningMacro(<< "This filter does not respond to Update(). Doing a GetOutput->Update() instead.");
}
// ETX
vtkITKImageToImageFilter()
{
// Need an import, export, and a ITK pipeline
this->vtkCast = vtkImageCast::New();
this->vtkExporter = vtkImageExport::New();
this->vtkImporter = vtkImageImport::New();
this->vtkExporter->SetInput ( this->vtkCast->GetOutput() );
this->m_Process = NULL;
this->m_ProgressCommand = MemberCommand::New();
this->m_ProgressCommand->SetCallbackFunction ( this, &vtkITKImageToImageFilter::HandleProgressEvent );
this->m_StartEventCommand = MemberCommand::New();
this->m_StartEventCommand->SetCallbackFunction ( this, &vtkITKImageToImageFilter::HandleStartEvent );
this->m_EndEventCommand = MemberCommand::New();
this->m_EndEventCommand->SetCallbackFunction ( this, &vtkITKImageToImageFilter::HandleEndEvent );
};
~vtkITKImageToImageFilter()
{
vtkDebugMacro ("Destructing vtkITKImageToImageFilter");
this->vtkExporter->Delete();
this->vtkImporter->Delete();
this->vtkCast->Delete();
};
// BTX
void LinkITKProgressToVTKProgress ( itk::ProcessObject* process )
{
if ( process )
{
this->m_Process = process;
this->m_Process->AddObserver ( itk::ProgressEvent(), this->m_ProgressCommand );
this->m_Process->AddObserver ( itk::StartEvent(), this->m_StartEventCommand );
this->m_Process->AddObserver ( itk::EndEvent(), this->m_EndEventCommand );
}
};
typedef itk::SimpleMemberCommand<vtkITKImageToImageFilter> MemberCommand;
typedef MemberCommand::Pointer MemberCommandPointer;
itk::ProcessObject::Pointer m_Process;
MemberCommandPointer m_ProgressCommand;
MemberCommandPointer m_StartEventCommand;
MemberCommandPointer m_EndEventCommand;
// ITK Progress object
// To/from VTK
vtkImageCast* vtkCast;
vtkImageImport* vtkImporter;
vtkImageExport* vtkExporter;
//ETX
private:
vtkITKImageToImageFilter(const vtkITKImageToImageFilter&); // Not implemented.
void operator=(const vtkITKImageToImageFilter&); // Not implemented.
};
#endif
|