]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Functors/ImageCostFunction.h
...
[FrontAlgorithms.git] / lib / fpa / Image / Functors / ImageCostFunction.h
1 #ifndef __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__
2 #define __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__
3
4 #include <limits>
5 #include <itkNumericTraits.h>
6 #include <itkObject.h>
7
8 namespace fpa
9 {
10   namespace Image
11   {
12     namespace Functors
13     {
14       /**
15        */
16       template< class I, class R >
17       class ImageCostFunction
18         : public itk::Object
19       {
20       public:
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;
26
27         typedef I TInputImage;
28         typedef R TResult;
29         typedef typename I::IndexType TIndex;
30
31       public:
32         itkNewMacro( Self );
33         itkTypeMacro( ImageCostFunction, itkObject );
34
35         itkGetConstObjectMacro( InputImage, I );
36         itkSetConstObjectMacro( InputImage, I );
37
38       public:
39         virtual R Evaluate( const TIndex& v, const TIndex& p ) const
40           {
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;
45
46             if( this->m_InputImage.IsNotNull( ) )
47             {
48               _TPixel pix = this->m_InputImage->GetPixel( v );
49               if( typeid( _TPixel ) != typeid( _TValue ) )
50               {
51                 _TVector* array = reinterpret_cast< _TVector* >( &pix );
52                 unsigned int n =
53                   this->m_InputImage->GetNumberOfComponentsPerPixel( );
54                 R sum = R( 0 );
55                 for( unsigned int i = 0; i < n; ++i )
56                   sum += R( ( *array )[ i ] );
57                 return( sum / R( n ) );
58               }
59               else
60                 return( R( *( reinterpret_cast< _TValue* >( &pix ) ) ) );
61             }
62             else
63               return( std::numeric_limits< R >::max( ) );
64           }
65
66       protected:
67         ImageCostFunction( )
68           : Superclass( )
69           { }
70         virtual ~ImageCostFunction( )
71           { }
72
73       private:
74         // Purposely not implemented
75         ImageCostFunction( const Self& );
76         void operator=( const Self& );
77
78       protected:
79         typename I::ConstPointer m_InputImage;
80       };
81
82     } // ecapseman
83
84   } // ecapseman
85
86 } // ecapseman
87
88 #endif // __FPA__IMAGE__FUNCTORS__IMAGECOSTFUNCTION__H__
89
90 // eof - $RCSfile$