ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkKalmanLinearEstimator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkKalmanLinearEstimator_h
00019 #define __itkKalmanLinearEstimator_h
00020 
00021 #include "itkMacro.h"
00022 
00023 #include "vnl/vnl_vector_fixed.h"
00024 #include "vnl/vnl_matrix_fixed.h"
00025 
00026 namespace itk
00027 {
00041 template< class T, unsigned int VEstimatorDimension >
00042 class ITK_EXPORT KalmanLinearEstimator
00043 {
00044 public:
00047   itkStaticConstMacro(Dimension, unsigned int,
00048                       VEstimatorDimension);
00049 
00052   typedef vnl_vector_fixed< T, VEstimatorDimension > VectorType;
00053 
00056   typedef vnl_matrix_fixed< T, VEstimatorDimension, VEstimatorDimension > MatrixType;
00057 
00062   typedef T ValueType;
00063 
00068   void UpdateWithNewMeasure(const ValueType & newMeasure,
00069                             const VectorType & newPredictor);
00070 
00074   void ClearEstimation(void)
00075   { m_Estimator = VectorType( T(0) ); }
00076 
00079   void ClearVariance(void)
00080   {
00081     m_Variance.set_identity();
00082   }
00083 
00090   void SetVariance(const ValueType & var = 1.0)
00091   {
00092     m_Variance.set_identity();
00093     m_Variance *= var;
00094   }
00096 
00102   void SetVariance(const MatrixType & m)
00103   { m_Variance = m; }
00104 
00107   const VectorType & GetEstimator(void) const
00108   { return m_Estimator; }
00109 
00112   const MatrixType & GetVariance(void) const
00113   { return m_Variance; }
00114 private:
00115 
00120   void UpdateVariance(const VectorType  &);
00121 
00124   VectorType m_Estimator;
00125 
00132   MatrixType m_Variance;
00133 };
00134 
00135 template< class T, unsigned int VEstimatorDimension >
00136 void
00137 KalmanLinearEstimator< T, VEstimatorDimension >
00138 ::UpdateWithNewMeasure(const ValueType  & newMeasure,
00139                        const VectorType & newPredictor)
00140 {
00141   ValueType measurePrediction = dot_product(newPredictor, m_Estimator);
00142 
00143   ValueType errorMeasurePrediction = newMeasure - measurePrediction;
00144 
00145   VectorType Corrector = m_Variance * newPredictor;
00146 
00147   for ( unsigned int j = 0; j < VEstimatorDimension; j++ )
00148     {
00149     m_Estimator(j) += Corrector(j) * errorMeasurePrediction;
00150     }
00151 
00152   UpdateVariance(newPredictor);
00153 }
00154 
00155 template< class T, unsigned int VEstimatorDimension >
00156 void
00157 KalmanLinearEstimator< T, VEstimatorDimension >
00158 ::UpdateVariance(const VectorType & newPredictor)
00159 {
00160   VectorType aux =  m_Variance * newPredictor;
00161 
00162   ValueType denominator = 1.0 / ( 1.0 +  dot_product(aux, newPredictor) );
00163 
00164   for ( unsigned int col = 0; col < VEstimatorDimension; col++ )
00165     {
00166     for ( unsigned int row = 0; row < VEstimatorDimension; row++ )
00167       {
00168       m_Variance(col, row) -= aux(col) * aux(row) * denominator;
00169       }
00170     }
00171 }
00172 } // end namespace itk
00173 
00174 #endif
00175