]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Functors/ImageAbsoluteDifferenceCostFunction.h
6171f26feda1afa65b7585344dc923bd2c7731f1
[FrontAlgorithms.git] / lib / fpa / Image / Functors / ImageAbsoluteDifferenceCostFunction.h
1 #ifndef __FPA__IMAGE__FUNCTORS__IMAGEABSOLUTEDIFFERENCECOSTFUNCTION__H__
2 #define __FPA__IMAGE__FUNCTORS__IMAGEABSOLUTEDIFFERENCECOSTFUNCTION__H__
3
4 #include <fpa/Image/Functors/ImageCostFunction.h>
5
6 namespace fpa
7 {
8   namespace Image
9   {
10     namespace Functors
11     {
12       /**
13        */
14       template< class I, class R >
15       class ImageAbsoluteDifferenceCostFunction
16         : public fpa::Image::Functors::ImageCostFunction< I, R >
17       {
18       public:
19         /// Type-related and pointers
20         typedef ImageAbsoluteDifferenceCostFunction                   Self;
21         typedef fpa::Image::Functors::ImageCostFunction< I, R > Superclass;
22         typedef itk::SmartPointer< Self >                          Pointer;
23         typedef itk::SmartPointer< const Self >               ConstPointer;
24
25         typedef typename Superclass::TInputImage TInputImage;
26         typedef typename Superclass::TResult     TResult;
27         typedef typename Superclass::TIndex      TIndex;
28
29       public:
30         itkNewMacro( Self );
31         itkTypeMacro( ImageAbsoluteDifferenceCostFunction, itkObject );
32
33       public:
34         virtual R Evaluate( const TIndex& v, const TIndex& p ) const
35           {
36             typedef typename I::PixelType                    _TPixel;
37             typedef typename itk::NumericTraits< _TPixel >   _TTraits;
38             typedef typename _TTraits::MeasurementVectorType _TVector;
39             typedef typename _TTraits::ValueType             _TValue;
40
41             if( this->m_InputImage.IsNotNull( ) )
42             {
43               _TPixel pix = this->m_InputImage->GetPixel( v );
44               pix        -= this->m_InputImage->GetPixel( p );
45               if( typeid( _TPixel ) != typeid( _TValue ) )
46               {
47                 _TVector* a = reinterpret_cast< _TVector* >( &pix );
48                 unsigned int n =
49                   this->m_InputImage->GetNumberOfComponentsPerPixel( );
50                 double sum = double( 0 );
51                 for( unsigned int i = 0; i < n; ++i )
52                   sum += ( double( ( *a )[ i ] ) * double( ( *a )[ i ] ) );
53                 return( R( std::sqrt( sum ) ) );
54               }
55               else
56               {
57                 double pix_val =
58                   double( *( reinterpret_cast< _TValue* >( &pix ) ) );
59                 return( R( std::fabs( pix_val ) ) );
60               }
61             }
62             else
63               return( std::numeric_limits< R >::max( ) );
64           }
65
66       protected:
67         ImageAbsoluteDifferenceCostFunction( )
68           : Superclass( )
69           { }
70         virtual ~ImageAbsoluteDifferenceCostFunction( )
71           { }
72
73       private:
74         // Purposely not implemented
75         ImageAbsoluteDifferenceCostFunction( const Self& );
76         void operator=( const Self& );
77       };
78
79     } // ecapseman
80
81   } // ecapseman
82
83 } // ecapseman
84
85 #endif // __FPA__IMAGE__FUNCTORS__IMAGEABSOLUTEDIFFERENCECOSTFUNCTION__H__
86
87 // eof - $RCSfile$