1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2if.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 ** $Log: phm2if.cpp,v $
7 ** Revision 1.1 2000/06/07 02:29:05 kevin
8 ** Initial C++ versions
10 ** Revision 1.10 2000/05/24 22:50:04 kevin
11 ** Added support for new SGP library
13 ** Revision 1.9 2000/05/16 04:33:59 kevin
14 ** Improved option processing
16 ** Revision 1.8 2000/05/11 01:06:30 kevin
17 ** Changed sprintf to snprintf
19 ** Revision 1.7 2000/05/08 20:02:32 kevin
22 ** Revision 1.6 2000/05/03 08:49:50 kevin
25 ** Revision 1.5 2000/04/30 19:17:54 kevin
26 ** *** empty log message ***
28 ** Revision 1.4 2000/04/30 04:06:13 kevin
29 ** Update Raysum i/o routines
30 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
32 ** Revision 1.3 2000/04/28 18:19:01 kevin
33 ** removed unused files
35 ** Revision 1.2 2000/04/28 13:50:45 kevin
36 ** Removed Makefile Makefile.in that are automatically generated by autoconf
38 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
39 ** Initial CVS import for first public release
43 ** This program is free software; you can redistribute it and/or modify
44 ** it under the terms of the GNU General Public License (version 2) as
45 ** published by the Free Software Foundation.
47 ** This program is distributed in the hope that it will be useful,
48 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
49 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 ** GNU General Public License for more details.
52 ** You should have received a copy of the GNU General Public License
53 ** along with this program; if not, write to the Free Software
54 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
55 ******************************************************************************/
58 * phm2sdf.c Generate a SDF image from a phantom
61 * 1984 - Final DOS verion
62 * 1999 - First UNIX version
67 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
68 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
70 static struct option my_options[] =
72 {"phantom", 1, 0, O_PHANTOM},
73 {"phmfile", 1, 0, O_PHMFILE},
74 {"desc", 1, 0, O_DESC},
75 {"nsample", 1, 0, O_NSAMPLE},
76 {"filter", 1, 0, O_FILTER},
77 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
78 {"filter-bw", 1, 0, O_FILTER_BW},
79 {"filter-param", 1, 0, O_FILTER_PARAM},
80 {"trace", 1, 0, O_TRACE},
81 {"verbose", 0, 0, O_VERBOSE},
82 {"debug", 0, 0, O_DEBUG},
83 {"help", 0, 0, O_HELP},
84 {"version", 0, 0, O_VERSION},
89 phm2sdf_usage (const char *program)
91 fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
92 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
94 fprintf(stdout," outfile Name of output file for image\n");
95 fprintf(stdout," nx Number of pixels X-axis\n");
96 fprintf(stdout," ny Number of pixels Y-axis\n");
97 fprintf(stdout," --phantom Phantom to use for projection\n");
98 fprintf(stdout," herman Herman head phantom\n");
99 fprintf(stdout," rowland Rowland head phantom\n");
100 fprintf(stdout," browland Bordered Rowland head phantom\n");
101 fprintf(stdout," unitpulse Unit pulse phantom\n");
102 fprintf(stdout," --phmfile Generate Phantom from phantom file\n");
103 fprintf(stdout," --filter Generate Phantom from a filter function\n");
104 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
105 fprintf(stdout," abs_sinc Abs * Sinc\n");
106 fprintf(stdout," abs_cos Abs * Cosine\n");
107 fprintf(stdout," abs_hamming Abs * Hamming\n");
108 fprintf(stdout," shepp Shepp-Logan\n");
109 fprintf(stdout," bandlimit Bandlimiting\n");
110 fprintf(stdout," sinc Sinc\n");
111 fprintf(stdout," cos Cosine\n");
112 fprintf(stdout," triangle Triangle\n");
113 fprintf(stdout," hamming Hamming\n");
114 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
115 fprintf(stdout," --filter-domain Set domain of filter\n");
116 fprintf(stdout," spatial Spatial domain (default)\n");
117 fprintf(stdout," freq Frequency domain\n");
118 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
119 fprintf(stdout," --desc Description of raysum\n");
120 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
121 fprintf(stdout," --trace Trace level to use\n");
122 fprintf(stdout," none No tracing (default)\n");
123 fprintf(stdout," text Trace text level\n");
124 fprintf(stdout," phm Trace phantom\n");
125 fprintf(stdout," rays Trace rays\n");
126 fprintf(stdout," plot Trace plot\n");
127 fprintf(stdout," clipping Trace clipping\n");
128 fprintf(stdout," --debug Debug mode\n");
129 fprintf(stdout," --verbose Verbose mode\n");
130 fprintf(stdout," --version Print version\n");
131 fprintf(stdout," --help Print this help message\n");
135 void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug);
139 phm2sdf_main (const int argc, char *const argv[])
141 ImageFile *im_global = NULL;
143 int opt_nx = 0, opt_ny = 0;
146 FilterType opt_filter = static_cast<FilterType>(-1);
147 DomainType opt_filter_domain = D_SPATIAL;
148 char *opt_outfile = NULL;
151 char opt_desc[256], opt_phmfilename[256];
152 char *endstr, *endptr;
153 double opt_filter_param = 1;
154 double opt_filter_bw = 1.;
155 int opt_trace = TRACE_NONE;
157 double time_start=0, time_end=0;
159 ImageFile *im_local = NULL;
161 char **mpi_argv = (char **) argv;
162 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
164 MPI_Init(&mpi_argc, &mpi_argv);
165 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
166 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
167 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
169 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
170 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
171 mpi_ct.nproc, MPI_MAX_PROCESS);
176 time_start = MPI_Wtime();
178 time_start = td_current_sec();
182 if (mpi_ct.my_rank == 0) {
185 strcpy(opt_desc, "");
186 strcpy(opt_phmfilename, "");
188 int c = getopt_long(argc, argv, "", my_options, NULL);
197 if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
198 phm2sdf_usage(argv[0]);
203 strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
204 phm = phm_create_from_file(opt_phmfilename);
206 if (mpi_ct.my_rank == 0)
207 fprintf(stderr, "Can't use phantom from file in MPI mode\n");
218 if ((opt_trace = opt_set_trace(optarg)) < 0) {
219 phm2sdf_usage(argv[0]);
224 if ((opt_filter = opt_set_filter(optarg)) < 0) {
225 phm2sdf_usage (argv[0]);
229 case O_FILTER_DOMAIN:
230 if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
231 phm2sdf_usage (argv[0]);
236 strncpy(opt_desc, optarg, sizeof(opt_desc));
239 opt_filter_bw = strtod(optarg, &endptr);
240 endstr = optarg + strlen(optarg);
241 if (endptr != endstr) {
242 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
243 phm2sdf_usage(argv[0]);
248 opt_filter_param = strtod(optarg, &endptr);
249 endstr = optarg + strlen(optarg);
250 if (endptr != endstr) {
251 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
252 phm2sdf_usage(argv[0]);
257 opt_nsample = strtol(optarg, &endptr, 10);
258 endstr = optarg + strlen(optarg);
259 if (endptr != endstr) {
260 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
261 phm2sdf_usage(argv[0]);
267 fprintf(stdout, "Version %s\n", VERSION);
269 fprintf(stderr, "Unknown version number");
273 phm2sdf_usage(argv[0]);
276 phm2sdf_usage(argv[0]);
281 if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
282 fprintf(stderr, "No phantom defined\n");
283 phm2sdf_usage(argv[0]);
287 if (optind + 3 != argc) {
288 phm2sdf_usage(argv[0]);
291 opt_outfile = argv[optind];
292 opt_nx = strtol(argv[optind+1], &endptr, 10);
293 endstr = argv[optind+1] + strlen(argv[optind+1]);
294 if (endptr != endstr) {
295 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
296 phm2sdf_usage(argv[0]);
299 opt_ny = strtol(argv[optind+2], &endptr, 10);
300 endstr = argv[optind+2] + strlen(argv[optind+2]);
301 if (endptr != endstr) {
302 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
303 phm2sdf_usage(argv[0]);
307 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
308 if (opt_phmfilename[0]) {
309 strncat(str, "phantom=", sizeof(str));
310 strncat(str, opt_phmfilename, sizeof(str));
312 else if (opt_phmnum != -1) {
313 strncat(str, "phantom=", sizeof(str));
314 strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
316 else if (opt_filter != -1) {
317 strncat(str, "filter=", sizeof(str));
318 strncat(str, name_of_filter(opt_filter), sizeof(str));
319 strncat(str, " - ", sizeof(str));
320 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
323 strncat(str, ": ", sizeof(str));
324 strncat(str, opt_desc, sizeof(str));
326 strncpy(opt_desc, str, sizeof(opt_desc));
329 printf("Rasterize Phantom to Image\n\n");
335 mpi_t1 = MPI_Wtime();
336 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
337 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
338 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
339 MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
340 MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
341 MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
342 MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
343 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
344 MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
345 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
346 MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
349 mpi_t2 = MPI_Wtime();
350 mpi_t = mpi_t2 - mpi_t1;
351 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
352 if (mpi_ct.my_rank == 0)
353 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
356 mpi_ct_calc_work_units(opt_nx);
358 if (mpi_ct.my_rank == 0) {
359 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
360 im_global->adf.fileCreate();
363 im_local = new ImageFile (opt_nx, opt_ny);
365 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
366 im_global->adf.fileCreate ();
370 phm = phm_create (opt_phmnum);
374 if (mpi_ct.my_rank == 0)
375 fprintf(stderr, "phmnum < 0\n");
380 ImageFileArray v = im_global->adf.getArray ();
384 if (phm->type == P_UNIT_PULSE) {
385 if (mpi_ct.my_rank == 0) {
386 v[opt_nx/2][opt_ny/2] = 1.;
389 } else if (opt_filter != -1) {
390 if (mpi_ct.my_rank == 0) {
391 image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
396 mpi_t1 = MPI_Wtime();
397 phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
398 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
400 mpi_t2 = MPI_Wtime();
401 mpi_t = mpi_t2 - mpi_t1;
402 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
403 if (mpi_ct.my_rank == 0)
404 printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
406 mpi_gather_image(im_global, im_local, opt_debug);
409 if (phm->type == P_UNIT_PULSE) {
410 v[opt_nx/2][opt_ny/2] = 1.;
412 } else if (opt_filter != -1) {
413 // image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
417 if (opt_trace >= TRACE_PHM)
420 // phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
425 time_end = MPI_Wtime();
427 time_end = td_current_sec();
431 if (mpi_ct.my_rank == 0)
434 im_global->adf.arrayDataWrite ();
435 calctime = time_end - time_start;
436 im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
437 im_global->adf.fileClose ();
439 fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime);
442 if (opt_trace >= TRACE_PHM)
447 printf ("Enter display size scale (nominal = 1): ");
448 scanf ("%d", &nscale);
449 printf ("Enter minimum and maximum densities (min, max): ");
450 scanf ("%lf %lf", &dmin, &dmax);
451 // image_display_scale (im_global, nscale, dmin, dmax);
462 void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug)
468 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
469 for (iw = 0; iw <= end_work_unit; iw++) {
470 MPI_Send(im_local->v[iw], im_local->ny, im_local->getMPIDataType(), 0, 0, mpi_ct.comm);
473 if (mpi_ct.my_rank == 0) {
474 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
475 end_work_unit = mpi_ct.start_work_unit[iproc]
476 + mpi_ct.local_work_units[iproc]
480 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
481 mpi_ct.start_work_unit[iproc], end_work_unit);
486 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
489 MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
499 main (const int argc, char *const argv[])
501 return (phm2sdf_main(argc, argv));