cmtkSplineWarpCongealingFunctional.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: 2736 $
00026 //
00027 //  $LastChangedDate: 2011-01-14 10:37:08 -0800 (Fri, 14 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkSplineWarpCongealingFunctional_h_included_
00034 #define __cmtkSplineWarpCongealingFunctional_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Registration/cmtkCongealingFunctional.h>
00039 
00040 #include <System/cmtkSmartPtr.h>
00041 #include <System/cmtkThreads.h>
00042 #include <System/cmtkMutexLock.h>
00043 #include <System/cmtkThreadSemaphore.h>
00044 
00045 #include <Base/cmtkUniformVolume.h>
00046 #include <Base/cmtkSplineWarpXform.h>
00047 #include <Base/cmtkHistogram.h>
00048 
00049 #include <IO/cmtkClassStream.h>
00050 
00051 #include <vector>
00052 
00053 namespace
00054 cmtk
00055 {
00056 
00059 
00070 class SplineWarpCongealingFunctional : 
00071   public CongealingFunctional<SplineWarpXform>
00072 {
00073 public:
00075   typedef CongealingFunctional<SplineWarpXform> Superclass;
00076 
00078   typedef Superclass::HistogramType HistogramType;
00079 
00081   typedef SplineWarpCongealingFunctional Self;
00082 
00084   typedef SmartPointer<Self> SmartPtr;
00085 
00088   virtual void InitializeXformsFromAffine( const Types::Coordinate gridSpacing, 
00089                                            std::vector<AffineXform::SmartPtr> initialAffineXformsVector, 
00090                                            const bool exactSpacing = true 
00091     );
00092 
00094   virtual void RefineTransformationGrids();
00095 
00097   virtual void SetTemplateGrid( UniformVolume::SmartPtr& templateGrid, const int downsample = 1, const bool useTemplateData = false );
00098     
00100   virtual Self::ReturnType Evaluate();
00101 
00103   virtual Self::ReturnType EvaluateAt( CoordinateVector& v )
00104   {
00105     return this->Superclass::EvaluateAt( v );
00106   }
00107 
00115   virtual Self::ReturnType EvaluateWithGradient( CoordinateVector& v, CoordinateVector& g, const Types::Coordinate step = 1 );
00116 
00117 private:
00119   virtual void UpdateActiveControlPoints();
00120   
00122   virtual void UpdateStandardDeviationByPixel();
00123 
00125   std::vector<double> m_EntropyByPixel;
00126 
00127 #ifdef CMTK_BUILD_MPI
00128 
00129   std::vector<double> m_EntropyByPixelMPI;
00130 #endif
00131 
00133   class EvaluateThreadParameters : 
00135     public ThreadParameters<Self>
00136   {
00137   public:
00139     double m_Entropy;
00140 
00145     unsigned int m_Count;
00146   };
00147   
00149   static void EvaluateThread( void* args, const size_t taskIdx, const size_t taskCnt, const size_t threadIdx, const size_t );
00150 
00152   class EvaluateLocalGradientThreadParameters : 
00154     public ThreadParameters<Self>
00155   {
00156   public:
00158     Types::Coordinate m_Step;
00159 
00161     Types::Coordinate* m_Gradient;
00162 
00163 #ifdef CMTK_BUILD_MPI
00164 
00165     size_t m_FirstIndexToCompute;
00166 
00167 #endif
00168   };
00169 
00171   size_t m_ControlPointIndexNext;
00172 
00174   size_t m_ControlPointIndexLast;
00175   
00177   MutexLock m_ControlPointIndexLock;
00178 
00180   class StaticThreadStorage
00181   {
00182   public:
00184     void Initialize( const Self* This );
00185     
00187     std::vector<Self::ReturnType> m_FPlus;
00188 
00190     std::vector<Self::ReturnType> m_FMinus;
00191 
00193     std::vector<unsigned int> m_CountByParameterPlus;
00194 
00196     std::vector<unsigned int> m_CountByParameterMinus;
00197 
00199     std::vector<SplineWarpXform::SmartPtr> m_Xforms;
00200 
00202     std::vector<Vector3D> m_VectorList;
00203     
00205     std::vector<size_t> m_Count;
00206     
00208     std::vector<HistogramType> m_Histogram;
00209 
00211     bool m_NeedToCopyXformParameters;
00212   };
00213 
00215   std::vector<StaticThreadStorage> m_StaticThreadStorage;
00216 
00222   static CMTK_THREAD_RETURN_TYPE EvaluateLocalGradientThreadFunc( void* args );
00223 
00224 #ifdef CMTK_BUILD_MPI
00225 
00226   typedef enum {
00227     MESSAGE_TAG_COMPUTE = 1,
00228     MESSAGE_TAG_RESULTS = 2,
00229     MESSAGE_TAG_FINISHED = 3
00230   } MessageTagMPI;
00231 
00233   ThreadSemaphore m_ThreadWorkSemaphore;
00234 
00236   ThreadSemaphore m_ThreadReadySemaphore;
00237 
00239   void ReorderGradientComponents( Types::Coordinate *const dst, const Types::Coordinate* src, const size_t fromCpIdx, const size_t toCpIdx );
00240 #endif
00241 };
00242 
00244 
00245 } // namespace cmtk
00246 
00247 #endif // #ifndef __cmtkSplineWarpCongealingFunctional_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines