cmtkGroupwiseRegistrationFunctionalBase.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkGroupwiseRegistrationFunctionalBase_h_included_
00034 #define __cmtkGroupwiseRegistrationFunctionalBase_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkFunctional.h>
00039 #include <Base/cmtkUniformVolume.h>
00040 #include <Base/cmtkXform.h>
00041 
00042 #include <System/cmtkSmartPtr.h>
00043 #include <System/cmtkThreads.h>
00044 
00045 #include <vector>
00046 
00047 #ifdef CMTK_BUILD_MPI
00048 #  include <mpi.h>
00049 #endif
00050 
00051 namespace
00052 cmtk
00053 {
00054 
00057 
00063 class GroupwiseRegistrationFunctionalBase : 
00065   public Functional
00066 {
00067 public:
00069   typedef Functional Superclass;
00070 
00072   typedef GroupwiseRegistrationFunctionalBase Self;
00073 
00075   typedef SmartPointer<Self> SmartPtr;
00076 
00078   class BadXform {};
00079 
00081   GroupwiseRegistrationFunctionalBase();
00082 
00084   virtual ~GroupwiseRegistrationFunctionalBase();
00085 
00091   virtual void SetFreeAndRereadImages( const bool flag = true )
00092   {
00093     this->m_FreeAndRereadImages = flag;
00094   }
00095 
00097   virtual void SetRepeatIntensityHistogramMatching( const bool flag = true )
00098   {
00099     this->m_RepeatIntensityHistogramMatching = flag;
00100     if ( flag )
00101       {
00102       this->SetFreeAndRereadImages( false );
00103       }
00104   }
00105 
00115   virtual void CreateTemplateGridFromTargets( const std::vector<UniformVolume::SmartPtr>& targets, const int downsample = 0 );
00116 
00119   virtual void CreateTemplateGrid( const DataGrid::IndexType& dims , const UniformVolume::CoordinateVectorType& deltas  );
00120 
00123   virtual void SetTemplateGrid( UniformVolume::SmartPtr& templateGrid , 
00124                                 const int downsample = 1 , const bool useTemplateData = false  );
00125 
00128   virtual UniformVolume::SmartPtr& GetTemplateGrid() 
00129   { 
00130     return this->m_TemplateGrid; 
00131   }
00132 
00135   virtual const UniformVolume* GetTemplateGrid() const
00136   {
00137     return this->m_TemplateGrid; 
00138   }
00139 
00143   virtual void SetTargetImages( std::vector<UniformVolume::SmartPtr>& tImages );
00144 
00147   virtual void GetOriginalTargetImages( std::vector<UniformVolume::SmartPtr>& tImages )
00148   {
00149     tImages = this->m_OriginalImageVector;
00150   }  
00151 
00154   virtual std::vector<UniformVolume::SmartPtr>& GetOriginalTargetImages()
00155   {
00156     return this->m_OriginalImageVector;
00157   }  
00158 
00161   virtual size_t GetNumberOfTargetImages() const
00162   {
00163     return this->m_OriginalImageVector.size();
00164   }  
00165 
00168   virtual UniformVolume::SmartPtr GetOriginalTargetImage( const size_t imageIdx )
00169   {
00170     return this->m_OriginalImageVector[imageIdx];
00171   }  
00172 
00175   virtual const UniformVolume* GetOriginalTargetImage( const size_t imageIdx ) const
00176   {
00177     return this->m_OriginalImageVector[imageIdx];
00178   }  
00179 
00183   virtual void SetGaussianSmoothImagesSigma( const Types::Coordinate gaussianSmoothImagesSigma )
00184   {
00185     this->m_GaussianSmoothImagesSigma = gaussianSmoothImagesSigma;
00186   }
00187 
00189   virtual void SetUserBackgroundValue( const byte value = 0 )
00190   {
00191     this->m_UserBackgroundValue = value;
00192     this->m_UserBackgroundFlag = true;
00193   }
00194 
00196   virtual void UnsetUserBackgroundValue()
00197   {
00198     this->m_UserBackgroundFlag = false;
00199   }
00200 
00202   virtual void SetForceZeroSum( const bool forceZeroSum = true )
00203   {
00204     this->m_ForceZeroSum = forceZeroSum;
00205   }
00206 
00208   virtual void SetForceZeroSumFirstN( const size_t forceZeroSumFirstN )
00209   {
00210     this->m_ForceZeroSumFirstN = forceZeroSumFirstN;
00211     this->m_ForceZeroSum = this->m_ForceZeroSum || (this->m_ForceZeroSumFirstN>0);
00212   }
00213 
00218   virtual void SetActiveImagesFromTo( const size_t from, const size_t to )
00219   {
00220     this->m_ActiveImagesFrom = from;
00221     this->m_ActiveImagesTo = to;
00222   }
00223 
00228   virtual void SetActiveXformsFromTo( const size_t from, const size_t to )
00229   {
00230     this->m_ActiveXformsFrom = from;
00231     this->m_ActiveXformsTo = to;
00232   }
00233 
00236   virtual void SetProbabilisticSampleDensity( const float density )
00237   {
00238     this->m_ProbabilisticSampleDensity = density;
00239   }
00240 
00243   virtual void SetProbabilisticSampleUpdatesAfter( const int iterations )
00244   {
00245     this->m_ProbabilisticSampleUpdatesAfter = iterations;
00246     this->m_ProbabilisticSampleUpdatesSince = 0;
00247   }
00248 
00251   void SetXforms( const std::vector<Xform::SmartPtr>& xformVector )
00252   {
00253     this->m_XformVector.resize( xformVector.size() );
00254     for ( size_t i = 0; i < this->m_XformVector.size(); ++i )
00255       {
00256       this->m_XformVector[i] = xformVector[i];
00257       }
00258   }
00259 
00264   virtual const Xform* GetGenericXformByIndex( const size_t idx ) const
00265   {
00266     return this->m_XformVector[idx];
00267   }
00268 
00273   virtual Xform::SmartPtr GetGenericXformByIndex( const size_t idx )
00274   {
00275     return this->m_XformVector[idx];
00276   }
00277 
00282   virtual const Xform* GetGenericActiveXformByIndex( const size_t idx ) const
00283   {
00284     return this->m_XformVector[idx + this->m_ActiveXformsFrom];
00285   }
00286 
00291   virtual Xform::SmartPtr GetGenericActiveXformByIndex( const size_t idx )
00292   {
00293     return this->m_XformVector[idx + this->m_ActiveXformsFrom];
00294   }
00295 
00301   virtual Types::Coordinate GetParamStep( const size_t idx, const Types::Coordinate mmStep = 1 ) const 
00302   {
00303     const size_t xformIdx = idx / this->m_ParametersPerXform;
00304     if ( (xformIdx >= this->m_ActiveXformsFrom) && (xformIdx < this->m_ActiveXformsTo) )
00305       {
00306       return this->m_XformVector[xformIdx]->GetParamStep( idx % this->m_ParametersPerXform, this->m_ImageVector[xformIdx]->Size, mmStep );
00307       }
00308     else
00309       {
00310       return 0.0;
00311       }
00312   }
00313   
00318   virtual size_t ParamVectorDim() const 
00319   { 
00320     return this->m_ParametersPerXform * this->m_XformVector.size(); 
00321   }
00322   
00328   virtual size_t VariableParamVectorDim() const
00329   { 
00330     return this->ParamVectorDim();
00331   }
00332   
00335   virtual void GetParamVector( CoordinateVector& v );
00336 
00339   virtual void SetParamVector( CoordinateVector& v );
00340 
00343   virtual void SetParamVector( CoordinateVector& v, const size_t xformIdx );
00344 
00347   virtual void SetParameter( const size_t param, const Types::Coordinate value );
00348 
00351   virtual void SetParameter( const size_t xform, const size_t param, const Types::Coordinate value );
00352 
00360   virtual Self::ReturnType EvaluateAt ( CoordinateVector& v );
00361 
00369   virtual Self::ReturnType EvaluateWithGradient( CoordinateVector& v, CoordinateVector& g, const Types::Coordinate step = 1 );
00370 
00375   virtual void AllocateStorage();
00376 
00378   void DebugWriteImages();
00379 
00380 protected:
00382   size_t m_NumberOfThreads;
00383 
00385   size_t m_NumberOfTasks;
00386 
00388   bool m_FreeAndRereadImages;
00389 
00391   bool m_ForceZeroSum;
00392   
00394   size_t m_ForceZeroSumFirstN;
00395 
00397   size_t m_ActiveImagesFrom;
00398 
00400   size_t m_ActiveImagesTo;
00401 
00403   size_t m_ActiveXformsFrom;
00404 
00406   size_t m_ActiveXformsTo;
00407 
00409   virtual void ForceZeroSumGradient( CoordinateVector& g ) const;
00410 
00412   size_t m_TemplateNumberOfPixels;
00413 
00415   size_t m_TemplateNumberOfSamples;
00416 
00418   UniformVolume::SmartPtr m_TemplateGrid;
00419 
00421   bool m_UseTemplateData;
00422 
00424   std::vector<byte> m_TemplateData;
00425 
00427   std::vector<UniformVolume::SmartPtr> m_ImageVector;
00428 
00430   std::vector<UniformVolume::SmartPtr> m_OriginalImageVector;
00431 
00433   std::vector<Xform::SmartPtr> m_XformVector;
00434 
00436   float m_ProbabilisticSampleDensity;
00437 
00439   std::vector<size_t> m_ProbabilisticSamples;
00440 
00444   int m_ProbabilisticSampleUpdatesAfter;
00445 
00448   int m_ProbabilisticSampleUpdatesSince;
00449 
00455   virtual void UpdateProbabilisticSamples();
00456 
00462   virtual void InterpolateAllImages();
00463 
00471   virtual void InterpolateImage( const size_t idx, byte* const destination ) { UNUSED(idx); UNUSED(destination); } // cannot make this pure virtual because we need to instantiate for affine initialization
00472 
00474   std::vector<byte*> m_Data;
00475 
00477   std::vector<byte> m_TempData;
00478 
00480   Types::Coordinate m_GaussianSmoothImagesSigma;
00481 
00483   static const byte m_PaddingValue = 255;
00484 
00486   byte m_UserBackgroundValue;
00487   
00489   bool m_UserBackgroundFlag;
00490 
00492   size_t m_ParametersPerXform;
00493 
00495   bool m_RepeatIntensityHistogramMatching;
00496 
00498   virtual bool Wiggle();
00499 
00500 #ifdef CMTK_BUILD_MPI
00501 
00502   int m_RankMPI;
00503 
00505   int m_SizeMPI;
00506 #endif
00507 
00509   virtual UniformVolume::SmartPtr PrepareSingleImage( UniformVolume::SmartPtr& image );
00510 
00512   virtual void PrepareTargetImages();
00513 
00515   virtual const UniformVolume::SmartPtr GetReformattedImage( const UniformVolume::SmartPtr& targetGrid, const size_t idx ) const;
00516 
00517 private:
00519   void CopyTemplateData();
00520 
00522   friend class GroupwiseRegistrationFunctionalAffineInitializer;
00523 };
00524 
00526 
00527 } // namespace cmtk
00528 
00529 #endif // #ifndef __cmtkGroupwiseRegistrationFunctionalBase_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines