r574: no message
[ctsim.git] / libctsim / backprojectors.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:         backprojectors.cpp         Classes for backprojection
5 **   Programmer:   Kevin Rosenberg
6 **   Date Started: June 2000
7 **
8 **  This is part of the CTSim program
9 **  Copyright (c) 1983-2001 Kevin Rosenberg
10 **
11 **  $Id: backprojectors.cpp,v 1.28 2001/02/23 02:06:01 kevin Exp $
12 **
13 **  This program is free software; you can redistribute it and/or modify
14 **  it under the terms of the GNU General Public License (version 2) as
15 **  published by the Free Software Foundation.
16 **
17 **  This program is distributed in the hope that it will be useful,
18 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 **  GNU General Public License for more details.
21 **
22 **  You should have received a copy of the GNU General Public License
23 **  along with this program; if not, write to the Free Software
24 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 ******************************************************************************/
26
27 #include "ct.h"
28
29 const int Backprojector::BPROJ_INVALID = -1;
30 const int Backprojector::BPROJ_TRIG = 0;
31 const int Backprojector::BPROJ_TABLE = 1;
32 const int Backprojector::BPROJ_DIFF = 2;
33 const int Backprojector::BPROJ_IDIFF = 3;
34
35 const char* const Backprojector::s_aszBackprojectName[] = 
36 {
37   {"trig"},
38   {"table"},
39   {"diff"},
40   {"idiff"},
41 };
42
43 const char* const Backprojector::s_aszBackprojectTitle[] = 
44 {
45   {"Direct Trigometric"},
46   {"Trigometric Table"},
47   {"Difference Iteration"},
48   {"Integer Difference Iteration"},
49 };
50
51 const int Backprojector::s_iBackprojectCount = sizeof(s_aszBackprojectName) / sizeof(const char*);
52
53 const int Backprojector::INTERP_INVALID = -1;
54 const int Backprojector::INTERP_NEAREST = 0;
55 const int Backprojector::INTERP_LINEAR = 1;
56 const int Backprojector::INTERP_CUBIC = 2;
57 const int Backprojector::INTERP_FREQ_PREINTERPOLATION = 3;
58 #if HAVE_BSPLINE_INTERP
59 const int Backprojector::INTERP_BSPLINE = 4;
60 const int Backprojector::INTERP_1BSPLINE = 5;
61 const int Backprojector::INTERP_2BSPLINE = 6;
62 const int Backprojector::INTERP_3BSPLINE = 7;
63 #endif
64
65 const char* const Backprojector::s_aszInterpName[] = 
66 {
67   {"nearest"},
68   {"linear"},
69   {"cubic"},
70 #if HAVE_FREQ_PREINTERP
71   {"freq_preinterpolationj"},
72 #endif
73 #if HAVE_BSPLINE_INTERP
74   {"bspline"},
75   {"1bspline"},
76   {"2bspline"},
77   {"3bspline"},
78 #endif
79 };
80
81 const char* const Backprojector::s_aszInterpTitle[] = 
82 {
83   {"Nearest"},
84   {"Linear"},
85   {"Cubic"},
86 #if HAVE_FREQ_PREINTERP
87   {"Frequency Preinterpolation"},
88 #endif
89 #if HAVE_BSPLINE_INTERP
90   {"B-Spline"},
91   {"B-Spline 1st Order"},
92   {"B-Spline 2nd Order"},
93   {"B-Spline 3rd Order"},
94 #endif
95 };
96
97 const int Backprojector::s_iInterpCount = sizeof(s_aszInterpName) / sizeof(const char*);
98
99
100
101 Backprojector::Backprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor)
102 {
103   m_fail = false;
104   m_pBackprojectImplem = NULL;
105   
106   initBackprojector (proj, im, backprojName, interpName, interpFactor);
107 }
108
109 void 
110 Backprojector::BackprojectView (const double* const viewData, const double viewAngle)
111 {
112   if (m_pBackprojectImplem != NULL)
113     m_pBackprojectImplem->BackprojectView (viewData, viewAngle);
114 }
115
116 void 
117 Backprojector::PostProcessing()
118 {
119   if (m_pBackprojectImplem != NULL)
120     m_pBackprojectImplem->PostProcessing();
121 }
122
123 Backprojector::~Backprojector ()
124 {
125   delete m_pBackprojectImplem;
126 }
127
128 // FUNCTION IDENTIFICATION
129 //     Backproject* projector = selectBackprojector (...)
130 //
131 // PURPOSE
132 //     Selects a backprojector based on BackprojType 
133 //     and initializes the backprojector
134
135 bool
136 Backprojector::initBackprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor)
137 {
138   m_nameBackproject = backprojName;
139   m_nameInterpolation = interpName;
140   m_pBackprojectImplem = NULL;
141   m_idBackproject = convertBackprojectNameToID (backprojName);
142   if (m_idBackproject == BPROJ_INVALID) {
143     m_fail = true;
144     m_failMessage = "Invalid backprojection name ";
145     m_failMessage += backprojName;
146   }
147   m_idInterpolation = convertInterpNameToID (interpName);
148   if (m_idInterpolation == INTERP_INVALID) {
149     m_fail = true;
150     m_failMessage = "Invalid interpolation name ";
151     m_failMessage += interpName;
152   }
153   
154   if (m_fail || m_idBackproject == BPROJ_INVALID || m_idInterpolation == INTERP_INVALID) {
155     m_fail = true;
156     return false;
157   }
158   
159   if (proj.geometry() == Scanner::GEOMETRY_EQUILINEAR)
160     m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectEquilinear(proj, im, m_idInterpolation, interpFactor));
161   else if (proj.geometry() == Scanner::GEOMETRY_EQUIANGULAR) 
162     m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectEquiangular(proj, im, m_idInterpolation, interpFactor));
163   else if (proj.geometry() == Scanner::GEOMETRY_PARALLEL) {
164     if (m_idBackproject == BPROJ_TRIG)
165       m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTrig (proj, im, m_idInterpolation, interpFactor));
166     else if (m_idBackproject == BPROJ_TABLE)
167       m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTable (proj, im, m_idInterpolation, interpFactor));
168     else if (m_idBackproject == BPROJ_DIFF)
169       m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectDiff (proj, im, m_idInterpolation, interpFactor));
170     else if (m_idBackproject == BPROJ_IDIFF)
171       m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectIntDiff (proj, im, m_idInterpolation, interpFactor));
172   } else {
173     m_fail = true;
174     m_failMessage = "Unable to select a backprojection method [Backprojector::initBackprojector]";
175     return false;
176   }
177   
178   return true;
179 }
180
181
182 int
183 Backprojector::convertBackprojectNameToID (const char* const backprojName)
184 {
185   int backprojID = BPROJ_INVALID;
186   
187   for (int i = 0; i < s_iBackprojectCount; i++)
188     if (strcasecmp (backprojName, s_aszBackprojectName[i]) == 0) {
189       backprojID = i;
190       break;
191     }
192     
193     return (backprojID);
194 }
195
196 const char*
197 Backprojector::convertBackprojectIDToName (int bprojID)
198 {
199   static const char *bprojName = "";
200   
201   if (bprojID >= 0 && bprojID < s_iBackprojectCount)
202     return (s_aszBackprojectName[bprojID]);
203   
204   return (bprojName);
205 }
206
207 const char*
208 Backprojector::convertBackprojectIDToTitle (const int bprojID)
209 {
210   static const char *bprojTitle = "";
211   
212   if (bprojID >= 0 && bprojID < s_iBackprojectCount)
213     return (s_aszBackprojectTitle[bprojID]);
214   
215   return (bprojTitle);
216 }
217
218
219 int
220 Backprojector::convertInterpNameToID (const char* const interpName)
221 {
222   int interpID = INTERP_INVALID;
223   
224   for (int i = 0; i < s_iInterpCount; i++)
225     if (strcasecmp (interpName, s_aszInterpName[i]) == 0) {
226       interpID = i;
227       break;
228     }
229     
230     return (interpID);
231 }
232
233 const char*
234 Backprojector::convertInterpIDToName (const int interpID)
235 {
236   static const char *interpName = "";
237   
238   if (interpID >= 0 && interpID < s_iInterpCount)
239     return (s_aszInterpName[interpID]);
240   
241   return (interpName);
242 }
243
244 const char*
245 Backprojector::convertInterpIDToTitle (const int interpID)
246 {
247   static const char *interpTitle = "";
248   
249   if (interpID >= 0 && interpID < s_iInterpCount)
250     return (s_aszInterpTitle[interpID]);
251   
252   return (interpTitle);
253 }
254
255
256
257 // CLASS IDENTICATION
258 //   Backproject
259 //
260 // PURPOSE
261 //   Pure virtual base class for all backprojectors.
262
263 Backproject::Backproject (const Projections& proj, ImageFile& im, int interpType, const int interpFactor)
264 : proj(proj), im(im), interpType(interpType), m_interpFactor(interpFactor), m_bPostProcessingDone(false)
265 {
266   detInc = proj.detInc();
267   nDet = proj.nDet();
268   iDetCenter = (nDet - 1) / 2;  // index refering to L=0 projection 
269   rotScale = proj.rotInc();
270   
271   if (proj.geometry() == Scanner::GEOMETRY_PARALLEL)
272     rotScale /= (proj.nView() * proj.rotInc() / PI); // scale by number of PI rotations
273   else if (proj.geometry() == Scanner::GEOMETRY_EQUIANGULAR || proj.geometry() == Scanner::GEOMETRY_EQUILINEAR)
274     rotScale /= (proj.nView() * proj.rotInc() / (2 * PI)); // scale by number of 2PI rotations
275   else
276     sys_error (ERR_SEVERE, "Invalid geometry type %d [Backproject::Backproject]", proj.geometry());
277   
278   v = im.getArray();
279   nx = im.nx();
280   ny = im.ny();
281   im.arrayDataClear();
282   
283   xMin = -proj.phmLen() / 2;      // Retangular coords of phantom
284   xMax = xMin + proj.phmLen();
285   yMin = -proj.phmLen() / 2;
286   yMax = yMin + proj.phmLen();
287   
288   xInc = (xMax - xMin) / nx;    // size of cells
289   yInc = (yMax - yMin) / ny;
290   
291   m_dFocalLength = proj.focalLength();
292 }
293
294 Backproject::~Backproject ()
295 {}
296
297 void
298 Backproject::PostProcessing()
299 {
300   m_bPostProcessingDone = true;
301 }
302
303 void
304 Backproject::ScaleImageByRotIncrement ()
305 {
306   for (int ix = 0; ix < nx; ix++)
307     for (int iy = 0; iy < ny; iy++)
308       v[ix][iy] *= rotScale;
309 }
310
311 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double r, double phi, double L, int iDetPos)
312 {
313   sys_error (ERR_WARNING, "r=%f, phi=%f", r, phi);
314   errorIndexOutsideDetector (ix, iy, theta, L, iDetPos);
315 }
316
317 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double L, int iDetPos)
318 {
319 #if 1
320   std::ostringstream os;
321   os << "ix=" << ix << ", iy=" << iy << ", theta=" << theta << ", L=" << L << ", detinc=" << detInc << "\n";
322   os << "ndet=" << nDet << ", detInc=" << detInc << ", iDetCenter=" << iDetCenter << "\n";
323   os << "xMin=" << xMin << ", xMax=" << xMax << ", xInc=" << xInc << "\n";
324   os << "yMin=" << yMin << ", yMax=" << yMax << ", yInc=" << yInc << "\n";
325   os << "iDetPos index outside bounds: " << iDetPos << " [backprojector]";;
326   
327   sys_error (ERR_WARNING, os.str().c_str());
328 #endif
329 }
330
331
332 // CLASS IDENTICATION
333 //   BackprojectTrig
334 //
335 // PURPOSE
336 //   Uses trigometric functions at each point in image for backprojection.
337
338 void
339 BackprojectTrig::BackprojectView (const double* const filteredProj, const double view_angle)
340 {
341   double theta = view_angle;
342   
343   CubicPolyInterpolator* pCubicInterp = NULL;
344   if (interpType == Backprojector::INTERP_CUBIC)
345     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
346   
347   double x = xMin + xInc / 2;   // Rectang coords of center of pixel 
348   for (int ix = 0; ix < nx; x += xInc, ix++) {
349     double y = yMin + yInc / 2;
350     for (int iy = 0; iy < ny; y += yInc, iy++) {
351       double r = sqrt (x * x + y * y);   // distance of cell from center
352       double phi = atan2 (y, x);         // angle of cell from center
353       double L = r * cos (theta - phi);  // position on detector
354       
355       if (interpType == Backprojector::INTERP_NEAREST) {
356         int iDetPos = iDetCenter + nearest<int> (L / detInc); // calc'd index in the filter raysum array
357         
358         if (iDetPos >= 0 && iDetPos < nDet)
359           v[ix][iy] += rotScale * filteredProj[iDetPos];
360       } else if (interpType == Backprojector::INTERP_LINEAR) {
361         double p = L / detInc;  // position along detector
362         double pFloor = floor (p);
363         int iDetPos = iDetCenter + static_cast<int>(pFloor);
364         double frac = p - pFloor;       // fraction distance from det
365         if (iDetPos >= 0 && iDetPos < nDet - 1)
366           v[ix][iy] += rotScale * ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
367       } else if (interpType = Backprojector::INTERP_CUBIC) {
368         double p = iDetCenter + (L / detInc);   // position along detector
369         if (p >= 0 && p < nDet)
370           v[ix][iy] += rotScale * pCubicInterp->interpolate (p);
371       }
372     }
373   }
374
375   if (interpType == Backprojector::INTERP_CUBIC)
376     delete pCubicInterp;
377 }  
378
379
380 // CLASS IDENTICATION
381 //   BackprojectTable
382 //
383 // PURPOSE
384 //   Precalculates trigometric function value for each point in image for backprojection.
385
386 BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, int interpType, const int interpFactor)
387 : Backproject (proj, im, interpType, interpFactor)
388 {
389   arrayR.initSetSize (im.nx(), im.ny());
390   arrayPhi.initSetSize (im.nx(), im.ny());
391   r = arrayR.getArray();
392   phi = arrayPhi.getArray();
393   
394   double x, y;                  // Rectang coords of center of pixel 
395   int ix, iy;
396   for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++)
397     for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) {
398       r[ix][iy] = sqrt (x * x + y * y);
399       phi[ix][iy] = atan2 (y, x);
400     }
401 }
402
403 BackprojectTable::~BackprojectTable ()
404 {
405 }
406
407 void
408 BackprojectTable::PostProcessing()
409 {
410   if (! m_bPostProcessingDone) {
411     ScaleImageByRotIncrement();
412     m_bPostProcessingDone = true;
413   }
414 }
415
416 void
417 BackprojectTable::BackprojectView (const double* const filteredProj, const double view_angle)
418 {
419   double theta = view_angle;
420   
421   CubicPolyInterpolator* pCubicInterp = NULL;
422   if (interpType == Backprojector::INTERP_CUBIC)
423     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
424   
425   for (int ix = 0; ix < nx; ix++) {
426     ImageFileColumn pImCol = v[ix];
427     
428     for (int iy = 0; iy < ny; iy++) {
429       double L = r[ix][iy] * cos (theta - phi[ix][iy]);
430       
431       if (interpType == Backprojector::INTERP_NEAREST) {
432         int iDetPos = iDetCenter + nearest<int>(L / detInc);    // calc index in the filtered raysum vector 
433         
434         if (iDetPos >= 0 && iDetPos < nDet)
435           pImCol[iy] += filteredProj[iDetPos];
436       } else if (interpType == Backprojector::INTERP_LINEAR) {
437         double dPos = L / detInc;               // position along detector 
438         double dPosFloor = floor (dPos);
439         int iDetPos = iDetCenter + static_cast<int>(dPosFloor);
440         double frac = dPos - dPosFloor; // fraction distance from det 
441         if (iDetPos >= 0 && iDetPos < nDet - 1)
442           pImCol[iy] += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
443       } else if (interpType = Backprojector::INTERP_CUBIC) {
444         double p = iDetCenter + (L / detInc);   // position along detector
445         if (p >= 0 && p < nDet)
446           pImCol[iy] += pCubicInterp->interpolate (p);
447       }
448     }   // end for y 
449   }     // end for x 
450
451   if (interpType == Backprojector::INTERP_CUBIC)
452     delete pCubicInterp;
453 }
454
455
456 // CLASS IDENTICATION
457 //   BackprojectDiff
458 //
459 // PURPOSE
460 //   Backprojects by precalculating the change in L position for each x & y step in the image.
461 //   Iterates in x & y direction by adding difference in L position
462
463 BackprojectDiff::BackprojectDiff (const Projections& proj, ImageFile& im, int interpType, const int interpFactor)
464 :  Backproject (proj, im, interpType, interpFactor)
465 {
466   // calculate center of first pixel v[0][0] 
467   double x = xMin + xInc / 2;
468   double y = yMin + yInc / 2;
469   start_r = sqrt (x * x + y * y);
470   start_phi = atan2 (y, x);
471   
472   im.arrayDataClear();
473 }
474
475 BackprojectDiff::~BackprojectDiff ()
476 {
477 }
478
479 void
480 BackprojectDiff::PostProcessing()
481 {
482   if (! m_bPostProcessingDone) {
483     ScaleImageByRotIncrement();
484     m_bPostProcessingDone = true;
485   }
486 }
487
488 void
489 BackprojectDiff::BackprojectView (const double* const filteredProj, const double view_angle)
490 {
491   double theta = view_angle;
492   
493   // Distance between detectors for an angle given in units of detectors 
494   double det_dx = xInc * cos (theta) / detInc;
495   double det_dy = yInc * sin (theta) / detInc;
496   
497   // calculate detPosition for first point in image (ix=0, iy=0) 
498   double detPosColStart = start_r * cos (theta - start_phi) / detInc;
499   
500   CubicPolyInterpolator* pCubicInterp = NULL;
501   if (interpType == Backprojector::INTERP_CUBIC)
502     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
503   
504   for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
505     double curDetPos = detPosColStart;
506     ImageFileColumn pImCol = v[ix];
507     
508     for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
509       if (interpType == Backprojector::INTERP_NEAREST) {
510         int iDetPos = iDetCenter + nearest<int> (curDetPos);    // calc index in the filtered raysum vector 
511         
512         if (iDetPos >= 0 && iDetPos < nDet)
513           *pImCol++ += filteredProj[iDetPos];
514       } else if (interpType == Backprojector::INTERP_LINEAR) {
515         double detPosFloor = floor (curDetPos);
516         int iDetPos = iDetCenter + static_cast<int>(detPosFloor);
517         double frac = curDetPos - detPosFloor;  // fraction distance from det 
518         if (iDetPos > 0 && iDetPos < nDet - 1)
519           *pImCol++ += filteredProj[iDetPos] + (frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos]));
520       } else if (interpType = Backprojector::INTERP_CUBIC) {
521         double p = iDetCenter + curDetPos;      // position along detector
522         if (p >= 0 && p < nDet)
523           *pImCol++  += pCubicInterp->interpolate (p);
524       }
525     }   // end for y
526   }     // end for x
527
528   if (interpType == Backprojector::INTERP_CUBIC)
529     delete pCubicInterp;
530 }
531
532
533 // CLASS IDENTICATION
534 //   BackprojectIntDiff
535 //
536 // PURPOSE
537 //   Highly optimized and integer version of BackprojectDiff
538
539 void
540 BackprojectIntDiff::BackprojectView (const double* const filteredProj, const double view_angle)
541 {
542   double theta = view_angle;  // add half PI to view angle to get perpendicular theta angle
543   static const int scaleShift = 16;
544   static const kint32 scale = (1 << scaleShift);
545   static const kint32 scaleBitmask = scale - 1;
546   static const kint32 halfScale = scale / 2;
547   static const double dInvScale = 1. / scale;
548   
549   const kint32 det_dx = nearest<kint32> (xInc * cos (theta) / detInc * scale);
550   const kint32 det_dy = nearest<kint32> (yInc * sin (theta) / detInc * scale);
551   
552   // calculate L for first point in image (0, 0) 
553   kint32 detPosColStart = nearest<kint32> ((start_r * cos (theta - start_phi) / detInc + iDetCenter) * scale);
554   
555   double* deltaFilteredProj = NULL;  
556   CubicPolyInterpolator* pCubicInterp = NULL;
557   if (interpType == Backprojector::INTERP_LINEAR) {
558     // precalculate scaled difference for linear interpolation
559     deltaFilteredProj = new double [nDet];
560     for (int i = 0; i < nDet - 1; i++)
561       deltaFilteredProj[i] = (filteredProj[i+1] - filteredProj[i]) * dInvScale;
562     deltaFilteredProj[nDet - 1] = 0;  // last detector
563   } else if (interpType == Backprojector::INTERP_CUBIC) {
564     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
565   }
566   
567   int iLastDet = nDet - 1;
568   for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
569     kint32 curDetPos = detPosColStart;
570     ImageFileColumn pImCol = v[ix];
571     
572     if (interpType == Backprojector::INTERP_NEAREST) {
573       for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
574         const int iDetPos = (curDetPos + halfScale) >> 16;
575         if (iDetPos >= 0 && iDetPos <= iLastDet)
576           *pImCol++ += filteredProj[iDetPos];
577       } // end for iy
578     } else if (interpType == Backprojector::INTERP_FREQ_PREINTERPOLATION) {
579       for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
580         const int iDetPos = ((curDetPos + halfScale) >> 16) * m_interpFactor;
581         if (iDetPos >= 0 && iDetPos <= iLastDet)
582           *pImCol++ += filteredProj[iDetPos];
583       } // end for iy
584     } else if (interpType == Backprojector::INTERP_LINEAR) {
585       for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
586         const kint32 iDetPos = curDetPos >> scaleShift;
587         const kint32 detRemainder = curDetPos & scaleBitmask;
588         if (iDetPos >= 0 && iDetPos <= iLastDet)
589           *pImCol++ += filteredProj[iDetPos] + (detRemainder * deltaFilteredProj[iDetPos]);
590       } // end for iy
591     } else if (interpType = Backprojector::INTERP_CUBIC) {
592       for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
593         *pImCol++ += pCubicInterp->interpolate (static_cast<double>(curDetPos) / 65536);
594       }
595     } // end Cubic
596   } // end for ix
597   
598   if (interpType == Backprojector::INTERP_LINEAR)
599     delete deltaFilteredProj;
600   else if (interpType == Backprojector::INTERP_CUBIC)
601     delete pCubicInterp;
602 }
603
604
605 void
606 BackprojectEquiangular::BackprojectView (const double* const filteredProj, const double view_angle)
607 {
608   double beta = view_angle;
609   
610   CubicPolyInterpolator* pCubicInterp = NULL;
611   if (interpType == Backprojector::INTERP_CUBIC)
612     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
613   
614   for (int ix = 0; ix < nx; ix++) {
615     ImageFileColumn pImCol = v[ix];
616     
617     for (int iy = 0; iy < ny; iy++) { 
618       double dAngleDiff = beta - phi[ix][iy];
619       double rcos_t = r[ix][iy] * cos (dAngleDiff);
620       double rsin_t = r[ix][iy] * sin (dAngleDiff);
621       double dFLPlusSin = m_dFocalLength + rsin_t;
622       double gamma =  atan (rcos_t / dFLPlusSin);
623       double dPos = gamma / detInc;  // position along detector
624       double dL2 = dFLPlusSin * dFLPlusSin + (rcos_t * rcos_t);
625       
626       if (interpType == Backprojector::INTERP_NEAREST) {
627         int iDetPos = iDetCenter + nearest<int>(dPos);  // calc index in the filtered raysum vector      
628         if (iDetPos >= 0 && iDetPos < nDet)
629           pImCol[iy] += filteredProj[iDetPos] / dL2;
630       } else if (interpType == Backprojector::INTERP_LINEAR) {
631         double dPosFloor = floor (dPos);
632         int iDetPos = iDetCenter + static_cast<int>(dPosFloor);
633         double frac = dPos - dPosFloor; // fraction distance from det 
634         if (iDetPos >= 0 && iDetPos < nDet - 1)
635           pImCol[iy] += (filteredProj[iDetPos] + frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos])) / dL2;
636       } else if (interpType == Backprojector::INTERP_CUBIC) {
637         double d = iDetCenter + dPos;           // position along detector 
638         if (d >= 0 && d < nDet)
639           pImCol[iy] += pCubicInterp->interpolate (d) / dL2;
640       }
641     }   // end for y 
642   }     // end for x 
643
644   if (interpType == Backprojector::INTERP_CUBIC)
645     delete pCubicInterp;
646 }
647
648 void
649 BackprojectEquilinear::BackprojectView (const double* const filteredProj, const double view_angle)
650 {
651   double beta = view_angle;
652   
653   CubicPolyInterpolator* pCubicInterp = NULL;
654   if (interpType == Backprojector::INTERP_CUBIC)
655     pCubicInterp = new CubicPolyInterpolator (filteredProj, nDet);
656   
657   for (int ix = 0; ix < nx; ix++) {
658     ImageFileColumn pImCol = v[ix];
659     
660     for (int iy = 0; iy < ny; iy++) {
661       double dAngleDiff = beta - phi[ix][iy];
662       double rcos_t = r[ix][iy] * cos (dAngleDiff);
663       double rsin_t = r[ix][iy] * sin (dAngleDiff);
664       
665       double dU = (m_dFocalLength + rsin_t) / m_dFocalLength;
666       double dDetPos =  rcos_t / dU;
667       // double to scale for imaginary detector that passes through origin
668       // of phantom, see Kak-Slaney Figure 3.22. This assumes that the detector is also
669       // located focal-length away from the origin.
670       dDetPos *= 2; 
671       double dPos = dDetPos / detInc;  // position along detector array 
672
673       if (interpType == Backprojector::INTERP_NEAREST) {
674         int iDetPos = iDetCenter + nearest<int>(dPos);  // calc index in the filtered raysum vector 
675         if (iDetPos >= 0 && iDetPos < nDet)     
676           pImCol[iy] += (filteredProj[iDetPos] / (dU * dU));
677       } else if (interpType == Backprojector::INTERP_LINEAR) {
678         double dPosFloor = floor (dPos);
679         int iDetPos = iDetCenter + static_cast<int>(dPosFloor);
680         double frac = dPos - dPosFloor; // fraction distance from det 
681         if (iDetPos >= 0 && iDetPos < nDet - 1)
682           pImCol[iy] += (filteredProj[iDetPos] + frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos]))
683                            / (dU * dU);
684       } else if (interpType == Backprojector::INTERP_CUBIC) {
685         double d = iDetCenter + dPos;           // position along detector 
686         if (d >= 0 && d < nDet)
687           pImCol[iy] += pCubicInterp->interpolate (d) / (dU * dU);
688       }
689     }   // end for y 
690   }     // end for x 
691
692   if (interpType == Backprojector::INTERP_CUBIC)
693     delete pCubicInterp;
694 }
695