1 /*****************************************************************************
5 ** Purpose: Convert an phantom object to an image file
6 ** Programmer: Kevin Rosenberg
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: phm2if.cpp,v 1.15 2000/06/26 21:15:24 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
32 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
33 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
35 static struct option my_options[] =
37 {"phantom", 1, 0, O_PHANTOM},
38 {"phmfile", 1, 0, O_PHMFILE},
39 {"desc", 1, 0, O_DESC},
40 {"nsample", 1, 0, O_NSAMPLE},
41 {"filter", 1, 0, O_FILTER},
42 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
43 {"filter-bw", 1, 0, O_FILTER_BW},
44 {"filter-param", 1, 0, O_FILTER_PARAM},
45 {"trace", 1, 0, O_TRACE},
46 {"verbose", 0, 0, O_VERBOSE},
47 {"debug", 0, 0, O_DEBUG},
48 {"help", 0, 0, O_HELP},
49 {"version", 0, 0, O_VERSION},
54 phm2if_usage (const char *program)
56 cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]" << endl;
57 cout << "Generate phantom image from a predefined --phantom or a --phmfile" << endl;
59 cout << " outfile Name of output file for image" << endl;
60 cout << " nx Number of pixels X-axis" << endl;
61 cout << " ny Number of pixels Y-axis" << endl;
62 cout << " --phantom Phantom to use for projection" << endl;
63 cout << " herman Herman head phantom" << endl;
64 cout << " bherman Bordered Herman head phantom" << endl;
65 cout << " rowland Rowland head phantom" << endl;
66 cout << " browland Bordered Rowland head phantom" << endl;
67 cout << " unitpulse Unit pulse phantom" << endl;
68 cout << " --phmfile Generate Phantom from phantom file" << endl;
69 cout << " --filter Generate Phantom from a filter function" << endl;
70 cout << " abs_bandlimit Abs * Bandlimiting" << endl;
71 cout << " abs_sinc Abs * Sinc" << endl;
72 cout << " abs_cos Abs * Cosine" << endl;
73 cout << " abs_hamming Abs * Hamming" << endl;
74 cout << " shepp Shepp-Logan" << endl;
75 cout << " bandlimit Bandlimiting" << endl;
76 cout << " sinc Sinc" << endl;
77 cout << " cos Cosine" << endl;
78 cout << " triangle Triangle" << endl;
79 cout << " hamming Hamming" << endl;
80 cout << " --filter-param Alpha level for Hamming filter" << endl;
81 cout << " --filter-domain Set domain of filter" << endl;
82 cout << " spatial Spatial domain (default)" << endl;
83 cout << " freq Frequency domain" << endl;
84 cout << " --filter-bw Filter bandwidth (default = 1)" << endl;
85 cout << " --desc Description of raysum" << endl;
86 cout << " --nsample Number of samples per axis per pixel (default = 1)" << endl;
87 cout << " --trace Trace level to use" << endl;
88 cout << " none No tracing (default)" << endl;
89 cout << " text Trace text level" << endl;
90 cout << " phm Trace phantom" << endl;
91 cout << " rays Trace rays" << endl;
92 cout << " plot Trace plot" << endl;
93 cout << " clipping Trace clipping" << endl;
94 cout << " --debug Debug mode" << endl;
95 cout << " --verbose Verbose mode" << endl;
96 cout << " --version Print version" << endl;
97 cout << " --help Print this help message" << endl;
101 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug);
105 phm2if_main (int argc, char* argv[])
107 ImageFile* imGlobal = NULL;
109 int opt_nx = 0, opt_ny = 0;
111 string optPhmName = Phantom::PHM_HERMAN_STR;
112 string optFilterName;
113 string optDomainName = SignalFilter::DOMAIN_SPATIAL_STR;
114 char *opt_outfile = NULL;
117 string opt_phmFileName;
118 char *endstr, *endptr;
119 double opt_filter_param = 1;
120 double opt_filter_bw = 1.;
121 int opt_trace = TRACE_NONE;
122 bool opt_verbose = false;
124 ImageFile* imLocal = NULL;
125 MPIWorld mpiWorld (argc, argv);
131 if (mpiWorld.getRank() == 0) {
134 int c = getopt_long(argc, argv, "", my_options, NULL);
146 opt_phmFileName = optarg;
155 if ((opt_trace = opt_set_trace(optarg)) < 0) {
156 phm2if_usage(argv[0]);
161 optFilterName = optarg;
163 case O_FILTER_DOMAIN:
164 optDomainName = optarg;
170 opt_filter_bw = strtod(optarg, &endptr);
171 endstr = optarg + strlen(optarg);
172 if (endptr != endstr) {
173 sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
174 phm2if_usage(argv[0]);
179 opt_filter_param = strtod(optarg, &endptr);
180 endstr = optarg + strlen(optarg);
181 if (endptr != endstr) {
182 sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
183 phm2if_usage(argv[0]);
188 opt_nsample = strtol(optarg, &endptr, 10);
189 endstr = optarg + strlen(optarg);
190 if (endptr != endstr) {
191 sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
192 phm2if_usage(argv[0]);
198 cout << "Version " << VERSION << endl;
200 cerr << "Unknown version number" << endl;
204 phm2if_usage(argv[0]);
207 phm2if_usage(argv[0]);
212 if (optPhmName == "" && optFilterName == "" && opt_phmFileName == "") {
213 cerr << "No phantom defined" << endl << endl;
214 phm2if_usage(argv[0]);
218 if (optind + 3 != argc) {
219 phm2if_usage(argv[0]);
222 opt_outfile = argv[optind];
223 opt_nx = strtol(argv[optind+1], &endptr, 10);
224 endstr = argv[optind+1] + strlen(argv[optind+1]);
225 if (endptr != endstr) {
226 sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
227 phm2if_usage(argv[0]);
230 opt_ny = strtol(argv[optind+2], &endptr, 10);
231 endstr = argv[optind+2] + strlen(argv[optind+2]);
232 if (endptr != endstr) {
233 sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
234 phm2if_usage(argv[0]);
239 oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
240 if (opt_phmFileName != "")
241 oss << "phantom=" << opt_phmFileName;
242 else if (optPhmName != "")
243 oss << "phantom=" << optPhmName;
244 else if (optFilterName != "") {
245 oss << "filter=" << optFilterName << " - " << optDomainName;
248 oss << ": " << opt_desc;
249 opt_desc = oss.str();
251 if (optPhmName != "") {
252 phm.createFromPhantom (optPhmName.c_str());
254 cout << phm.failMessage() << endl << endl;
255 phm2if_usage(argv[0]);
260 if (opt_phmFileName != "") {
261 phm.createFromFile(opt_phmFileName.c_str());
263 if (mpiWorld.getRank() == 0)
264 cerr << "Can't use phantom from file in MPI mode" << endl;
270 cout << "Rasterize Phantom to Image" << endl << endl;
276 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
277 mpiWorld.BcastString (optPhmName);
278 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
284 mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
285 mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
287 mpiWorld.BcastString (optFilterName);
288 mpiWorld.BcastString (optDomainName);
291 timerBcast.timerEndAndReport ("Time to broadcast variables");
293 mpiWorld.setTotalWorkUnits (opt_nx);
295 if (mpiWorld.getRank() > 0 && optPhmName != "")
296 phm.createFromPhantom (optPhmName.c_str());
298 if (mpiWorld.getRank() == 0) {
299 imGlobal = new ImageFile (opt_nx, opt_ny);
301 imLocal = new ImageFile (opt_nx, opt_ny);
303 imGlobal = new ImageFile (opt_nx, opt_ny);
308 if (mpiWorld.getRank() == 0)
309 v = imGlobal->getArray ();
311 if (phm.getComposition() == P_UNIT_PULSE) {
312 if (mpiWorld.getRank() == 0) {
313 v[opt_nx/2][opt_ny/2] = 1.;
315 } else if (optFilterName != "") {
316 if (mpiWorld.getRank() == 0) {
317 imGlobal->filterResponse (optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param);
320 TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
321 phm.convertToImagefile (*imLocal, opt_nsample, opt_trace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits());
323 timerRasterize.timerEndAndReport ("Time to rasterize phantom");
325 TimerCollectiveMPI timerGather (mpiWorld.getComm());
326 mpi_gather_image (mpiWorld, imGlobal, imLocal, opt_debug);
328 timerGather.timerEndAndReport ("Time to gather image");
331 v = imGlobal->getArray ();
332 if (phm.getComposition() == P_UNIT_PULSE) {
333 v[opt_nx/2][opt_ny/2] = 1.;
334 } else if (optFilterName != "") {
335 imGlobal->filterResponse (optDomainName.c_str(), opt_filter_bw, optFilterName.c_str(), opt_filter_param);
338 if (opt_trace >= TRACE_PHM)
341 phm.convertToImagefile (*imGlobal, opt_nsample, opt_trace);
346 if (mpiWorld.getRank() == 0)
349 double calctime = timerProgram.timerEnd ();
350 imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc.c_str(), calctime);
351 imGlobal->fileWrite (opt_outfile);
353 cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
355 if (opt_trace >= TRACE_PHM) {
359 printf ("Enter display size scale (nominal = 1): ");
360 scanf ("%d", &nscale);
361 printf ("Enter minimum and maximum densities (min, max): ");
362 scanf ("%lf %lf", &dmin, &dmax);
363 imGlobal->displayScaling (nscale, dmin, dmax);
373 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug)
375 ImageFileArray vLocal = imLocal->getArray();
376 ImageFileArray vGlobal = NULL;
377 int nyLocal = imLocal->ny();
379 if (mpiWorld.getRank() == 0)
380 vGlobal = imGlobal->getArray();
382 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
383 mpiWorld.getComm().Send(vLocal[iw], nyLocal, imLocal->getMPIDataType(), 0, 0);
385 if (mpiWorld.getRank() == 0) {
386 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
387 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
389 mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, imLocal->getMPIDataType(), iProc, 0, status);
399 main (int argc, char* argv[])
404 retval = phm2if_main(argc, argv);
405 } catch (exception e) {
406 cerr << "Exception: " << e.what() << endl;
408 cerr << "Unknown exception" << endl;