r120: *** empty log message ***
[ctsim.git] / src / ctrec.cpp
index c9c3ead1f20c3ec1df854945278d9d27b9fd888c..dc945e35bb9c1524003437177190a7b8f754ed8d 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: ctrec.cpp,v 1.8 2000/06/13 16:20:31 kevin Exp $
+**  $Id: ctrec.cpp,v 1.14 2000/06/25 17:32:24 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
@@ -95,7 +95,7 @@ ctrec_usage (const char *program)
 
 
 #ifdef HAVE_MPI
-static void ScatterProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int debug);
+static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int debug);
 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
 #endif
 
@@ -103,24 +103,24 @@ static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* i
 int 
 ctrec_main (int argc, char * argv[])
 {
-  ImageFile *imGlobal = NULL;
-  RAYSUM *rsGlobal = NULL;
-  char *rs_name, *im_filename = NULL;
-  char remark[MAXREMARK];
-  char filt_name[80];
+  Projections projGlobal;
+  ImageFile* imGlobal = NULL;
+  char* filenameProj = NULL;
+  char* filenameImage = NULL;
+  string remark;
   char *endptr;
-  int opt_verbose = 0;
-  int opt_debug = 0;
-  int opt_trace = TRACE_NONE;
-  double opt_filter_param = 1;
-  FilterType opt_filter = FILTER_ABS_BANDLIMIT;
-  InterpolationType opt_interp = I_LINEAR;
-  int opt_interp_param = 1;
-  BackprojType opt_backproj = O_BPROJ_DIFF2;
+  int optVerbose = 0;
+  int optDebug = 0;
+  int optTrace = TRACE_NONE;
+  double optFilterParam = -1;
+  string optFilterName = SignalFilter::FILTER_ABS_BANDLIMIT_STR;
+  string optInterpName = Backprojector::INTERP_LINEAR_STR;
+  string optBackprojName = Backprojector::BPROJ_IDIFF2_STR;
+  //  string optFilterMethodName = SignalFilter::FILTER_METHOD_CONVOLUTION_STR;
+  int optInterpParam = 1;
   int nx, ny;
 #ifdef HAVE_MPI
-  ImageFile *imLocal;
-  RAYSUM *rsLocal;
+  ImageFile* imLocal;
   int mpi_nview, mpi_ndet;
   double mpi_detinc, mpi_rotinc, mpi_phmlen;
   MPIWorld mpiWorld (argc, argv);
@@ -141,48 +141,39 @@ ctrec_main (int argc, char * argv[])
       switch (c)
        {
        case O_INTERP:
-         if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
-           ctrec_usage(argv[0]);
-           return (1);
-         }
+         optInterpName = optarg;
          break;
        case O_FILTER:
-         if ((opt_filter = opt_set_filter(optarg)) < 0) {
-           ctrec_usage(argv[0]);
-           return (1);
-         }
+         optFilterName = optarg;
          break;
        case O_BACKPROJ:
-         if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
-             ctrec_usage(argv[0]);
-             return (1);
-         }
+         optBackprojName = optarg;
          break;
        case O_FILTER_PARAM:
-         opt_filter_param = strtod(optarg, &endptr);
+         optFilterParam = strtod(optarg, &endptr);
          if (endptr != optarg + strlen(optarg)) {
            ctrec_usage(argv[0]);
          }
          break;
        case O_VERBOSE:
-         opt_verbose = 1;
+         optVerbose = 1;
          break;
        case O_DEBUG:
-         opt_debug = 1;
+         optDebug = 1;
          break;
        case O_TRACE:
-         if ((opt_trace = opt_set_trace(optarg)) < 0) {
+         if ((optTrace = opt_set_trace(optarg)) < 0) {
            ctrec_usage(argv[0]);
            return (1);
          }
          break;
         case O_VERSION:
 #ifdef VERSION
-       cout <<  "Version " <<  VERSION << endl;
+         cout <<  "Version " <<  VERSION << endl;
 #else
-        cout << "Unknown version number" << endl;
+          cout << "Unknown version number" << endl;
 #endif
-         exit(0);
+         return (0);
        case O_HELP:
        case '?':
          ctrec_usage(argv[0]);
@@ -198,23 +189,24 @@ ctrec_main (int argc, char * argv[])
       return (1);
     }
 
-    rs_name = argv[optind];
+    filenameProj = argv[optind];
   
-    im_filename = argv[optind + 1];
+    filenameImage = argv[optind + 1];
   
     nx = strtol(argv[optind + 2], &endptr, 10);
     ny = strtol(argv[optind + 3], &endptr, 10);
   
-    if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
-      snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
-              name_of_filter (opt_filter), opt_filter_param)
+    ostringstream filterDesc;
+    if (optFilterParam >= 0)
+      filterDesc << optFilterName << ": alpha=" << optFilterParam
     else
-      snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
-  
-    snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
-            nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
+      filterDesc << optFilterName;
+
+    ostringstream label;
+    label << "Reconstruct: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << optInterpName << ", " << optBackprojName;
+    remark = label.str();
   
-    if (opt_verbose)
+    if (optVerbose)
       cout << "Remark: " << remark << endl;
 #ifdef HAVE_MPI
   }
@@ -222,27 +214,26 @@ ctrec_main (int argc, char * argv[])
 
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0) {
-    rsGlobal = raysum_open (rs_name);
-    raysum_read (rsGlobal);
-    if (opt_verbose)
-      raysum_print_info(rsGlobal);
-
-    mpi_ndet = rsGlobal->ndet;
-    mpi_nview = rsGlobal->nview;
-    mpi_detinc = rsGlobal->det_inc;
-    mpi_phmlen = rsGlobal->phmlen;
-    mpi_rotinc = rsGlobal->rot_inc;
+    projGlobal.read (filenameProj);
+    if (optVerbose)
+      projGlobal.printScanInfo();
+
+    mpi_ndet = projGlobal.nDet();
+    mpi_nview = projGlobal.nView();
+    mpi_detinc = projGlobal.detInc();
+    mpi_phmlen = projGlobal.phmLen();
+    mpi_rotinc = projGlobal.rotInc();
   }
 
   TimerCollectiveMPI timerBcast (mpiWorld.getComm());
-  mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_interp, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
-  mpiWorld.getComm().Bcast (&opt_interp_param, 1, MPI::INT, 0);
-  mpiWorld.getComm().Bcast (&opt_backproj, 1, MPI::INT, 0);
+  mpiWorld.BcastString (optBackprojName);
+  mpiWorld.BcastString (optFilterName);
+  mpiWorld.BcastString (optInterpName);
+  mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&optInterpParam, 1, MPI::INT, 0);
   mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
   mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
   mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
@@ -250,65 +241,61 @@ ctrec_main (int argc, char * argv[])
   mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
   mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
   mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
-  if (opt_verbose)
+  if (optVerbose)
       timerBcast.timerEndAndReport ("Time to broadcast variables");
 
   mpiWorld.setTotalWorkUnits (mpi_nview);
 
-  rsLocal = raysum_create (NULL, mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
-  rsLocal->ndet = mpi_ndet;
-  rsLocal->nview = mpi_nview;
-  rsLocal->det_inc = mpi_detinc;
-  rsLocal->phmlen = mpi_phmlen;
-  rsLocal->rot_inc = mpi_rotinc;
+  Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
+  projLocal.setDetInc (mpi_detinc);
+  projLocal.setPhmLen (mpi_phmlen);
+  projLocal.setRotInc (mpi_rotinc);
 
   TimerCollectiveMPI timerScatter (mpiWorld.getComm());
-  ScatterProjectionsMPI (mpiWorld, rsGlobal, rsLocal, opt_debug);
-  if (opt_verbose)
+  ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, optDebug);
+  if (optVerbose)
       timerScatter.timerEndAndReport ("Time to scatter projections");
 
   if (mpiWorld.getRank() == 0) {
-    imGlobal = new ImageFile (im_filename, nx, ny);
+    imGlobal = new ImageFile (filenameImage, nx, ny);
     imGlobal->fileCreate();
   }
 
   imLocal = new ImageFile (nx, ny);
 #else
-  rsGlobal = raysum_open (rs_name);
-  raysum_read (rsGlobal);
-  if (opt_verbose)
-    raysum_print_info(rsGlobal);
+  projGlobal.read (filenameProj);
+  if (optVerbose)
+    projGlobal.printScanInfo();
 
-  imGlobal = new ImageFile (im_filename, nx, ny);
+  imGlobal = new ImageFile (filenameImage, nx, ny);
   imGlobal->fileCreate();
 #endif
 
 #ifdef HAVE_MPI
   TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
-  proj_reconst (*imLocal, rsLocal, opt_filter, opt_filter_param, opt_interp, opt_interp_param, opt_backproj, opt_trace);
-  if (opt_verbose)
+  projLocal.reconstruct (*imLocal, optFilterName.c_str(), optFilterParam, optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
+  if (optVerbose)
       timerReconstruct.timerEndAndReport ("Time to reconstruct");
 
   TimerCollectiveMPI timerReduce (mpiWorld.getComm());
   ReduceImageMPI (mpiWorld, imLocal, imGlobal);
-  if (opt_verbose)
+  if (optVerbose)
       timerReduce.timerEndAndReport ("Time to reduce image");
 #else
-  proj_reconst (*imGlobal, rsGlobal, opt_filter, opt_filter_param, opt_interp, opt_interp_param, opt_backproj, opt_trace);
+  projGlobal.reconstruct (*imGlobal, optFilterName.c_str(), optFilterParam, optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
 #endif
 
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0)
 #endif
     {
-      raysum_close (rsGlobal);
-      double calctime = timerProgram.timerEnd();
+      double calcTime = timerProgram.timerEnd();
       imGlobal->arrayDataWrite ();
-      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, rsGlobal->remark, rsGlobal->calctime);
-      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark, calctime);
+      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, projGlobal.remark(), projGlobal.calcTime());
+      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark.c_str(), calcTime);
       imGlobal->fileClose ();
-      if (opt_verbose)
-       cout << "Run time: " << calctime << " seconds" << endl;
+      if (optVerbose)
+       cout << "Run time: " << calcTime << " seconds" << endl;
     }
 #ifdef HAVE_MPI
   MPI::Finalize();
@@ -324,25 +311,34 @@ ctrec_main (int argc, char * argv[])
 //////////////////////////////////////////////////////////////////////////////////////
 
 #ifdef HAVE_MPI
-static void ScatterProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug)
+static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int optDebug)
 {
   if (mpiWorld.getRank() == 0) {
     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
-       mpiWorld.getComm().Send(&rsGlobal->view[iw]->ndet, 1, MPI::INT, iProc, 0);
-       mpiWorld.getComm().Send(&rsGlobal->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0);
-       mpiWorld.getComm().Send(rsGlobal->view[iw]->detval, rsGlobal->ndet, MPI::FLOAT, iProc, 0);
+       DetectorArray& detarray = projGlobal.getDetectorArray( iw );
+       int nDet = detarray.nDet();
+       DetectorValue* detval = detarray.detValues();
+
+       double viewAngle = detarray.viewAngle();
+       mpiWorld.getComm().Send(&nDet, 1, MPI::INT, iProc, 0);
+       mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, iProc, 0);
+       mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, iProc, 0);
       }
     }
   }
 
   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
     MPI::Status status;
-    mpiWorld.getComm().Recv(&rsLocal->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
-    mpiWorld.getComm().Recv(&rsLocal->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
-    mpiWorld.getComm().Recv(rsLocal->view[iw]->detval, rsLocal->ndet, MPI::FLOAT, 0, 0, status);
+    int nDet;
+    double viewAngle;
+    DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
+
+    mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, 0, 0, status);
+    mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, 0, 0, status);
+    mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, 0, 0, status);
+    projLocal.getDetectorArray(iw).setViewAngle( viewAngle );
   }
-  rsLocal->nview = mpiWorld.getMyLocalWorkUnits();
 }
 
 static void
@@ -367,7 +363,17 @@ ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
 int 
 main (int argc, char* argv[])
 {
-  return (ctrec_main(argc, argv));
+  int retval = 1;
+
+  try {
+    retval = ctrec_main(argc, argv);
+  } catch (exception e) {
+    cerr << "Exception: " << e.what() << endl;
+  } catch (...) {
+    cerr << "Unknown exception" << endl;
+  }
+
+  return (retval);
 }
 #endif