freVersorRigid3DTransformController.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freVersorRigid3DTransformController.cxx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
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   //Parameters
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   //Media
00064   profile.MediaMap().AddMedia("movingInitialImage",ControllerID::IntensityImage3DMediaController,DASet,3);
00065   profile.MediaMap().AddMedia("fixedInitialImage",ControllerID::IntensityImage3DMediaController,DASet,3);
00066 
00067   //Only verso optimizer are allowed to interact with these transformers
00068   //of the same registration processor  
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   { //rotation and/or translation should be initialized via LandmarkBasedTransformInitializer
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   { //set rotation via ITV
00120     for (int iIndex=0; iIndex<3; iIndex++)
00121     {
00122       pTransformSetup->Parameters().SetParameterValue(cParam_CurrentTransformValues,itv[iIndex],iIndex);
00123     };
00124   }
00125   else
00126   { //rotation has been initialized via itk::LandmarkBasedTransformInitializer
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   { //set translation via ITV; override any settings done via itk::LandmarkBasedTransformInitializer
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     { //no translation, center via "Center" parameter
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     { //intitalize center and translation via itk::CenteredTransformInitializer
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       //write parameters
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     { //translation and center have been calculated via itk::LandmarkBasedTransformInitializer
00202       //center is already set only copy translation to the transformvalues parameter
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     { //First reference is the center point
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       //set the translation
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 } //end of namespace free

Generated at Sat Oct 13 18:02:07 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000