00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "freVersorRigid3DTransformController.h"
00024
00025 #include "freIntensityImageMediaController.h"
00026 #include "freTransformSetupAdaptor.h"
00027
00028 #include "itkCenteredTransformInitializer.h"
00029 #include "itkLandmarkBasedTransformInitializer.h"
00030
00031 namespace FREE
00032 {
00033
00037 DefineParameterMacro(VersorRigid3DTransformController,CenterInitializationType,"CenterInitializationType","Type of rotation center computation and translation initialisation, if not set via ITV.\n0: Use the center specified via the parameter 'Center' (translation is zero).\n1: use geometrical center for calculation\n2: use center of mass of images for calculation.\n3: center and translation is computet via LandmarkBasedInitializer using the initial point sets.\n4: center and translation are defined via the first point in the initial point sets.");
00038 DefineParameterMacro(VersorRigid3DTransformController,Center,"Center","Coordinates of the center used as rotation center if CenterInitializationType = 0.");
00039
00040 VersorRigid3DTransformController::
00041 VersorRigid3DTransformController()
00042 {
00043 this->UpdateControllerID(ControllerID::VersorRigid3DTransformController);
00044 this->m_Description = "A versor based rotations and translation in 3 dimensional space.";
00045 };
00046
00047 void
00048 VersorRigid3DTransformController::
00049 GenerateProfile(CtrlProfile::ControllerProfile& profile,
00050 const SessionComponentCache* pComponentCache,
00051 bool bRegardOldSetup) const
00052 {
00053 Superclass::GenerateProfile(profile,pComponentCache,bRegardOldSetup);
00054
00055
00056 profile.Parameters().AddParameter(cParam_CenterInitializationType,Parameter::PVTInteger,cParamDsc_CenterInitializationType,1,"0");
00057 profile.Parameters().AddParameter(cParam_Center,Parameter::PVTInteger,cParamDsc_Center,3,"0");
00058 profile.Parameters().AddParameter(cParam_InitialisationMethod,Parameter::PVTInteger,std::string(cParamDsc_InitialisationMethod)+"\n1: rotation (versors) is set by itv\n2: translation is set by itv\n3: versors and translation are set by itv.",1,"0");
00059 profile.Parameters().AddParameter(cParam_InitialTransformValues,CtrlProfile::Parameter::PVTDouble,"1..3: versor\n4..6: translation vector (x|y|z)",6,"0");
00060 profile.Parameters().AddParameter(cParam_CurrentTransformValues,CtrlProfile::Parameter::PVTDouble,"1..3: versor\n4..6: translation vector (x|y|z)",6,"0");
00061 profile.Parameters().AddParameter(cParam_TransformScale,CtrlProfile::Parameter::PVTDouble,"1..3: versor\n4..6: translation vector (x|y|z)",6,"1",-1,true);
00062
00063
00064 profile.MediaMap().AddMedia("movingInitialImage",ControllerID::IntensityImage3DMediaController,DASet,3);
00065 profile.MediaMap().AddMedia("fixedInitialImage",ControllerID::IntensityImage3DMediaController,DASet,3);
00066
00067
00068
00069 CtrlProfile::ProfileOption* pOption = profile.Requirements().AddRequirement(std::string("../")+cComp_MainOptimizer)->AddProfileOption();
00070 pOption->Inheritance().AddAncestor("VersorRigid3DTransformOptimizerBase");
00071 pOption->CheckForInheritance();
00072 };
00073
00074 bool
00075 VersorRigid3DTransformController::
00076 SetInitialTransformValues( ComponentType* pTransformComponent,
00077 SessionComponentCache* pComponentCache,
00078 SessionInfo* pSessionInfo,
00079 const int& iInitializeByITV) const
00080 {
00081 int iCenterType;
00082
00083 if (!pComponentCache->SetupIsAssigned()) throwCtrlExceptionMacro("","Cannot initialize transform values, transform setup is not assigned in cache.");
00084 ComponentSetup* pTransformSetup = pComponentCache->Setup();
00085
00086 try
00087 {
00088 SessionAccessor::GetParameterValue(pComponentCache,cParam_CenterInitializationType,iCenterType);
00089 }
00090 catchAllNPassMacro("Error while retrieving parameter values.");
00091
00092 TransformSetupAdaptor adaptor(pTransformSetup);
00093
00094 ParameterArrayType itv = adaptor.GetInitialTransformParameters();
00095 ParameterArrayType fixedCenter(3);
00096 ParameterArrayType movingCenter(3);
00097
00098 ReferencePointsAccessor<3> pointsAccessor(pComponentCache);
00099
00100 typedef ImageTypes<3>::InternalImageType InternalImageType;
00101
00102 pTransformComponent->SetIdentity();
00103
00104 if ((1!=(iInitializeByITV&1))||((2!=(iInitializeByITV&2))&&(iCenterType==3)))
00105 {
00106 typedef itk::LandmarkBasedTransformInitializer<ComponentType, InternalImageType, InternalImageType> LandmarkInitializerType;
00107 LandmarkInitializerType::Pointer lmInitializer = LandmarkInitializerType::New();
00108
00109 if (!pointsAccessor.MovingPointsExist()) throwCtrlExceptionMacro("","Cannot compute rotation via reference points. No moving reference point set defined. Please check media linkage.");
00110 if (!pointsAccessor.FixedPointsExist()) throwCtrlExceptionMacro("","Cannot compute rotation via reference points. No fixed reference point set defined. Please check media linkage.");
00111
00112 lmInitializer->SetFixedLandmarks(pointsAccessor.GetFixedPoints()->GetPoints()->CastToSTLContainer());
00113 lmInitializer->SetMovingLandmarks(pointsAccessor.GetMovingPoints()->GetPoints()->CastToSTLContainer());
00114 lmInitializer->SetTransform(pTransformComponent);
00115 lmInitializer->InitializeTransform();
00116 }
00117
00118 if (1==(iInitializeByITV&1))
00119 {
00120 for (int iIndex=0; iIndex<3; iIndex++)
00121 {
00122 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,itv[iIndex],iIndex);
00123 };
00124 }
00125 else
00126 {
00127 for (int iIndex=0; iIndex<3; iIndex++)
00128 {
00129 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,pTransformComponent->GetParameters().GetElement(iIndex),iIndex);
00130 };
00131 }
00132
00133 if (2==(iInitializeByITV&2))
00134 {
00135 for (int iIndex=0; iIndex<3; iIndex++)
00136 {
00137 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,itv[iIndex+3],iIndex+3);
00138 };
00139 }
00140 else
00141 {
00142 if (iCenterType==0)
00143 {
00144 try
00145 {
00146 double dCenterValue;
00147 ImageTypes<3>::PointType centerPoint;
00148
00149 for (int iIndex=0; iIndex<3; iIndex++)
00150 {
00151 SessionAccessor::GetParameterValue(pComponentCache,cParam_Center,dCenterValue,iIndex);
00152 centerPoint[iIndex] = dCenterValue;
00153 }
00154 pTransformComponent->SetCenter(centerPoint);
00155 }
00156 catchAllNPassMacro("Error while retrieving center point parameter values.");
00157 }
00158 else if ((iCenterType==1)||(iCenterType==2))
00159 {
00160 typedef itk::CenteredTransformInitializer< ComponentType, InternalImageType, InternalImageType> TransformInitializerType;
00161 TransformInitializerType::Pointer initializer = TransformInitializerType::New();
00162
00163 SessionAccessor::GenericMediaPointer smpGenericInitialFixedImage = SessionAccessor::GetLinkedMedia("fixedInitialImage",pComponentCache,pSessionInfo);
00164 if (smpGenericInitialFixedImage.IsNull()) throwCtrlExceptionMacro("","Cannot set fixed center via initial image. No initial fixed image linked. Please check media linkage.");
00165 SessionAccessor::GenericMediaPointer smpGenericInitialMovingImage = SessionAccessor::GetLinkedMedia("movingInitialImage",pComponentCache,pSessionInfo);
00166 if (smpGenericInitialMovingImage.IsNull()) throwCtrlExceptionMacro("","Cannot set moving center via initial image. No initial fixed image linked. Please check media linkage.");
00167
00168 InternalImageType* pInitialFixedImage = 0;
00169 InternalImageType* pInitialMovingImage = 0;
00170
00171 try
00172 {
00173 pInitialFixedImage = dynamic_cast<InternalImageType*>(smpGenericInitialFixedImage.GetPointer());
00174 }
00175 catchAllNPassMacro("Error while casting initial fixed image");
00176 if (!pInitialFixedImage) throwCtrlExceptionMacro("","Error while casting initial fixed image.");
00177
00178 try
00179 {
00180 pInitialMovingImage = dynamic_cast<InternalImageType*>(smpGenericInitialMovingImage.GetPointer());
00181 }
00182 catchAllNPassMacro("Error while casting initial fixed image");
00183 if (!pInitialMovingImage) throwCtrlExceptionMacro("","Error while casting initial fixed image.");
00184
00185 initializer->SetTransform( pTransformComponent );
00186 initializer->SetFixedImage( pInitialFixedImage );
00187 initializer->SetMovingImage( pInitialMovingImage );
00188
00189 if (iCenterType==1) initializer->GeometryOn();
00190 else initializer->MomentsOn();
00191
00192 initializer->InitializeTransform();
00193
00194
00195 for (int iIndex=0; iIndex<3; iIndex++)
00196 {
00197 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,pTransformComponent->GetTranslation().GetElement(iIndex),iIndex+3);
00198 }
00199 }
00200 else if (iCenterType==3)
00201 {
00202
00203 for (int iIndex=0; iIndex<3; iIndex++)
00204 {
00205 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,pTransformComponent->GetTranslation().GetElement(iIndex),iIndex+3);
00206 }
00207 }
00208 else if (iCenterType==4)
00209 {
00210 if (!pointsAccessor.MovingPointsExist()) throwCtrlExceptionMacro("","Cannot set moving center via reference points. No moving reference point set defined. Please check media linkage.");
00211 if (!pointsAccessor.FixedPointsExist()) throwCtrlExceptionMacro("","Cannot set fixed center via reference points. No fixed reference point set defined. Please check media linkage.");
00212 fixedCenter = pointsAccessor.GetFixedPointArray(0);
00213 movingCenter = pointsAccessor.GetMovingPointArray(0);
00214
00215 pTransformComponent->SetCenter(pointsAccessor.GetFixedPoint(0));
00216
00217
00218 for (int iIndex=0; iIndex<3; iIndex++)
00219 {
00220 pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,movingCenter[iIndex]-fixedCenter[iIndex],3+iIndex);
00221 };
00222 }
00223 }
00224 return true;
00225 };
00226
00227 }