#ifndef __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__ #define __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__ #include #include #include namespace fpa { namespace Image { namespace Functors { /** */ template< class I, class R > class ImageCostFunction : public itk::Object { public: /// Type-related and pointers typedef ImageCostFunction Self; typedef itk::Object Superclass; typedef itk::SmartPointer< Self > Pointer; typedef itk::SmartPointer< const Self > ConstPointer; typedef I TInputImage; typedef R TResult; typedef typename I::IndexType TIndex; public: itkNewMacro( Self ); itkTypeMacro( ImageCostFunction, itkObject ); itkGetConstObjectMacro( InputImage, I ); itkSetConstObjectMacro( InputImage, I ); public: virtual R Evaluate( const TIndex& v, const TIndex& p ) const { typedef typename I::PixelType _TPixel; typedef typename itk::NumericTraits< _TPixel > _TTraits; typedef typename _TTraits::MeasurementVectorType _TVector; typedef typename _TTraits::ValueType _TValue; if( this->m_InputImage.IsNotNull( ) ) { _TPixel pix = this->m_InputImage->GetPixel( v ); if( typeid( _TPixel ) != typeid( _TValue ) ) { _TVector* array = reinterpret_cast< _TVector* >( &pix ); unsigned int n = this->m_InputImage->GetNumberOfComponentsPerPixel( ); R sum = R( 0 ); for( unsigned int i = 0; i < n; ++i ) sum += R( ( *array )[ i ] ); return( sum / R( n ) ); } else return( R( *( reinterpret_cast< _TValue* >( &pix ) ) ) ); } else return( std::numeric_limits< R >::max( ) ); } protected: ImageCostFunction( ) : Superclass( ) { } virtual ~ImageCostFunction( ) { } private: // Purposely not implemented ImageCostFunction( const Self& ); void operator=( const Self& ); protected: typename I::ConstPointer m_InputImage; }; } // ecapseman } // ecapseman } // ecapseman #endif // __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__ // eof - $RCSfile$