X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=src%2Fphm2if.cpp;fp=src%2Fphm2if.cpp;h=9d32f1ea069d6d55faad56a955ca130526d1ce4b;hp=0000000000000000000000000000000000000000;hb=f173363bba9997045e5ec825e64d6253ec4da235;hpb=98f0fd3cec5ca685966017168efca4f14ebbf0b9 diff --git a/src/phm2if.cpp b/src/phm2if.cpp new file mode 100644 index 0000000..9d32f1e --- /dev/null +++ b/src/phm2if.cpp @@ -0,0 +1,503 @@ +/***************************************************************************** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: phm2if.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $ +** $Log: phm2if.cpp,v $ +** Revision 1.1 2000/06/07 02:29:05 kevin +** Initial C++ versions +** +** Revision 1.10 2000/05/24 22:50:04 kevin +** Added support for new SGP library +** +** Revision 1.9 2000/05/16 04:33:59 kevin +** Improved option processing +** +** Revision 1.8 2000/05/11 01:06:30 kevin +** Changed sprintf to snprintf +** +** Revision 1.7 2000/05/08 20:02:32 kevin +** ANSI C changes +** +** Revision 1.6 2000/05/03 08:49:50 kevin +** Code cleanup +** +** Revision 1.5 2000/04/30 19:17:54 kevin +** *** empty log message *** +** +** 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/28 18:19:01 kevin +** removed unused files +** +** 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 +******************************************************************************/ + +/* FILE + * phm2sdf.c Generate a SDF image from a phantom + * + * HISTORY + * 1984 - Final DOS verion + * 1999 - First UNIX version + */ + +#include "ct.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 }; + +static struct option my_options[] = +{ + {"phantom", 1, 0, O_PHANTOM}, + {"phmfile", 1, 0, O_PHMFILE}, + {"desc", 1, 0, O_DESC}, + {"nsample", 1, 0, O_NSAMPLE}, + {"filter", 1, 0, O_FILTER}, + {"filter-domain", 1, 0, O_FILTER_DOMAIN}, + {"filter-bw", 1, 0, O_FILTER_BW}, + {"filter-param", 1, 0, O_FILTER_PARAM}, + {"trace", 1, 0, O_TRACE}, + {"verbose", 0, 0, O_VERBOSE}, + {"debug", 0, 0, O_DEBUG}, + {"help", 0, 0, O_HELP}, + {"version", 0, 0, O_VERSION}, + {0, 0, 0, 0} +}; + +void +phm2sdf_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"); +} + +#ifdef MPI_CT +void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug); +#endif + +int +phm2sdf_main (const int argc, char *const argv[]) +{ + ImageFile *im_global = NULL; + PHANTOM *phm = NULL; + int opt_nx = 0, opt_ny = 0; + int opt_nsample = 1; + int opt_phmnum = -1; + FilterType opt_filter = static_cast(-1); + DomainType opt_filter_domain = D_SPATIAL; + char *opt_outfile = NULL; + int opt_debug = 0; + char str[256]; + char opt_desc[256], opt_phmfilename[256]; + char *endstr, *endptr; + double opt_filter_param = 1; + double opt_filter_bw = 1.; + int opt_trace = TRACE_NONE; + int opt_verbose = 0; + double time_start=0, time_end=0; +#ifdef MPI_CT + ImageFile *im_local = NULL; + int mpi_argc = argc; + char **mpi_argv = (char **) argv; + double mpi_t1, mpi_t2, 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); + } +#endif + +#ifdef MPI_CT + 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_phmfilename, ""); + while (1) { + int c = getopt_long(argc, argv, "", my_options, NULL); + char *endptr = NULL; + char *endstr; + + if (c == -1) + break; + + switch (c) { + case O_PHANTOM: + if ((opt_phmnum = opt_set_phantom(optarg)) < 0) { + phm2sdf_usage(argv[0]); + return (1); + } + break; + case O_PHMFILE: + strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename)); + phm = phm_create_from_file(opt_phmfilename); +#ifdef MPI_CT + if (mpi_ct.my_rank == 0) + fprintf(stderr, "Can't use phantom from file in MPI mode\n"); + return (1); +#endif + break; + case O_VERBOSE: + opt_verbose = 1; + break; + case O_DEBUG: + opt_debug = 1; + break; + case O_TRACE: + if ((opt_trace = opt_set_trace(optarg)) < 0) { + phm2sdf_usage(argv[0]); + return (1); + } + break; + case O_FILTER: + if ((opt_filter = opt_set_filter(optarg)) < 0) { + phm2sdf_usage (argv[0]); + return (1); + } + break; + case O_FILTER_DOMAIN: + if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) { + phm2sdf_usage (argv[0]); + return (1); + } + break; + case O_DESC: + strncpy(opt_desc, optarg, sizeof(opt_desc)); + 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); + phm2sdf_usage(argv[0]); + return (1); + } + break; + case O_FILTER_PARAM: + opt_filter_param = strtod(optarg, &endptr); + endstr = optarg + strlen(optarg); + if (endptr != endstr) { + fprintf(stderr,"Error setting --filter-param to %s\n", optarg); + phm2sdf_usage(argv[0]); + return (1); + } + break; + case O_NSAMPLE: + opt_nsample = strtol(optarg, &endptr, 10); + endstr = optarg + strlen(optarg); + if (endptr != endstr) { + fprintf(stderr,"Error setting --nsample to %s\n", optarg); + phm2sdf_usage(argv[0]); + return (1); + } + break; + case O_VERSION: +#ifdef VERSION + fprintf(stdout, "Version %s\n", VERSION); +#else + fprintf(stderr, "Unknown version number"); +#endif + case O_HELP: + case '?': + phm2sdf_usage(argv[0]); + return (0); + default: + phm2sdf_usage(argv[0]); + return (1); + } + } + + if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) { + fprintf(stderr, "No phantom defined\n"); + phm2sdf_usage(argv[0]); + return (1); + } + + if (optind + 3 != argc) { + phm2sdf_usage(argv[0]); + return (1); + } + opt_outfile = argv[optind]; + 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]); + phm2sdf_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]); + 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)); + } + 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)); + } + if (opt_desc[0]) { + strncat(str, ": ", sizeof(str)); + strncat(str, opt_desc, sizeof(str)); + } + strncpy(opt_desc, str, sizeof(opt_desc)); + + if (opt_verbose) + printf("Rasterize Phantom to Image\n\n"); +#ifdef MPI_CT + } +#endif + +#ifdef MPI_CT + mpi_t1 = MPI_Wtime(); + 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); + MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm); + MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm); + MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm); + + 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 Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g); + } + + mpi_ct_calc_work_units(opt_nx); + + if (mpi_ct.my_rank == 0) { + im_global = new ImageFile (opt_outfile, opt_nx, opt_ny); + im_global->adf.fileCreate(); + } + + im_local = new ImageFile (opt_nx, opt_ny); +#else + im_global = new ImageFile (opt_outfile, opt_nx, opt_ny); + im_global->adf.fileCreate (); +#endif + + if (opt_phmnum >= 0) + phm = phm_create (opt_phmnum); + +#ifdef MPI_CT + else { + if (mpi_ct.my_rank == 0) + fprintf(stderr, "phmnum < 0\n"); + exit(1); + } +#endif + + ImageFileArray v = im_global->adf.getArray (); + double calctime = 0; + +#ifdef MPI_CT + if (phm->type == P_UNIT_PULSE) { + if (mpi_ct.my_rank == 0) { + v[opt_nx/2][opt_ny/2] = 1.; + calctime = 0; + } + } else if (opt_filter != -1) { + if (mpi_ct.my_rank == 0) { + image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace); + calctime = 0; + } + } else { + if (opt_verbose) + mpi_t1 = MPI_Wtime(); + phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank], + mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace); + 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 compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g); + } + mpi_gather_image(im_global, im_local, opt_debug); + } +#else + 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; + } else { +#if HAVE_SGP + if (opt_trace >= TRACE_PHM) + phm_show(phm); +#endif + // phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace); + } +#endif + +#ifdef MPI_CT + time_end = MPI_Wtime(); +#else + time_end = td_current_sec(); +#endif + +#ifdef MPI_CT + if (mpi_ct.my_rank == 0) +#endif + { + im_global->adf.arrayDataWrite (); + calctime = time_end - time_start; + im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime); + im_global->adf.fileClose (); + if (opt_verbose) + fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime); + } + + if (opt_trace >= TRACE_PHM) + { + double dmin, dmax; + int nscale; + + printf ("Enter display size scale (nominal = 1): "); + scanf ("%d", &nscale); + printf ("Enter minimum and maximum densities (min, max): "); + scanf ("%lf %lf", &dmin, &dmax); + // image_display_scale (im_global, nscale, dmin, dmax); + } + + phm_free (phm); + + return (0); +} + + + +#ifdef MPI_CT +void mpi_gather_image (ImageFile *im_global, ImageFile *im_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(im_local->v[iw], im_local->ny, im_local->getMPIDataType(), 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 (%d-%d)\n", iproc, + mpi_ct.start_work_unit[iproc], end_work_unit); + fflush(stdout); + } + + + for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) { + MPI_Status status; + + MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status); + } + } + } + +} +#endif + +#ifndef NO_MAIN +int +main (const int argc, char *const argv[]) +{ + return (phm2sdf_main(argc, argv)); +} +#endif