1 /*****************************************************************************
4 ** Name: backprojectors.cpp Classes for backprojection
5 ** Programmer: Kevin Rosenberg
6 ** Date Started: June 2000
8 ** This is part of the CTSim program
9 ** Copyright (C) 1983-2000 Kevin Rosenberg
11 ** $Id: backprojectors.cpp,v 1.12 2000/08/25 15:59:13 kevin Exp $
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.
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.
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 ******************************************************************************/
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;
37 const char* Backprojector::s_aszBackprojectName[] =
47 const char* Backprojector::s_aszBackprojectTitle[] =
49 {"Direct Trigometric"},
50 {"Trigometric Table"},
51 {"Difference Iteration"},
52 {"Difference Iteration Optimized"},
53 {"Integer Difference Iteration Optimized"},
54 {"Integer Difference Iteration Highly-Optimized"},
57 const int Backprojector::s_iBackprojectCount = sizeof(s_aszBackprojectName) / sizeof(const char*);
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;
70 const char* Backprojector::s_aszInterpName[] =
74 {"freq_preinterpolationj"},
75 #if HAVE_BSPLINE_INTERP
83 const char* Backprojector::s_aszInterpTitle[] =
87 {"Frequency Preinterpolationj"},
88 #if HAVE_BSPLINE_INTERP
90 {"B-Spline 1st Order"},
91 {"B-Spline 2nd Order"},
92 {"B-Spline 3rd Order"},
96 const int Backprojector::s_iInterpCount = sizeof(s_aszInterpName) / sizeof(const char*);
100 Backprojector::Backprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor)
103 m_pBackprojectImplem = NULL;
105 initBackprojector (proj, im, backprojName, interpName, interpFactor);
109 Backprojector::BackprojectView (const double* const viewData, const double viewAngle)
111 if (m_pBackprojectImplem != NULL)
112 m_pBackprojectImplem->BackprojectView (viewData, viewAngle);
115 Backprojector::~Backprojector ()
117 delete m_pBackprojectImplem;
120 // FUNCTION IDENTIFICATION
121 // Backproject* projector = selectBackprojector (...)
124 // Selects a backprojector based on BackprojType
125 // and initializes the backprojector
128 Backprojector::initBackprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor)
130 m_nameBackproject = backprojName;
131 m_nameInterpolation = interpName;
132 m_pBackprojectImplem = NULL;
133 m_idBackproject = convertBackprojectNameToID (backprojName);
134 if (m_idBackproject == BPROJ_INVALID) {
136 m_failMessage = "Invalid backprojection name ";
137 m_failMessage += backprojName;
139 m_idInterpolation = convertInterpNameToID (interpName);
140 if (m_idInterpolation == INTERP_INVALID) {
142 m_failMessage = "Invalid interpolation name ";
143 m_failMessage += interpName;
146 if (m_fail || m_idBackproject == BPROJ_INVALID || m_idInterpolation == INTERP_INVALID) {
151 if (m_idBackproject == BPROJ_TRIG)
152 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTrig (proj, im, m_idInterpolation, interpFactor));
153 else if (m_idBackproject == BPROJ_TABLE)
154 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTable (proj, im, m_idInterpolation, interpFactor));
155 else if (m_idBackproject == BPROJ_DIFF)
156 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectDiff (proj, im, m_idInterpolation, interpFactor));
157 else if (m_idBackproject == BPROJ_DIFF2)
158 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectDiff2 (proj, im, m_idInterpolation, interpFactor));
159 else if (m_idBackproject == BPROJ_IDIFF2)
160 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectIntDiff2 (proj, im, m_idInterpolation, interpFactor));
161 else if (m_idBackproject == BPROJ_IDIFF3)
162 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectIntDiff3 (proj, im, m_idInterpolation, interpFactor));
165 m_failMessage = "Unable to select a backprojection method [Backprojector::initBackprojector]";
174 Backprojector::convertBackprojectNameToID (const char* const backprojName)
176 int backprojID = BPROJ_INVALID;
178 for (int i = 0; i < s_iBackprojectCount; i++)
179 if (strcasecmp (backprojName, s_aszBackprojectName[i]) == 0) {
188 Backprojector::convertBackprojectIDToName (int bprojID)
190 static const char *bprojName = "";
192 if (bprojID >= 0 && bprojID < s_iBackprojectCount)
193 return (s_aszBackprojectName[bprojID]);
199 Backprojector::convertBackprojectIDToTitle (const int bprojID)
201 static const char *bprojTitle = "";
203 if (bprojID >= 0 && bprojID < s_iBackprojectCount)
204 return (s_aszBackprojectTitle[bprojID]);
211 Backprojector::convertInterpNameToID (const char* const interpName)
213 int interpID = INTERP_INVALID;
215 for (int i = 0; i < s_iInterpCount; i++)
216 if (strcasecmp (interpName, s_aszInterpName[i]) == 0) {
225 Backprojector::convertInterpIDToName (const int interpID)
227 static const char *interpName = "";
229 if (interpID >= 0 && interpID < s_iInterpCount)
230 return (s_aszInterpName[interpID]);
236 Backprojector::convertInterpIDToTitle (const int interpID)
238 static const char *interpTitle = "";
240 if (interpID >= 0 && interpID < s_iInterpCount)
241 return (s_aszInterpTitle[interpID]);
243 return (interpTitle);
248 // CLASS IDENTICATION
252 // Pure virtual base class for all backprojectors.
254 Backproject::Backproject (const Projections& proj, ImageFile& im, const int interpType, const int interpFactor)
255 : proj(proj), im(im), interpType(interpType), m_interpFactor(interpFactor)
257 detInc = proj.detInc();
259 iDetCenter = (nDet - 1) / 2; // index refering to L=0 projection
260 rotInc = proj.rotInc();
267 xMin = -proj.phmLen() / 2; // Retangular coords of phantom
268 xMax = xMin + proj.phmLen();
269 yMin = -proj.phmLen() / 2;
270 yMax = yMin + proj.phmLen();
272 xInc = (xMax - xMin) / nx; // size of cells
273 yInc = (yMax - yMin) / ny;
276 Backproject::~Backproject ()
280 Backproject::ScaleImageByRotIncrement ()
282 for (int ix = 0; ix < nx; ix++)
283 for (int iy = 0; iy < ny; iy++)
287 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double r, double phi, double L, int iDetPos)
289 sys_error (ERR_WARNING, "r=%f, phi=%f", r, phi);
290 errorIndexOutsideDetector (ix, iy, theta, L, iDetPos);
293 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double L, int iDetPos)
296 os << "ix=" << ix << ", iy=" << iy << ", theta=" << theta << ", L=" << L << ", detinc=" << detInc << "\n";
297 os << "ndet=" << nDet << ", detInc=" << detInc << ", iDetCenter=" << iDetCenter << "\n";
298 os << "xMin=" << xMin << ", xMax=" << xMax << ", xInc=" << xInc << "\n";
299 os << "yMin=" << yMin << ", yMax=" << yMax << ", yInc=" << yInc << "\n";
300 os << "iDetPos index outside bounds: " << iDetPos << " [backprojector]";;
302 sys_error (ERR_WARNING, os.str().c_str());
306 // CLASS IDENTICATION
310 // Uses trigometric functions at each point in image for backprojection.
313 BackprojectTrig::BackprojectView (const double* const filteredProj, const double view_angle)
315 double theta = HALFPI + view_angle; // Add PI/2 to get perpendicular angle to detector
317 double x, y; // Rectang coords of center of pixel
319 for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++)
320 for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) {
321 double r = sqrt (x * x + y * y); // distance of cell from center
322 double phi = atan2 (y, x); // angle of cell from center
323 double L = r * cos (theta - phi); // position on detector
325 if (interpType == Backprojector::INTERP_NEAREST) {
326 int iDetPos = iDetCenter + nearest<int> (L / detInc); // calc'd index in the filter raysum array
328 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
329 errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos);
331 v[ix][iy] += rotInc * filteredProj[iDetPos];
332 } else if (interpType == Backprojector::INTERP_LINEAR) {
333 double p = L / detInc; // position along detector
334 double pFloor = floor (p);
335 int iDetPos = iDetCenter + static_cast<int>(pFloor);
336 double frac = p - pFloor; // fraction distance from det
337 if (iDetPos < 0 || iDetPos >= nDet - 1) // check for impossible: index outside of raysum pos
338 errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos);
340 v[ix][iy] += rotInc * ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
346 // CLASS IDENTICATION
350 // Precalculates trigometric function value for each point in image for backprojection.
352 BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, int interpType, const int interpFactor)
353 : Backproject::Backproject (proj, im, interpType, interpFactor)
355 arrayR.initSetSize (nx, ny);
356 arrayPhi.initSetSize (nx, ny);
357 r = arrayR.getArray();
358 phi = arrayPhi.getArray();
360 double x, y; // Rectang coords of center of pixel
362 for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++)
363 for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) {
364 r[ix][iy] = sqrt (x * x + y * y);
365 phi[ix][iy] = atan2 (y, x);
369 BackprojectTable::~BackprojectTable ()
371 ScaleImageByRotIncrement();
375 BackprojectTable::BackprojectView (const double* const filteredProj, const double view_angle)
377 double theta = HALFPI + view_angle; // add half PI to view angle to get perpendicular theta angle
379 for (int ix = 0; ix < nx; ix++) {
380 ImageFileColumn pImCol = v[ix];
382 for (int iy = 0; iy < ny; iy++) {
383 double L = r[ix][iy] * cos (theta - phi[ix][iy]);
385 if (interpType == Backprojector::INTERP_NEAREST) {
386 int iDetPos = iDetCenter + nearest<int>(L / detInc); // calc index in the filtered raysum vector
388 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
389 errorIndexOutsideDetector (ix, iy, theta, r[ix][iy], phi[ix][iy], L, iDetPos);
391 pImCol[iy] += filteredProj[iDetPos];
392 } else if (interpType == Backprojector::INTERP_LINEAR) {
393 double dPos = L / detInc; // position along detector
394 double dPosFloor = floor (dPos);
395 int iDetPos = iDetCenter + static_cast<int>(dPosFloor);
396 double frac = dPos - dPosFloor; // fraction distance from det
397 if (iDetPos < 0 || iDetPos >= nDet - 1)
398 errorIndexOutsideDetector (ix, iy, theta, r[ix][iy], phi[ix][iy], L, iDetPos);
400 pImCol[iy] += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
407 // CLASS IDENTICATION
411 // Backprojects by precalculating the change in L position for each x & y step in the image.
412 // Iterates in x & y direction by adding difference in L position
414 BackprojectDiff::BackprojectDiff (const Projections& proj, ImageFile& im, int interpType, const int interpFactor)
415 : Backproject::Backproject (proj, im, interpType, interpFactor)
417 // calculate center of first pixel v[0][0]
418 double x = xMin + xInc / 2;
419 double y = yMin + yInc / 2;
420 start_r = sqrt (x * x + y * y);
421 start_phi = atan2 (y, x);
426 BackprojectDiff::~BackprojectDiff()
428 ScaleImageByRotIncrement();
432 BackprojectDiff::BackprojectView (const double* const filteredProj, const double view_angle)
434 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
435 double det_dx = xInc * sin (theta);
436 double det_dy = yInc * cos (theta);
437 double lColStart = start_r * cos (theta - start_phi); // calculate L for first point in image
439 for (int ix = 0; ix < nx; ix++, lColStart += det_dx) {
440 double curDetPos = lColStart;
441 ImageFileColumn pImCol = v[ix];
443 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
445 printf ("[%2d,%2d]: %8.5f ", ix, iy, curDetPos);
447 if (interpType == Backprojector::INTERP_NEAREST) {
448 int iDetPos = iDetCenter + nearest<int>(curDetPos / detInc); // calc index in the filtered raysum vector
450 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
451 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
453 pImCol[iy] += filteredProj[iDetPos];
454 } else if (interpType == Backprojector::INTERP_LINEAR) {
455 double detPos = curDetPos / detInc; // position along detector
456 double detPosFloor = floor (detPos);
457 int iDetPos = iDetCenter + static_cast<int>(detPosFloor);
458 double frac = detPos - detPosFloor; // fraction distance from det
459 if (iDetPos < 0 || iDetPos >= nDet - 1)
460 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
462 pImCol[iy] += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
469 // CLASS IDENTICATION
473 // Optimized version of BackprojectDiff
476 BackprojectDiff2::BackprojectView (const double* const filteredProj, const double view_angle)
478 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
480 // Distance betw. detectors for an angle given in units of detectors
481 double det_dx = xInc * sin (theta) / detInc;
482 double det_dy = yInc * cos (theta) / detInc;
484 // calculate detPosition for first point in image (ix=0, iy=0)
485 double detPosColStart = start_r * cos (theta - start_phi) / detInc;
488 printf ("start_r=%8.5f, start_phi=%8.5f, rotInc=%8.5f\n", start_r, start_phi, rotInc);
490 for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
491 double curDetPos = detPosColStart;
492 ImageFileColumn pImCol = v[ix];
494 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
496 printf ("[%2d,%2d]: %8.5f %8.5f\n", ix, iy, curDetPos, filteredProj[iDetCenter + nearest<int>(curDetPos)]);
498 if (interpType == Backprojector::INTERP_NEAREST) {
499 int iDetPos = iDetCenter + nearest<int> (curDetPos); // calc index in the filtered raysum vector
501 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
502 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
504 *pImCol++ += filteredProj[iDetPos];
505 } else if (interpType == Backprojector::INTERP_LINEAR) {
506 double detPosFloor = floor (curDetPos);
507 int iDetPos = iDetCenter + static_cast<int>(detPosFloor);
508 double frac = curDetPos - detPosFloor; // fraction distance from det
509 if (iDetPos < 0 || iDetPos >= nDet - 1)
510 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
512 *pImCol++ += filteredProj[iDetPos] + (frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos]));
518 // CLASS IDENTICATION
519 // BackprojectIntDiff2
522 // Integer version of BackprojectDiff2
525 BackprojectIntDiff2::BackprojectView (const double* const filteredProj, const double view_angle)
527 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
529 static const kint32 scale = 1 << 16;
530 static const double dScale = scale;
531 static const kint32 halfScale = scale / 2;
533 const kint32 det_dx = nearest<kint32> (xInc * sin (theta) / detInc * scale);
534 const kint32 det_dy = nearest<kint32> (yInc * cos (theta) / detInc * scale);
536 // calculate L for first point in image (0, 0)
537 kint32 detPosColStart = nearest<kint32> (start_r * cos (theta - start_phi) / detInc * scale);
539 for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
540 kint32 curDetPos = detPosColStart;
541 ImageFileColumn pImCol = v[ix];
543 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
544 if (interpType == Backprojector::INTERP_NEAREST) {
545 int detPosNearest = (curDetPos >= 0 ? ((curDetPos + halfScale) / scale) : ((curDetPos - halfScale) / scale));
546 int iDetPos = iDetCenter + detPosNearest; // calc index in the filtered raysum vector
548 if (iDetPos < 0 || iDetPos >= nDet) // check for index outside of raysum pos
549 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
551 *pImCol++ += filteredProj[iDetPos];
552 } else if (interpType == Backprojector::INTERP_LINEAR) {
553 kint32 detPosFloor = curDetPos / scale;
554 kint32 detPosRemainder = curDetPos % scale;
555 if (detPosRemainder < 0) {
557 detPosRemainder += scale;
559 int iDetPos = iDetCenter + detPosFloor;
560 double frac = detPosRemainder / dScale;
561 if (iDetPos < 0 || iDetPos >= nDet - 1)
562 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
564 *pImCol++ += ((1.-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
570 // CLASS IDENTICATION
571 // BackprojectIntDiff3
574 // Highly optimized version of BackprojectIntDiff2
577 BackprojectIntDiff3::BackprojectView (const double* const filteredProj, const double view_angle)
579 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
580 static const int scaleShift = 16;
581 static const kint32 scale = (1 << scaleShift);
582 static const kint32 scaleBitmask = scale - 1;
583 static const kint32 halfScale = scale / 2;
584 static const double dInvScale = 1. / scale;
586 const kint32 det_dx = nearest<kint32> (xInc * sin (theta) / detInc * scale);
587 const kint32 det_dy = nearest<kint32> (yInc * cos (theta) / detInc * scale);
589 // calculate L for first point in image (0, 0)
590 kint32 detPosColStart = nearest<kint32> ((start_r * cos (theta - start_phi) / detInc + iDetCenter) * scale);
592 // precalculate scaled difference for linear interpolation
593 double deltaFilteredProj [nDet];
594 if (interpType == Backprojector::INTERP_LINEAR) {
595 for (int i = 0; i < nDet - 1; i++)
596 deltaFilteredProj[i] = (filteredProj[i+1] - filteredProj[i]) * dInvScale;
598 deltaFilteredProj[nDet - 1] = 0; // last detector
600 int iLastDet = nDet - 1;
601 for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
602 kint32 curDetPos = detPosColStart;
603 ImageFileColumn pImCol = v[ix];
605 if (interpType == Backprojector::INTERP_NEAREST) {
606 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
607 const int iDetPos = (curDetPos + halfScale) >> 16;
608 if (iDetPos >= 0 && iDetPos <= iLastDet)
609 *pImCol++ += filteredProj[iDetPos];
611 } else if (interpType == Backprojector::INTERP_FREQ_PREINTERPOLATION) {
612 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
613 const int iDetPos = ((curDetPos + halfScale) >> 16) * m_interpFactor;
614 if (iDetPos >= 0 && iDetPos <= iLastDet)
615 *pImCol++ += filteredProj[iDetPos];
617 } else if (interpType == Backprojector::INTERP_LINEAR) {
618 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
619 const kint32 iDetPos = curDetPos >> scaleShift;
620 const kint32 detRemainder = curDetPos & scaleBitmask;
621 if (iDetPos >= 0 && iDetPos <= iLastDet)
622 *pImCol++ += filteredProj[iDetPos] + (detRemainder * deltaFilteredProj[iDetPos]);