X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fbackprojectors.cpp;h=726e5b8d30189c14e472f5e4eee802930742964e;hp=9e350923ab4594287c868e21ee4c8f4b371bf8a0;hb=7ec2cd66921180a624813dff9f8bac76c6b268cc;hpb=2d39e823ba389fc68e5317c422b55be006094252 diff --git a/libctsim/backprojectors.cpp b/libctsim/backprojectors.cpp index 9e35092..726e5b8 100644 --- a/libctsim/backprojectors.cpp +++ b/libctsim/backprojectors.cpp @@ -8,7 +8,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: backprojectors.cpp,v 1.3 2000/06/22 10:17:28 kevin Exp $ +** $Id: backprojectors.cpp,v 1.21 2001/01/01 10:14:34 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -26,22 +26,93 @@ #include "ct.h" -Backprojector::Backprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName) +const int Backprojector::BPROJ_INVALID = -1; +const int Backprojector::BPROJ_TRIG = 0; +const int Backprojector::BPROJ_TABLE = 1; +const int Backprojector::BPROJ_DIFF = 2; +const int Backprojector::BPROJ_DIFF2 = 3; +const int Backprojector::BPROJ_IDIFF2 = 4; +const int Backprojector::BPROJ_IDIFF3 = 5; + +const char* Backprojector::s_aszBackprojectName[] = +{ + {"trig"}, + {"table"}, + {"diff"}, + {"diff2"}, + {"idiff2"}, + {"idiff3"}, +}; + +const char* Backprojector::s_aszBackprojectTitle[] = +{ + {"Direct Trigometric"}, + {"Trigometric Table"}, + {"Difference Iteration"}, + {"Difference Iteration Optimized"}, + {"Integer Difference Iteration Optimized"}, + {"Integer Difference Iteration Highly-Optimized"}, +}; + +const int Backprojector::s_iBackprojectCount = sizeof(s_aszBackprojectName) / sizeof(const char*); + +const int Backprojector::INTERP_INVALID = -1; +const int Backprojector::INTERP_NEAREST = 0; +const int Backprojector::INTERP_LINEAR = 1; +const int Backprojector::INTERP_FREQ_PREINTERPOLATION = 2; +#if HAVE_BSPLINE_INTERP +const int Backprojector::INTERP_BSPLINE = 3; +const int Backprojector::INTERP_1BSPLINE = 4; +const int Backprojector::INTERP_2BSPLINE = 5; +const int Backprojector::INTERP_3BSPLINE = 6; +#endif + +const char* Backprojector::s_aszInterpName[] = +{ + {"nearest"}, + {"linear"}, + {"freq_preinterpolationj"}, +#if HAVE_BSPLINE_INTERP + {"bspline"}, + {"1bspline"}, + {"2bspline"}, + {"3bspline"}, +#endif +}; + +const char* Backprojector::s_aszInterpTitle[] = +{ + {"Nearest"}, + {"Linear"}, + {"Frequency Preinterpolation"}, +#if HAVE_BSPLINE_INTERP + {"B-Spline"}, + {"B-Spline 1st Order"}, + {"B-Spline 2nd Order"}, + {"B-Spline 3rd Order"}, +#endif +}; + +const int Backprojector::s_iInterpCount = sizeof(s_aszInterpName) / sizeof(const char*); + + + +Backprojector::Backprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor) { m_fail = false; m_pBackprojectImplem = NULL; - initBackprojector (proj, im, backprojName, interpName); + initBackprojector (proj, im, backprojName, interpName, interpFactor); } void Backprojector::BackprojectView (const double* const viewData, const double viewAngle) { - if (m_pBackprojectImplem) + if (m_pBackprojectImplem != NULL) m_pBackprojectImplem->BackprojectView (viewData, viewAngle); } -Backprojector::~Backprojector (void) +Backprojector::~Backprojector () { delete m_pBackprojectImplem; } @@ -54,137 +125,151 @@ Backprojector::~Backprojector (void) // and initializes the backprojector bool -Backprojector::initBackprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName) +Backprojector::initBackprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName, const int interpFactor) { m_nameBackproject = backprojName; m_nameInterpolation = interpName; - m_idBackproject = convertBackprojectNameToID (backprojName); - m_idInterpolation = convertInterpolationNameToID (interpName); m_pBackprojectImplem = NULL; - - if (m_idBackproject == BPROJ_INVALID || m_idInterpolation == INTERP_INVALID) { + m_idBackproject = convertBackprojectNameToID (backprojName); + if (m_idBackproject == BPROJ_INVALID) { m_fail = true; - return false; + m_failMessage = "Invalid backprojection name "; + m_failMessage += backprojName; + } + m_idInterpolation = convertInterpNameToID (interpName); + if (m_idInterpolation == INTERP_INVALID) { + m_fail = true; + m_failMessage = "Invalid interpolation name "; + m_failMessage += interpName; } - if (m_idBackproject == BPROJ_TRIG) - m_pBackprojectImplem = static_cast(new BackprojectTrig (proj, im, m_idInterpolation)); - else if (m_idBackproject == BPROJ_TABLE) - m_pBackprojectImplem = static_cast(new BackprojectTable (proj, im, m_idInterpolation)); - else if (m_idBackproject == BPROJ_DIFF) - m_pBackprojectImplem = static_cast(new BackprojectDiff (proj, im, m_idInterpolation)); - else if (m_idBackproject == BPROJ_DIFF2) - m_pBackprojectImplem = static_cast(new BackprojectDiff2 (proj, im, m_idInterpolation)); - else if (m_idBackproject == BPROJ_IDIFF2) - m_pBackprojectImplem = static_cast(new BackprojectIntDiff2 (proj, im, m_idInterpolation)); - else { + if (m_fail || m_idBackproject == BPROJ_INVALID || m_idInterpolation == INTERP_INVALID) { m_fail = true; return false; } + if (proj.geometry() == Scanner::GEOMETRY_EQUILINEAR) + m_pBackprojectImplem = static_cast(new BackprojectEquilinear(proj, im, m_idInterpolation, interpFactor)); + else if (proj.geometry() == Scanner::GEOMETRY_EQUIANGULAR) + m_pBackprojectImplem = static_cast(new BackprojectEquiangular(proj, im, m_idInterpolation, interpFactor)); + else if (proj.geometry() == Scanner::GEOMETRY_PARALLEL) { + if (m_idBackproject == BPROJ_TRIG) + m_pBackprojectImplem = static_cast(new BackprojectTrig (proj, im, m_idInterpolation, interpFactor)); + else if (m_idBackproject == BPROJ_TABLE) + m_pBackprojectImplem = static_cast(new BackprojectTable (proj, im, m_idInterpolation, interpFactor)); + else if (m_idBackproject == BPROJ_DIFF) + m_pBackprojectImplem = static_cast(new BackprojectDiff (proj, im, m_idInterpolation, interpFactor)); + else if (m_idBackproject == BPROJ_DIFF2) + m_pBackprojectImplem = static_cast(new BackprojectDiff2 (proj, im, m_idInterpolation, interpFactor)); + else if (m_idBackproject == BPROJ_IDIFF2) + m_pBackprojectImplem = static_cast(new BackprojectIntDiff2 (proj, im, m_idInterpolation, interpFactor)); + else if (m_idBackproject == BPROJ_IDIFF3) + m_pBackprojectImplem = static_cast(new BackprojectIntDiff3 (proj, im, m_idInterpolation, interpFactor)); + } else { + m_fail = true; + m_failMessage = "Unable to select a backprojection method [Backprojector::initBackprojector]"; + return false; + } + return true; } -const Backprojector::BackprojectID +int Backprojector::convertBackprojectNameToID (const char* const backprojName) { - BackprojectID backprojID = BPROJ_INVALID; - - if (strcasecmp (backprojName, BPROJ_TRIG_STR) == 0) - backprojID = BPROJ_TRIG; - else if (strcasecmp (backprojName, BPROJ_TABLE_STR) == 0) - backprojID = BPROJ_TABLE; - else if (strcasecmp (backprojName, BPROJ_DIFF_STR) == 0) - backprojID = BPROJ_DIFF; - else if (strcasecmp (backprojName, BPROJ_DIFF2_STR) == 0) - backprojID = BPROJ_DIFF2; - else if (strcasecmp (backprojName, BPROJ_IDIFF2_STR) == 0) - backprojID = BPROJ_IDIFF2; + int backprojID = BPROJ_INVALID; + + for (int i = 0; i < s_iBackprojectCount; i++) + if (strcasecmp (backprojName, s_aszBackprojectName[i]) == 0) { + backprojID = i; + break; + } return (backprojID); } const char* -Backprojector::convertBackprojectIDToName (const BackprojectID bprojID) +Backprojector::convertBackprojectIDToName (int bprojID) { - const char *bprojName = ""; - - if (bprojID == BPROJ_TRIG) - bprojName = BPROJ_TRIG_STR; - else if (bprojID == BPROJ_TABLE) - bprojName = BPROJ_TABLE_STR; - else if (bprojID == BPROJ_DIFF) - bprojName = BPROJ_DIFF_STR; - else if (bprojID == BPROJ_DIFF2) - bprojName = BPROJ_DIFF2_STR; - else if (bprojID == BPROJ_IDIFF2) - bprojName = BPROJ_IDIFF2_STR; + static const char *bprojName = ""; + + if (bprojID >= 0 && bprojID < s_iBackprojectCount) + return (s_aszBackprojectName[bprojID]); return (bprojName); } +const char* +Backprojector::convertBackprojectIDToTitle (const int bprojID) +{ + static const char *bprojTitle = ""; + + if (bprojID >= 0 && bprojID < s_iBackprojectCount) + return (s_aszBackprojectTitle[bprojID]); + + return (bprojTitle); +} -const Backprojector::InterpolationID -Backprojector::convertInterpolationNameToID (const char* const interpName) +int +Backprojector::convertInterpNameToID (const char* const interpName) { - InterpolationID interpID = INTERP_INVALID; + int interpID = INTERP_INVALID; + + for (int i = 0; i < s_iInterpCount; i++) + if (strcasecmp (interpName, s_aszInterpName[i]) == 0) { + interpID = i; + break; + } - if (strcasecmp (interpName, INTERP_NEAREST_STR) == 0) - interpID = INTERP_NEAREST; - else if (strcasecmp (interpName, INTERP_LINEAR_STR) == 0) - interpID = INTERP_LINEAR; -#if HAVE_BSPLINE_INTERP - else if (strcasecmp (interpName, INTERP_BSPLINE_STR) == 0) - interpID = INTERP_BSPLINE; -#endif - return (interpID); } +const char* +Backprojector::convertInterpIDToName (const int interpID) +{ + static const char *interpName = ""; -/* NAME - * name_of_interp Return name of interpolation method - * - * SYNOPSIS - * name = name_of_interp (interp_type) - * char *name Name of interpolation method - * int interp_type Method of interpolation - * - * NOTES - * Returns NULL if interp_type is invalid - */ + if (interpID >= 0 && interpID < s_iInterpCount) + return (s_aszInterpName[interpID]); + + return (interpName); +} const char* -Backprojector::convertInterpolationIDToName (const InterpolationID interpID) +Backprojector::convertInterpIDToTitle (const int interpID) { - if (interpID == INTERP_NEAREST) - return (INTERP_NEAREST_STR); - else if (interpID == INTERP_LINEAR) - return (INTERP_LINEAR_STR); -#if HAVE_BSPLINE_INTERP - else if (interpID == INTERP_BSPLINE) - return (INTERP_BSPLINE_STR); -#endif - else - return (""); + static const char *interpTitle = ""; + + if (interpID >= 0 && interpID < s_iInterpCount) + return (s_aszInterpTitle[interpID]); + + return (interpTitle); } + // CLASS IDENTICATION // Backproject // // PURPOSE // Pure virtual base class for all backprojectors. -Backproject::Backproject (const Projections& proj, ImageFile& im, const Backprojector::InterpolationID interpType) - : proj(proj), im(im), interpType(interpType) +Backproject::Backproject (const Projections& proj, ImageFile& im, int interpType, const int interpFactor) + : proj(proj), im(im), interpType(interpType), m_interpFactor(interpFactor) { detInc = proj.detInc(); nDet = proj.nDet(); iDetCenter = (nDet - 1) / 2; // index refering to L=0 projection - rotInc = proj.rotInc(); + rotScale = proj.rotInc(); + + if (proj.geometry() == Scanner::GEOMETRY_PARALLEL) + rotScale /= (proj.nView() * proj.rotInc() / PI); // scale by number of PI rotations + else if (proj.geometry() == Scanner::GEOMETRY_EQUIANGULAR || proj.geometry() == Scanner::GEOMETRY_EQUILINEAR) + rotScale /= (proj.nView() * proj.rotInc() / (2 * PI)); // scale by number of 2PI rotations + else + sys_error (ERR_SEVERE, "Invalid geometry type %d [Backproject::Backproject]", proj.geometry()); v = im.getArray(); nx = im.nx(); @@ -199,35 +284,38 @@ Backproject::Backproject (const Projections& proj, ImageFile& im, const Backproj xInc = (xMax - xMin) / nx; // size of cells yInc = (yMax - yMin) / ny; - if (interpType != Backprojector::INTERP_NEAREST && interpType != Backprojector::INTERP_LINEAR) - sys_error (ERR_WARNING, "Illegal interpType %d [selectBackprojector]", interpType); + m_dFocalLength = proj.focalLength(); } -Backproject::~Backproject (void) +Backproject::~Backproject () {} void -Backproject::ScaleImageByRotIncrement (void) +Backproject::ScaleImageByRotIncrement () { for (int ix = 0; ix < nx; ix++) for (int iy = 0; iy < ny; iy++) - v[ix][iy] *= rotInc; + v[ix][iy] *= rotScale; } void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double r, double phi, double L, int iDetPos) { - printf ("r=%f, phi=%f\n", r, phi); + sys_error (ERR_WARNING, "r=%f, phi=%f", r, phi); errorIndexOutsideDetector (ix, iy, theta, L, iDetPos); } void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double L, int iDetPos) -{ - printf ("ix=%d, iy=%d\n", ix, iy); - printf ("theta=%f, L=%f, detInc=%f\n", theta, L, detInc); - printf ("proj.ndet=%d, proj.detInc=%.4f, iDetCenter=%d\n", nDet, detInc, iDetCenter); - printf ("xMin=%15.8f, xMax=%15.8f, xInc=%15.8f\n", xMin, xMax, xInc); - printf ("yMin=%15.8f, yMax=%15.8f, yInc=%15.8f\n", yMin, yMax, yInc); - sys_error (ERR_WARNING, "iDetPos index outside bounds: %d [backprojector]", iDetPos); +{ +#if 1 + std::ostringstream os; + os << "ix=" << ix << ", iy=" << iy << ", theta=" << theta << ", L=" << L << ", detinc=" << detInc << "\n"; + os << "ndet=" << nDet << ", detInc=" << detInc << ", iDetCenter=" << iDetCenter << "\n"; + os << "xMin=" << xMin << ", xMax=" << xMax << ", xInc=" << xInc << "\n"; + os << "yMin=" << yMin << ", yMax=" << yMax << ", yInc=" << yInc << "\n"; + os << "iDetPos index outside bounds: " << iDetPos << " [backprojector]";; + + sys_error (ERR_WARNING, os.str().c_str()); +#endif } @@ -240,12 +328,12 @@ void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, doubl void BackprojectTrig::BackprojectView (const double* const filteredProj, const double view_angle) { - double theta = HALFPI + view_angle; // Add PI/2 to get perpendicular angle to detector - int ix, iy; - double x, y; // Rectang coords of center of pixel + double theta = view_angle; - for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++) - for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) { + double x = xMin + xInc / 2; // Rectang coords of center of pixel + for (int ix = 0; ix < nx; x += xInc, ix++) { + double y = yMin + yInc / 2; + for (int iy = 0; iy < ny; y += yInc, iy++) { double r = sqrt (x * x + y * y); // distance of cell from center double phi = atan2 (y, x); // angle of cell from center double L = r * cos (theta - phi); // position on detector @@ -256,7 +344,7 @@ BackprojectTrig::BackprojectView (const double* const filteredProj, const double if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos); else - v[ix][iy] += rotInc * filteredProj[iDetPos]; + v[ix][iy] += rotScale * filteredProj[iDetPos]; } else if (interpType == Backprojector::INTERP_LINEAR) { double p = L / detInc; // position along detector double pFloor = floor (p); @@ -265,9 +353,10 @@ BackprojectTrig::BackprojectView (const double* const filteredProj, const double if (iDetPos < 0 || iDetPos >= nDet - 1) // check for impossible: index outside of raysum pos errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos); else - v[ix][iy] += rotInc * ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]); + v[ix][iy] += rotScale * ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]); } } + } } @@ -277,11 +366,11 @@ BackprojectTrig::BackprojectView (const double* const filteredProj, const double // PURPOSE // Precalculates trigometric function value for each point in image for backprojection. -BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, Backprojector::InterpolationID interpType) - : Backproject::Backproject (proj, im, interpType) +BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, int interpType, const int interpFactor) + : Backproject (proj, im, interpType, interpFactor) { - arrayR.initSetSize (nx, ny); - arrayPhi.initSetSize (nx, ny); + arrayR.initSetSize (im.nx(), im.ny()); + arrayPhi.initSetSize (im.nx(), im.ny()); r = arrayR.getArray(); phi = arrayPhi.getArray(); @@ -294,7 +383,7 @@ BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, Back } } -BackprojectTable::~BackprojectTable (void) +BackprojectTable::~BackprojectTable () { ScaleImageByRotIncrement(); } @@ -302,7 +391,7 @@ BackprojectTable::~BackprojectTable (void) void BackprojectTable::BackprojectView (const double* const filteredProj, const double view_angle) { - double theta = HALFPI + view_angle; // add half PI to view angle to get perpendicular theta angle + double theta = view_angle; for (int ix = 0; ix < nx; ix++) { ImageFileColumn pImCol = v[ix]; @@ -339,8 +428,8 @@ BackprojectTable::BackprojectView (const double* const filteredProj, const doubl // Backprojects by precalculating the change in L position for each x & y step in the image. // Iterates in x & y direction by adding difference in L position -BackprojectDiff::BackprojectDiff (const Projections& proj, ImageFile& im, Backprojector::InterpolationID interpType) - : Backproject::Backproject (proj, im, interpType) +BackprojectDiff::BackprojectDiff (const Projections& proj, ImageFile& im, int interpType, const int interpFactor) + : Backproject (proj, im, interpType, interpFactor) { // calculate center of first pixel v[0][0] double x = xMin + xInc / 2; @@ -359,9 +448,9 @@ BackprojectDiff::~BackprojectDiff() void BackprojectDiff::BackprojectView (const double* const filteredProj, const double view_angle) { - double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle - double det_dx = xInc * sin (theta); - double det_dy = yInc * cos (theta); + double theta = view_angle; // add half PI to view angle to get perpendicular theta angle + double det_dx = xInc * cos (theta); + double det_dy = yInc * sin (theta); double lColStart = start_r * cos (theta - start_phi); // calculate L for first point in image for (int ix = 0; ix < nx; ix++, lColStart += det_dx) { @@ -370,7 +459,7 @@ BackprojectDiff::BackprojectView (const double* const filteredProj, const double for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) { #ifdef DEBUG - printf ("[%2d,%2d]: %8.5lf ", ix, iy, curDetPos); + printf ("[%2d,%2d]: %8.5f ", ix, iy, curDetPos); #endif if (interpType == Backprojector::INTERP_NEAREST) { int iDetPos = iDetCenter + nearest(curDetPos / detInc); // calc index in the filtered raysum vector @@ -403,17 +492,17 @@ BackprojectDiff::BackprojectView (const double* const filteredProj, const double void BackprojectDiff2::BackprojectView (const double* const filteredProj, const double view_angle) { - double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle + double theta = view_angle; // Distance betw. detectors for an angle given in units of detectors - double det_dx = xInc * sin (theta) / detInc; - double det_dy = yInc * cos (theta) / detInc; + double det_dx = xInc * cos (theta) / detInc; + double det_dy = yInc * sin (theta) / detInc; // calculate detPosition for first point in image (ix=0, iy=0) double detPosColStart = start_r * cos (theta - start_phi) / detInc; #ifdef DEBUG - printf ("start_r=%8.5f, start_phi=%8.5f, rotInc=%8.5f\n", start_r, start_phi, rotInc); + printf ("start_r=%8.5f, start_phi=%8.5f, rotScale=%8.5f\n", start_r, start_phi, rotScale); #endif for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) { double curDetPos = detPosColStart; @@ -421,7 +510,7 @@ BackprojectDiff2::BackprojectView (const double* const filteredProj, const doubl for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) { #ifdef DEBUG - printf ("[%2d,%2d]: %8.5f %8.5f\n", ix, iy, curDetPos, filteredProj[iDetCenter + nearest(L))]); + printf ("[%2d,%2d]: %8.5f %8.5f\n", ix, iy, curDetPos, filteredProj[iDetCenter + nearest(curDetPos)]); #endif if (interpType == Backprojector::INTERP_NEAREST) { int iDetPos = iDetCenter + nearest (curDetPos); // calc index in the filtered raysum vector @@ -437,7 +526,7 @@ BackprojectDiff2::BackprojectView (const double* const filteredProj, const doubl if (iDetPos < 0 || iDetPos >= nDet - 1) errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos); else - *pImCol++ += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]); + *pImCol++ += filteredProj[iDetPos] + (frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos])); } } // end for y } // end for x @@ -452,14 +541,14 @@ BackprojectDiff2::BackprojectView (const double* const filteredProj, const doubl void BackprojectIntDiff2::BackprojectView (const double* const filteredProj, const double view_angle) { - double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle + double theta = view_angle; - kint32 scale = 1 << 16; - double dScale = scale; - kint32 halfScale = scale / 2; + static const kint32 scale = 1 << 16; + static const double dScale = scale; + static const kint32 halfScale = scale / 2; - kint32 det_dx = nearest (xInc * sin (theta) / detInc * scale); - kint32 det_dy = nearest (yInc * cos (theta) / detInc * scale); + const kint32 det_dx = nearest (xInc * cos (theta) / detInc * scale); + const kint32 det_dy = nearest (yInc * sin (theta) / detInc * scale); // calculate L for first point in image (0, 0) kint32 detPosColStart = nearest (start_r * cos (theta - start_phi) / detInc * scale); @@ -473,7 +562,7 @@ BackprojectIntDiff2::BackprojectView (const double* const filteredProj, const do int detPosNearest = (curDetPos >= 0 ? ((curDetPos + halfScale) / scale) : ((curDetPos - halfScale) / scale)); int iDetPos = iDetCenter + detPosNearest; // calc index in the filtered raysum vector - if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos + if (iDetPos < 0 || iDetPos >= nDet) // check for index outside of raysum pos errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos); else *pImCol++ += filteredProj[iDetPos]; @@ -489,8 +578,147 @@ BackprojectIntDiff2::BackprojectView (const double* const filteredProj, const do if (iDetPos < 0 || iDetPos >= nDet - 1) errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos); else - *pImCol++ += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]); + *pImCol++ += ((1.-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]); } } // end for y } // end for x } + +// CLASS IDENTICATION +// BackprojectIntDiff3 +// +// PURPOSE +// Highly optimized version of BackprojectIntDiff2 + +void +BackprojectIntDiff3::BackprojectView (const double* const filteredProj, const double view_angle) +{ + double theta = view_angle; // add half PI to view angle to get perpendicular theta angle + static const int scaleShift = 16; + static const kint32 scale = (1 << scaleShift); + static const kint32 scaleBitmask = scale - 1; + static const kint32 halfScale = scale / 2; + static const double dInvScale = 1. / scale; + + const kint32 det_dx = nearest (xInc * cos (theta) / detInc * scale); + const kint32 det_dy = nearest (yInc * sin (theta) / detInc * scale); + + // calculate L for first point in image (0, 0) + kint32 detPosColStart = nearest ((start_r * cos (theta - start_phi) / detInc + iDetCenter) * scale); + + // precalculate scaled difference for linear interpolation + double* deltaFilteredProj = new double [nDet]; + if (interpType == Backprojector::INTERP_LINEAR) { + for (int i = 0; i < nDet - 1; i++) + deltaFilteredProj[i] = (filteredProj[i+1] - filteredProj[i]) * dInvScale; + } + deltaFilteredProj[nDet - 1] = 0; // last detector + + int iLastDet = nDet - 1; + for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) { + kint32 curDetPos = detPosColStart; + ImageFileColumn pImCol = v[ix]; + + if (interpType == Backprojector::INTERP_NEAREST) { + for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) { + const int iDetPos = (curDetPos + halfScale) >> 16; + if (iDetPos >= 0 && iDetPos <= iLastDet) + *pImCol++ += filteredProj[iDetPos]; + } // end for iy + } else if (interpType == Backprojector::INTERP_FREQ_PREINTERPOLATION) { + for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) { + const int iDetPos = ((curDetPos + halfScale) >> 16) * m_interpFactor; + if (iDetPos >= 0 && iDetPos <= iLastDet) + *pImCol++ += filteredProj[iDetPos]; + } // end for iy + } else if (interpType == Backprojector::INTERP_LINEAR) { + for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) { + const kint32 iDetPos = curDetPos >> scaleShift; + const kint32 detRemainder = curDetPos & scaleBitmask; + if (iDetPos >= 0 && iDetPos <= iLastDet) + *pImCol++ += filteredProj[iDetPos] + (detRemainder * deltaFilteredProj[iDetPos]); + } // end for iy + } //end linear + } // end for ix + + delete deltaFilteredProj; +} + + +void +BackprojectEquiangular::BackprojectView (const double* const filteredProj, const double view_angle) +{ + double beta = view_angle; + + for (int ix = 0; ix < nx; ix++) { + ImageFileColumn pImCol = v[ix]; + + for (int iy = 0; iy < ny; iy++) { + double dAngleDiff = beta - phi[ix][iy]; + double rcos_t = r[ix][iy] * cos (dAngleDiff); + double rsin_t = r[ix][iy] * sin (dAngleDiff); + double dFLPlusSin = m_dFocalLength + rsin_t; + double gamma = atan (rcos_t / dFLPlusSin); + double dL2 = dFLPlusSin * dFLPlusSin + (rcos_t * rcos_t); + + if (interpType == Backprojector::INTERP_NEAREST) { + int iDetPos =iDetCenter + nearest(gamma / detInc); // calc index in the filtered raysum vector + + if (iDetPos < 0 || iDetPos >= nDet) { // check for impossible: index outside of raysum pos + ; // errorIndexOutsideDetector (ix, iy, beta, r[ix][iy], phi[ix][iy], gamma, iDetPos); + } else + pImCol[iy] += filteredProj[iDetPos] / dL2; + } else if (interpType == Backprojector::INTERP_LINEAR) { + double dPos = gamma / detInc; // position along detector + double dPosFloor = floor (dPos); + int iDetPos = iDetCenter + static_cast(dPosFloor); + double frac = dPos - dPosFloor; // fraction distance from det + if (iDetPos < 0 || iDetPos >= nDet - 1) { + ; // errorIndexOutsideDetector (ix, iy, beta, r[ix][iy], phi[ix][iy], gamma, iDetPos); + } else + pImCol[iy] += (filteredProj[iDetPos] + frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos])) / dL2; + } + } // end for y + } // end for x +} + +void +BackprojectEquilinear::BackprojectView (const double* const filteredProj, const double view_angle) +{ + double beta = view_angle; + + for (int ix = 0; ix < nx; ix++) { + ImageFileColumn pImCol = v[ix]; + + for (int iy = 0; iy < ny; iy++) { + double dAngleDiff = beta - phi[ix][iy]; + double rcos_t = r[ix][iy] * cos (dAngleDiff); + double rsin_t = r[ix][iy] * sin (dAngleDiff); + + double dU = (m_dFocalLength + rsin_t) / m_dFocalLength; + double dDetPos = rcos_t / dU; + // double to scale for imaginary detector that passes through origin + // of phantom, see Kak-Slaney Figure 3.22 + dDetPos *= 2; + + if (interpType == Backprojector::INTERP_NEAREST) { + int iDetPos = iDetCenter + nearest(dDetPos / detInc); // calc index in the filtered raysum vector + + if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos + ; /// errorIndexOutsideDetector (ix, iy, beta, r[ix][iy], phi[ix][iy], dDetPos, iDetPos); + else + pImCol[iy] += (filteredProj[iDetPos] / (dU * dU)); + } else if (interpType == Backprojector::INTERP_LINEAR) { + double dPos = dDetPos / detInc; // position along detector + double dPosFloor = floor (dPos); + int iDetPos = iDetCenter + static_cast(dPosFloor); + double frac = dPos - dPosFloor; // fraction distance from det + if (iDetPos < 0 || iDetPos >= nDet - 1) + ; // errorIndexOutsideDetector (ix, iy, beta, r[ix][iy], phi[ix][iy], dDetPos, iDetPos); + else + pImCol[iy] += (filteredProj[iDetPos] + frac * (filteredProj[iDetPos+1] - filteredProj[iDetPos])) / (dU * dU); + } + } // end for y + } // end for x +} +