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.4 2000/06/25 17:32:24 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 Backprojector::Backprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName)
32 m_pBackprojectImplem = NULL;
34 initBackprojector (proj, im, backprojName, interpName);
38 Backprojector::BackprojectView (const double* const viewData, const double viewAngle)
40 if (m_pBackprojectImplem)
41 m_pBackprojectImplem->BackprojectView (viewData, viewAngle);
44 Backprojector::~Backprojector (void)
46 delete m_pBackprojectImplem;
49 // FUNCTION IDENTIFICATION
50 // Backproject* projector = selectBackprojector (...)
53 // Selects a backprojector based on BackprojType
54 // and initializes the backprojector
57 Backprojector::initBackprojector (const Projections& proj, ImageFile& im, const char* const backprojName, const char* const interpName)
59 m_nameBackproject = backprojName;
60 m_nameInterpolation = interpName;
61 m_pBackprojectImplem = NULL;
62 m_idBackproject = convertBackprojectNameToID (backprojName);
63 if (m_idBackproject == BPROJ_INVALID) {
65 m_failMessage = "Invalid backprojection name ";
66 m_failMessage += backprojName;
68 m_idInterpolation = convertInterpolationNameToID (interpName);
69 if (m_idInterpolation == INTERP_INVALID) {
71 m_failMessage = "Invalid interpolation name ";
72 m_failMessage += interpName;
75 if (m_fail || m_idBackproject == BPROJ_INVALID || m_idInterpolation == INTERP_INVALID) {
80 if (m_idBackproject == BPROJ_TRIG)
81 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTrig (proj, im, m_idInterpolation));
82 else if (m_idBackproject == BPROJ_TABLE)
83 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectTable (proj, im, m_idInterpolation));
84 else if (m_idBackproject == BPROJ_DIFF)
85 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectDiff (proj, im, m_idInterpolation));
86 else if (m_idBackproject == BPROJ_DIFF2)
87 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectDiff2 (proj, im, m_idInterpolation));
88 else if (m_idBackproject == BPROJ_IDIFF2)
89 m_pBackprojectImplem = static_cast<Backproject*>(new BackprojectIntDiff2 (proj, im, m_idInterpolation));
92 m_failMessage = "Unable to select a backprojection method [Backprojector::initBackprojector]";
100 const Backprojector::BackprojectID
101 Backprojector::convertBackprojectNameToID (const char* const backprojName)
103 BackprojectID backprojID = BPROJ_INVALID;
105 if (strcasecmp (backprojName, BPROJ_TRIG_STR) == 0)
106 backprojID = BPROJ_TRIG;
107 else if (strcasecmp (backprojName, BPROJ_TABLE_STR) == 0)
108 backprojID = BPROJ_TABLE;
109 else if (strcasecmp (backprojName, BPROJ_DIFF_STR) == 0)
110 backprojID = BPROJ_DIFF;
111 else if (strcasecmp (backprojName, BPROJ_DIFF2_STR) == 0)
112 backprojID = BPROJ_DIFF2;
113 else if (strcasecmp (backprojName, BPROJ_IDIFF2_STR) == 0)
114 backprojID = BPROJ_IDIFF2;
120 Backprojector::convertBackprojectIDToName (const BackprojectID bprojID)
122 const char *bprojName = "";
124 if (bprojID == BPROJ_TRIG)
125 bprojName = BPROJ_TRIG_STR;
126 else if (bprojID == BPROJ_TABLE)
127 bprojName = BPROJ_TABLE_STR;
128 else if (bprojID == BPROJ_DIFF)
129 bprojName = BPROJ_DIFF_STR;
130 else if (bprojID == BPROJ_DIFF2)
131 bprojName = BPROJ_DIFF2_STR;
132 else if (bprojID == BPROJ_IDIFF2)
133 bprojName = BPROJ_IDIFF2_STR;
140 const Backprojector::InterpolationID
141 Backprojector::convertInterpolationNameToID (const char* const interpName)
143 InterpolationID interpID = INTERP_INVALID;
145 if (strcasecmp (interpName, INTERP_NEAREST_STR) == 0)
146 interpID = INTERP_NEAREST;
147 else if (strcasecmp (interpName, INTERP_LINEAR_STR) == 0)
148 interpID = INTERP_LINEAR;
149 #if HAVE_BSPLINE_INTERP
150 else if (strcasecmp (interpName, INTERP_BSPLINE_STR) == 0)
151 interpID = INTERP_BSPLINE;
159 * name_of_interp Return name of interpolation method
162 * name = name_of_interp (interp_type)
163 * char *name Name of interpolation method
164 * int interp_type Method of interpolation
167 * Returns NULL if interp_type is invalid
171 Backprojector::convertInterpolationIDToName (const InterpolationID interpID)
173 if (interpID == INTERP_NEAREST)
174 return (INTERP_NEAREST_STR);
175 else if (interpID == INTERP_LINEAR)
176 return (INTERP_LINEAR_STR);
177 #if HAVE_BSPLINE_INTERP
178 else if (interpID == INTERP_BSPLINE)
179 return (INTERP_BSPLINE_STR);
186 // CLASS IDENTICATION
190 // Pure virtual base class for all backprojectors.
192 Backproject::Backproject (const Projections& proj, ImageFile& im, const Backprojector::InterpolationID interpType)
193 : proj(proj), im(im), interpType(interpType)
195 detInc = proj.detInc();
197 iDetCenter = (nDet - 1) / 2; // index refering to L=0 projection
198 rotInc = proj.rotInc();
205 xMin = -proj.phmLen() / 2; // Retangular coords of phantom
206 xMax = xMin + proj.phmLen();
207 yMin = -proj.phmLen() / 2;
208 yMax = yMin + proj.phmLen();
210 xInc = (xMax - xMin) / nx; // size of cells
211 yInc = (yMax - yMin) / ny;
213 if (interpType != Backprojector::INTERP_NEAREST && interpType != Backprojector::INTERP_LINEAR)
214 sys_error (ERR_WARNING, "Illegal interpType %d [selectBackprojector]", interpType);
217 Backproject::~Backproject (void)
221 Backproject::ScaleImageByRotIncrement (void)
223 for (int ix = 0; ix < nx; ix++)
224 for (int iy = 0; iy < ny; iy++)
228 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double r, double phi, double L, int iDetPos)
230 printf ("r=%f, phi=%f\n", r, phi);
231 errorIndexOutsideDetector (ix, iy, theta, L, iDetPos);
234 void Backproject::errorIndexOutsideDetector (int ix, int iy, double theta, double L, int iDetPos)
236 printf ("ix=%d, iy=%d\n", ix, iy);
237 printf ("theta=%f, L=%f, detInc=%f\n", theta, L, detInc);
238 printf ("proj.ndet=%d, proj.detInc=%.4f, iDetCenter=%d\n", nDet, detInc, iDetCenter);
239 printf ("xMin=%15.8f, xMax=%15.8f, xInc=%15.8f\n", xMin, xMax, xInc);
240 printf ("yMin=%15.8f, yMax=%15.8f, yInc=%15.8f\n", yMin, yMax, yInc);
241 sys_error (ERR_WARNING, "iDetPos index outside bounds: %d [backprojector]", iDetPos);
245 // CLASS IDENTICATION
249 // Uses trigometric functions at each point in image for backprojection.
252 BackprojectTrig::BackprojectView (const double* const filteredProj, const double view_angle)
254 double theta = HALFPI + view_angle; // Add PI/2 to get perpendicular angle to detector
256 double x, y; // Rectang coords of center of pixel
258 for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++)
259 for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) {
260 double r = sqrt (x * x + y * y); // distance of cell from center
261 double phi = atan2 (y, x); // angle of cell from center
262 double L = r * cos (theta - phi); // position on detector
264 if (interpType == Backprojector::INTERP_NEAREST) {
265 int iDetPos = iDetCenter + nearest<int> (L / detInc); // calc'd index in the filter raysum array
267 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
268 errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos);
270 v[ix][iy] += rotInc * filteredProj[iDetPos];
271 } else if (interpType == Backprojector::INTERP_LINEAR) {
272 double p = L / detInc; // position along detector
273 double pFloor = floor (p);
274 int iDetPos = iDetCenter + static_cast<int>(pFloor);
275 double frac = p - pFloor; // fraction distance from det
276 if (iDetPos < 0 || iDetPos >= nDet - 1) // check for impossible: index outside of raysum pos
277 errorIndexOutsideDetector (ix, iy, theta, r, phi, L, iDetPos);
279 v[ix][iy] += rotInc * ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
285 // CLASS IDENTICATION
289 // Precalculates trigometric function value for each point in image for backprojection.
291 BackprojectTable::BackprojectTable (const Projections& proj, ImageFile& im, Backprojector::InterpolationID interpType)
292 : Backproject::Backproject (proj, im, interpType)
294 arrayR.initSetSize (nx, ny);
295 arrayPhi.initSetSize (nx, ny);
296 r = arrayR.getArray();
297 phi = arrayPhi.getArray();
299 double x, y; // Rectang coords of center of pixel
301 for (x = xMin + xInc / 2, ix = 0; ix < nx; x += xInc, ix++)
302 for (y = yMin + yInc / 2, iy = 0; iy < ny; y += yInc, iy++) {
303 r[ix][iy] = sqrt (x * x + y * y);
304 phi[ix][iy] = atan2 (y, x);
308 BackprojectTable::~BackprojectTable (void)
310 ScaleImageByRotIncrement();
314 BackprojectTable::BackprojectView (const double* const filteredProj, const double view_angle)
316 double theta = HALFPI + view_angle; // add half PI to view angle to get perpendicular theta angle
318 for (int ix = 0; ix < nx; ix++) {
319 ImageFileColumn pImCol = v[ix];
321 for (int iy = 0; iy < ny; iy++) {
322 double L = r[ix][iy] * cos (theta - phi[ix][iy]);
324 if (interpType == Backprojector::INTERP_NEAREST) {
325 int iDetPos = iDetCenter + nearest<int>(L / detInc); // calc index in the filtered raysum vector
327 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
328 errorIndexOutsideDetector (ix, iy, theta, r[ix][iy], phi[ix][iy], L, iDetPos);
330 pImCol[iy] += filteredProj[iDetPos];
331 } else if (interpType == Backprojector::INTERP_LINEAR) {
332 double dPos = L / detInc; // position along detector
333 double dPosFloor = floor (dPos);
334 int iDetPos = iDetCenter + static_cast<int>(dPosFloor);
335 double frac = dPos - dPosFloor; // fraction distance from det
336 if (iDetPos < 0 || iDetPos >= nDet - 1)
337 errorIndexOutsideDetector (ix, iy, theta, r[ix][iy], phi[ix][iy], L, iDetPos);
339 pImCol[iy] += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
346 // CLASS IDENTICATION
350 // Backprojects by precalculating the change in L position for each x & y step in the image.
351 // Iterates in x & y direction by adding difference in L position
353 BackprojectDiff::BackprojectDiff (const Projections& proj, ImageFile& im, Backprojector::InterpolationID interpType)
354 : Backproject::Backproject (proj, im, interpType)
356 // calculate center of first pixel v[0][0]
357 double x = xMin + xInc / 2;
358 double y = yMin + yInc / 2;
359 start_r = sqrt (x * x + y * y);
360 start_phi = atan2 (y, x);
365 BackprojectDiff::~BackprojectDiff()
367 ScaleImageByRotIncrement();
371 BackprojectDiff::BackprojectView (const double* const filteredProj, const double view_angle)
373 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
374 double det_dx = xInc * sin (theta);
375 double det_dy = yInc * cos (theta);
376 double lColStart = start_r * cos (theta - start_phi); // calculate L for first point in image
378 for (int ix = 0; ix < nx; ix++, lColStart += det_dx) {
379 double curDetPos = lColStart;
380 ImageFileColumn pImCol = v[ix];
382 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
384 printf ("[%2d,%2d]: %8.5lf ", ix, iy, curDetPos);
386 if (interpType == Backprojector::INTERP_NEAREST) {
387 int iDetPos = iDetCenter + nearest<int>(curDetPos / detInc); // calc index in the filtered raysum vector
389 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
390 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
392 pImCol[iy] += filteredProj[iDetPos];
393 } else if (interpType == Backprojector::INTERP_LINEAR) {
394 double detPos = curDetPos / detInc; // position along detector
395 double detPosFloor = floor (detPos);
396 int iDetPos = iDetCenter + static_cast<int>(detPosFloor);
397 double frac = detPos - detPosFloor; // fraction distance from det
398 if (iDetPos < 0 || iDetPos >= nDet - 1)
399 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
401 pImCol[iy] += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
408 // CLASS IDENTICATION
412 // Optimized version of BackprojectDiff
415 BackprojectDiff2::BackprojectView (const double* const filteredProj, const double view_angle)
417 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
419 // Distance betw. detectors for an angle given in units of detectors
420 double det_dx = xInc * sin (theta) / detInc;
421 double det_dy = yInc * cos (theta) / detInc;
423 // calculate detPosition for first point in image (ix=0, iy=0)
424 double detPosColStart = start_r * cos (theta - start_phi) / detInc;
427 printf ("start_r=%8.5f, start_phi=%8.5f, rotInc=%8.5f\n", start_r, start_phi, rotInc);
429 for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
430 double curDetPos = detPosColStart;
431 ImageFileColumn pImCol = v[ix];
433 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
435 printf ("[%2d,%2d]: %8.5f %8.5f\n", ix, iy, curDetPos, filteredProj[iDetCenter + nearest<int>(L))]);
437 if (interpType == Backprojector::INTERP_NEAREST) {
438 int iDetPos = iDetCenter + nearest<int> (curDetPos); // calc index in the filtered raysum vector
440 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
441 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
443 *pImCol++ += filteredProj[iDetPos];
444 } else if (interpType == Backprojector::INTERP_LINEAR) {
445 double detPosFloor = floor (curDetPos);
446 int iDetPos = iDetCenter + static_cast<int>(detPosFloor);
447 double frac = curDetPos - detPosFloor; // fraction distance from det
448 if (iDetPos < 0 || iDetPos >= nDet - 1)
449 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
451 *pImCol++ += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);
457 // CLASS IDENTICATION
458 // BackprojectIntDiff2
461 // Integer version of BackprojectDiff2
464 BackprojectIntDiff2::BackprojectView (const double* const filteredProj, const double view_angle)
466 double theta = - view_angle; // add half PI to view angle to get perpendicular theta angle
468 kint32 scale = 1 << 16;
469 double dScale = scale;
470 kint32 halfScale = scale / 2;
472 kint32 det_dx = nearest<kint32> (xInc * sin (theta) / detInc * scale);
473 kint32 det_dy = nearest<kint32> (yInc * cos (theta) / detInc * scale);
475 // calculate L for first point in image (0, 0)
476 kint32 detPosColStart = nearest<kint32> (start_r * cos (theta - start_phi) / detInc * scale);
478 for (int ix = 0; ix < nx; ix++, detPosColStart += det_dx) {
479 kint32 curDetPos = detPosColStart;
480 ImageFileColumn pImCol = v[ix];
482 for (int iy = 0; iy < ny; iy++, curDetPos += det_dy) {
483 if (interpType == Backprojector::INTERP_NEAREST) {
484 int detPosNearest = (curDetPos >= 0 ? ((curDetPos + halfScale) / scale) : ((curDetPos - halfScale) / scale));
485 int iDetPos = iDetCenter + detPosNearest; // calc index in the filtered raysum vector
487 if (iDetPos < 0 || iDetPos >= nDet) // check for impossible: index outside of raysum pos
488 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
490 *pImCol++ += filteredProj[iDetPos];
491 } else if (interpType == Backprojector::INTERP_LINEAR) {
492 kint32 detPosFloor = curDetPos / scale;
493 kint32 detPosRemainder = curDetPos % scale;
494 if (detPosRemainder < 0) {
496 detPosRemainder += scale;
498 int iDetPos = iDetCenter + detPosFloor;
499 double frac = detPosRemainder / dScale;
500 if (iDetPos < 0 || iDetPos >= nDet - 1)
501 errorIndexOutsideDetector (ix, iy, theta, curDetPos, iDetPos);
503 *pImCol++ += ((1-frac) * filteredProj[iDetPos] + frac * filteredProj[iDetPos+1]);