r94: finished c++ conversions
[ctsim.git] / src / phm2if.cpp
index 63d5a49b7a38ce2987cddd707fe0f78aab929c9f..208de9acd7dd6cb5710984b5602da27530f38f92 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: phm2if.cpp,v 1.7 2000/06/09 11:03:08 kevin Exp $
+**  $Id: phm2if.cpp,v 1.8 2000/06/13 16:20:31 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
 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/
 
-/* FILE
- *   phm2sdf.c                 Generate a SDF image from a phantom
- *
- * HISTORY
- *   1984 - Final DOS verion
- *   1999 - First UNIX version
- */
-
 #include "ct.h"
+#include "timer.h"
+
 
 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP, 
        O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
@@ -57,59 +51,59 @@ static struct option my_options[] =
 };
 
 void 
-phm2sdf_usage (const char *program)
+phm2if_usage (const char *program)
 {
-  fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program)); 
-  fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
-  fprintf(stdout,"\n");
-  fprintf(stdout,"     outfile         Name of output file for image\n");
-  fprintf(stdout,"     nx              Number of pixels X-axis\n");
-  fprintf(stdout,"     ny              Number of pixels Y-axis\n");
-  fprintf(stdout,"     --phantom       Phantom to use for projection\n");
-  fprintf(stdout,"        herman       Herman head phantom\n");
-  fprintf(stdout,"        rowland      Rowland head phantom\n");
-  fprintf(stdout,"        browland     Bordered Rowland head phantom\n");
-  fprintf(stdout,"        unitpulse    Unit pulse phantom\n");
-  fprintf(stdout,"     --phmfile       Generate Phantom from phantom file\n");
-  fprintf(stdout,"     --filter        Generate Phantom from a filter function\n");
-  fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting\n");
-  fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
-  fprintf(stdout,"       abs_cos       Abs * Cosine\n");
-  fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
-  fprintf(stdout,"       shepp         Shepp-Logan\n");
-  fprintf(stdout,"       bandlimit     Bandlimiting\n");
-  fprintf(stdout,"       sinc          Sinc\n");
-  fprintf(stdout,"       cos           Cosine\n");
-  fprintf(stdout,"       triangle      Triangle\n");
-  fprintf(stdout,"       hamming       Hamming\n");
-  fprintf(stdout,"     --filter-param  Alpha level for Hamming filter\n");
-  fprintf(stdout,"     --filter-domain Set domain of filter\n");
-  fprintf(stdout,"         spatial     Spatial domain (default)\n");
-  fprintf(stdout,"         freq        Frequency domain\n");
-  fprintf(stdout,"     --filter-bw     Filter bandwidth (default = 1)\n");
-  fprintf(stdout,"     --desc          Description of raysum\n");
-  fprintf(stdout,"     --nsample       Number of samples per axis per pixel (default = 1)\n");
-  fprintf(stdout,"     --trace         Trace level to use\n");
-  fprintf(stdout,"        none         No tracing (default)\n");
-  fprintf(stdout,"        text         Trace text level\n");
-  fprintf(stdout,"        phm          Trace phantom\n");
-  fprintf(stdout,"        rays         Trace rays\n");
-  fprintf(stdout,"        plot         Trace plot\n");
-  fprintf(stdout,"        clipping     Trace clipping\n");
-  fprintf(stdout,"     --debug         Debug mode\n");
-  fprintf(stdout,"     --verbose       Verbose mode\n");
-  fprintf(stdout,"     --version       Print version\n");
-  fprintf(stdout,"     --help          Print this help message\n");
+  cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]" << endl;
+  cout << "Generate phantom image from a predefined --phantom or a --phmfile" << endl;
+  cout << endl;
+  cout << "     outfile         Name of output file for image" << endl;
+  cout << "     nx              Number of pixels X-axis" << endl;
+  cout << "     ny              Number of pixels Y-axis" << endl;
+  cout << "     --phantom       Phantom to use for projection" << endl;
+  cout << "        herman       Herman head phantom" << endl;
+  cout << "        rowland      Rowland head phantom" << endl;
+  cout << "        browland     Bordered Rowland head phantom" << endl;
+  cout << "        unitpulse    Unit pulse phantom" << endl;
+  cout << "     --phmfile       Generate Phantom from phantom file" << endl;
+  cout << "     --filter        Generate Phantom from a filter function" << endl;
+  cout << "       abs_bandlimit Abs * Bandlimiting" << endl;
+  cout << "       abs_sinc      Abs * Sinc" << endl;
+  cout << "       abs_cos       Abs * Cosine" << endl;
+  cout << "       abs_hamming   Abs * Hamming" << endl;
+  cout << "       shepp         Shepp-Logan" << endl;
+  cout << "       bandlimit     Bandlimiting" << endl;
+  cout << "       sinc          Sinc" << endl;
+  cout << "       cos           Cosine" << endl;
+  cout << "       triangle      Triangle" << endl;
+  cout << "       hamming       Hamming" << endl;
+  cout << "     --filter-param  Alpha level for Hamming filter" << endl;
+  cout << "     --filter-domain Set domain of filter" << endl;
+  cout << "         spatial     Spatial domain (default)" << endl;
+  cout << "         freq        Frequency domain" << endl;
+  cout << "     --filter-bw     Filter bandwidth (default = 1)" << endl;
+  cout << "     --desc          Description of raysum" << endl;
+  cout << "     --nsample       Number of samples per axis per pixel (default = 1)" << endl;
+  cout << "     --trace         Trace level to use" << endl;
+  cout << "        none         No tracing (default)" << endl;
+  cout << "        text         Trace text level" << endl;
+  cout << "        phm          Trace phantom" << endl;
+  cout << "        rays         Trace rays" << endl;
+  cout << "        plot         Trace plot" << endl;
+  cout << "        clipping     Trace clipping" << endl;
+  cout << "     --debug         Debug mode" << endl;
+  cout << "     --verbose       Verbose mode" << endl;
+  cout << "     --version       Print version" << endl;
+  cout << "     --help          Print this help message" << endl;
 }
 
 #ifdef HAVE_MPI
-void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_local, const int opt_debug);
+void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug);
 #endif
 
 int 
-phm2sdf_main (int argc, char* argv[])
+phm2if_main (int argc, char* argv[])
 {
-  ImageFile* im_global = NULL;
+  ImageFile* imGlobal = NULL;
   PHANTOM *phm = NULL;
   int opt_nx = 0, opt_ny = 0;
   int opt_nsample = 1;
@@ -125,23 +119,16 @@ phm2sdf_main (int argc, char* argv[])
   double opt_filter_bw = 1.;
   int opt_trace = TRACE_NONE;
   int opt_verbose = 0;
-  double time_start=0, time_end=0;
 #ifdef HAVE_MPI
-  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
-  ImageFile* im_local = NULL;
+  ImageFile* imLocal = NULL;
   MPIWorld mpiWorld (argc, argv);
 #endif
 
-#ifdef HAVE_MPI
-  time_start = MPI::Wtime();
-#else
-  time_start = td_current_sec();
-#endif
+  Timer timerProgram;
   
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0) {
 #endif
-
     strcpy(opt_desc, "");
     strcpy(opt_phmfilename, "");
     while (1) {
@@ -155,7 +142,7 @@ phm2sdf_main (int argc, char* argv[])
       switch (c) {
       case O_PHANTOM:
        if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
-         phm2sdf_usage(argv[0]);
+         phm2if_usage(argv[0]);
          return (1);
        }
        break;
@@ -164,7 +151,7 @@ phm2sdf_main (int argc, char* argv[])
        phm = phm_create_from_file(opt_phmfilename);
 #ifdef HAVE_MPI
        if (mpiWorld.getRank() == 0) 
-         fprintf(stderr, "Can't use phantom from file in MPI mode\n");
+         cerr << "Can't use phantom from file in MPI mode" << endl;
        return (1);
 #endif
        break;
@@ -176,19 +163,19 @@ phm2sdf_main (int argc, char* argv[])
        break;
       case O_TRACE:
        if ((opt_trace = opt_set_trace(optarg)) < 0) {
-         phm2sdf_usage(argv[0]);
+         phm2if_usage(argv[0]);
          return (1);
        }
        break;
       case O_FILTER:
        if ((opt_filter = opt_set_filter(optarg)) < 0) {
-         phm2sdf_usage (argv[0]);
+         phm2if_usage (argv[0]);
          return (1);
        }
        break;
       case O_FILTER_DOMAIN:
        if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
-         phm2sdf_usage (argv[0]);
+         phm2if_usage (argv[0]);
          return (1);
        }
        break;
@@ -200,7 +187,7 @@ phm2sdf_main (int argc, char* argv[])
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
          fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
-         phm2sdf_usage(argv[0]);
+         phm2if_usage(argv[0]);
          return (1);
        }
        break;
@@ -209,7 +196,7 @@ phm2sdf_main (int argc, char* argv[])
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
          fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
-         phm2sdf_usage(argv[0]);
+         phm2if_usage(argv[0]);
          return (1);
        }
        break;
@@ -218,34 +205,34 @@ phm2sdf_main (int argc, char* argv[])
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
          fprintf(stderr,"Error setting --nsample to %s\n", optarg);
-         phm2sdf_usage(argv[0]);
+         phm2if_usage(argv[0]);
          return (1);
        }
        break;
         case O_VERSION:
 #ifdef VERSION
-         fprintf(stdout, "Version %s\n", VERSION);
+         cout <<  "Version " << VERSION << endl;
 #else
-         fprintf(stderr, "Unknown version number");
+          cerr << "Unknown version number" << endl;
 #endif
       case O_HELP:
       case '?':
-       phm2sdf_usage(argv[0]);
+       phm2if_usage(argv[0]);
        return (0);
       default:
-       phm2sdf_usage(argv[0]);
+       phm2if_usage(argv[0]);
        return (1);
       }
     }
     
     if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
-      fprintf(stderr, "No phantom defined\n");
-      phm2sdf_usage(argv[0]);
+      cerr << "No phantom defined" << endl;
+      phm2if_usage(argv[0]);
       return (1);
     }
 
     if (optind + 3 != argc) {
-      phm2sdf_usage(argv[0]);
+      phm2if_usage(argv[0]);
       return (1);
     }
     opt_outfile = argv[optind];
@@ -253,14 +240,14 @@ phm2sdf_main (int argc, char* argv[])
     endstr = argv[optind+1] + strlen(argv[optind+1]);
     if (endptr != endstr) {
       fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
-      phm2sdf_usage(argv[0]);
+      phm2if_usage(argv[0]);
       return (1);
     }
     opt_ny = strtol(argv[optind+2], &endptr, 10);
     endstr = argv[optind+2] + strlen(argv[optind+2]);
     if (endptr != endstr) {
       fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
-      phm2sdf_usage(argv[0]);
+      phm2if_usage(argv[0]);
       return (1);
     }
     
@@ -286,13 +273,13 @@ phm2sdf_main (int argc, char* argv[])
     strncpy(opt_desc, str, sizeof(opt_desc));
     
     if (opt_verbose)
-      printf("Rasterize Phantom to Image\n\n");
+      cout << "Rasterize Phantom to Image" << endl << endl;
 #ifdef HAVE_MPI
   }
 #endif
 
 #ifdef HAVE_MPI
-  mpi_t1 = MPI::Wtime();
+  TimerMPI 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);
@@ -305,24 +292,19 @@ phm2sdf_main (int argc, char* argv[])
   mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
   mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
 
-  if (opt_verbose) {
-    mpi_t2 = MPI::Wtime();
-    mpi_t = mpi_t2 - mpi_t1;
-    mpiWorld.getComm().Reduce (&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-    if (mpiWorld.getRank() == 0)
-      printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
-  }
+  if (opt_verbose)
+    timerBcast.timerEndAndReport ("Time to broadcast variables");
 
   mpiWorld.setTotalWorkUnits (opt_nx);
 
   if (mpiWorld.getRank() == 0) {
-    im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
-    im_global->fileCreate();
+    imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
+    imGlobal->fileCreate();
   }
-  im_local = new ImageFile (opt_nx, opt_ny);
+  imLocal = new ImageFile (opt_nx, opt_ny);
 #else
-  im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
-  im_global->fileCreate ();
+  imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
+  imGlobal->fileCreate ();
 #endif
 
   if (opt_phmnum >= 0)
@@ -331,76 +313,60 @@ phm2sdf_main (int argc, char* argv[])
 #ifdef HAVE_MPI
   else {
     if (mpiWorld.getRank() == 0)
-      fprintf(stderr, "phmnum < 0\n");
+      cerr << "phmnum < 0" << endl;
     exit(1);
   }
 #endif
 
-  double calctime = 0;
   ImageFileArray v;
-
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0)
-    v = im_global->getArray ();
+    v = imGlobal->getArray ();
 
   if (phm->type == P_UNIT_PULSE) {
     if (mpiWorld.getRank() == 0) {
       v[opt_nx/2][opt_ny/2] = 1.;
-      calctime = 0;
     }
   } else if (opt_filter != -1) {
     if (mpiWorld.getRank() == 0) {
-      image_filter_response (*im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
-      calctime = 0;
+      image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
     }
   } else {
-    mpiWorld.getComm().Barrier();
+    TimerMPI timerRasterize (mpiWorld.getComm());
+    phm_to_imagefile (phm, *imLocal, mpiWorld.getMyStartWorkUnit(),  mpiWorld.getMyLocalWorkUnits(), opt_nsample, opt_trace);
+    if (opt_verbose)
+      timerRasterize.timerEndAndReport ("Time to rasterize phantom");
 
+    TimerMPI timerGather (mpiWorld.getComm());
+    mpi_gather_image (mpiWorld, imGlobal, imLocal, opt_debug);
     if (opt_verbose)
-      mpi_t1 = MPI::Wtime();
-    phm_to_imagefile (phm, *im_local, mpiWorld.getMyStartWorkUnit(),  mpiWorld.getMyLocalWorkUnits(), opt_nsample, opt_trace);
-    if (opt_verbose) {
-      mpi_t2 = MPI::Wtime();
-      mpi_t = mpi_t2 - mpi_t1;
-      mpiWorld.getComm().Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-      if (mpiWorld.getRank() == 0)
-       printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
-    }
-    mpi_gather_image (mpiWorld, im_global, im_local, opt_debug);
+      timerGather.timerEndAndReport ("Time to gather image");
   }
 #else
-  v = im_global->getArray ();
+  v = imGlobal->getArray ();
   if (phm->type == P_UNIT_PULSE) {
     v[opt_nx/2][opt_ny/2] = 1.;
-    calctime = 0;
   } else if (opt_filter != -1) {
-    image_filter_response (*im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
-    calctime = 0;
+    image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
   } else {
 #if HAVE_SGP
       if (opt_trace >= TRACE_PHM)
        phm_show(phm);
 #endif
-      phm_to_imagefile (phm, *im_global, 0, opt_nx, opt_nsample, opt_trace);
+      phm_to_imagefile (phm, *imGlobal, 0, opt_nx, opt_nsample, opt_trace);
   }
 #endif
 
-#ifdef HAVE_MPI
-  time_end = MPI::Wtime();
-#else
-  time_end = td_current_sec();
-#endif
-
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0) 
 #endif
   {
-    im_global->arrayDataWrite ();
-    calctime = time_end - time_start;
-    im_global->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
-    im_global->fileClose ();
+    imGlobal->arrayDataWrite ();
+    double calctime = timerProgram.timerEnd ();
+    imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
+    imGlobal->fileClose ();
     if (opt_verbose)
-      fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime);
+      cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
 
     if (opt_trace >= TRACE_PHM) {
       double dmin, dmax;
@@ -410,7 +376,7 @@ phm2sdf_main (int argc, char* argv[])
       scanf ("%d", &nscale);
       printf ("Enter minimum and maximum densities (min, max): ");
       scanf ("%lf %lf", &dmin, &dmax);
-      image_display_scale (*im_global, nscale, dmin, dmax);
+      image_display_scale (*imGlobal, nscale, dmin, dmax);
     }
   }
 
@@ -422,23 +388,23 @@ phm2sdf_main (int argc, char* argv[])
 
 
 #ifdef HAVE_MPI
-void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_local, const int opt_debug)
+void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug)
 {
-  ImageFileArray vLocal = im_local->getArray();
+  ImageFileArray vLocal = imLocal->getArray();
   ImageFileArray vGlobal = NULL;
-  int nyLocal = im_local->ny();
+  int nyLocal = imLocal->ny();
 
   if (mpiWorld.getRank() == 0)
-    vGlobal = im_global->getArray();
+    vGlobal = imGlobal->getArray();
   
   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
-    mpiWorld.getComm().Send(vLocal[iw], nyLocal, im_local->getMPIDataType(), 0, 0);
+    mpiWorld.getComm().Send(vLocal[iw], nyLocal, imLocal->getMPIDataType(), 0, 0);
 
   if (mpiWorld.getRank() == 0) {
     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
        MPI::Status status;
-       mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, im_local->getMPIDataType(), iProc, 0, status);
+       mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, imLocal->getMPIDataType(), iProc, 0, status);
       }
     }
   }
@@ -450,6 +416,6 @@ void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_l
 int 
 main (int argc, char* argv[])
 {
-  return (phm2sdf_main(argc, argv));
+  return (phm2if_main(argc, argv));
 }
 #endif