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