CASToR  1.1
Tomographic Reconstruction (PET/SPECT)
 All Classes Files Functions Variables Typedefs Macros Groups Pages
vDeformation.cc
Go to the documentation of this file.
1 
2 /*
3  Implementation of class vDeformation
4 
5  - separators: X
6  - doxygen: X
7  - default initialization: X
8  - CASTOR_DEBUG: X
9  - CASTOR_VERBOSE: X
10 */
11 
18 #include "vDeformation.hh"
20 #include "oImageSpace.hh"
21 
22 
23 // =====================================================================
24 // ---------------------------------------------------------------------
25 // ---------------------------------------------------------------------
26 // =====================================================================
27 /*
28  \fn vDeformation
29  \brief Constructor of vDeformation. Simply set all data members to default values.
30 */
32 {
33  mp_ID = NULL;
35  m_verbose = -1;
36  m_checked = false;
37  m_initialized = false;
38 }
39 
40 
41 
42 
43 // =====================================================================
44 // ---------------------------------------------------------------------
45 // ---------------------------------------------------------------------
46 // =====================================================================
47 /*
48  \fn ~vDeformation
49  \brief Destructor of vDeformation.
50 */
52 
53 
54 
55 
56 // =====================================================================
57 // ---------------------------------------------------------------------
58 // ---------------------------------------------------------------------
59 // =====================================================================
67 {
68  if(m_verbose>=2) Cout("vDeformation::CheckParameters ..."<< endl);
69 
70  // Check image dimensions
71  if (mp_ID==NULL)
72  {
73  Cerr("***** vDeformation::CheckParameters() -> No image dimensions provided !" << endl);
74  return 1;
75  }
76 
77  // Check verbosity
78  if (m_verbose<0)
79  {
80  Cerr("***** vDeformation::CheckParameters() -> Wrong verbosity level provided !" << endl);
81  return 1;
82  }
83 
84  // Check number of basis functions
85  if (m_nbTransformations <0)
86  {
87  Cerr("***** vDeformation::CheckParameters() -> Number of transformations in the deformation has not been initialized !" << endl);
88  return 1;
89  }
90 
91  // Check parameters of the child class (if this function is overloaded)
93  {
94  Cerr("***** vDeformation::CheckParameters() -> An error occurred while checking parameters of the child dynamic class !" << endl);
95  return 1;
96  }
97 
98  // Normal end
99  m_checked = true;
100  return 0;
101 }
102 
103 
104 
105 
106 
107 
108 
109 // =====================================================================
110 // ---------------------------------------------------------------------
111 // ---------------------------------------------------------------------
112 // =====================================================================
113 /*
114  \fn ApplyDeformationsToBackwardImage
115  \param ap_Image : required to access the backward image and its deformation backup matrice
116  \param a_defIdx : index of the deformation
117  \brief Apply backward transformation of the backward image to the reference position
118  \details Loop on frames
119  Recover any potential data stored in the backup matrice m2p_defTmpBackwardImage
120  \return 0 if success, positive value otherwise
121 */
123 {
124  if(m_verbose >= 2) Cout("vDeformation::ApplyDeformationsToBackwardImage ... " <<endl);
125 
126  #ifdef CASTOR_DEBUG
127  if (!m_initialized)
128  {
129  Cerr("***** vDeformation::ApplyDeformationsToBackwardImage() -> Called while not initialized !" << endl);
130  Exit(EXIT_DEBUG);
131  }
132  #endif
133 
134  for(int bimg=0; bimg<ap_Image->GetNbBackwardImages(); bimg++)
135  for(int fr=0; fr<mp_ID->GetNbTimeFrames(); fr++)
136  for(int rimg=0; rimg<mp_ID->GetNbRespGates(); rimg++)
137  for(int cimg=0; cimg<mp_ID->GetNbCardGates(); cimg++)
138  {
139  // Perform backward deformation
140  if(ApplyDeformations(ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg], ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg], BACKWARD_DEFORMATION, a_defIdx) )
141  {
142  Cerr("***** vDeformation::ApplyDeformationsToBackwardImage()-> An error occured while performing backward deformation of the backward image !" << endl);
143  Cerr("***** frame index " << fr << " respiratory image index " << rimg<< " cardiac image index " << cimg<< " !" << endl);
144  return 1;
145  }
146 
147  // Recover the content of the temporary backup deformation image to the backward image
148  for(int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
149  ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg][v] += ap_Image->m5p_defTmpBackwardImage[bimg][fr][rimg][cimg][v] ;
150  }
151 
152  return 0;
153 }
154 
155 
156 
157 
158 // =====================================================================
159 // ---------------------------------------------------------------------
160 // ---------------------------------------------------------------------
161 // =====================================================================
162 /*
163  \fn ApplyDeformationsToHistoSensitivityImage
164  \param ap_Image : required to access the backward image and its deformation backup matrice
165  \param a_defIdx : index of the deformation
166  \brief Apply backward transformations of the sensitivity image to the reference position (histogram mode)
167  \details Loop on frames
168  Recover any potential data stored in the backup matrice m4p_defTmpSensitivityImage
169  \return 0 if success, positive value otherwise
170 */
172 {
173  if(m_verbose >= 2) Cout("vDeformation::ApplyDeformationsToHistoSensitivityImage ... " <<endl);
174 
175  #ifdef CASTOR_DEBUG
176  if (!m_initialized)
177  {
178  Cerr("***** vDeformation::ApplyDeformationsToHistoSensitivityImage() -> Called while not initialized !" << endl);
179  Exit(EXIT_DEBUG);
180  }
181  #endif
182 
183  for(int fr=0; fr<mp_ID->GetNbTimeFrames(); fr++)
184  for(int rimg=0; rimg<mp_ID->GetNbRespGates() ; rimg++)
185  for(int cimg=0; cimg<mp_ID->GetNbCardGates() ; cimg++)
186  {
187  // Perform backward deformation
188  if( ApplyDeformations(ap_Image->m5p_sensitivity[0][fr][rimg][cimg], ap_Image->m5p_sensitivity[0][fr][rimg][cimg], BACKWARD_DEFORMATION, a_defIdx) )
189  {
190  Cerr("***** vDeformation::ApplyDeformationsToHistoSensitivityImage()-> An error occured while performing backward deformation of the backward image !" << endl);
191  Cerr("***** frame index " << fr << " respiratory image index " << rimg<< " cardiac image index " << cimg<< " !" << endl);
192  return 1;
193  }
194 
195  // Recover the content of the temporary backup deformation image to the sensitivity image
196  for(int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
197  ap_Image->m5p_sensitivity[0][fr][rimg][cimg][v] += ap_Image->m4p_defTmpSensitivityImage[fr][rimg][cimg][v];
198  }
199 
200  return 0;
201 }
202 
203 
204 
205 
206 // =====================================================================
207 // ---------------------------------------------------------------------
208 // ---------------------------------------------------------------------
209 // =====================================================================
210 /*
211  \fn PerformDeformation
212  \param ap_Image : required to access oImageSpace image matrices
213  \param a_defIdx : index of the deformation
214  \param fr : frame index
215  \param rimg : respiratory image index
216  \param cimg : cardiac image index
217  \brief Apply deformations during reconstruction
218  \details 1. Recover all the data of the multithreaded backward image matrices in the first one (thread index 0)
219  2. Perform backward deformation of the backward image to the reference position with defIdx-1
220  3. Add coefficients of the backward image matrice to the temporary backup image matrice & reset backward image
221  4. Apply forward deformation of the forward image (backward deformation to the reference position with defIdx-1, and forward deformation with defIdx)
222  \return 0 if success, positive value otherwise
223 */
224 int vDeformation::PerformDeformation(oImageSpace* ap_Image, int a_defIdx, int fr, int rimg, int cimg)
225 {
226  if(m_verbose >= 2) Cout("vDeformation::PerformDeformation ... " <<endl);
227 
228  #ifdef CASTOR_DEBUG
229  if (!m_initialized)
230  {
231  Cerr("***** vDeformation::PerformDeformation() -> Called while not initialized !" << endl);
232  Exit(EXIT_DEBUG);
233  }
234  #endif
235 
236  // REDUCE
237  for (int bimg=0; bimg<ap_Image->GetNbBackwardImages(); bimg++)
238  {
239  for (int th=1 ; th<mp_ID->GetNbThreadsForProjection() ; th++) //TODO add loops
240  {
241  // Synchronization of the multi-threaded backwardImages inside the first image Reduce)
242  for(int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
243  ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg][v] += ap_Image->m6p_backwardImage[bimg][th][fr][rimg][cimg][v];
244  }
245 
246  // BACKWARD DEFORMATION of the backward image //TODO add loops
247  if (ApplyDeformations(ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg], ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg], BACKWARD_DEFORMATION, a_defIdx-1) )
248  {
249  Cerr("***** vDeformation::ApplyDeformations()-> An error occured while performing backward deformation of the backward image !" << endl);
250  Cerr("***** frame index " << fr << " respiratory image index " << rimg<< " cardiac image index " << cimg<< " !" << endl);
251  return 1;
252  }
253 
254  // Store Backward deformation update coefficients in the temporary backup image //TODO add loops
255  for (int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
256  {
257  ap_Image->m5p_defTmpBackwardImage[bimg][fr][rimg][cimg][v] += ap_Image->m6p_backwardImage[bimg][0][fr][rimg][cimg][v];
258  }
259  }
260 
261  // Reset the backward images (i.e set all voxels to the specific fr/rimg/cimg to 0)
262  for (int bimg=0; bimg<ap_Image->GetNbBackwardImages(); bimg++)
263  for(int th=0 ; th<mp_ID->GetNbThreadsForProjection() ; th++)
264  for (int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
265  ap_Image->m6p_backwardImage[bimg][th][fr][rimg][cimg][v] = 0.;
266 
267 
268  // FORWARD DEFORMATION of the Forward image
269  if (ApplyDeformations(ap_Image->m4p_forwardImage[fr][rimg][cimg], ap_Image->m4p_forwardImage[fr][rimg][cimg], FORWARD_DEFORMATION, a_defIdx) )
270  {
271  Cerr("***** vDeformation::ApplyDeformations()-> An error occured while performing forward deformation of the forward image !" << endl);
272  Cerr("***** frame index " << fr << " respiratory image index " << rimg<< " cardiac image index " << cimg<< " !" << endl);
273  return 1;
274  }
275  return 0;
276 }
277 
278 
279 
280 
281 // =====================================================================
282 // ---------------------------------------------------------------------
283 // ---------------------------------------------------------------------
284 // =====================================================================
285 /*
286  \fn PerformHistoSensitivityDeformation
287  \param ap_Image : required to access oImageSpace image matrices
288  \param a_defIdx : index of the deformation
289  \param fr : frame index
290  \param rimg : respiratory image index
291  \param cimg : cardiac image index
292  \brief Apply deformations on the sensitivity image during reconstruction in histogram mode
293  \details 1. Recover all the data of the multithreaded sensitivity image matrice in the first one (thread index 0)
294  2. Perform backward deformation of the sensitivity image to the reference position with defIdx-1
295  3. Add coefficients of the sensitivity image matrice to the temporary backup image matrice & reset sensitivity image
296  \return 0 if success, positive value otherwise
297 */
298 int vDeformation::PerformHistoSensitivityDeformation(oImageSpace* ap_Image, int a_defIdx, int fr, int rimg, int cimg)
299 {
300  if(m_verbose >= 2) Cout("vDeformation::PerformHistoSensitivityDeformation ... " <<endl);
301 
302  #ifdef CASTOR_DEBUG
303  if (!m_initialized)
304  {
305  Cerr("***** vDeformation::PerformHistoSensitivityDeformation() -> Called while not initialized !" << endl);
306  Exit(EXIT_DEBUG);
307  }
308  #endif
309 
310  // REDUCE
311  for (int th=1 ; th<mp_ID->GetNbThreadsForProjection() ; th++)
312  {
313  // Synchronisation of the multi-threaded sensitivity images inside the first image )
314  for(int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
315  ap_Image->m5p_sensitivity[0][fr][rimg][cimg][v] += ap_Image->m5p_sensitivity[th][fr][rimg][cimg][v];
316  }
317 
318  // BACKWARD DEFORMATION of the sensitivity image
319  if (ApplyDeformations(ap_Image->m5p_sensitivity[0][fr][rimg][cimg], ap_Image->m5p_sensitivity[0][fr][rimg][cimg], BACKWARD_DEFORMATION, a_defIdx-1) )
320  {
321  Cerr("***** vDeformation::PerformHistoSensitivityDeformation()-> An error occured while performing backward deformation of the sensitivity image !" << endl);
322  Cerr("***** frame index " << fr << " respiratory image index " << rimg<< " cardiac image index " << cimg<< " !" << endl);
323  return 1;
324  }
325 
326  // Store Backward deformation update coefficients in temporary image
327  for (int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
328  {
329  ap_Image->m4p_defTmpSensitivityImage[fr][rimg][cimg][v] += ap_Image->m5p_sensitivity[0][fr][rimg][cimg][v];
330  }
331 
332  // Reset the sensitivity images (i.e set all voxels to the specific fr/rimg/cimg to 0)
333  for (int th=0; th<mp_ID->GetNbThreadsForProjection(); th++)
334  for (int v=0; v<mp_ID->GetNbVoxXYZ(); v++)
335  ap_Image->m5p_sensitivity[th][fr][rimg][cimg][v] = 0.;
336 
337  return 0;
338 }
339 
340 
341 
342 
343 // =====================================================================
344 // ---------------------------------------------------------------------
345 // ---------------------------------------------------------------------
346 // =====================================================================
347 /*
348  \fn PerformSensitivityDeformation
349  \param ap_Image : required to access oImageSpace image matrices
350  \param a_defDirection : a direction for the deformation to perform (forward or backward)
351  \param a_defIdx : index of the deformation
352  \param fr : frame index
353  \param rg : respiratory gate index
354  \param cg : cardiac gate index
355  \brief Apply image deformations during sensitivity image generation for list-mode
356  \details Depending on the deformation direction (forward or backward):
357  Forward : Perform forward deformation of the forward image to the deformation index position
358  Backward: Perform backward deformation of the backward image to the reference position
359  \return 0 if success, positive value otherwise
360 */
361 int vDeformation::PerformSensitivityDeformation(oImageSpace* ap_Image, int a_defDirection, int a_defIdx, int fr, int rg, int cg)
362 {
363  if(m_verbose >= 2) Cout("vDeformation::PerformSensitivityDeformation ... " <<endl);
364 
365  #ifdef CASTOR_DEBUG
366  if (!m_initialized)
367  {
368  Cerr("***** vDeformation::PerformSensitivityDeformation() -> Called while not initialized !" << endl);
369  Exit(EXIT_DEBUG);
370  }
371  #endif
372 
373  if (a_defDirection == FORWARD_DEFORMATION)
374  {
375  if (ApplyDeformations(ap_Image->m4p_forwardImage[fr][rg][cg], ap_Image->m4p_forwardImage[fr][rg][cg], a_defDirection, a_defIdx) )
376  {
377  Cerr("***** vDeformation::PerformSensitivityDeformation -> An error occured while performing forward deformation !" << endl);
378  Cerr("***** frame index " << fr << " respiratory gate index " << rg<< " cardiac gate index " << cg<< " !" << endl);
379  return 1;
380  }
381  }
382  else if (a_defDirection == BACKWARD_DEFORMATION)
383  {
384  if (ApplyDeformations(ap_Image->m6p_backwardImage[0][0][fr][rg][cg], ap_Image->m6p_backwardImage[0][0][fr][rg][cg], a_defDirection, a_defIdx) )
385  {
386  Cerr("***** vDeformation::PerformSensitivityDeformation -> An error occured while performing backward deformation !" << endl);
387  Cerr("***** frame index " << fr << " respiratory gate index " << rg<< " cardiac gate index " << cg<< " !" << endl);
388  return 1;
389  }
390  }
391  else
392  {
393  Cerr("***** vDeformation::PerformDeformation -> Unknown type of deformation !" << endl);
394  return 1;
395  }
396 
397  return 0;
398 }
FLTNB **** m4p_forwardImage
Definition: oImageSpace.hh:68
Declaration of class oImageDimensionsAndQuantification.
vDeformation()
Constructor of vDeformation. Simply set all data members to default values.
Definition: vDeformation.cc:31
virtual ~vDeformation()
Destructor of vDeformation.
Definition: vDeformation.cc:51
oImageDimensionsAndQuantification * mp_ID
int m_nbTransformations
virtual int PerformHistoSensitivityDeformation(oImageSpace *ap_Image, int a_defIdx, int fr, int rimg, int cimg)
Apply deformations on the sensitivity image during reconstruction in histogram mode.
virtual int CheckParameters()
This function is used to check parameters after the latter have been all set using Set functions...
Definition: vDeformation.cc:66
void Exit(int code)
virtual int ApplyDeformationsToBackwardImage(oImageSpace *ap_Image, int a_defIdx)
Apply backward transformation of the backward image to the reference position.
#define Cerr(MESSAGE)
virtual int ApplyDeformations(FLTNB *ap_inputImage, FLTNB *ap_outputImage, int a_direction, int a_defIdx)=0
This function prepares the deformation to perform It is a virtual pure deformation function to be i...
FLTNB ****** m6p_backwardImage
Definition: oImageSpace.hh:75
virtual int PerformDeformation(oImageSpace *ap_Image, int a_defIdx, int fr, int rimg, int cimg)
Apply deformations during reconstruction.
#define BACKWARD_DEFORMATION
Definition: vDeformation.hh:16
int GetNbBackwardImages()
Definition: oImageSpace.hh:559
Declaration of class vDeformation.
Declaration of class oImageSpace.
int GetNbCardGates()
Get the number of cardiac gates.
virtual int PerformSensitivityDeformation(oImageSpace *ap_Image, int a_defDirection, int a_defIdx, int fr, int rg, int cg)
Apply image deformations during sensitivity image generation for list-mode.
FLTNB ***** m5p_defTmpBackwardImage
Definition: oImageSpace.hh:124
This class holds all the matrices in the image domain that can be used in the algorithm: image...
Definition: oImageSpace.hh:41
int GetNbTimeFrames()
Get the number of time frames.
INTNB GetNbVoxXYZ()
Get the total number of voxels.
#define EXIT_DEBUG
Definition: gVariables.hh:69
FLTNB **** m4p_defTmpSensitivityImage
Definition: oImageSpace.hh:135
int GetNbThreadsForProjection()
Get the number of threads used for projections.
virtual int CheckSpecificParameters()=0
This function is used to check the parameters of the child functions before initialization if require...
int GetNbRespGates()
Get the number of respiratory gates.
#define Cout(MESSAGE)
#define FORWARD_DEFORMATION
Definition: vDeformation.hh:15
virtual int ApplyDeformationsToHistoSensitivityImage(oImageSpace *ap_Image, int a_defIdx)
Apply backward transformations of the sensitivity image to the reference position (histogram mode) ...
FLTNB ***** m5p_sensitivity
Definition: oImageSpace.hh:85