00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __frePointSetRegistrationProcessor_txx
00023 #define __frePointSetRegistrationProcessor_txx
00024
00025 #include "frePointSetRegistrationProcessor.h"
00026 #include "freOptimizerControllerBase.h"
00027 #include "freTransformSetupAdaptor.h"
00028
00029 namespace FREE
00030 {
00031
00035
00036
00037 template <unsigned int VImageDimension>
00038 const long
00039 PointSetRegistrationProcessor<VImageDimension>::
00040 GetMaxIterationCount() const
00041 {
00042 if (this->m_pComponentCache)
00043 {
00044 SessionComponentCache* pCache = this->m_pComponentCache->SubCaches().GetElement(cComp_MainOptimizer);
00045 if(pCache)
00046 {
00047 const GenericOptimizerController* pOptCtrl = dynamic_cast<const GenericOptimizerController*>(pCache->Controller());
00048 if (!pOptCtrl) throwExceptionMacro("Error. Cannot get max iteration count. Component of main optimizer cache is not a descendant of OptimizerControllerInterface.");
00049 return pOptCtrl->GetMaxIterationCount(pCache,this->m_iResolutionLevel);
00050 }
00051 }
00052 return -1;
00053 };
00054
00055 template <unsigned int VImageDimension>
00056 void
00057 PointSetRegistrationProcessor<VImageDimension>::
00058 ComputeRegistration()
00059 {
00060 Superclass::ComputeRegistration();
00061 if (this->m_Transform.IsNull())
00062 throwExceptionMacro("Cannot start registration; no transformation component available.");
00063 if (this->m_Metric.IsNull())
00064 throwExceptionMacro("Cannot start registration; no metric component available.");
00065 if (this->m_Optimizer.IsNull())
00066 throwExceptionMacro("Cannot start registration; no optimizer component available.");
00067
00068 if (this->m_FixedPointSet.IsNull())
00069 throwExceptionMacro("Cannot start registration; no fixed point set available.");
00070 if (this->m_MovingPointSet.IsNull())
00071 throwExceptionMacro("Cannot start registration; no moving point set available.");
00072
00073 if (!this->m_pComponentCache)
00074 throwExceptionMacro("Cannot start registration; no processor cache available. Ensure that the registration is initialized properly.");
00075
00076 this->m_pOptimizerCache = this->m_pComponentCache->GetCacheByIDPath(cIDPSelf+IDPath(cComp_MainOptimizer));
00077 if (!this->m_pOptimizerCache)
00078 throwExceptionMacro("Cannot start registration; no optimizer cache available. Ensure that the registration is initialized properly.");
00079
00080 this->m_lCurIteration = 0;
00081
00082 long optimizerObserverID = 0;
00083
00084 try
00085 {
00086
00087 if(this->fnOnProgress.IsNotNull())
00088 this->fnOnProgress->Execute(RSTInitProcessor,"Connecting registration components",this);
00089
00090 this->m_Registration->SetMetric(this->m_Metric);
00091 this->m_Registration->SetOptimizer(this->m_Optimizer);
00092 this->m_Registration->SetTransform(this->m_Transform);
00093
00094
00095 if(this->fnOnProgress.IsNotNull())
00096 this->fnOnProgress->Execute(RSTInitProcessor, "Setting initial transform parameters and setup values of components",this);
00097
00098 ComponentSetup* pTransformSetup = this->GetComponentSetup()->Components().GetElement(cComp_MainTransform);
00099 if (!pTransformSetup)
00100 throwExceptionMacro("Component setup of MainTransform does not exist.");
00101
00102 TransformSetupAdaptor transformAdaptor(pTransformSetup);
00103 this->m_Registration->SetInitialTransformParameters(transformAdaptor.GetCurrentTransformParameters());
00104
00105
00106 if(this->fnOnProgress.IsNotNull())
00107 this->fnOnProgress->Execute(RSTInitProcessor, "Connecting Point sets",this);
00108 this->m_Registration->SetFixedPointSet(this->m_FixedPointSet);
00109 this->m_Registration->SetMovingPointSet(this->m_MovingPointSet);
00110
00111
00112 std::string sParams = "";
00113 for ( int iIndex = 0; iIndex < transformAdaptor.GetCurrentTransformParameters().Size(); iIndex++)
00114 {
00115 sParams =sParams+"; "+FREE::Convert::ToStr( transformAdaptor.GetCurrentTransformParameters().GetElement(iIndex),5);
00116 }
00117 if(this->fnOnProgress.IsNotNull())
00118 this->fnOnProgress->Execute(RSTInitProcessor, "Initial transform parameters: "+sParams,this);
00119
00120
00121 this->m_IterationObserver->fnOnNotify = this->m_IterationNotificationEvent;
00122 optimizerObserverID = this->m_Optimizer->AddObserver(itk::IterationEvent(), this->m_IterationObserver);
00123
00124
00125
00126 }
00127 catchAllNPassMacro("Unknown Error while preparing the registration.");
00128
00129 try
00130 {
00131 if(this->fnOnProgress.IsNotNull())
00132 this->fnOnProgress->Execute(RSTProcessing,"",this);
00133
00134
00135
00136 if(this->fnOnNextLevel.IsNotNull())
00137 this->fnOnNextLevel->Execute(this->m_iResolutionLevel,this);
00138
00139 this->m_Registration->StartRegistration();
00140
00141 this->m_Optimizer->RemoveObserver(optimizerObserverID);
00142 }
00143 catchAllNPassMacro("Unknown Error while itk processing the registration.");
00144
00145
00146 TransformSetupAdaptor finalTransformAdaptor(this->GetComponentSetup()->Components().GetElement(cComp_MainTransform));
00147 finalTransformAdaptor.SetCurrentTransformParameters(this->m_Registration->GetLastTransformParameters());
00148
00149 this->m_CurrentTransformParameters = this->m_Registration->GetLastTransformParameters();
00150 };
00151
00152 template <unsigned int VImageDimension>
00153 PointSetRegistrationProcessor<VImageDimension>::
00154 PointSetRegistrationProcessor()
00155 {
00156 this->m_Metric = NULL;
00157 this->m_Optimizer = NULL;
00158 this->m_pOptimizerCache = NULL;
00159
00160 this->m_Registration = RegistrationType::New();
00161
00162 this->m_IterationObserver = IterationObserver::New();
00163 this->m_IterationNotificationEvent = NotificationEvent<Self>::New(this, &Self::OnNewIteration);
00164 }
00165
00166 template <unsigned int VImageDimension>
00167 PointSetRegistrationProcessor<VImageDimension>::
00168 ~PointSetRegistrationProcessor()
00169 {};
00170
00171 template <unsigned int VImageDimension>
00172 typename PointSetRegistrationProcessor<VImageDimension>::TransformFieldPointer
00173 PointSetRegistrationProcessor<VImageDimension>::
00174 ComputeTransformationField(const PointType& origin, const RegionType& region, const SpacingType& spacing)
00175 {
00176 return TransformInterface::ComputeTransformationField(origin,region,spacing);
00177 };
00178
00179 template <unsigned int VImageDimension>
00180 bool
00181 PointSetRegistrationProcessor<VImageDimension>::
00182 TransformationIsValid () const
00183 {
00184 return this->m_bRegistrationStarted;
00185 };
00186
00187 template <unsigned int VImageDimension>
00188 void
00189 PointSetRegistrationProcessor<VImageDimension>::
00190 NotifyProgress(const RegistrationStatusType status, const std::string& sComment)
00191 {
00192 if(this->fnOnProgress.IsNotNull())
00193 this->fnOnProgress->Execute(status, sComment,this);
00194 };
00195
00196 template <unsigned int VImageDimension>
00197 typename PointSetRegistrationProcessor<VImageDimension>::TransformFunctionMediaPointer
00198 PointSetRegistrationProcessor<VImageDimension>::
00199 GetTransformationFunction()
00200 {
00201 PointType origin;
00202 origin.Fill(0.0);
00203
00204 RegionType region;
00205 typename RegionType::IndexType index;
00206 index.Fill(0);
00207 typename RegionType::SizeType size;
00208 size.Fill(0);
00209 region.SetIndex(index);
00210 region.SetSize(size);
00211
00212 SpacingType spacing;
00213 spacing.Fill(1);
00214
00215 return GetTransformationFunction(origin,region,spacing);
00216 };
00217
00218 template <unsigned int VImageDimension>
00219 typename PointSetRegistrationProcessor<VImageDimension>::TransformFunctionMediaPointer
00220 PointSetRegistrationProcessor<VImageDimension>::
00221 GetInverseTransformationFunction()
00222 {
00223 PointType origin;
00224 origin.Fill(0.0);
00225
00226 RegionType region;
00227 typename RegionType::IndexType index;
00228 index.Fill(0);
00229 typename RegionType::SizeType size;
00230 size.Fill(0);
00231 region.SetIndex(index);
00232 region.SetSize(size);
00233
00234 SpacingType spacing;
00235 spacing.Fill(1);
00236
00237 return GetInverseTransformationFunction(origin,region,spacing);
00238 };
00239
00240 template <unsigned int VImageDimension>
00241 void
00242 PointSetRegistrationProcessor<VImageDimension>::
00243 ReleaseInputs()
00244 {
00245 Superclass::ReleaseInputs();
00246 this->m_FixedPointSet = NULL;
00247 this->m_MovingPointSet = NULL;
00248 };
00249
00250 template <unsigned int VImageDimension>
00251 void
00252 PointSetRegistrationProcessor<VImageDimension>::
00253 OnNewIteration(void* pSender, long threadID)
00254 {
00255 if(this->fnOnNextIteration.IsNotNull())
00256 this->fnOnNextIteration->Execute(this->m_lCurIteration, NULL, this, 0);
00257 this->m_lCurIteration++;
00258
00259
00260
00261
00262
00263
00264 const GenericOptimizerController* pOptimizerCtrl = dynamic_cast<const GenericOptimizerController*>(this->m_pOptimizerCache->Controller());
00265 if (!pOptimizerCtrl) throwExceptionMacro("Error. Cannot get current position. Component of main optimizer cache is not a descendant of OptimizerControllerInterface.");
00266 this->m_CurrentTransformParameters = pOptimizerCtrl->GetCurrentPosition(this->m_pOptimizerCache);
00267 };
00268
00269 }
00270
00271 #endif