]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/IterativeGaussianModelEstimator.h
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / IterativeGaussianModelEstimator.h
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__H__
6 #define __CPEXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__H__
7
8 #include <vector>
9 #include <itkConceptChecking.h>
10 #include <itkObject.h>
11 #include <itkObjectFactory.h>
12
13 #include <itkCovariantVector.h>
14 #include <itkMatrix.h>
15 #include <itkPoint.h>
16 #include <itkVector.h>
17
18 namespace cpExtensions
19 {
20   namespace Algorithms
21   {
22     /**
23      * Recursive formulation taken from:
24      * http://lmb.informatik.uni-freiburg.de/lectures/mustererkennung/Englische_Folien/07_c_ME_en.pdf
25      */
26     template< class _TScalar, unsigned int _VDimension >
27     class IterativeGaussianModelEstimator
28       : public itk::Object
29     {
30     public:
31       typedef IterativeGaussianModelEstimator Self;
32       typedef itk::Object                     Superclass;
33       typedef itk::SmartPointer< Self >       Pointer;
34       typedef itk::SmartPointer< const Self > ConstPointer;
35
36       typedef _TScalar TScalar;
37       itkStaticConstMacro( Dimension, unsigned int, _VDimension );
38
39       // Begin concept checking
40 #ifdef ITK_USE_CONCEPT_CHECKING
41       itkConceptMacro(
42         ScalarTypeHasFloatResolution,
43         ( itk::Concept::IsFloatingPoint< _TScalar > )
44         );
45 #endif
46       // End concept checking
47
48       typedef itk::Matrix< TScalar, Dimension, Dimension > TMatrix;
49       typedef itk::Vector< TScalar, Dimension > TVector;
50
51     public:
52       itkNewMacro( Self );
53       itkTypeMacro( IterativeGaussianModelEstimator, itkObject );
54
55       itkGetConstMacro( Mean, TVector );
56       itkGetConstMacro( Covariance, TMatrix );
57       itkGetConstMacro( UnbiasedCovariance, TMatrix );
58       itkGetConstMacro( NumberOfSamples, unsigned long );
59
60       itkSetMacro( Mean, TVector );
61       itkSetMacro( Covariance, TMatrix );
62       itkSetMacro( NumberOfSamples, unsigned long );
63
64     public:
65       void Clear( );
66
67       template< class _TOtherScalar >
68       void AddSample( const _TOtherScalar& x1, ... );
69
70       template< class _TOtherScalar >
71       void AddSample( const _TOtherScalar* array );
72
73       template< class _TOtherScalar >
74       void AddSample( const vnl_vector< _TOtherScalar >& v );
75
76       template< class _TOtherScalar >
77       void AddSample(
78         const itk::CovariantVector< _TOtherScalar, _VDimension >& v
79         );
80
81       template< class _TOtherScalar >
82       void AddSample( const itk::Point< _TOtherScalar, _VDimension >& p );
83
84       template< class _TOtherScalar >
85       void AddSample( const itk::Vector< _TOtherScalar, _VDimension >& v );
86
87       template< class _TOtherScalar >
88       _TScalar SquaredMahalanobis( const _TOtherScalar& x1, ... ) const;
89
90       template< class _TOtherScalar >
91       _TScalar SquaredMahalanobis( const _TOtherScalar* array ) const;
92
93       template< class _TOtherScalar >
94       _TScalar SquaredMahalanobis(
95         const vnl_vector< _TOtherScalar >& v
96         ) const;
97
98       template< class _TOtherScalar >
99       _TScalar SquaredMahalanobis(
100         const itk::CovariantVector< _TOtherScalar, _VDimension >& v
101         ) const;
102
103       template< class _TOtherScalar >
104       _TScalar SquaredMahalanobis(
105         const itk::Point< _TOtherScalar, _VDimension >& p
106         ) const;
107
108       template< class _TOtherScalar >
109       _TScalar SquaredMahalanobis(
110         const itk::Vector< _TOtherScalar, _VDimension >& v
111         ) const;
112
113       template< class _TOtherScalar >
114       _TScalar Density( const _TOtherScalar& x1, ... ) const;
115
116       template< class _TOtherScalar >
117       _TScalar Density( const _TOtherScalar* array ) const;
118
119       template< class _TOtherScalar >
120       _TScalar Density(
121         const vnl_vector< _TOtherScalar >& v
122         ) const;
123
124       template< class _TOtherScalar >
125       _TScalar Density(
126         const itk::CovariantVector< _TOtherScalar, _VDimension >& v
127         ) const;
128
129       template< class _TOtherScalar >
130       _TScalar Density(
131         const itk::Point< _TOtherScalar, _VDimension >& p
132         ) const;
133
134       template< class _TOtherScalar >
135       _TScalar Density(
136         const itk::Vector< _TOtherScalar, _VDimension >& v
137         ) const;
138
139     protected:
140       IterativeGaussianModelEstimator( );
141       virtual ~IterativeGaussianModelEstimator( );
142
143     protected:
144       void _AddSample( const TVector& v ) const;
145       _TScalar _SquaredMahalanobis( const TVector& v ) const;
146       _TScalar _Density( const TVector& v ) const;
147
148     private:
149       // Purposely not implemented
150       IterativeGaussianModelEstimator( const Self& other );
151       void operator=( const Self& other );
152
153     protected:
154       // Recursive avg/cov values
155       mutable unsigned long m_NumberOfSamples;
156       mutable TVector m_Mean;
157       mutable TMatrix m_Covariance;
158       mutable TMatrix m_UnbiasedCovariance;
159       mutable bool m_InverseUpdated;
160       mutable TMatrix m_InverseUnbiasedCovariance;
161     };
162
163   } // ecapseman
164
165 } // ecapseman
166
167 #include <cpExtensions/Algorithms/IterativeGaussianModelEstimator.hxx>
168
169 #endif // __CPEXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__H__
170
171 // eof - $RCSfile$