r102: fixed mpi bug
[ctsim.git] / src / phm2if.cpp
index 208de9acd7dd6cb5710984b5602da27530f38f92..0eb5c9e1ebc544c765b6788db92e2cccf4b5ce5e 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: phm2if.cpp,v 1.8 2000/06/13 16:20:31 kevin Exp $
+**  $Id: phm2if.cpp,v 1.11 2000/06/19 17:58:13 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
@@ -104,7 +104,7 @@ int
 phm2if_main (int argc, char* argv[])
 {
   ImageFile* imGlobal = NULL;
-  PHANTOM *phm = NULL;
+  Phantom phm;
   int opt_nx = 0, opt_ny = 0;
   int opt_nsample = 1;
   int opt_phmnum = -1;
@@ -112,13 +112,13 @@ phm2if_main (int argc, char* argv[])
   DomainType opt_filter_domain = D_SPATIAL;
   char *opt_outfile = NULL;
   int opt_debug = 0;
-  char str[256];
-  char opt_desc[256], opt_phmfilename[256];
+  string opt_desc;
+  string opt_phmfilename;
   char *endstr, *endptr;
   double opt_filter_param = 1;
   double opt_filter_bw = 1.;
   int opt_trace = TRACE_NONE;
-  int opt_verbose = 0;
+  bool opt_verbose = false;
 #ifdef HAVE_MPI
   ImageFile* imLocal = NULL;
   MPIWorld mpiWorld (argc, argv);
@@ -129,8 +129,6 @@ phm2if_main (int argc, char* argv[])
 #ifdef HAVE_MPI
   if (mpiWorld.getRank() == 0) {
 #endif
-    strcpy(opt_desc, "");
-    strcpy(opt_phmfilename, "");
     while (1) {
       int c = getopt_long(argc, argv, "", my_options, NULL);
       char *endptr = NULL;
@@ -147,8 +145,8 @@ phm2if_main (int argc, char* argv[])
        }
        break;
       case O_PHMFILE:
-       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
-       phm = phm_create_from_file(opt_phmfilename);
+       opt_phmfilename = optarg;
+       phm.createFromFile(opt_phmfilename.c_str());
 #ifdef HAVE_MPI
        if (mpiWorld.getRank() == 0) 
          cerr << "Can't use phantom from file in MPI mode" << endl;
@@ -156,7 +154,7 @@ phm2if_main (int argc, char* argv[])
 #endif
        break;
       case O_VERBOSE:
-       opt_verbose = 1;
+       opt_verbose = true;
        break;
       case O_DEBUG:
        opt_debug = 1;
@@ -180,13 +178,13 @@ phm2if_main (int argc, char* argv[])
        }
        break;
       case O_DESC:
-       strncpy(opt_desc, optarg, sizeof(opt_desc));
+       opt_desc =  optarg;
        break;
       case O_FILTER_BW:
        opt_filter_bw = strtod(optarg, &endptr);
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
-         fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
+         sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
          phm2if_usage(argv[0]);
          return (1);
        }
@@ -195,7 +193,7 @@ phm2if_main (int argc, char* argv[])
        opt_filter_param = strtod(optarg, &endptr);
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
-         fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
+         sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
          phm2if_usage(argv[0]);
          return (1);
        }
@@ -204,7 +202,7 @@ phm2if_main (int argc, char* argv[])
        opt_nsample = strtol(optarg, &endptr, 10);
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
-         fprintf(stderr,"Error setting --nsample to %s\n", optarg);
+         sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
          phm2if_usage(argv[0]);
          return (1);
        }
@@ -225,7 +223,7 @@ phm2if_main (int argc, char* argv[])
       }
     }
     
-    if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
+    if (phm.nPElem() == 0 && opt_phmnum == -1 && opt_filter == -1) {
       cerr << "No phantom defined" << endl;
       phm2if_usage(argv[0]);
       return (1);
@@ -239,38 +237,31 @@ phm2if_main (int argc, char* argv[])
     opt_nx = strtol(argv[optind+1], &endptr, 10);
     endstr = argv[optind+1] + strlen(argv[optind+1]);
     if (endptr != endstr) {
-      fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
+      sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
       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]);
+      sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
       phm2if_usage(argv[0]);
       return (1);
     }
     
-    snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
-    if (opt_phmfilename[0]) {
-      strncat(str, "phantom=", sizeof(str));
-      strncat(str, opt_phmfilename, sizeof(str));
-    }
-    else if (opt_phmnum != -1) {
-      strncat(str, "phantom=", sizeof(str));
-      strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
-    }
+    ostringstream oss;
+    oss << "nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
+    if (opt_phmfilename.length())
+      oss << "phantom=" << opt_phmfilename;
+    else if (opt_phmnum != -1)
+      oss << "phantom=" << name_of_phantom(opt_phmnum);
     else if (opt_filter != -1) {
-      strncat(str, "filter=", sizeof(str));
-      strncat(str, name_of_filter(opt_filter), sizeof(str));
-      strncat(str, " - ", sizeof(str));
-      strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
+      oss << "filter=" << name_of_filter(opt_filter);
+      oss << " - " << name_of_filter_domain(opt_filter_domain);
     }
-    if (opt_desc[0]) {
-      strncat(str, ": ", sizeof(str));
-      strncat(str, opt_desc, sizeof(str));
-    }
-    strncpy(opt_desc, str, sizeof(opt_desc));
+    if (opt_desc.length())
+      oss << ": " << opt_desc;
+    opt_desc = oss.str();
     
     if (opt_verbose)
       cout << "Rasterize Phantom to Image" << endl << endl;
@@ -279,7 +270,7 @@ phm2if_main (int argc, char* argv[])
 #endif
 
 #ifdef HAVE_MPI
-  TimerMPI timerBcast (mpiWorld.getComm());
+  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);
@@ -308,13 +299,13 @@ phm2if_main (int argc, char* argv[])
 #endif
 
   if (opt_phmnum >= 0)
-      phm = phm_create (opt_phmnum);
+      phm.create (opt_phmnum);
 
 #ifdef HAVE_MPI
   else {
     if (mpiWorld.getRank() == 0)
       cerr << "phmnum < 0" << endl;
-    exit(1);
+    return (1);
   }
 #endif
 
@@ -323,7 +314,7 @@ phm2if_main (int argc, char* argv[])
   if (mpiWorld.getRank() == 0)
     v = imGlobal->getArray ();
 
-  if (phm->type == P_UNIT_PULSE) {
+  if (phm.getComposition() == P_UNIT_PULSE) {
     if (mpiWorld.getRank() == 0) {
       v[opt_nx/2][opt_ny/2] = 1.;
     }
@@ -332,26 +323,26 @@ phm2if_main (int argc, char* argv[])
       image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
     }
   } else {
-    TimerMPI timerRasterize (mpiWorld.getComm());
+    TimerCollectiveMPI 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());
+    TimerCollectiveMPI timerGather (mpiWorld.getComm());
     mpi_gather_image (mpiWorld, imGlobal, imLocal, opt_debug);
     if (opt_verbose)
       timerGather.timerEndAndReport ("Time to gather image");
   }
 #else
   v = imGlobal->getArray ();
-  if (phm->type == P_UNIT_PULSE) {
+  if (phm.getComposition() == P_UNIT_PULSE) {
     v[opt_nx/2][opt_ny/2] = 1.;
   } else if (opt_filter != -1) {
     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);
+       phm.show();
 #endif
       phm_to_imagefile (phm, *imGlobal, 0, opt_nx, opt_nsample, opt_trace);
   }
@@ -363,7 +354,7 @@ phm2if_main (int argc, char* argv[])
   {
     imGlobal->arrayDataWrite ();
     double calctime = timerProgram.timerEnd ();
-    imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
+    imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc.c_str(), calctime);
     imGlobal->fileClose ();
     if (opt_verbose)
       cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
@@ -380,8 +371,6 @@ phm2if_main (int argc, char* argv[])
     }
   }
 
-  phm_free (phm);
-
   return (0);
 }
 
@@ -416,6 +405,16 @@ void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLoc
 int 
 main (int argc, char* argv[])
 {
-  return (phm2if_main(argc, argv));
+  int retval = 1;
+
+  try {
+    retval = phm2if_main(argc, argv);
+  } catch (exception e) {
+    cerr << "Exception: " << e.what() << endl;
+  } catch (...) {
+    cerr << "Unknown exception" << endl;
+  }
+
+  return (retval);
 }
 #endif