CASToR  1.0
Tomographic Reconstruction (PET/SPECT)
oDynamicModelManager.cc
Go to the documentation of this file.
00001 
00002 /*
00003   Implementation of class oDynamicModelManager
00004 
00005   - separators: X
00006   - doxygen: X
00007   - default initialization: X
00008   - CASTOR_DEBUG: X
00009   - CASTOR_VERBOSE: X
00010 */
00011 
00012 
00019 #include "oDynamicModelManager.hh"
00020 #include "sAddonManager.hh"
00021 
00022 // =====================================================================
00023 // ---------------------------------------------------------------------
00024 // ---------------------------------------------------------------------
00025 // =====================================================================
00026 /*
00027   \fn oDynamicModelManager
00028   \brief Constructor of oDynamicModelManager. Simply set all data members to default values.
00029 */
00030 oDynamicModelManager::oDynamicModelManager()
00031 {
00032   // Image dimensions
00033   mp_ID = NULL;
00034     // Options for each model type
00035   m_options = "";
00036 
00037   // Model object and associated bool
00038   mp_DynamicModel = NULL;
00039   m_UseModel = false;
00040 
00041   // Verbosity
00042   m_verbose = -1;
00043   m_checked = false;
00044   m_initialized = false;
00045 }
00046 
00047 // =====================================================================
00048 // ---------------------------------------------------------------------
00049 // ---------------------------------------------------------------------
00050 // =====================================================================
00051 /*
00052   \fn ~oDynamicModelManager
00053   \brief Destructor of oDynamicModelManager. Free memory from all allocated tabs.
00054 */
00055 oDynamicModelManager::~oDynamicModelManager() 
00056 {
00057   // Delete model objects
00058   if (mp_DynamicModel) delete mp_DynamicModel;
00059 }
00060 
00061 // =====================================================================
00062 // ---------------------------------------------------------------------
00063 // ---------------------------------------------------------------------
00064 // =====================================================================
00065 /*
00066   \fn CheckParameters
00067   \brief This function is used to check parameters after the latter
00068          have been all set using Set functions.
00069   \return 0 if success, positive value otherwise.
00070 */
00071 int oDynamicModelManager::CheckParameters()
00072 {
00073   #ifdef CASTOR_VERBOSE
00074   if (m_verbose>=2) Cout("oDynamicModelManager::CheckParameters() ..."<< endl);
00075   #endif
00076   
00077   // Check image dimensions
00078   if (mp_ID==NULL)
00079   {
00080     Cerr("***** oDynamicModelManager::CheckParameters() -> No image dimensions provided !" << endl);
00081     return 1;
00082   }
00083   // Check verbosity
00084   if (m_verbose<0)
00085   {
00086     Cerr("***** oDynamicModelManager::CheckParameters() -> Wrong verbosity level provided !" << endl);
00087     return 1;
00088   }
00089   
00090   // Normal end
00091   m_checked = true;
00092   return 0;
00093 }
00094 
00095 // =====================================================================
00096 // ---------------------------------------------------------------------
00097 // ---------------------------------------------------------------------
00098 // =====================================================================
00099 /*
00100   \fn Initialize
00101   \brief Set the dynamic model flag and instanciate/initialize model objects
00102          through the ParseOptionsAndInitializeDeformations() private function.
00103   \return 0 if success, positive value otherwise.
00104 */
00105 int oDynamicModelManager::Initialize()
00106 {
00107   // Forbid initialization without check
00108   if (!m_checked)
00109   {
00110     Cerr("***** oDynamicModelManager::Initialize() -> Must call CheckParameters() before Initialize() !" << endl);
00111     return 1;
00112   }
00113   
00114   // Check options
00115   if (m_options=="")
00116   {
00117     m_initialized = true;
00118     m_UseModel = false;
00119     return 0;
00120   }
00121 
00122   // We have a model
00123   m_UseModel = true;
00124 
00125   // Verbose
00126   if (m_verbose>=1) Cout("oDynamicModelManager::Initialize() -> Initialize models" << endl);
00127   
00128   // Parse model options and initialize them
00129   if (ParseOptionsAndInitializeModel())
00130   {
00131     Cerr("***** oDynamicModelManager::Initialize() -> A problem occured while parsing model options and initializing them !" << endl);
00132     return 1;
00133   }
00134 
00135   // Normal end
00136   m_initialized = true;
00137   return 0;
00138 }
00139 
00140 // =====================================================================
00141 // ---------------------------------------------------------------------
00142 // ---------------------------------------------------------------------
00143 // =====================================================================
00144 /*
00145   \fn ParseOptionsAndInitializeProjectors
00146   \brief Parse dynamic model options contained in the previously provided
00147          strings. This function is called inside the Initialize() function.
00148   \details Manage the options reading and initialize specific vDynamicModel
00149            Options are a string containing first the name of the model,
00150            then either a ':' and a configuration file specific to the model
00151            - or - as many ',' as needed parameters for this model.
00152            Specific pure virtual functions of the vDynamicModel are used to read parameters and initialize them.
00153   \return 0 if success, positive value otherwise
00154 */
00155 int oDynamicModelManager::ParseOptionsAndInitializeModel()
00156 {
00157   #ifdef CASTOR_VERBOSE
00158   if (m_verbose>=3) Cout("oDynamicModelManager::ParseOptionsAndInitializeModel ..."<< endl); 
00159   #endif
00160 
00161   string dynamic_model = "";
00162   string list_options = "";
00163   string file_options = "";
00164 
00165   // This is for the automatic initialization of the models
00166   typedef vDynamicModel *(*maker_dynamic_model) ();
00167 
00168   // Get model's list from addon manager
00169   std::map <string,maker_dynamic_model> list = sAddonManager::GetInstance()->mp_listOfDynamicModels;
00170 
00171   size_t colon, comma;
00172 
00173   // ---------------------------------------------------------------------------------------------------
00174   // Manage model for respiratory motion
00175   // ---------------------------------------------------------------------------------------------------
00176     
00177   // First, check if we have dynamic data
00178   if (mp_ID->GetNbTimeFrames() <= 1 &&
00179       mp_ID->GetNbRespGates() <= 1 &&
00180       mp_ID->GetNbCardGates() <= 1)
00181   {
00182     Cerr("***** oDynamicModelManager::CheckParameters() -> Dynamic model should be used with more than one time frame/dynamic gate !" << endl);
00183     return 1;
00184   }
00185 
00186   // ______________________________________________________________________________
00187   // Get the model name in the options and isolate the real model's options
00188 
00189   // Search for a colon ":", this indicates that a configuration file is provided after the model name
00190   colon = m_options.find_first_of(":");
00191   comma = m_options.find_first_of(",");
00192 
00193   // Case 1: we have a colon
00194   if (colon!=string::npos)
00195   {
00196     // Get the model name before the colon
00197     dynamic_model = m_options.substr(0,colon);
00198     // Get the configuration file after the colon
00199     file_options = m_options.substr(colon+1);
00200     // List of options is empty
00201     list_options = "";
00202   }
00203   // Case 2: we have a comma
00204   else if (comma!=string::npos)
00205   {
00206     // Get the model name before the first comma
00207     dynamic_model = m_options.substr(0,comma);
00208     // Get the list of options after the first comma
00209     list_options = m_options.substr(comma+1);
00210     // Configuration file is empty
00211     file_options = "";
00212   }
00213   // Case 3: no colon and no comma (a single model name)
00214   else
00215   {
00216     // Get the model name
00217     dynamic_model = m_options;
00218     // Configuration file is empty
00219     file_options = "";
00220     // List of options is empty
00221     list_options = "";
00222   }
00223 
00224   // Create the model
00225   if (list[dynamic_model]) mp_DynamicModel = list[dynamic_model]();
00226   else
00227   {
00228     Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> Model '" << dynamic_model << "' does not exist !" << endl);
00229     sAddonManager::GetInstance()->ShowHelpDynamicModel();
00230     return 1;
00231   }
00232   mp_DynamicModel->SetImageDimensionsAndQuantification(mp_ID);
00233   mp_DynamicModel->SetVerbose(m_verbose);
00234   // Provide configuration file if any
00235   if (file_options!="" && mp_DynamicModel->ReadAndCheckConfigurationFile(file_options))
00236   {
00237     Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while reading and checking frame dynamic model's configuration file !" << endl);
00238     return 1;
00239   }
00240   // Provide options if any
00241   if (list_options!="" && mp_DynamicModel->ReadAndCheckOptionsList(list_options))
00242   {
00243     Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while parsing and reading frame dynamic model's options !" << endl);
00244     return 1;
00245   }  
00246   // Check parameters
00247   if (mp_DynamicModel->CheckParameters())
00248   {
00249     Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while checking frame dynamic model parameters !" << endl);
00250     return 1;
00251   }
00252   // Initialize the model
00253   if (mp_DynamicModel->Initialize())
00254   {
00255     Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while initializing frame dynamic model !" << endl);
00256     return 1;
00257   }
00258 
00259   // Normal end
00260   return 0;
00261 }
00262 
00263 // =====================================================================
00264 // ---------------------------------------------------------------------
00265 // ---------------------------------------------------------------------
00266 // =====================================================================
00267 /*
00268   \fn ApplyDynamicModel
00269   \param ap_ImageS : pointer to the ImageSpace
00270   \param a_ite : index of the actual iteration
00271   \param a_sset : index of the actual subset
00272   \brief Call successively EstimateModelParameters() ans FitModel() 
00273          functions of the dynamic model object is 'm_UseModel' is on.
00274   \return 0 if success, positive value otherwise
00275 */
00276 int oDynamicModelManager::ApplyDynamicModel(oImageSpace* ap_ImageS, int a_ite, int a_sset) 
00277 {
00278   #ifdef CASTOR_DEBUG
00279   if (!m_initialized)
00280   {
00281     Cerr("***** oDynamicModelManager::ApplyDynamicModel() -> Called while not initialized !" << endl);
00282     Exit(EXIT_DEBUG);
00283   }
00284   #endif
00285   
00286   if (m_UseModel)
00287   {
00288     // Verbose
00289     if(m_verbose>=2) Cout("oDynamicModelManager::ApplyDynamicModel ..."<< endl);
00290 
00291     // Estimate model parameters
00292     if( mp_DynamicModel->EstimateModelParameters(ap_ImageS, a_ite, a_sset) )
00293       {
00294       Cerr("***** oDynamicModelManager::StepPostProcessInsideSubsetLoop() -> A problem occured while applying dynamic model to current estimate images !" << endl);
00295       return 1;
00296     }
00297     
00298     // Fit model to the serie of dynamic images
00299     if( mp_DynamicModel->FitModel(ap_ImageS, a_ite, a_sset) )
00300     {
00301       Cerr("***** oDynamicModelManager::StepPostProcessInsideSubsetLoop() -> A problem occured while applying dynamic model to current estimate images !" << endl);
00302       return 1;
00303     }
00304   }
00305 
00306   return 0;
00307 }
00308 
00309 // =====================================================================
00310 // ---------------------------------------------------------------------
00311 // ---------------------------------------------------------------------
00312 // =====================================================================
00313 /*
00314   \fn SaveParametricImages
00315   \param a_ite
00316   \brief Call SaveCoeffImages() function of the dynamic model object is 
00317         'm_UseModel' is on, in order to save any parameter image
00318   \return 0 if success, positive value otherwise
00319 */
00320 int oDynamicModelManager::SaveParametricImages(int a_ite)
00321 {
00322   if (m_UseModel)
00323   {
00324     // Verbose
00325     if(m_verbose>=2) Cout("oDynamicModelManager::SaveParametricImages ..."<< endl);
00326     // Save images
00327     if (mp_DynamicModel->SaveParametricImages(a_ite))
00328     {
00329       Cerr("***** oDynamicModelManager::SaveParametricImages() -> A problem occured while trying to save image coefficients !" << endl);
00330       return 1;
00331     }
00332   }
00333   return 0;
00334 }
00335 
00336 // =====================================================================
00337 // ---------------------------------------------------------------------
00338 // ---------------------------------------------------------------------
00339 // =====================================================================
 All Classes Files Functions Variables Typedefs Defines