+++ /dev/null
-/*****************************************************************************
-** FILE IDENTIFICATION
-**
-** Name: phm2if.cpp
-** Purpose: Convert an phantom object to an image file
-** Programmer: Kevin Rosenberg
-** Date Started: 1984
-**
-** This is part of the CTSim program
-** Copyright (C) 1983-2000 Kevin Rosenberg
-**
-** $Id: phm2if.cpp,v 1.16 2000/07/04 22:21:01 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
-** 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"
-#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 };
-
-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
-phm2if_usage (const char *program)
-{
- 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* imGlobal, ImageFile* imLocal, const int opt_debug);
-#endif
-
-int
-phm2if_main (int argc, char* argv[])
-{
- ImageFile* imGlobal = NULL;
- Phantom phm;
- int opt_nx = 0, opt_ny = 0;
- int opt_nsample = 1;
- string optPhmName = Phantom::PHM_HERMAN_STR;
- string optFilterName;
- string optDomainName = SignalFilter::DOMAIN_SPATIAL_STR;
- char *opt_outfile = NULL;
- int opt_debug = 0;
- string opt_desc;
- string opt_phmFileName;
- char *endstr, *endptr;
- double opt_filter_param = 1;
- double opt_filter_bw = 1.;
- int opt_trace = TRACE_NONE;
- bool opt_verbose = false;
-#ifdef HAVE_MPI
- ImageFile* imLocal = NULL;
- MPIWorld mpiWorld (argc, argv);
-#endif
-
- Timer timerProgram;
-
-#ifdef HAVE_MPI
- if (mpiWorld.getRank() == 0) {
-#endif
- 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:
- optPhmName = optarg;
- break;
- case O_PHMFILE:
- opt_phmFileName = optarg;
- break;
- case O_VERBOSE:
- opt_verbose = true;
- break;
- case O_DEBUG:
- opt_debug = 1;
- break;
- case O_TRACE:
- if ((opt_trace = convertTraceNameToID(optarg)) == TRACE_INVALID) {
- phm2if_usage(argv[0]);
- return (1);
- }
- break;
- case O_FILTER:
- optFilterName = optarg;
- break;
- case O_FILTER_DOMAIN:
- optDomainName = optarg;
- break;
- case O_DESC:
- opt_desc = optarg;
- break;
- case O_FILTER_BW:
- opt_filter_bw = strtod(optarg, &endptr);
- endstr = optarg + strlen(optarg);
- if (endptr != endstr) {
- sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
- phm2if_usage(argv[0]);
- return (1);
- }
- break;
- case O_FILTER_PARAM:
- opt_filter_param = strtod(optarg, &endptr);
- endstr = optarg + strlen(optarg);
- if (endptr != endstr) {
- sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
- phm2if_usage(argv[0]);
- return (1);
- }
- break;
- case O_NSAMPLE:
- opt_nsample = strtol(optarg, &endptr, 10);
- endstr = optarg + strlen(optarg);
- if (endptr != endstr) {
- sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
- phm2if_usage(argv[0]);
- return (1);
- }
- break;
- case O_VERSION:
-#ifdef VERSION
- cout << "Version " << VERSION << endl;
-#else
- cerr << "Unknown version number" << endl;
-#endif
- case O_HELP:
- case '?':
- phm2if_usage(argv[0]);
- return (0);
- default:
- phm2if_usage(argv[0]);
- return (1);
- }
- }
-
- if (optPhmName == "" && optFilterName == "" && opt_phmFileName == "") {
- cerr << "No phantom defined" << endl << endl;
- phm2if_usage(argv[0]);
- return (1);
- }
-
- if (optind + 3 != argc) {
- 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) {
- 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) {
- sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
- phm2if_usage(argv[0]);
- return (1);
- }
-
- ostringstream oss;
- oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
- if (opt_phmFileName != "")
- oss << "phantom=" << opt_phmFileName;
- else if (optPhmName != "")
- oss << "phantom=" << optPhmName;
- else if (optFilterName != "") {
- oss << "filter=" << optFilterName << " - " << optDomainName;
- }
- if (opt_desc != "")
- oss << ": " << opt_desc;
- opt_desc = oss.str();
-
- if (optPhmName != "") {
- phm.createFromPhantom (optPhmName.c_str());
- if (phm.fail()) {
- cout << phm.failMessage() << endl << endl;
- phm2if_usage(argv[0]);
- return (1);
- }
- }
-
- if (opt_phmFileName != "") {
- phm.createFromFile(opt_phmFileName.c_str());
-#ifdef HAVE_MPI
- if (mpiWorld.getRank() == 0)
- cerr << "Can't use phantom from file in MPI mode" << endl;
- return (1);
-#endif
- }
-
- if (opt_verbose)
- cout << "Rasterize Phantom to Image" << endl << endl;
-#ifdef HAVE_MPI
- }
-#endif
-
-#ifdef HAVE_MPI
- 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_filter_param, 1, MPI::DOUBLE, 0);
- mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
-
- 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) {
- imGlobal = new ImageFile (opt_nx, opt_ny);
- }
- imLocal = new ImageFile (opt_nx, opt_ny);
-#else
- imGlobal = new ImageFile (opt_nx, opt_ny);
-#endif
-
- ImageFileArray v;
-#ifdef HAVE_MPI
- if (mpiWorld.getRank() == 0)
- v = imGlobal->getArray ();
-
- if (phm.getComposition() == P_UNIT_PULSE) {
- if (mpiWorld.getRank() == 0) {
- v[opt_nx/2][opt_ny/2] = 1.;
- }
- } else if (optFilterName != "") {
- if (mpiWorld.getRank() == 0) {
- imGlobal->filterResponse (optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param);
- }
- } else {
- 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)
- timerGather.timerEndAndReport ("Time to gather image");
- }
-#else
- v = imGlobal->getArray ();
- if (phm.getComposition() == P_UNIT_PULSE) {
- v[opt_nx/2][opt_ny/2] = 1.;
- } else if (optFilterName != "") {
- imGlobal->filterResponse (optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param);
- } else {
-#if HAVE_SGP
- if (opt_trace >= TRACE_PHM)
- phm.show();
-#endif
- phm.convertToImagefile (*imGlobal, opt_nsample, opt_trace);
- }
-#endif
-
-#ifdef HAVE_MPI
- if (mpiWorld.getRank() == 0)
-#endif
- {
- double calctime = timerProgram.timerEnd ();
- imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc.c_str(), calctime);
- imGlobal->fileWrite (opt_outfile);
- if (opt_verbose)
- cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
-
- 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);
- imGlobal->displayScaling (nscale, dmin, dmax);
- }
- }
-
- return (0);
-}
-
-
-
-#ifdef HAVE_MPI
-void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug)
-{
- ImageFileArray vLocal = imLocal->getArray();
- ImageFileArray vGlobal = NULL;
- int nyLocal = imLocal->ny();
-
- if (mpiWorld.getRank() == 0)
- vGlobal = imGlobal->getArray();
-
- for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
- 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, imLocal->getMPIDataType(), iProc, 0, status);
- }
- }
- }
-
-}
-#endif
-
-#ifndef NO_MAIN
-int
-main (int argc, char* 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