1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2if.cpp,v 1.4 2000/06/07 10:12:05 kevin Exp $
6 ** $Log: phm2if.cpp,v $
7 ** Revision 1.4 2000/06/07 10:12:05 kevin
8 ** Upgraded from MPI to MPI++
10 ** Revision 1.3 2000/06/07 07:43:19 kevin
11 ** Converted to IF data files and C++
13 ** Revision 1.2 2000/06/07 03:50:27 kevin
14 ** *** empty log message ***
16 ** Revision 1.1 2000/06/07 02:29:05 kevin
17 ** Initial C++ versions
19 ** Revision 1.10 2000/05/24 22:50:04 kevin
20 ** Added support for new SGP library
22 ** Revision 1.9 2000/05/16 04:33:59 kevin
23 ** Improved option processing
25 ** Revision 1.8 2000/05/11 01:06:30 kevin
26 ** Changed sprintf to snprintf
28 ** Revision 1.7 2000/05/08 20:02:32 kevin
31 ** Revision 1.6 2000/05/03 08:49:50 kevin
34 ** Revision 1.5 2000/04/30 19:17:54 kevin
35 ** *** empty log message ***
37 ** Revision 1.4 2000/04/30 04:06:13 kevin
38 ** Update Raysum i/o routines
39 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
41 ** Revision 1.3 2000/04/28 18:19:01 kevin
42 ** removed unused files
44 ** Revision 1.2 2000/04/28 13:50:45 kevin
45 ** Removed Makefile Makefile.in that are automatically generated by autoconf
47 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
48 ** Initial CVS import for first public release
52 ** This program is free software; you can redistribute it and/or modify
53 ** it under the terms of the GNU General Public License (version 2) as
54 ** published by the Free Software Foundation.
56 ** This program is distributed in the hope that it will be useful,
57 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
58 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 ** GNU General Public License for more details.
61 ** You should have received a copy of the GNU General Public License
62 ** along with this program; if not, write to the Free Software
63 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
64 ******************************************************************************/
67 * phm2sdf.c Generate a SDF image from a phantom
70 * 1984 - Final DOS verion
71 * 1999 - First UNIX version
76 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
77 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
79 static struct option my_options[] =
81 {"phantom", 1, 0, O_PHANTOM},
82 {"phmfile", 1, 0, O_PHMFILE},
83 {"desc", 1, 0, O_DESC},
84 {"nsample", 1, 0, O_NSAMPLE},
85 {"filter", 1, 0, O_FILTER},
86 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
87 {"filter-bw", 1, 0, O_FILTER_BW},
88 {"filter-param", 1, 0, O_FILTER_PARAM},
89 {"trace", 1, 0, O_TRACE},
90 {"verbose", 0, 0, O_VERBOSE},
91 {"debug", 0, 0, O_DEBUG},
92 {"help", 0, 0, O_HELP},
93 {"version", 0, 0, O_VERSION},
98 phm2sdf_usage (const char *program)
100 fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
101 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
102 fprintf(stdout,"\n");
103 fprintf(stdout," outfile Name of output file for image\n");
104 fprintf(stdout," nx Number of pixels X-axis\n");
105 fprintf(stdout," ny Number of pixels Y-axis\n");
106 fprintf(stdout," --phantom Phantom to use for projection\n");
107 fprintf(stdout," herman Herman head phantom\n");
108 fprintf(stdout," rowland Rowland head phantom\n");
109 fprintf(stdout," browland Bordered Rowland head phantom\n");
110 fprintf(stdout," unitpulse Unit pulse phantom\n");
111 fprintf(stdout," --phmfile Generate Phantom from phantom file\n");
112 fprintf(stdout," --filter Generate Phantom from a filter function\n");
113 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
114 fprintf(stdout," abs_sinc Abs * Sinc\n");
115 fprintf(stdout," abs_cos Abs * Cosine\n");
116 fprintf(stdout," abs_hamming Abs * Hamming\n");
117 fprintf(stdout," shepp Shepp-Logan\n");
118 fprintf(stdout," bandlimit Bandlimiting\n");
119 fprintf(stdout," sinc Sinc\n");
120 fprintf(stdout," cos Cosine\n");
121 fprintf(stdout," triangle Triangle\n");
122 fprintf(stdout," hamming Hamming\n");
123 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
124 fprintf(stdout," --filter-domain Set domain of filter\n");
125 fprintf(stdout," spatial Spatial domain (default)\n");
126 fprintf(stdout," freq Frequency domain\n");
127 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
128 fprintf(stdout," --desc Description of raysum\n");
129 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
130 fprintf(stdout," --trace Trace level to use\n");
131 fprintf(stdout," none No tracing (default)\n");
132 fprintf(stdout," text Trace text level\n");
133 fprintf(stdout," phm Trace phantom\n");
134 fprintf(stdout," rays Trace rays\n");
135 fprintf(stdout," plot Trace plot\n");
136 fprintf(stdout," clipping Trace clipping\n");
137 fprintf(stdout," --debug Debug mode\n");
138 fprintf(stdout," --verbose Verbose mode\n");
139 fprintf(stdout," --version Print version\n");
140 fprintf(stdout," --help Print this help message\n");
144 void mpi_gather_image (ImageFile& im_global, ImageFile& im_local, const int opt_debug);
148 phm2sdf_main (const int argc, char *const argv[])
150 ImageFile* im_global = NULL;
152 int opt_nx = 0, opt_ny = 0;
155 FilterType opt_filter = static_cast<FilterType>(-1);
156 DomainType opt_filter_domain = D_SPATIAL;
157 char *opt_outfile = NULL;
160 char opt_desc[256], opt_phmfilename[256];
161 char *endstr, *endptr;
162 double opt_filter_param = 1;
163 double opt_filter_bw = 1.;
164 int opt_trace = TRACE_NONE;
166 double time_start=0, time_end=0;
168 ImageFile* im_local = NULL;
170 char **mpi_argv = (char **) argv;
171 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
173 MPI::Init (mpi_argc, mpi_argv);
174 mpi_ct.comm = MPI::COMM_WORLD.Dup ();
175 mpi_ct.nproc = mpi_ct.comm.Get_size();
176 mpi_ct.my_rank = mpi_ct.comm.Get_rank();
178 if (mpi_ct.nproc > CT_MPI_MAX_PROCESS) {
179 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
180 mpi_ct.nproc, CT_MPI_MAX_PROCESS);
185 time_start = MPI::Wtime();
187 time_start = td_current_sec();
191 if (mpi_ct.my_rank == 0) {
194 strcpy(opt_desc, "");
195 strcpy(opt_phmfilename, "");
197 int c = getopt_long(argc, argv, "", my_options, NULL);
206 if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
207 phm2sdf_usage(argv[0]);
212 strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
213 phm = phm_create_from_file(opt_phmfilename);
215 if (mpi_ct.my_rank == 0)
216 fprintf(stderr, "Can't use phantom from file in MPI mode\n");
227 if ((opt_trace = opt_set_trace(optarg)) < 0) {
228 phm2sdf_usage(argv[0]);
233 if ((opt_filter = opt_set_filter(optarg)) < 0) {
234 phm2sdf_usage (argv[0]);
238 case O_FILTER_DOMAIN:
239 if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
240 phm2sdf_usage (argv[0]);
245 strncpy(opt_desc, optarg, sizeof(opt_desc));
248 opt_filter_bw = strtod(optarg, &endptr);
249 endstr = optarg + strlen(optarg);
250 if (endptr != endstr) {
251 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
252 phm2sdf_usage(argv[0]);
257 opt_filter_param = strtod(optarg, &endptr);
258 endstr = optarg + strlen(optarg);
259 if (endptr != endstr) {
260 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
261 phm2sdf_usage(argv[0]);
266 opt_nsample = strtol(optarg, &endptr, 10);
267 endstr = optarg + strlen(optarg);
268 if (endptr != endstr) {
269 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
270 phm2sdf_usage(argv[0]);
276 fprintf(stdout, "Version %s\n", VERSION);
278 fprintf(stderr, "Unknown version number");
282 phm2sdf_usage(argv[0]);
285 phm2sdf_usage(argv[0]);
290 if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
291 fprintf(stderr, "No phantom defined\n");
292 phm2sdf_usage(argv[0]);
296 if (optind + 3 != argc) {
297 phm2sdf_usage(argv[0]);
300 opt_outfile = argv[optind];
301 opt_nx = strtol(argv[optind+1], &endptr, 10);
302 endstr = argv[optind+1] + strlen(argv[optind+1]);
303 if (endptr != endstr) {
304 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
305 phm2sdf_usage(argv[0]);
308 opt_ny = strtol(argv[optind+2], &endptr, 10);
309 endstr = argv[optind+2] + strlen(argv[optind+2]);
310 if (endptr != endstr) {
311 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
312 phm2sdf_usage(argv[0]);
316 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
317 if (opt_phmfilename[0]) {
318 strncat(str, "phantom=", sizeof(str));
319 strncat(str, opt_phmfilename, sizeof(str));
321 else if (opt_phmnum != -1) {
322 strncat(str, "phantom=", sizeof(str));
323 strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
325 else if (opt_filter != -1) {
326 strncat(str, "filter=", sizeof(str));
327 strncat(str, name_of_filter(opt_filter), sizeof(str));
328 strncat(str, " - ", sizeof(str));
329 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
332 strncat(str, ": ", sizeof(str));
333 strncat(str, opt_desc, sizeof(str));
335 strncpy(opt_desc, str, sizeof(opt_desc));
338 printf("Rasterize Phantom to Image\n\n");
344 mpi_t1 = MPI::Wtime();
345 mpi_ct.comm.Bcast (&opt_verbose, 1, MPI::INT, 0);
346 mpi_ct.comm.Bcast (&opt_debug, 1, MPI::INT, 0);
347 mpi_ct.comm.Bcast (&opt_trace, 1, MPI::INT, 0);
348 mpi_ct.comm.Bcast (&opt_nx, 1, MPI::INT, 0);
349 mpi_ct.comm.Bcast (&opt_ny, 1, MPI::INT, 0);
350 mpi_ct.comm.Bcast (&opt_nsample, 1, MPI::INT, 0);
351 mpi_ct.comm.Bcast (&opt_phmnum, 1, MPI::INT, 0);
352 mpi_ct.comm.Bcast (&opt_filter, 1, MPI::INT, 0);
353 mpi_ct.comm.Bcast (&opt_filter_domain, 1, MPI::INT, 0);
354 mpi_ct.comm.Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
355 mpi_ct.comm.Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
358 mpi_t2 = MPI::Wtime();
359 mpi_t = mpi_t2 - mpi_t1;
360 mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
361 if (mpi_ct.my_rank == 0)
362 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
365 mpi_ct_calc_work_units(opt_nx);
367 if (mpi_ct.my_rank == 0) {
368 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
369 im_global->adf.fileCreate();
372 im_local = new ImageFile (opt_nx, opt_ny);
374 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
375 im_global->adf.fileCreate ();
379 phm = phm_create (opt_phmnum);
383 if (mpi_ct.my_rank == 0)
384 fprintf(stderr, "phmnum < 0\n");
389 ImageFileArray v = im_global->getArray ();
393 if (phm->type == P_UNIT_PULSE) {
394 if (mpi_ct.my_rank == 0) {
395 v[opt_nx/2][opt_ny/2] = 1.;
398 } else if (opt_filter != -1) {
399 if (mpi_ct.my_rank == 0) {
400 image_filter_response (*im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
405 mpi_t1 = MPI::Wtime();
406 phm_to_imagefile (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);
408 mpi_t2 = MPI::Wtime();
409 mpi_t = mpi_t2 - mpi_t1;
410 mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
411 if (mpi_ct.my_rank == 0)
412 printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
414 mpi_gather_image(*im_global, *im_local, opt_debug);
417 if (phm->type == P_UNIT_PULSE) {
418 v[opt_nx/2][opt_ny/2] = 1.;
420 } else if (opt_filter != -1) {
421 image_filter_response (*im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
425 if (opt_trace >= TRACE_PHM)
428 phm_to_imagefile (phm, *im_global, 0, opt_nx, opt_nsample, opt_trace);
433 time_end = MPI::Wtime();
435 time_end = td_current_sec();
439 if (mpi_ct.my_rank == 0)
442 im_global->adf.arrayDataWrite ();
443 calctime = time_end - time_start;
444 im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
445 im_global->adf.fileClose ();
447 fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime);
450 if (opt_trace >= TRACE_PHM) {
454 printf ("Enter display size scale (nominal = 1): ");
455 scanf ("%d", &nscale);
456 printf ("Enter minimum and maximum densities (min, max): ");
457 scanf ("%lf %lf", &dmin, &dmax);
458 image_display_scale (*im_global, nscale, dmin, dmax);
469 void mpi_gather_image (ImageFile& im_global, ImageFile& im_local, const int opt_debug)
471 ImageFileArray vLocal = im_local.getArray();
472 ImageFileArray vGlobal = im_global.getArray();
473 int nyLocal = im_local.ny();
474 int nyGlobal = im_global.ny();
476 int end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
477 for (int iw = 0; iw <= end_work_unit; iw++) {
478 mpi_ct.comm.Send(vLocal[iw], nyLocal, im_local.getMPIDataType(), 0, 0);
481 if (mpi_ct.my_rank == 0) {
482 for (int iproc = 0; iproc < mpi_ct.nproc; iproc++) {
483 end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc] - 1;
486 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc, mpi_ct.start_work_unit[iproc], end_work_unit);
490 for (int iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
492 mpi_ct.comm.Recv(vGlobal[iw], nyGlobal, MPI::FLOAT, iproc, 0, status);
502 main (const int argc, char *const argv[])
504 return (phm2sdf_main(argc, argv));