1 #ifndef __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__
2 #define __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__
5 #include <itkNumericTraits.h>
16 template< class I, class R >
17 class ImageCostFunction
21 /// Type-related and pointers
22 typedef ImageCostFunction Self;
23 typedef itk::Object Superclass;
24 typedef itk::SmartPointer< Self > Pointer;
25 typedef itk::SmartPointer< const Self > ConstPointer;
27 typedef I TInputImage;
29 typedef typename I::IndexType TIndex;
33 itkTypeMacro( ImageCostFunction, itkObject );
35 itkGetConstObjectMacro( InputImage, I );
36 itkSetConstObjectMacro( InputImage, I );
39 virtual R Evaluate( const TIndex& v, const TIndex& p ) const
41 typedef typename I::PixelType _TPixel;
42 typedef typename itk::NumericTraits< _TPixel > _TTraits;
43 typedef typename _TTraits::MeasurementVectorType _TVector;
44 typedef typename _TTraits::ValueType _TValue;
46 if( this->m_InputImage.IsNotNull( ) )
48 _TPixel pix = this->m_InputImage->GetPixel( v );
49 if( typeid( _TPixel ) != typeid( _TValue ) )
51 _TVector* array = reinterpret_cast< _TVector* >( &pix );
53 this->m_InputImage->GetNumberOfComponentsPerPixel( );
55 for( unsigned int i = 0; i < n; ++i )
56 sum += R( ( *array )[ i ] );
57 return( sum / R( n ) );
60 return( R( *( reinterpret_cast< _TValue* >( &pix ) ) ) );
63 return( std::numeric_limits< R >::max( ) );
70 virtual ~ImageCostFunction( )
74 // Purposely not implemented
75 ImageCostFunction( const Self& );
76 void operator=( const Self& );
79 typename I::ConstPointer m_InputImage;
88 #endif // __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__