CASToR  1.1
Tomographic Reconstruction (PET/SPECT)
 All Classes Files Functions Variables Typedefs Macros Groups Pages
oDynamicModelManager.cc
Go to the documentation of this file.
1 
2 /*
3  Implementation of class oDynamicModelManager
4 
5  - separators: X
6  - doxygen: X
7  - default initialization: X
8  - CASTOR_DEBUG: X
9  - CASTOR_VERBOSE: X
10 */
11 
12 
19 #include "oDynamicModelManager.hh"
20 #include "sAddonManager.hh"
21 
22 // =====================================================================
23 // ---------------------------------------------------------------------
24 // ---------------------------------------------------------------------
25 // =====================================================================
26 /*
27  \fn oDynamicModelManager
28  \brief Constructor of oDynamicModelManager. Simply set all data members to default values.
29 */
31 {
32  // Image dimensions
33  mp_ID = NULL;
34  // Options for each model type
35  m_options = "";
36 
37  // Model object and associated bool
38  mp_DynamicModel = NULL;
39  m_UseModel = false;
40 
41  // Verbosity
42  m_verbose = -1;
43  m_checked = false;
44  m_initialized = false;
45 }
46 
47 // =====================================================================
48 // ---------------------------------------------------------------------
49 // ---------------------------------------------------------------------
50 // =====================================================================
51 /*
52  \fn ~oDynamicModelManager
53  \brief Destructor of oDynamicModelManager. Free memory from all allocated tabs.
54 */
56 {
57  // Delete model objects
58  if (mp_DynamicModel) delete mp_DynamicModel;
59 }
60 
61 // =====================================================================
62 // ---------------------------------------------------------------------
63 // ---------------------------------------------------------------------
64 // =====================================================================
65 /*
66  \fn CheckParameters
67  \brief This function is used to check parameters after the latter
68  have been all set using Set functions.
69  \return 0 if success, positive value otherwise.
70 */
72 {
73  #ifdef CASTOR_VERBOSE
74  if (m_verbose>=2) Cout("oDynamicModelManager::CheckParameters() ..."<< endl);
75  #endif
76 
77  // Check image dimensions
78  if (mp_ID==NULL)
79  {
80  Cerr("***** oDynamicModelManager::CheckParameters() -> No image dimensions provided !" << endl);
81  return 1;
82  }
83  // Check verbosity
84  if (m_verbose<0)
85  {
86  Cerr("***** oDynamicModelManager::CheckParameters() -> Wrong verbosity level provided !" << endl);
87  return 1;
88  }
89 
90  // Normal end
91  m_checked = true;
92  return 0;
93 }
94 
95 // =====================================================================
96 // ---------------------------------------------------------------------
97 // ---------------------------------------------------------------------
98 // =====================================================================
99 /*
100  \fn Initialize
101  \brief Set the dynamic model flag and instanciate/initialize model objects
102  through the ParseOptionsAndInitializeDeformations() private function.
103  \return 0 if success, positive value otherwise.
104 */
106 {
107  // Forbid initialization without check
108  if (!m_checked)
109  {
110  Cerr("***** oDynamicModelManager::Initialize() -> Must call CheckParameters() before Initialize() !" << endl);
111  return 1;
112  }
113 
114  // Check options
115  if (m_options=="")
116  {
117  m_initialized = true;
118  m_UseModel = false;
119  return 0;
120  }
121 
122  // We have a model
123  m_UseModel = true;
124 
125  // Verbose
126  if (m_verbose>=1) Cout("oDynamicModelManager::Initialize() -> Initialize models" << endl);
127 
128  // Parse model options and initialize them
130  {
131  Cerr("***** oDynamicModelManager::Initialize() -> A problem occured while parsing model options and initializing them !" << endl);
132  return 1;
133  }
134 
135  // Normal end
136  m_initialized = true;
137  return 0;
138 }
139 
140 // =====================================================================
141 // ---------------------------------------------------------------------
142 // ---------------------------------------------------------------------
143 // =====================================================================
144 /*
145  \fn ParseOptionsAndInitializeProjectors
146  \brief Parse dynamic model options contained in the previously provided
147  strings. This function is called inside the Initialize() function.
148  \details Manage the options reading and initialize specific vDynamicModel
149  Options are a string containing first the name of the model,
150  then either a ':' and a configuration file specific to the model
151  - or - as many ',' as needed parameters for this model.
152  Specific pure virtual functions of the vDynamicModel are used to read parameters and initialize them.
153  \return 0 if success, positive value otherwise
154 */
156 {
157  #ifdef CASTOR_VERBOSE
158  if (m_verbose>=3) Cout("oDynamicModelManager::ParseOptionsAndInitializeModel ..."<< endl);
159  #endif
160 
161  string dynamic_model = "";
162  string list_options = "";
163  string file_options = "";
164 
165  // This is for the automatic initialization of the models
166  typedef vDynamicModel *(*maker_dynamic_model) ();
167 
168  // Get model's list from addon manager
169  std::map <string,maker_dynamic_model> list = sAddonManager::GetInstance()->mp_listOfDynamicModels;
170 
171  size_t colon, comma;
172 
173  // ---------------------------------------------------------------------------------------------------
174  // Manage model for respiratory motion
175  // ---------------------------------------------------------------------------------------------------
176 
177  // First, check if we have dynamic data
178  if (mp_ID->GetNbTimeFrames() <= 1 &&
179  mp_ID->GetNbRespGates() <= 1 &&
180  mp_ID->GetNbCardGates() <= 1)
181  {
182  Cerr("***** oDynamicModelManager::CheckParameters() -> Dynamic model should be used with more than one time frame/dynamic gate !" << endl);
183  return 1;
184  }
185 
186  // ______________________________________________________________________________
187  // Get the model name in the options and isolate the real model's options
188 
189  // Search for a colon ":", this indicates that a configuration file is provided after the model name
190  colon = m_options.find_first_of(":");
191  comma = m_options.find_first_of(",");
192 
193  // Case 1: we have a colon
194  if (colon!=string::npos)
195  {
196  // Get the model name before the colon
197  dynamic_model = m_options.substr(0,colon);
198  // Get the configuration file after the colon
199  file_options = m_options.substr(colon+1);
200  // List of options is empty
201  list_options = "";
202  }
203  // Case 2: we have a comma
204  else if (comma!=string::npos)
205  {
206  // Get the model name before the first comma
207  dynamic_model = m_options.substr(0,comma);
208  // Get the list of options after the first comma
209  list_options = m_options.substr(comma+1);
210  // Configuration file is empty
211  file_options = "";
212  }
213  // Case 3: no colon and no comma (a single model name)
214  else
215  {
216  // Get the model name
217  dynamic_model = m_options;
218  // Configuration file is empty
219  file_options = "";
220  // List of options is empty
221  list_options = "";
222  }
223 
224  // Create the model
225  if (list[dynamic_model]) mp_DynamicModel = list[dynamic_model]();
226  else
227  {
228  Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> Model '" << dynamic_model << "' does not exist !" << endl);
230  return 1;
231  }
234  // Provide configuration file if any
235  if (file_options!="" && mp_DynamicModel->ReadAndCheckConfigurationFile(file_options))
236  {
237  Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while reading and checking frame dynamic model's configuration file !" << endl);
238  return 1;
239  }
240  // Provide options if any
241  if (list_options!="" && mp_DynamicModel->ReadAndCheckOptionsList(list_options))
242  {
243  Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while parsing and reading frame dynamic model's options !" << endl);
244  return 1;
245  }
246  // Check parameters
248  {
249  Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while checking frame dynamic model parameters !" << endl);
250  return 1;
251  }
252  // Initialize the model
254  {
255  Cerr("***** oDynamicModelManager::ParseOptionsAndInitializeModel() -> A problem occured while initializing frame dynamic model !" << endl);
256  return 1;
257  }
258 
259  // Normal end
260  return 0;
261 }
262 
263 // =====================================================================
264 // ---------------------------------------------------------------------
265 // ---------------------------------------------------------------------
266 // =====================================================================
267 /*
268  \fn ApplyDynamicModel
269  \param ap_ImageS : pointer to the ImageSpace
270  \param a_ite : index of the actual iteration
271  \param a_sset : index of the actual subset
272  \brief Call successively EstimateModelParameters() ans FitModel()
273  functions of the dynamic model object is 'm_UseModel' is on.
274  \return 0 if success, positive value otherwise
275 */
276 int oDynamicModelManager::ApplyDynamicModel(oImageSpace* ap_ImageS, int a_ite, int a_sset)
277 {
278  #ifdef CASTOR_DEBUG
279  if (!m_initialized)
280  {
281  Cerr("***** oDynamicModelManager::ApplyDynamicModel() -> Called while not initialized !" << endl);
282  Exit(EXIT_DEBUG);
283  }
284  #endif
285 
286  if (m_UseModel)
287  {
288  // Verbose
289  if(m_verbose>=2) Cout("oDynamicModelManager::ApplyDynamicModel ..."<< endl);
290 
291  // Estimate model parameters
292  if( mp_DynamicModel->EstimateModelParameters(ap_ImageS, a_ite, a_sset) )
293  {
294  Cerr("***** oDynamicModelManager::StepPostProcessInsideSubsetLoop() -> A problem occured while applying dynamic model to current estimate images !" << endl);
295  return 1;
296  }
297 
298  // Fit model to the serie of dynamic images
299  if( mp_DynamicModel->FitModel(ap_ImageS, a_ite, a_sset) )
300  {
301  Cerr("***** oDynamicModelManager::StepPostProcessInsideSubsetLoop() -> A problem occured while applying dynamic model to current estimate images !" << endl);
302  return 1;
303  }
304  }
305 
306  return 0;
307 }
308 
309 // =====================================================================
310 // ---------------------------------------------------------------------
311 // ---------------------------------------------------------------------
312 // =====================================================================
313 /*
314  \fn SaveParametricImages
315  \param a_ite
316  \brief Call SaveCoeffImages() function of the dynamic model object is
317  'm_UseModel' is on, in order to save any parameter image
318  \return 0 if success, positive value otherwise
319 */
321 {
322  if (m_UseModel)
323  {
324  // Verbose
325  if(m_verbose>=2) Cout("oDynamicModelManager::SaveParametricImages ..."<< endl);
326  // Save images
328  {
329  Cerr("***** oDynamicModelManager::SaveParametricImages() -> A problem occured while trying to save image coefficients !" << endl);
330  return 1;
331  }
332  }
333  return 0;
334 }
335 
336 // =====================================================================
337 // ---------------------------------------------------------------------
338 // ---------------------------------------------------------------------
339 // =====================================================================
Declaration of class oDynamicModelManager.
virtual int CheckParameters()
This function is used to check parameters after the latter have been all set using Set functions...
int ApplyDynamicModel(oImageSpace *ap_ImageS, int a_ite, int a_sset)
void ShowHelpDynamicModel()
Show help about all implemented dynamic models.
void SetImageDimensionsAndQuantification(oImageDimensionsAndQuantification *ap_ImageDimensionsAndQuantification)
Set the image dimensions in use.
This is the mother class of dynamic model classes.
virtual int ReadAndCheckOptionsList(string a_listOptions)=0
This function is used to read parameters from a string. It is pure virtual so must be implemented b...
oDynamicModelManager()
Constructor of oDynamicModelManager. Simply set all data members to default values.
int Initialize()
Set the dynamic model flag and instanciate/initialize model objects through the ParseOptionsAndInitia...
virtual int Initialize()=0
This function is used to initialize specific data related to the child deformation model...
void Exit(int code)
static sAddonManager * GetInstance()
int ParseOptionsAndInitializeModel()
Parse dynamic model options contained in the previously provided strings. This function is called ins...
#define Cerr(MESSAGE)
virtual int FitModel(oImageSpace *ap_Image, int a_ite, int a_sset)=0
This function is pure virtual so must be implemented by children. It is used to fit the dynamic mod...
virtual int ReadAndCheckConfigurationFile(string a_fileOptions)=0
This function is used to read options from a configuration file. It is pure virtual so must be impl...
oImageDimensionsAndQuantification * mp_ID
void SetVerbose(int a_verbose)
Set the verbose level.
vDynamicModel * mp_DynamicModel
int CheckParameters()
This function is used to check parameters after the latter have been all set using Set functions...
int GetNbCardGates()
Get the number of cardiac gates.
std::map< string, maker_dynamic_model > mp_listOfDynamicModels
This class holds all the matrices in the image domain that can be used in the algorithm: image...
Definition: oImageSpace.hh:41
int SaveParametricImages(int a_ite)
Call SaveParametricImages() function of the dynamic model object is 'm_UseModel' is on...
int GetNbTimeFrames()
Get the number of time frames.
#define EXIT_DEBUG
Definition: gVariables.hh:69
int GetNbRespGates()
Get the number of respiratory gates.
#define Cout(MESSAGE)
~oDynamicModelManager()
Destructor of oDynamicModelManager. Free memory from all allocated tabs.
virtual int EstimateModelParameters(oImageSpace *ap_Image, int a_ite, int a_sset)=0
This function is pure virtual so must be implemented by children. It can be used to estimate any te...
Declaration of class sAddonManager.
virtual int SaveParametricImages(int a_ite)=0
This function is pure virtual so must be implemented by children Call SaveParametricImages() functi...