Interim work on creating projections from an imagefile
[ctsim.git] / libctsim / scanner.cpp
index 850b99eef9f8a4c9da0185ce89bd507cbc622913..34864fb018a6ce94a1ba63bd1cb2904777226614 100644 (file)
@@ -312,10 +312,10 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS
 #ifdef HAVE_OPENMP
   #pragma omp parallel for
 #endif
-  
+
   for (int iView = 0;  iView < iNumViews; iView++) {
     double viewAngle = start_angle + (iView * proj.rotInc());
-    
+
     // With OpenMP, need to calculate source and detector positions at each view
     GRFMTX_2D rotmtx;
     mtx2_offset_rot (rotmtx, viewAngle, m_dXCenter, m_dYCenter);
@@ -326,12 +326,12 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS
       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 );
 
@@ -370,7 +370,6 @@ Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iS
 
       m_pSGP->setMarker (SGP::MARKER_BDIAMOND);
 
-      
       m_pSGP->setColor (C_BLACK);
       m_pSGP->setPenWidth (2);
       if (m_idGeometry == GEOMETRY_PARALLEL) {
@@ -605,13 +604,45 @@ 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();
+    int nx = im->nx(), ny = im->ny();
+
+    // Convert endpoints into image pixel coordinates
+    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]");
+    }
+    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);
+
+    double xlen = xmax - xmin, 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;
+
+    // Use Bresenham integer line stepping to step to each pixel, and walked through image
+
+    sys_error(ERR_WARNING, "Not yet able to project through imagefile. Line (%.3f,%.3f) - (%.3f,%.3f)", px1, py1, px2, py2);
+
+  } 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);
 }