00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __freTranslationTransformController_h
00023 #define __freTranslationTransformController_h
00024
00025 #include "freITKTransformControllerBase.h"
00026 #include "freTransformInitializer.h"
00027
00028 #include "freIntensityImageMediaController.h"
00029
00030 #include "itkTranslationTransform.h"
00031
00032 namespace FREE
00033 {
00034
00044 freControllerIDMacro(TranslationTransformControllerBase, "TranslationTransformBase");
00045 template <int VDimension>
00046 class TranslationTransformControllerBase : public ITKTransformControllerBase< itk::TranslationTransform < ScalarType, VDimension >, VDimension >
00047 {
00048 public:
00049 typedef itk::TranslationTransform < ScalarType, VDimension > ComponentType;
00050 typedef ITKTransformControllerBase<ComponentType, VDimension> Superclass;
00051
00052 itkTypeMacro(TranslationTransformControllerBase, ITKTransformControllerBase);
00053
00054 DeclareParameterMacro(TranslationInitializationType);
00055
00056 protected:
00059 TranslationTransformControllerBase()
00060 {
00061
00062 this->UpdateControllerID(ControllerID::TranslationTransformControllerBase);
00063 this->m_Description = "A rigid translation transform.";
00064 };
00065
00066 virtual void GenerateProfile(CtrlProfile::ControllerProfile& profile,
00067 const SessionComponentCache* pComponentCache,
00068 bool bRegardOldSetup) const
00069 {
00070 Superclass::GenerateProfile(profile,pComponentCache,bRegardOldSetup);
00071
00072
00073 profile.Parameters().AddParameter(cParam_TranslationInitializationType,Parameter::PVTInteger,cParamDsc_TranslationInitializationType,1,"0");
00074 profile.Parameters().AddParameter(cParam_InitialisationMethod,Parameter::PVTInteger,std::string(cParamDsc_InitialisationMethod)+"\n1: translation is set by itv",1,"0");
00075
00076
00077 if (VDimension==2)
00078 {
00079 profile.MediaMap().AddMedia("movingInitialImage",ControllerID::IntensityImage2DMediaController,DASet,VDimension);
00080 profile.MediaMap().AddMedia("fixedInitialImage",ControllerID::IntensityImage2DMediaController,DASet,VDimension);
00081 }
00082 else
00083 {
00084 profile.MediaMap().AddMedia("movingInitialImage",ControllerID::IntensityImage3DMediaController,DASet,VDimension);
00085 profile.MediaMap().AddMedia("fixedInitialImage",ControllerID::IntensityImage3DMediaController,DASet,VDimension);
00086 }
00087 };
00088
00089 virtual bool SetInitialTransformValues( ComponentType* pTransformComponent,
00090 SessionComponentCache* pComponentCache,
00091 SessionInfo* pSessionInfo,
00092 const int& iInitializeByITV) const
00093 {
00094 if (!iInitializeByITV)
00095 {
00096 ParameterArrayType translation(VDimension);
00097
00098 int iTransInitType;
00099
00100 if (!pComponentCache->SetupIsAssigned()) throwCtrlExceptionMacro("","Cannot initialize transform values, transform setup is not assigned in cache.");
00101 ComponentSetup* pTransformSetup = pComponentCache->Setup();
00102
00103 if (!pTransformSetup->Parameters().GetParameterValue(cParam_TranslationInitializationType,iTransInitType)) throwExceptionMacro("Missing parameter: " << cParam_TranslationInitializationType);
00104
00105 if (iTransInitType)
00106 {
00107 ParameterArrayType fixedCenter(VDimension);
00108 ParameterArrayType movingCenter(VDimension);
00109
00110 CenterInitializer<PixelType,VDimension> centerInitializer;
00111
00112 if (iTransInitType==1) centerInitializer.GeometryOn();
00113 else centerInitializer.MomentsOn();
00114
00115 typedef typename ImageTypes<VDimension>::InternalImageType InternalImageType;
00116
00117 SessionComponentCache::GenericComponentType* pGenericInitialFixedImage = DirectSessionComponentAccessor::GetRepositoryElement("fixedInitialImage",pComponentCache);
00118 SessionComponentCache::GenericComponentType* pGenericInitialMovingImage = DirectSessionComponentAccessor::GetRepositoryElement("movingInitialImage",pComponentCache);
00119 if (!pGenericInitialFixedImage) throwCtrlExceptionMacro("","Cannot set fixed center via initial image. No initial fixed image linked. Please check media linkage.");
00120 if (!pGenericInitialMovingImage) throwCtrlExceptionMacro("","Cannot set moving center via initial image. No initial fixed image linked. Please check media linkage.");
00121
00122 InternalImageType* pInitialFixedImage = 0;
00123 InternalImageType* pInitialMovingImage = 0;
00124
00125 try
00126 {
00127 pInitialFixedImage = dynamic_cast<InternalImageType*>(pGenericInitialFixedImage);
00128 }
00129 catchAllNPassMacro("Error while casting initial fixed image");
00130 if (!pInitialFixedImage) throwCtrlExceptionMacro("","Error while casting initial fixed image.");
00131
00132 try
00133 {
00134 pInitialMovingImage = dynamic_cast<InternalImageType*>(pGenericInitialMovingImage);
00135 }
00136 catchAllNPassMacro("Error while casting initial fixed image");
00137 if (!pInitialMovingImage) throwCtrlExceptionMacro("","Error while casting initial fixed image.");
00138
00139 centerInitializer.SetImage(pInitialFixedImage);
00140 centerInitializer.GetCenter(fixedCenter);
00141
00142 centerInitializer.SetImage(pInitialMovingImage);
00143 centerInitializer.GetCenter(movingCenter);
00144
00145 for (int iIndex=0; iIndex<VDimension; iIndex++)
00146 {
00147 translation[iIndex] = movingCenter[iIndex]-fixedCenter[iIndex];
00148 };
00149 }
00150 else
00151 {
00152 ReferencePointsAccessor<VDimension> pointsAccessor(pComponentCache);
00153
00154 if (!pointsAccessor.MovingPointsExist()) throwCtrlExceptionMacro("","Cannot estimate translation via reference points. No moving reference point set defined. Please check media linkage.");
00155 if (!pointsAccessor.FixedPointsExist()) throwCtrlExceptionMacro("","Cannot estimate translation via via reference points. No fixed reference point set defined. Please check media linkage.");
00156
00157 translation.fill(0);
00158 for (int iIndex = 0; iIndex<pointsAccessor.GetFixedPointsCount(); iIndex++)
00159 {
00160 ParameterArrayType movingRef = pointsAccessor.GetMovingPointArray(iIndex);
00161 ParameterArrayType fixedRef = pointsAccessor.GetFixedPointArray(iIndex);
00162
00163 for (int iIndex=0; iIndex<VDimension; iIndex++)
00164 {
00165 translation[iIndex] += movingRef[iIndex] - fixedRef[iIndex];
00166 };
00167 };
00168
00169 int iCoupleCount = pointsAccessor.GetFixedPointsCount();
00170 if (iCoupleCount<1) iCoupleCount = 1;
00171 for (int iIndex=0; iIndex<VDimension; iIndex++)
00172 {
00173 translation[iIndex] = translation[iIndex]/iCoupleCount;
00174 };
00175 }
00176
00177 for (int iIndex=0; iIndex<pTransformSetup->Parameters().ParameterSize(cParam_CurrentTransformValues); iIndex++)
00178 {
00179 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,translation[iIndex],iIndex);
00180 };
00181 };
00182 return true;
00183 };
00184
00185 void SetMediaCasted(const MediaID& mediaID, GenericMediaType* pMedia, ComponentType* pComponent, SessionComponentCache* pComponentCache,
00186 SessionInfo* pSessionInfo) const
00187 {
00188 if (mediaID == "movingInitialImage")
00189 {
00190 DirectSessionComponentAccessor::SetRepositoryElement("movingInitialImage",pMedia,pComponentCache);
00191 }
00192 else if (mediaID == "fixedInitialImage")
00193 {
00194 DirectSessionComponentAccessor::SetRepositoryElement("fixedInitialImage",pMedia,pComponentCache);
00195 }
00196 else Superclass::SetMediaCasted(mediaID, pMedia, pComponent, pComponentCache, pSessionInfo);
00197 };
00198
00199 };
00200
00201 template <int VDimension>
00202 const char* const TranslationTransformControllerBase<VDimension>::
00203 cParam_TranslationInitializationType = "TranslationInitializationType";
00204
00205 template <int VDimension>
00206 const char* const TranslationTransformControllerBase<VDimension>::
00207 cParamDsc_TranslationInitializationType = "Type of translation computation.\n0: Translation will be estimated by the translation vectors of all reference pairs.\n1: Use geometrical image centers for the translation calculation\n2: Use centers of mass for translation calculation.";
00208
00209
00219 freControllerIDMacro(Translation2DTransformController, "Translation 2D Transform");
00220 class Translation2DTransformController : public TranslationTransformControllerBase<2>
00221 {
00222 public:
00223 itkTypeMacro(Translation2DTransformController, TranslationTransformControllerBase);
00224
00225 Translation2DTransformController();
00226
00227 protected:
00228 virtual void GenerateProfile(CtrlProfile::ControllerProfile& profile,
00229 const SessionComponentCache* pComponentCache,
00230 bool bRegardOldSetup) const;
00231 };
00232
00242 freControllerIDMacro(Translation3DTransformController, "Translation 3D Transform");
00243 class Translation3DTransformController : public TranslationTransformControllerBase<3>
00244 {
00245 public:
00246 itkTypeMacro(Translation3DTransformController, TranslationTransformControllerBase);
00247
00248 Translation3DTransformController();
00249
00250 protected:
00251 virtual void GenerateProfile(CtrlProfile::ControllerProfile& profile,
00252 const SessionComponentCache* pComponentCache,
00253 bool bRegardOldSetup) const;
00254 };
00255
00256 }
00257
00258 #endif