r72: Initial C++ versions
[ctsim.git] / src / phm2rs.cpp
diff --git a/src/phm2rs.cpp b/src/phm2rs.cpp
new file mode 100644 (file)
index 0000000..1e15883
--- /dev/null
@@ -0,0 +1,433 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** PURPOSE
+**   Generate raysum projections from phantom object
+**
+** HISTORY
+**   1984 -- Final DOS version  
+**   1999 -- First UNIX version
+**
+**  $Id: phm2rs.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: phm2rs.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.12  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  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
+**
+**  Revision 1.4  2000/04/30 04:06:13  kevin
+**  Update Raysum i/o routines
+**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
+**
+**  Revision 1.3  2000/04/29 23:24:56  kevin
+**  *** empty log message ***
+**
+**  Revision 1.2  2000/04/28 13:50:45  kevin
+**  Removed Makefile Makefile.in that are automatically generated by autoconf
+**
+**  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
+**  Initial CVS import for first public release
+**
+**
+**
+**  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
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+******************************************************************************/
+
+
+#include "ct.h"
+
+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},
+  {"phmfile", 1, 0, O_PHMFILE},
+  {"desc", 1, 0, O_DESC},
+  {"nray", 1, 0, O_NRAY},
+  {"rotangle", 1, 0, O_ROTANGLE},
+  {"trace", 1, 0, O_TRACE},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"debug", 0, 0, O_DEBUG},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+
+void 
+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] [--phmfile filename] [OPTIONS]\n", kbasename(program));
+  fprintf(stdout,"Calculate raysums (projections) through phantom object, either\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");
+  fprintf(stdout,"     nview        Number of rotated views\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    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,"        phm       Trace phantom image\n");
+  fprintf(stdout,"        rays      Trace rays\n");
+  fprintf(stdout,"        plot      Trace plot\n");
+  fprintf(stdout,"        clipping  Trace clipping\n");
+  fprintf(stdout,"     --verbose    Verbose mode\n");
+  fprintf(stdout,"     --debug      Debug mode\n");
+  fprintf(stdout,"     --version    Print version\n");
+  fprintf(stdout,"     --help       Print this help message\n");
+#ifdef MPI_CT
+  }
+  MPI_Abort(mpi_ct.comm, 1);
+#endif
+}
+
+#ifdef MPI_CT
+void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
+#endif
+
+int 
+phm2rs_main (const int argc, char *const argv[])
+{
+  DETECTOR *det;
+  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_phmnum = -1;
+  int opt_verbose = 0;
+  int opt_debug = 0;
+  double opt_rotangle = 1;
+  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 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
+
+  MPI_Init(&mpi_argc, &mpi_argv);
+  MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
+  MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
+  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);
+    exit(1);
+  }
+  time_start = MPI_Wtime();
+#endif
+  
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+#endif
+    strcpy(opt_desc, "");
+    strcpy(opt_phmfilename, "");
+    while (1) {
+      int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
+      char *endptr = NULL;
+      char *endstr;
+      
+      if (c == -1)
+       break;
+      
+      switch (c) {
+      case O_PHANTOM:
+       if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       phm = phm_create (opt_phmnum);
+       break;
+      case O_PHMFILE:
+#ifdef MPI_CT
+       if (mpi_ct.my_rank == 0)
+         fprintf(stderr, "Can not read phantom from file in MPI mode\n");
+       return (1);
+#endif
+       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
+       phm = phm_create_from_file(opt_phmfilename);
+       break;
+      case O_VERBOSE:
+       opt_verbose = 1;
+       break;
+      case O_DEBUG:
+       opt_debug = 1;
+       break;
+       break;
+      case O_TRACE:
+       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));
+       break;
+      case O_ROTANGLE:
+       opt_rotangle = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_NRAY:
+       opt_nray = strtol(optarg, &endptr, 10);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --nray to %s\n", optarg);
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       break;
+        case O_VERSION:
+#ifdef VERSION
+         fprintf(stdout, "Version %s\n", VERSION);
+#else
+         fprintf(stderr, "Unknown version number");
+#endif
+         exit(0);
+      case O_HELP:
+      case '?':
+       phm2rs_usage(argv[0]);
+       return (0);
+      default:
+       phm2rs_usage(argv[0]);
+       return (1);
+      }
+    }
+  
+    if (phm == NULL) {
+      fprintf(stderr, "No phantom defined\n");
+      phm2rs_usage(argv[0]);
+      return (1);
+    }
+    if (optind + 3 != argc) {
+      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]);
+      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]);
+      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_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));
+    }
+    if (opt_desc[0]) {
+      strncat(str, ": ", sizeof(str));
+      strncat(str, opt_desc, sizeof(str));
+    }
+    strncpy(opt_desc, str, sizeof(opt_desc));
+#ifdef MPI_CT
+  }
+#endif
+
+#ifdef MPI_CT
+  MPI_Barrier(mpi_ct.comm);
+  MPI_Bcast(&opt_rotangle, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  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_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_phmnum >= 0)
+    phm = phm_create (opt_phmnum);
+#endif
+
+  opt_rotangle *= PI;
+  det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
+
+#ifdef MPI_CT
+  mpi_ct_calc_work_units(opt_nview);
+  if (mpi_ct.my_rank == 0) {
+    rs_global = raysum_create_from_det (opt_outfile, det);
+    raysum_alloc_views(rs_global);
+  }
+  
+  rs_local = raysum_create_from_det (NULL, det);
+  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
+  if (opt_debug)
+    printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
+
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+  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;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
+  }
+
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+  mpi_gather_rs(rs_global, rs_local, opt_debug);
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
+  }
+#else
+  rs_global = raysum_create_from_det (opt_outfile, det);
+  raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
+#endif
+  
+#ifndef MPI_CT
+  time_end = td_current_sec();
+#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) 
+#endif
+    {
+      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)
+{
+  int iproc;
+  int end_work_unit;
+  int iw;
+
+  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
+  for (iw = 0; iw <= end_work_unit; iw++) {
+    MPI_Send(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm);
+    MPI_Send(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm);
+    MPI_Send(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm);
+  }
+
+  if (mpi_ct.my_rank == 0) {
+    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
+      end_work_unit = mpi_ct.start_work_unit[iproc] 
+       + mpi_ct.local_work_units[iproc] 
+       - 1;
+
+      if (opt_debug)
+       fprintf(stdout, "Recv rs data from process %d\n", iproc);
+
+      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+       MPI_Status status;
+
+       MPI_Recv(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm, &status);
+       MPI_Recv(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm, &status);
+       MPI_Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
+      }
+    }
+  }
+
+}
+#endif
+
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (phm2rs_main(argc, argv));
+}
+#endif
+