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.11 2000/06/19 17:58:13 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 << " rowland Rowland head phantom" << endl;
65 cout << " browland Bordered Rowland head phantom" << endl;
66 cout << " unitpulse Unit pulse phantom" << endl;
67 cout << " --phmfile Generate Phantom from phantom file" << endl;
68 cout << " --filter Generate Phantom from a filter function" << endl;
69 cout << " abs_bandlimit Abs * Bandlimiting" << endl;
70 cout << " abs_sinc Abs * Sinc" << endl;
71 cout << " abs_cos Abs * Cosine" << endl;
72 cout << " abs_hamming Abs * Hamming" << endl;
73 cout << " shepp Shepp-Logan" << endl;
74 cout << " bandlimit Bandlimiting" << endl;
75 cout << " sinc Sinc" << endl;
76 cout << " cos Cosine" << endl;
77 cout << " triangle Triangle" << endl;
78 cout << " hamming Hamming" << endl;
79 cout << " --filter-param Alpha level for Hamming filter" << endl;
80 cout << " --filter-domain Set domain of filter" << endl;
81 cout << " spatial Spatial domain (default)" << endl;
82 cout << " freq Frequency domain" << endl;
83 cout << " --filter-bw Filter bandwidth (default = 1)" << endl;
84 cout << " --desc Description of raysum" << endl;
85 cout << " --nsample Number of samples per axis per pixel (default = 1)" << endl;
86 cout << " --trace Trace level to use" << endl;
87 cout << " none No tracing (default)" << endl;
88 cout << " text Trace text level" << endl;
89 cout << " phm Trace phantom" << endl;
90 cout << " rays Trace rays" << endl;
91 cout << " plot Trace plot" << endl;
92 cout << " clipping Trace clipping" << endl;
93 cout << " --debug Debug mode" << endl;
94 cout << " --verbose Verbose mode" << endl;
95 cout << " --version Print version" << endl;
96 cout << " --help Print this help message" << endl;
100 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug);
104 phm2if_main (int argc, char* argv[])
106 ImageFile* imGlobal = NULL;
108 int opt_nx = 0, opt_ny = 0;
111 FilterType opt_filter = static_cast<FilterType>(-1);
112 DomainType opt_filter_domain = D_SPATIAL;
113 char *opt_outfile = NULL;
116 string opt_phmfilename;
117 char *endstr, *endptr;
118 double opt_filter_param = 1;
119 double opt_filter_bw = 1.;
120 int opt_trace = TRACE_NONE;
121 bool opt_verbose = false;
123 ImageFile* imLocal = NULL;
124 MPIWorld mpiWorld (argc, argv);
130 if (mpiWorld.getRank() == 0) {
133 int c = getopt_long(argc, argv, "", my_options, NULL);
142 if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
143 phm2if_usage(argv[0]);
148 opt_phmfilename = optarg;
149 phm.createFromFile(opt_phmfilename.c_str());
151 if (mpiWorld.getRank() == 0)
152 cerr << "Can't use phantom from file in MPI mode" << endl;
163 if ((opt_trace = opt_set_trace(optarg)) < 0) {
164 phm2if_usage(argv[0]);
169 if ((opt_filter = opt_set_filter(optarg)) < 0) {
170 phm2if_usage (argv[0]);
174 case O_FILTER_DOMAIN:
175 if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
176 phm2if_usage (argv[0]);
184 opt_filter_bw = strtod(optarg, &endptr);
185 endstr = optarg + strlen(optarg);
186 if (endptr != endstr) {
187 sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
188 phm2if_usage(argv[0]);
193 opt_filter_param = strtod(optarg, &endptr);
194 endstr = optarg + strlen(optarg);
195 if (endptr != endstr) {
196 sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
197 phm2if_usage(argv[0]);
202 opt_nsample = strtol(optarg, &endptr, 10);
203 endstr = optarg + strlen(optarg);
204 if (endptr != endstr) {
205 sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
206 phm2if_usage(argv[0]);
212 cout << "Version " << VERSION << endl;
214 cerr << "Unknown version number" << endl;
218 phm2if_usage(argv[0]);
221 phm2if_usage(argv[0]);
226 if (phm.nPElem() == 0 && opt_phmnum == -1 && opt_filter == -1) {
227 cerr << "No phantom defined" << endl;
228 phm2if_usage(argv[0]);
232 if (optind + 3 != argc) {
233 phm2if_usage(argv[0]);
236 opt_outfile = argv[optind];
237 opt_nx = strtol(argv[optind+1], &endptr, 10);
238 endstr = argv[optind+1] + strlen(argv[optind+1]);
239 if (endptr != endstr) {
240 sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
241 phm2if_usage(argv[0]);
244 opt_ny = strtol(argv[optind+2], &endptr, 10);
245 endstr = argv[optind+2] + strlen(argv[optind+2]);
246 if (endptr != endstr) {
247 sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
248 phm2if_usage(argv[0]);
253 oss << "nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
254 if (opt_phmfilename.length())
255 oss << "phantom=" << opt_phmfilename;
256 else if (opt_phmnum != -1)
257 oss << "phantom=" << name_of_phantom(opt_phmnum);
258 else if (opt_filter != -1) {
259 oss << "filter=" << name_of_filter(opt_filter);
260 oss << " - " << name_of_filter_domain(opt_filter_domain);
262 if (opt_desc.length())
263 oss << ": " << opt_desc;
264 opt_desc = oss.str();
267 cout << "Rasterize Phantom to Image" << endl << endl;
273 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
274 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
275 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
276 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
277 mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
278 mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&opt_filter_domain, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
284 mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
287 timerBcast.timerEndAndReport ("Time to broadcast variables");
289 mpiWorld.setTotalWorkUnits (opt_nx);
291 if (mpiWorld.getRank() == 0) {
292 imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
293 imGlobal->fileCreate();
295 imLocal = new ImageFile (opt_nx, opt_ny);
297 imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
298 imGlobal->fileCreate ();
302 phm.create (opt_phmnum);
306 if (mpiWorld.getRank() == 0)
307 cerr << "phmnum < 0" << endl;
314 if (mpiWorld.getRank() == 0)
315 v = imGlobal->getArray ();
317 if (phm.getComposition() == P_UNIT_PULSE) {
318 if (mpiWorld.getRank() == 0) {
319 v[opt_nx/2][opt_ny/2] = 1.;
321 } else if (opt_filter != -1) {
322 if (mpiWorld.getRank() == 0) {
323 image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
326 TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
327 phm_to_imagefile (phm, *imLocal, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), opt_nsample, opt_trace);
329 timerRasterize.timerEndAndReport ("Time to rasterize phantom");
331 TimerCollectiveMPI timerGather (mpiWorld.getComm());
332 mpi_gather_image (mpiWorld, imGlobal, imLocal, opt_debug);
334 timerGather.timerEndAndReport ("Time to gather image");
337 v = imGlobal->getArray ();
338 if (phm.getComposition() == P_UNIT_PULSE) {
339 v[opt_nx/2][opt_ny/2] = 1.;
340 } else if (opt_filter != -1) {
341 image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
344 if (opt_trace >= TRACE_PHM)
347 phm_to_imagefile (phm, *imGlobal, 0, opt_nx, opt_nsample, opt_trace);
352 if (mpiWorld.getRank() == 0)
355 imGlobal->arrayDataWrite ();
356 double calctime = timerProgram.timerEnd ();
357 imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc.c_str(), calctime);
358 imGlobal->fileClose ();
360 cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
362 if (opt_trace >= TRACE_PHM) {
366 printf ("Enter display size scale (nominal = 1): ");
367 scanf ("%d", &nscale);
368 printf ("Enter minimum and maximum densities (min, max): ");
369 scanf ("%lf %lf", &dmin, &dmax);
370 image_display_scale (*imGlobal, nscale, dmin, dmax);
380 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug)
382 ImageFileArray vLocal = imLocal->getArray();
383 ImageFileArray vGlobal = NULL;
384 int nyLocal = imLocal->ny();
386 if (mpiWorld.getRank() == 0)
387 vGlobal = imGlobal->getArray();
389 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
390 mpiWorld.getComm().Send(vLocal[iw], nyLocal, imLocal->getMPIDataType(), 0, 0);
392 if (mpiWorld.getRank() == 0) {
393 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
394 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
396 mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, imLocal->getMPIDataType(), iProc, 0, status);
406 main (int argc, char* argv[])
411 retval = phm2if_main(argc, argv);
412 } catch (exception e) {
413 cerr << "Exception: " << e.what() << endl;
415 cerr << "Unknown exception" << endl;