X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=src%2Fphm2if.cpp;h=75119c9642b2fab18ffe01fdb1f50c1cff78f4e1;hp=63d5a49b7a38ce2987cddd707fe0f78aab929c9f;hb=2d39e823ba389fc68e5317c422b55be006094252;hpb=4b16507e92bb80b09575b28bed66810e33d2681f diff --git a/src/phm2if.cpp b/src/phm2if.cpp index 63d5a49..75119c9 100644 --- a/src/phm2if.cpp +++ b/src/phm2if.cpp @@ -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.13 2000/06/22 10:17:28 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 @@ -25,15 +25,9 @@ ** 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,93 +51,85 @@ 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 << " bherman Bordered 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; - PHANTOM *phm = NULL; + ImageFile* imGlobal = NULL; + Phantom phm; 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; + string optPhmName; + string optFilterName; + string optDomainName = "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; - double time_start=0, time_end=0; + bool opt_verbose = false; #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) { int c = getopt_long(argc, argv, "", my_options, NULL); char *endptr = NULL; @@ -154,53 +140,49 @@ phm2sdf_main (int argc, char* argv[]) switch (c) { case O_PHANTOM: - if ((opt_phmnum = opt_set_phantom(optarg)) < 0) { - phm2sdf_usage(argv[0]); + optPhmName = optarg; + if (! phm.createFromPhantom (optPhmName.c_str())) { + cout << "Invalid phantom name " << optPhmName << endl << endl; + phm2if_usage(argv[0]); return (1); } 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) - 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; case O_VERBOSE: - opt_verbose = 1; + opt_verbose = true; break; case O_DEBUG: opt_debug = 1; 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]); - return (1); - } + optFilterName = optarg; break; case O_FILTER_DOMAIN: - if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) { - phm2sdf_usage (argv[0]); - return (1); - } + optDomainName = optarg; 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); - phm2sdf_usage(argv[0]); + sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg); + phm2if_usage(argv[0]); return (1); } break; @@ -208,8 +190,8 @@ phm2sdf_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); - phm2sdf_usage(argv[0]); + sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg); + phm2if_usage(argv[0]); return (1); } break; @@ -217,190 +199,154 @@ phm2sdf_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); - phm2sdf_usage(argv[0]); + sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg); + 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]); + if (phm.nPElem() == 0 && optPhmName == "" && optFilterName == "") { + 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]; 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]); + 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]); - phm2sdf_usage(argv[0]); + 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)); - } - 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)); + ostringstream oss; + oss << "nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", "; + if (opt_phmfilename.length()) + oss << "phantom=" << opt_phmfilename; + else if (optPhmName != "") + oss << "phantom=" << optPhmName; + else if (optFilterName != "") { + oss << "filter=" << optFilterName << " - " << optDomainName; } - 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) - 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(); + TimerCollectiveMPI timerBcast (mpiWorld.getComm()); + mpiWorld.BcastString (optPhmName); 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); mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0); mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0); mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0); - mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0); - mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0); - mpiWorld.getComm().Bcast (&opt_filter_domain, 1, MPI::INT, 0); 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); - } + mpiWorld.BcastString (optFilterName); + mpiWorld.BcastString (optDomainName); + + if (opt_verbose) + timerBcast.timerEndAndReport ("Time to broadcast variables"); mpiWorld.setTotalWorkUnits (opt_nx); + if (mpiWorld.getRank() > 0 && optPhmName != "") + phm.createFromPhantom (optPhmName.c_str()); + 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) - phm = phm_create (opt_phmnum); - -#ifdef HAVE_MPI - else { - if (mpiWorld.getRank() == 0) - fprintf(stderr, "phmnum < 0\n"); - 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 (phm.getComposition() == P_UNIT_PULSE) { if (mpiWorld.getRank() == 0) { v[opt_nx/2][opt_ny/2] = 1.; - calctime = 0; } - } else if (opt_filter != -1) { + } else if (optFilterName != "") { 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, optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param, opt_trace); } } else { - mpiWorld.getComm().Barrier(); + TimerCollectiveMPI timerRasterize (mpiWorld.getComm()); + phm.convertToImagefile (*imLocal, opt_nsample, opt_trace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits()); + if (opt_verbose) + timerRasterize.timerEndAndReport ("Time to rasterize phantom"); + TimerCollectiveMPI 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 (); - if (phm->type == P_UNIT_PULSE) { + v = imGlobal->getArray (); + if (phm.getComposition() == 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 (optFilterName != "") { + image_filter_response (*imGlobal, optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param, opt_trace); } else { #if HAVE_SGP if (opt_trace >= TRACE_PHM) - phm_show(phm); + phm.show(); #endif - phm_to_imagefile (phm, *im_global, 0, opt_nx, opt_nsample, opt_trace); + phm.convertToImagefile (*imGlobal, 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.c_str(), 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,35 +356,33 @@ 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); } } - phm_free (phm); - return (0); } #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 +394,16 @@ void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_l int main (int argc, char* argv[]) { - return (phm2sdf_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