r56: Improved option processing
[ctsim.git] / src / phm2rs.c
index 3ed5e97644a0a153ddc6ddb3b1e92d8d7bd5bad5..b92c9141de8fa9e5fe72aad21d37484667a26e70 100644 (file)
@@ -2,8 +2,30 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: phm2rs.c,v 1.5 2000/04/30 10:13:27 kevin Exp $
+** PURPOSE
+**   Generate raysum projections from phantom object
+**
+** HISTORY
+**   1984 -- Final DOS version  
+**   1999 -- First UNIX version
+**
+**  $Id: phm2rs.c,v 1.11 2000/05/16 04:33:59 kevin Exp $
 **  $Log: phm2rs.c,v $
+**  Revision 1.11  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.9  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.8  2000/05/04 18:16:34  kevin
+**  renamed filter definitions
+**
+**  Revision 1.7  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  Revision 1.6  2000/04/30 18:23:53  kevin
+**  Code cleaning
+**
 **  Revision 1.5  2000/04/30 10:13:27  kevin
 **  Fixed MPI bugs
 **
 **  along with this program; if not, write to the Free Software
 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/
-/* FILE
- *   phm2rs.c          Generate raysum projections from phantom object
- *
- * HISTORY
- *   1984 -- Final DOS version  
- *   1999 -- First UNIX version
- */
+
 
 #include "ct.h"
 
-#define O_PHANTOM    1
-#define O_DESC       2
-#define O_NRAY       3
-#define O_ROTANGLE   4
-#define O_TRACE      5
-#define O_VERBOSE    6
-#define O_HELP       7
-#define O_PICFILE    8
-#define O_DEBUG     10
-#define O_VERSION   11
-
-static struct option my_options[] = 
+enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
+       O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
+
+static struct option phm2rs_options[] = 
 {
   {"phantom", 1, 0, O_PHANTOM},
-  {"picfile", 1, 0, O_PICFILE},
+  {"phmfile", 1, 0, O_PHMFILE},
   {"desc", 1, 0, O_DESC},
   {"nray", 1, 0, O_NRAY},
   {"rotangle", 1, 0, O_ROTANGLE},
@@ -71,16 +79,17 @@ static struct option my_options[] =
   {0, 0, 0, 0}
 };
 
+
 void 
-usage (const char *program)
+phm2rs_usage (const char *program)
 {
 #ifdef MPI_CT
 if (mpi_ct.my_rank == 0)
   {
 #endif  
-  fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--picfile filename] [OPTIONS]\n", kbasename(program));
+  fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
   fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
-  fprintf(stdout,"a predefined --phantom or a --picfile\n");
+  fprintf(stdout,"a predefined --phantom or a --phmfile\n");
   fprintf(stdout,"\n");
   fprintf(stdout,"     outfile      Name of output file for raysums\n");
   fprintf(stdout,"     ndet         Number of detectors\n");
@@ -90,14 +99,14 @@ if (mpi_ct.my_rank == 0)
   fprintf(stdout,"        rowland   Rowland head phantom\n");
   fprintf(stdout,"        browland  Bordered Rowland head phantom\n");
   fprintf(stdout,"        unitpulse Unit pulse phantom\n");
-  fprintf(stdout,"     --picfile    Get Phantom from picture file\n");
+  fprintf(stdout,"     --phmfile    Get Phantom from phantom file\n");
   fprintf(stdout,"     --desc       Description of raysum\n");
   fprintf(stdout,"     --nray       Number of rays per detector (default = 1)\n");
   fprintf(stdout,"     --rotangle   Degrees to rotate view through, multiple of PI (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,"        pic       Trace picture\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");
@@ -109,33 +118,36 @@ if (mpi_ct.my_rank == 0)
   }
   MPI_Abort(mpi_ct.comm, 1);
 #endif
-  exit(1);
 }
 
-
+#ifdef MPI_CT
 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
+#endif
 
 int 
-main (const int argc, char *const argv[])
+phm2rs_main (const int argc, char *const argv[])
 {
   DETECTOR *det;
-  PICTURE *pic = NULL;
-  RAYSUM *rs_global;
-  char str[256], *opt_outfile, opt_desc[MAXREMARK+1];
-  char opt_picfilename[256];
+  PHANTOM *phm = NULL;
+  RAYSUM *rs_global = NULL;
+  char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1];
+  char opt_phmfilename[256];
   char *endptr, *endstr;
   int opt_ndet, opt_nview;
   int opt_nray = 1;
-  int opt_trace = 0, opt_picnum = -1;
+  int opt_trace = 0, opt_phmnum = -1;
   int opt_verbose = 0;
   int opt_debug = 0;
   double opt_rotangle = 1;
-  double time_start, time_end;
-#ifdef MPI_CT
+  double time_start = 0, time_end = 0;
+
+#ifndef MPI_CT
+  time_start = td_current_sec();
+#else
   RAYSUM *rs_local;
   int mpi_argc = argc;
   char **mpi_argv = (char **) argv;
-  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
+  double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
 
   MPI_Init(&mpi_argc, &mpi_argv);
   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
@@ -143,22 +155,19 @@ main (const int argc, char *const argv[])
   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
 
   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
-    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
-             mpi_ct.nproc, MPI_MAX_PROCESS);
+    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)", mpi_ct.nproc, MPI_MAX_PROCESS);
     exit(1);
   }
   time_start = MPI_Wtime();
-#else
-  time_start = td_current_sec();
 #endif
   
 #ifdef MPI_CT
   if (mpi_ct.my_rank == 0) {
 #endif
     strcpy(opt_desc, "");
-    strcpy(opt_picfilename, "");
+    strcpy(opt_phmfilename, "");
     while (1) {
-      int c = getopt_long(argc, argv, "", my_options, NULL);
+      int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
       char *endptr = NULL;
       char *endstr;
       
@@ -167,17 +176,20 @@ main (const int argc, char *const argv[])
       
       switch (c) {
       case O_PHANTOM:
-       opt_picnum = opt_set_picture(optarg, argv[0]);
-       pic = create_pic(opt_picnum);
+       if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       phm = phm_create (opt_phmnum);
        break;
-      case O_PICFILE:
+      case O_PHMFILE:
 #ifdef MPI_CT
        if (mpi_ct.my_rank == 0)
-         fprintf(stderr, "Can not read picture from file in MPI mode\n");
-       exit(1);
+         fprintf(stderr, "Can not read phantom from file in MPI mode\n");
+       return (1);
 #endif
-       strncpy(opt_picfilename, optarg, sizeof(opt_picfilename));
-       pic = create_pic_from_file(opt_picfilename);
+       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
+       phm = phm_create_from_file(opt_phmfilename);
        break;
       case O_VERBOSE:
        opt_verbose = 1;
@@ -187,7 +199,10 @@ main (const int argc, char *const argv[])
        break;
        break;
       case O_TRACE:
-       opt_trace = opt_set_trace(optarg, argv[0]);
+       if ((opt_trace = opt_set_trace(optarg)) < 0) {
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
        break;
       case O_DESC:
        strncpy(opt_desc, optarg, sizeof(opt_desc));
@@ -197,8 +212,8 @@ main (const int argc, char *const argv[])
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
          fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
-         usage(argv[0]);
-         exit(1);
+         phm2rs_usage(argv[0]);
+         return (1);
        }
        break;
       case O_NRAY:
@@ -206,8 +221,8 @@ main (const int argc, char *const argv[])
        endstr = optarg + strlen(optarg);
        if (endptr != endstr) {
          fprintf(stderr,"Error setting --nray to %s\n", optarg);
-         usage(argv[0]);
-         exit(1);
+         phm2rs_usage(argv[0]);
+         return (1);
        }
        break;
         case O_VERSION:
@@ -219,48 +234,49 @@ main (const int argc, char *const argv[])
          exit(0);
       case O_HELP:
       case '?':
-       usage(argv[0]);
-       exit(0);
+       phm2rs_usage(argv[0]);
+       return (0);
       default:
-       usage(argv[0]);
-       exit(1);
+       phm2rs_usage(argv[0]);
+       return (1);
       }
     }
   
-    if (pic == NULL) {
+    if (phm == NULL) {
       fprintf(stderr, "No phantom defined\n");
-      usage(argv[0]);
-      exit(1);
+      phm2rs_usage(argv[0]);
+      return (1);
     }
     if (optind + 3 != argc) {
-      usage(argv[0]);
-      exit(1);
+      phm2rs_usage(argv[0]);
+      return (1);
     }
+
     opt_outfile = argv[optind];
     opt_ndet = strtol(argv[optind+1], &endptr, 10);
     endstr = argv[optind+1] + strlen(argv[optind+1]);
     if (endptr != endstr) {
       fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]);
-      usage(argv[0]);
-      exit(1);
+      phm2rs_usage(argv[0]);
+      return (1);
     }
     opt_nview = strtol(argv[optind+2], &endptr, 10);
     endstr = argv[optind+2] + strlen(argv[optind+2]);
     if (endptr != endstr) {
       fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]);
-      usage(argv[0]);
-      exit(1);
+      phm2rs_usage(argv[0]);
+      return (1);
     }
 
     snprintf(str, sizeof(str), 
             "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ", 
             opt_ndet, opt_nview, opt_nray, opt_rotangle);
-    if (opt_picfilename[0]) {
+    if (opt_phmfilename[0]) {
       strncat(str, "Phantom=", sizeof(str));
-      strncat(str, opt_picfilename, sizeof(str));
-    } else if (opt_picnum != -1) {
+      strncat(str, opt_phmfilename, sizeof(str));
+    } else if (opt_phmnum != -1) {
       strncat(str, "Phantom=", sizeof(str));
-      strncat(str, name_of_picture(opt_picnum), sizeof(str));
+      strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
     }
     if (opt_desc[0]) {
       strncat(str, ": ", sizeof(str));
@@ -277,17 +293,17 @@ main (const int argc, char *const argv[])
   MPI_Bcast(&opt_nview, 1, MPI_INT, 0, mpi_ct.comm);
   MPI_Bcast(&opt_ndet, 1, MPI_INT, 0, mpi_ct.comm);
   MPI_Bcast(&opt_nray, 1, MPI_INT, 0, mpi_ct.comm);
-  MPI_Bcast(&opt_picnum, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
 
-  if (mpi_ct.my_rank > 0 && opt_picnum > 0)
-    pic = create_pic(opt_picnum);
+  if (mpi_ct.my_rank > 0 && opt_phmnum >= 0)
+    phm = phm_create (opt_phmnum);
 #endif
 
   opt_rotangle *= PI;
-  det = detect_create (pic, opt_ndet, opt_nview, opt_nray, opt_rotangle);
+  det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
 
 #ifdef MPI_CT
   mpi_ct_calc_work_units(opt_nview);
@@ -303,7 +319,7 @@ main (const int argc, char *const argv[])
 
   if (opt_verbose)
     mpi_t1 = MPI_Wtime();
-  raysum_collect (rs_local, det, pic, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
+  raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
   if (opt_verbose) {
     mpi_t2 = MPI_Wtime();
     mpi_t = mpi_t2 - mpi_t1;
@@ -324,51 +340,46 @@ main (const int argc, char *const argv[])
   }
 #else
   rs_global = raysum_create_from_det (opt_outfile, det);
-  raysum_collect (rs_global, det, pic, 0, opt_trace, FALSE);
+  raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
 #endif
   
-#ifdef MPI_CT
-  time_end = MPI_Wtime();
-  if (mpi_ct.my_rank == 0) {
-    rs_global->calctime = time_end - time_start;
-    strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
-    if (opt_verbose) {
-      fprintf(stdout, "Remark: %s\n", rs_global->remark);
-      fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
-    }
-  }
-#else
+#ifndef MPI_CT
   time_end = td_current_sec();
-  rs_global->calctime = time_end - time_start;
-  strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
-  if (opt_verbose) {
-    fprintf(stdout, "Remark: %s\n", rs_global->remark);
-    fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
-  }
+#else
+  time_end = MPI_Wtime();
+  if (mpi_ct.my_rank == 0) 
 #endif
+    {
+      rs_global->calctime = time_end - time_start;
+      strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
+      if (opt_verbose) {
+       fprintf(stdout, "Remark: %s\n", rs_global->remark);
+       fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
+      }
+    }
 
 #ifdef MPI_CT
-  if (mpi_ct.my_rank == 0) {
-    raysum_write (rs_global);
-    raysum_close (rs_global);
-  }
-#else  
-  raysum_write (rs_global);
-  raysum_close (rs_global);
+  if (mpi_ct.my_rank == 0) 
 #endif
-
-  detect_free (det);
-
-  if (opt_trace >= TRACE_PIC)
     {
-      crt_set_cpos (1, 1);
-      printf("Finished\n");
+      raysum_write (rs_global);
+      raysum_close (rs_global);
     }
 
+  phm_free (phm);
+  detector_free (det);
+
   return (0);
 }
 
 
+/* FUNCTION
+ *    mpi_gather_rs
+ *
+ * SYNOPSIS
+ *    Gather's raysums from all processes in rs_local in rs_global in process 0
+ */
+
 #ifdef MPI_CT
 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
 {
@@ -404,3 +415,13 @@ void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
 
 }
 #endif
+
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (phm2rs_main(argc, argv));
+}
+#endif
+