freSetupInterpolator.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: freSetupInterpolator.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 #include "freSetupInterpolator.h"
00023 #include "freExceptions.h"
00024 #include "freControllerBase.h"
00025 
00026 
00027 namespace FREE
00028 {
00029 
00033 
00034 
00035 void
00036 SetupInterpolator::
00037 SetStartSetup(Setup* pSetup)
00038 {
00039   m_pStartSetup = pSetup;
00040   ResetIteration();
00041 }; 
00042 
00043 Setup*
00044 SetupInterpolator::
00045 GetStartSetup()
00046 { return m_pStartSetup; };
00047 
00048 void
00049 SetupInterpolator::
00050 SetStopSetup(Setup* pSetup) 
00051 {
00052   m_pStopSetup = pSetup;
00053   ResetIteration();
00054 }; 
00055 
00056 Setup*
00057 SetupInterpolator::
00058 GetStopSetup()
00059 { return m_pStopSetup; };
00060 
00061 void
00062 SetupInterpolator::
00063 SetIterations(const int iIterations)
00064 { 
00065   m_Iterations = iIterations;
00066   ResetIteration();
00067 };
00068 
00069 const int
00070 SetupInterpolator::
00071 GetIterations()
00072 { return m_Iterations; };
00073 
00074 const int
00075 SetupInterpolator::
00076 GetCurrentIteration()
00077 { return m_CurrentIteration; };
00078 
00079 void
00080 SetupInterpolator::
00081 ResetIteration()
00082 { m_CurrentIteration = 0; };
00083 
00084 void
00085 SetupInterpolator::
00086 SetSetupName(const std::string&  sName)
00087 { m_sName = sName; };
00088 
00089 const std::string
00090 SetupInterpolator::
00091 GetSetupName()
00092 { return m_sName; };
00093 
00094 void
00095 SetupInterpolator::
00096 ResetName()
00097 { m_sName = ""; };
00098 
00099 Setup::Pointer
00100 SetupInterpolator::
00101 InterpolateSetup(const int iActIteration, const int iIterations)
00102 {
00103   if (!m_pStartSetup) throwExceptionMacro("Interpolation aborted, no start setup set.");
00104   if (!m_pStopSetup) throwExceptionMacro("Interpolation aborted, no stop setup set.");
00105         if ((iActIteration> iIterations+1) | (iActIteration<0)) throwExceptionMacro("Interpolation aborted, iteration out of range (1.."<<iIterations<<") : "<<iActIteration);
00106 
00107   Setup::Pointer pInterpolatedSetup = Setup::New();
00108   *pInterpolatedSetup = *m_pStartSetup;
00109 
00110   if (m_sName!="") pInterpolatedSetup->SetName(m_sName);
00111   pInterpolatedSetup->SetName(pInterpolatedSetup->GetName()+"_"+FREE::Convert::ToStr(iActIteration)+"_"+FREE::Convert::ToStr(iIterations+1));
00112 
00113   Setup::SectionVector sections = pInterpolatedSetup->GetSections();
00114   for (int iSection = 0; iSection < sections.size(); iSection++)
00115   {
00116     ComponentSetup* pSection = sections[iSection];
00117     InterpolateComponent(pSection,iSection,iActIteration,iIterations);
00118   };
00119 
00120   return pInterpolatedSetup;
00121 };
00122 
00123 Setup::Pointer
00124 SetupInterpolator::
00125 InterpolateSetup(const int iActIteration)
00126 {
00127   return InterpolateSetup(iActIteration, m_Iterations);
00128 };
00129 
00130 Setup::Pointer
00131 SetupInterpolator::
00132 InterpolateNext()
00133 {
00134   if (m_CurrentIteration>m_Iterations+1) return NULL;
00135   Setup::Pointer pNew = InterpolateSetup(m_CurrentIteration, m_Iterations);
00136   m_CurrentIteration++;
00137   return pNew;
00138 };
00139 
00140 SetupInterpolator::
00141 SetupInterpolator()
00142 {
00143   ResetIteration();
00144   ResetName();
00145   m_Iterations = 0;
00146   m_sName = "";
00147   m_pStartSetup = NULL;
00148   m_pStopSetup = NULL;
00149 };
00150 
00151 SetupInterpolator::
00152 ~SetupInterpolator()  {};
00153 
00154 void
00155 SetupInterpolator::
00156 InterpolateParameter(ComponentSetup* pInterpolate, ComponentSetup* pStart, ComponentSetup* pStop, const std::string& sKey, const int& iActIteration, const int& iIterations)
00157 {
00158   GenericComponentController* pController = m_Central.GetController(pStart->GetControllerID());
00159         if (!pController) throwExceptionMacro("No controller found for component (ComponentID: "<<pStart->GetComponentID()<<"; ControllerID: "<<pStart->GetControllerID()<<")");
00160 
00161   CtrlProfile::Parameter* pParam = pController->GetProfile(0)->Parameters().GetElement(sKey);
00162 
00163   if (!pParam) throwExceptionMacro("Parameter not found in controller profile. Missing key: " << sKey);
00164 
00165   Parameter::ParameterValueType paramType = pParam->GetValueType();
00166 
00167   if ((paramType==Parameter::PVTDouble) | (paramType==Parameter::PVTInteger) | (paramType==Parameter::PVTLong) | (paramType==Parameter::PVTULong))
00168   {
00169     if (!pStart->Parameters().ParameterExists(sKey)) throwExceptionMacro("Parameter not found in start setup. Missing key: " << sKey);
00170     if (!pStop->Parameters().ParameterExists(sKey)) throwExceptionMacro("Parameter not found in stop setup. Missing key: " << sKey);
00171 
00172     for (int iLevelIndex=0; iLevelIndex<pStart->Parameters().ParameterLayerCount(sKey); iLevelIndex++)
00173     {
00174       for (int iIndex=0; iIndex<pStart->Parameters().ParameterSize(sKey); iIndex++)
00175       {
00176         double startValue;
00177         double stopValue;
00178         if ((pStart->Parameters().GetParameterValue(sKey,startValue,iIndex,iLevelIndex)) &
00179             (pStop->Parameters().GetParameterValue(sKey,stopValue,iIndex,iLevelIndex)))
00180         {
00181           double newValue = startValue+((stopValue-startValue)*iActIteration/(iIterations+1));
00182           switch (paramType)
00183           {
00184             case Parameter::PVTULong: pInterpolate->Parameters().SetParameterValue(sKey,(unsigned long)newValue,iIndex,iLevelIndex); break;
00185             case Parameter::PVTLong: pInterpolate->Parameters().SetParameterValue(sKey,(long)newValue,iIndex,iLevelIndex); break;
00186             case Parameter::PVTInteger: pInterpolate->Parameters().SetParameterValue(sKey,(int)newValue,iIndex,iLevelIndex); break;
00187             default: pInterpolate->Parameters().SetParameterValue(sKey,newValue,iIndex,iLevelIndex);
00188           };
00189         };//else at least one of the parameters is empty and cannot be interpolated (e.g. no initial reference points)
00190       };
00191     };
00192   };
00193 };
00194 
00195 void
00196 SetupInterpolator::
00197 InterpolateComponent(ComponentSetup *pComponent, const int& iSectionID, const int& iActIteration, const int& iIterations)
00198 {
00199   Setup::SectionVector startSections = m_pStartSetup->GetSections();
00200   Setup::SectionVector stopSections = m_pStopSetup->GetSections();
00201 
00202   ComponentSetup* pStartSection = startSections[iSectionID];
00203   ComponentSetup* pStopSection = stopSections[iSectionID];
00204 
00205   if (!pStopSection) throwExceptionMacro("Section not found in stop setup. SectionID: " << iSectionID);
00206   
00207   ComponentSetup* pStart = m_pStartSetup->GetComponentByIDPath(pComponent->GetIDPath());
00208   ComponentSetup* pStop = m_pStopSetup->GetComponentByIDPath(pComponent->GetIDPath());
00209 
00210   if (!pStart) throwExceptionMacro("Component not found in start setup. ComponentID: " << pComponent->GetIDPath().operator std::string());
00211   if (!pStop) throwExceptionMacro("Component not found in stop setup. ComponentID: " << pComponent->GetIDPath().operator std::string());
00212 
00213   for (int iIndex=0; iIndex<pComponent->Parameters().Size(); iIndex++)
00214   {
00215     std::string  sKey = pComponent->Parameters().GetParameterName(iIndex);
00216     InterpolateParameter(pComponent,pStart,pStop,sKey,iActIteration,iIterations);
00217   };
00218 
00219   for (int iIndex=0; iIndex<pComponent->Components().Size(); iIndex++)
00220   { //interpolate the subcomponents
00221     std::string  sKey = pComponent->Parameters().GetParameterName(iIndex);
00222     InterpolateComponent(pComponent->Components().GetElement(iIndex),iSectionID,iActIteration,iIterations);
00223   };
00224 };
00225 
00226 }//End of Namespace free

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