X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fscanner.cpp;h=204c9cb13b86b30a3c8130ad0eea0d1a05af3c61;hp=ce43bfcc5a4b43498111c3365c78bba106dc45de;hb=dca8d386ab6a4e33050b456cff4547d6adfdf249;hpb=d84febd85035c7985334618cdda43df21824efbe diff --git a/libctsim/scanner.cpp b/libctsim/scanner.cpp index ce43bfc..204c9cb 100644 --- a/libctsim/scanner.cpp +++ b/libctsim/scanner.cpp @@ -255,13 +255,13 @@ Scanner::convertGeometryNameToID (const char* const geomName) { int id = GEOMETRY_INVALID; - for (int i = 0; i < s_iGeometryCount; i++) + for (int i = 0; i < s_iGeometryCount; i++) { if (strcasecmp (geomName, s_aszGeometryName[i]) == 0) { id = i; break; } - - return (id); + } + return (id); } @@ -292,6 +292,15 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS collectProjections (proj, phm, iStartView, iNumViews, iOffsetView, iStorageOffset, trace, pSGP); } +static void mtx2_offset_rot (GRFMTX_2D m, double angle, double x, double y) { + GRFMTX_2D temp; + xlat_mtx2 (m, -x, -y); + rot_mtx2 (temp, angle); + mult_mtx2 (m, temp, m); + xlat_mtx2 (temp, x, y); + mult_mtx2 (m, temp, m); +} + void Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView, const int iNumViews, const int iOffsetView, int iStorageOffset, @@ -299,37 +308,37 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS { m_trace = trace; double start_angle = (iStartView + iOffsetView) * proj.rotInc(); + int parallel_enabled = 1; + UNUSED(parallel_enabled); - // Calculate initial rotation matrix - GRFMTX_2D rotmtx_initial, temp; - xlat_mtx2 (rotmtx_initial, -m_dXCenter, -m_dYCenter); - rot_mtx2 (temp, start_angle); - mult_mtx2 (rotmtx_initial, temp, rotmtx_initial); - xlat_mtx2 (temp, m_dXCenter, m_dYCenter); - mult_mtx2 (rotmtx_initial, temp, rotmtx_initial); - - double xd1=0, yd1=0, xd2=0, yd2=0; - if (m_idGeometry != GEOMETRY_EQUIANGULAR) { - xd1 = m_initPos.xd1; - yd1 = m_initPos.yd1; - xd2 = m_initPos.xd2; - yd2 = m_initPos.yd2; - xform_mtx2 (rotmtx_initial, xd1, yd1); // rotate detector endpoints - xform_mtx2 (rotmtx_initial, xd2, yd2); // to initial view_angle - } +#if HAVE_SGP + if (pSGP && (m_trace >= Trace::TRACE_PHANTOM)) + parallel_enabled = 0; +#endif - double xs1 = m_initPos.xs1; - double ys1 = m_initPos.ys1; - double xs2 = m_initPos.xs2; - double ys2 = m_initPos.ys2; - xform_mtx2 (rotmtx_initial, xs1, ys1); // rotate source endpoints to - xform_mtx2 (rotmtx_initial, xs2, ys2); // initial view angle +#if HAVE_OPENMP + #pragma omp parallel for if (parallel_enabled) +#endif + for (int iView = 0; iView < iNumViews; iView++) { + double viewAngle = start_angle + (iView * proj.rotInc()); - int iView; - double viewAngle; - for (iView = 0, viewAngle = start_angle; iView < iNumViews; iView++, viewAngle += proj.rotInc()) { - int iStoragePosition = iView + iStorageOffset; + // With OpenMP, need to calculate source and detector positions at each view + GRFMTX_2D rotmtx; + mtx2_offset_rot (rotmtx, viewAngle, m_dXCenter, m_dYCenter); + double xd1=0, yd1=0, xd2=0, yd2=0; + if (m_idGeometry != GEOMETRY_EQUIANGULAR) { + xd1 = m_initPos.xd1; yd1 = m_initPos.yd1; + xd2 = m_initPos.xd2; yd2 = m_initPos.yd2; + xform_mtx2 (rotmtx, xd1, yd1); // rotate detector endpoints + xform_mtx2 (rotmtx, xd2, yd2); // to initial view_angle + } + double xs1 = m_initPos.xs1, ys1 = m_initPos.ys1; + double xs2 = m_initPos.xs2, ys2 = m_initPos.ys2; + xform_mtx2 (rotmtx, xs1, ys1); // rotate source endpoints to + xform_mtx2 (rotmtx, xs2, ys2); // initial view angle + + int iStoragePosition = iView + iStorageOffset; DetectorArray& detArray = proj.getDetectorArray( iStoragePosition ); #ifdef HAVE_SGP @@ -366,11 +375,7 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS traceShowParam ("Samples / Ray:", "%d", PROJECTION_TRACE_ROW_SAMPLES, C_BLUE, m_nSample); m_pSGP->setMarker (SGP::MARKER_BDIAMOND); - } -#endif -#ifdef HAVE_SGP - if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) { m_pSGP->setColor (C_BLACK); m_pSGP->setPenWidth (2); if (m_idGeometry == GEOMETRY_PARALLEL) { @@ -398,6 +403,7 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS if (m_trace > Trace::TRACE_CONSOLE) traceShowParam ("Current View:", "%d (%.0f%%)", PROJECTION_TRACE_ROW_CURR_VIEW, C_RED, iView + iStartView, (iView + iStartView) / static_cast(m_nView) * 100.); #endif + if (m_trace == Trace::TRACE_CONSOLE) std::cout << "Current View: " << iView+iStartView << std::endl; @@ -409,12 +415,7 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS // rs_plot (detArray, xd1, yd1, dXCenter, dYCenter, theta); } #endif - xform_mtx2 (m_rotmtxIncrement, xs1, ys1); - xform_mtx2 (m_rotmtxIncrement, xs2, ys2); - if (m_idGeometry != GEOMETRY_EQUIANGULAR) { - xform_mtx2 (m_rotmtxIncrement, xd1, yd1); // rotate detector endpoints - xform_mtx2 (m_rotmtxIncrement, xd2, yd2); - } + } /* for each iView */ } @@ -596,6 +597,133 @@ Scanner::traceShowParamRasterOp (int iRasterOp, const char *szLabel, const char } } +void swap_xy_points (double& x1, double& y1, double& x2, double& y2) +{ + double temp = x1; x1 = x2; x2 = temp; + temp = y1; y1 = y2; y2 = temp; +} + +class WeightedPoint { +public: + int x, y; + double weight; + WeightedPoint (int _x, int _y, double _weight) + : x(_x), y(_y), weight(_weight) + {} +}; + + +/* FUNCTION + * Name: projection_pixel_weights + * Purpose: Returns a vector of WeightedPoint with the length of + * line that intersects with each pixel + */ + +void +projection_pixel_weights (std::vector& wp, const int nx, const int ny, + double x1, double y1, double x2, double y2) +{ + double ylen = fabs(y2-y1); + double xlen = fabs(x2-x1); + bool swap_xy = false, invert_slope = false; + double slope; + + if (ylen > xlen) { + swap_xy = true; + slope = xlen / ylen; + if (y2 < y1) // swap start/end so always moving from bottom to top + swap_xy_points (x1, y1, x2, y2); + if (x2 < x1) { + invert_slope = true; + } + } else { +#if DEBUG + if (ylen == xlen) + sys_error(ERR_WARNING, "Slope == 1"); +#endif + slope = ylen / xlen; + if (x2 < x1) // swap start/end so always moving from left to right in image + swap_xy_points (x1, y1, x2, y2); + if (y2 < y1) { + invert_slope = true; + } + } + double angle = atan(fabs(slope)); + double minor_dist = sin(angle); // distance along minor axis + double pixel_len = 1 / cos(angle); + + int minor_dir = 1; + if (invert_slope) { + minor_dir = -1; + slope = -slope; + } + + double x = x1, y = y1; + int ix = floor(x); + int iy = floor(y); + double ydelta = y - iy; + double xdelta = x - ix; + + double min_delta; + int *imaj, *imin; + int max_maj, max_min; + if (swap_xy) { + min_delta = xdelta; + imaj = &iy; + imin = &ix; + max_maj = ny; + max_min = nx; + } else { + min_delta = ydelta; + imaj = &ix; + imin = &iy; + max_maj = nx; + max_min = ny; + } + +#if DEBUG + sys_error(ERR_TRACE, "m=%6.3f swap_xy=%d invert=%d len=%8.6f min_delta=%.4g minor_dist=%6.3f (%.3f,%.3f)-(%.3f,%.3f)", + slope, swap_xy, invert_slope, pixel_len, min_delta, minor_dist, x1, y1, x2, y2); +#endif + + // if position of minor axis is at edge of image, but will be moving into pixel within image + if (*imin == max_min && invert_slope) { + (*imin)--; // select the pixel within image +#if DEBUG + sys_error(ERR_TRACE, "Moving pixel inside image, adding %f to min_delta", (1+slope)); +#endif + min_delta += (1+slope); + } + + while (*imaj < max_maj && *imin < max_min && *imin >= 0) { + double next_min_delta = min_delta + slope; + + if (((!invert_slope) && (next_min_delta < 1)) || + (invert_slope && (next_min_delta > 0))) { + // stay within same pixel + double w = pixel_len; + WeightedPoint p (ix, iy, w); + wp.push_back(p); +#if DEBUG + sys_error(ERR_TRACE, " Full pixel: (%3d,%3d)=%.4g, min_delta=%.4g", ix, iy, w, min_delta); +#endif + min_delta = next_min_delta; + } else { + // Scale partial pixel_len into pixel + double norm_delta = invert_slope ? min_delta : (1 - min_delta); + double p1_line = norm_delta * pixel_len; + WeightedPoint p1 (ix, iy, p1_line); + wp.push_back (p1); +#if DEBUG + sys_error(ERR_TRACE, " Part pixel: (%3d,%3d)=%.4g, min_delta=%.4g", ix, iy, p1_line, min_delta); +#endif + (*imin) += minor_dir; + min_delta = next_min_delta - minor_dir; + } + (*imaj)++; + } + +} /* NAME @@ -609,13 +737,51 @@ Scanner::traceShowParamRasterOp (int iRasterOp, const char *szLabel, const char */ double -Scanner::projectSingleLine (const Phantom& phm, const double x1, const double y1, const double x2, const double y2) +Scanner::projectSingleLine (const Phantom& phm, double x1, double y1, double x2, double y2) { - // check ray against each pelem in Phantom double rsum = 0.0; - for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++) - rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2); + if (phm.isImagefile()) { + // Project through an imagefile + + const ImageFile* im = phm.getImagefile(); + const ImageFileArray v = im->getArray(); + + // Get image axis extents + int nx = im->nx(), ny = im->ny(); + double xmin=0, xmax=nx, ymin=0, ymax=ny; // default coordinate + if (! im->getAxisExtent (xmin, xmax, ymin, ymax)) { + sys_error(ERR_WARNING, "Axis extent not available [Scanner::projectSingleLine]"); + } + + // Clip line in image object coordinates + double rect[4]; + rect[0] = xmin; rect[1] = ymin; + rect[2] = xmax; rect[3] = ymax; + bool accept = clip_rect (x1, y1, x2, y2, rect); + if (! accept) + return (0.0); + + // Convert to pixel coordinates + double xlen = xmax - xmin; + double ylen = ymax - ymin; + double px1 = nx * (x1 - xmin) / xlen; + double px2 = nx * (x2 - xmin) / xlen; + double py1 = ny * (y1 - ymin) / ylen; + double py2 = ny * (y2 - ymin) / ylen; + + std::vector wp; + projection_pixel_weights (wp, nx, ny, px1, py1, px2, py2); + for (unsigned int i = 0; i < wp.size(); i++) { + WeightedPoint& p = wp[i]; + rsum += v[p.x][p.y] * p.weight; + } + } else { + + // Project through each pelem in Phantom + for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++) + rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2); + } return (rsum); }