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