1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2if.cpp,v 1.2 2000/06/07 03:50:27 kevin Exp $
6 ** $Log: phm2if.cpp,v $
7 ** Revision 1.2 2000/06/07 03:50:27 kevin
8 ** *** empty log message ***
10 ** Revision 1.1 2000/06/07 02:29:05 kevin
11 ** Initial C++ versions
13 ** Revision 1.10 2000/05/24 22:50:04 kevin
14 ** Added support for new SGP library
16 ** Revision 1.9 2000/05/16 04:33:59 kevin
17 ** Improved option processing
19 ** Revision 1.8 2000/05/11 01:06:30 kevin
20 ** Changed sprintf to snprintf
22 ** Revision 1.7 2000/05/08 20:02:32 kevin
25 ** Revision 1.6 2000/05/03 08:49:50 kevin
28 ** Revision 1.5 2000/04/30 19:17:54 kevin
29 ** *** empty log message ***
31 ** Revision 1.4 2000/04/30 04:06:13 kevin
32 ** Update Raysum i/o routines
33 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
35 ** Revision 1.3 2000/04/28 18:19:01 kevin
36 ** removed unused files
38 ** Revision 1.2 2000/04/28 13:50:45 kevin
39 ** Removed Makefile Makefile.in that are automatically generated by autoconf
41 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
42 ** Initial CVS import for first public release
46 ** This program is free software; you can redistribute it and/or modify
47 ** it under the terms of the GNU General Public License (version 2) as
48 ** published by the Free Software Foundation.
50 ** This program is distributed in the hope that it will be useful,
51 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
52 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 ** GNU General Public License for more details.
55 ** You should have received a copy of the GNU General Public License
56 ** along with this program; if not, write to the Free Software
57 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
58 ******************************************************************************/
61 * phm2sdf.c Generate a SDF image from a phantom
64 * 1984 - Final DOS verion
65 * 1999 - First UNIX version
70 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
71 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
73 static struct option my_options[] =
75 {"phantom", 1, 0, O_PHANTOM},
76 {"phmfile", 1, 0, O_PHMFILE},
77 {"desc", 1, 0, O_DESC},
78 {"nsample", 1, 0, O_NSAMPLE},
79 {"filter", 1, 0, O_FILTER},
80 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
81 {"filter-bw", 1, 0, O_FILTER_BW},
82 {"filter-param", 1, 0, O_FILTER_PARAM},
83 {"trace", 1, 0, O_TRACE},
84 {"verbose", 0, 0, O_VERBOSE},
85 {"debug", 0, 0, O_DEBUG},
86 {"help", 0, 0, O_HELP},
87 {"version", 0, 0, O_VERSION},
92 phm2sdf_usage (const char *program)
94 fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
95 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
97 fprintf(stdout," outfile Name of output file for image\n");
98 fprintf(stdout," nx Number of pixels X-axis\n");
99 fprintf(stdout," ny Number of pixels Y-axis\n");
100 fprintf(stdout," --phantom Phantom to use for projection\n");
101 fprintf(stdout," herman Herman head phantom\n");
102 fprintf(stdout," rowland Rowland head phantom\n");
103 fprintf(stdout," browland Bordered Rowland head phantom\n");
104 fprintf(stdout," unitpulse Unit pulse phantom\n");
105 fprintf(stdout," --phmfile Generate Phantom from phantom file\n");
106 fprintf(stdout," --filter Generate Phantom from a filter function\n");
107 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
108 fprintf(stdout," abs_sinc Abs * Sinc\n");
109 fprintf(stdout," abs_cos Abs * Cosine\n");
110 fprintf(stdout," abs_hamming Abs * Hamming\n");
111 fprintf(stdout," shepp Shepp-Logan\n");
112 fprintf(stdout," bandlimit Bandlimiting\n");
113 fprintf(stdout," sinc Sinc\n");
114 fprintf(stdout," cos Cosine\n");
115 fprintf(stdout," triangle Triangle\n");
116 fprintf(stdout," hamming Hamming\n");
117 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
118 fprintf(stdout," --filter-domain Set domain of filter\n");
119 fprintf(stdout," spatial Spatial domain (default)\n");
120 fprintf(stdout," freq Frequency domain\n");
121 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
122 fprintf(stdout," --desc Description of raysum\n");
123 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
124 fprintf(stdout," --trace Trace level to use\n");
125 fprintf(stdout," none No tracing (default)\n");
126 fprintf(stdout," text Trace text level\n");
127 fprintf(stdout," phm Trace phantom\n");
128 fprintf(stdout," rays Trace rays\n");
129 fprintf(stdout," plot Trace plot\n");
130 fprintf(stdout," clipping Trace clipping\n");
131 fprintf(stdout," --debug Debug mode\n");
132 fprintf(stdout," --verbose Verbose mode\n");
133 fprintf(stdout," --version Print version\n");
134 fprintf(stdout," --help Print this help message\n");
138 void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug);
142 phm2sdf_main (const int argc, char *const argv[])
144 ImageFile *im_global = NULL;
146 int opt_nx = 0, opt_ny = 0;
149 FilterType opt_filter = static_cast<FilterType>(-1);
150 DomainType opt_filter_domain = D_SPATIAL;
151 char *opt_outfile = NULL;
154 char opt_desc[256], opt_phmfilename[256];
155 char *endstr, *endptr;
156 double opt_filter_param = 1;
157 double opt_filter_bw = 1.;
158 int opt_trace = TRACE_NONE;
160 double time_start=0, time_end=0;
162 ImageFile *im_local = NULL;
164 char **mpi_argv = (char **) argv;
165 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
167 MPI_Init(&mpi_argc, &mpi_argv);
168 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
169 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
170 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
172 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
173 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
174 mpi_ct.nproc, MPI_MAX_PROCESS);
179 time_start = MPI_Wtime();
181 time_start = td_current_sec();
185 if (mpi_ct.my_rank == 0) {
188 strcpy(opt_desc, "");
189 strcpy(opt_phmfilename, "");
191 int c = getopt_long(argc, argv, "", my_options, NULL);
200 if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
201 phm2sdf_usage(argv[0]);
206 strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
207 phm = phm_create_from_file(opt_phmfilename);
209 if (mpi_ct.my_rank == 0)
210 fprintf(stderr, "Can't use phantom from file in MPI mode\n");
221 if ((opt_trace = opt_set_trace(optarg)) < 0) {
222 phm2sdf_usage(argv[0]);
227 if ((opt_filter = opt_set_filter(optarg)) < 0) {
228 phm2sdf_usage (argv[0]);
232 case O_FILTER_DOMAIN:
233 if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
234 phm2sdf_usage (argv[0]);
239 strncpy(opt_desc, optarg, sizeof(opt_desc));
242 opt_filter_bw = strtod(optarg, &endptr);
243 endstr = optarg + strlen(optarg);
244 if (endptr != endstr) {
245 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
246 phm2sdf_usage(argv[0]);
251 opt_filter_param = strtod(optarg, &endptr);
252 endstr = optarg + strlen(optarg);
253 if (endptr != endstr) {
254 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
255 phm2sdf_usage(argv[0]);
260 opt_nsample = strtol(optarg, &endptr, 10);
261 endstr = optarg + strlen(optarg);
262 if (endptr != endstr) {
263 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
264 phm2sdf_usage(argv[0]);
270 fprintf(stdout, "Version %s\n", VERSION);
272 fprintf(stderr, "Unknown version number");
276 phm2sdf_usage(argv[0]);
279 phm2sdf_usage(argv[0]);
284 if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
285 fprintf(stderr, "No phantom defined\n");
286 phm2sdf_usage(argv[0]);
290 if (optind + 3 != argc) {
291 phm2sdf_usage(argv[0]);
294 opt_outfile = argv[optind];
295 opt_nx = strtol(argv[optind+1], &endptr, 10);
296 endstr = argv[optind+1] + strlen(argv[optind+1]);
297 if (endptr != endstr) {
298 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
299 phm2sdf_usage(argv[0]);
302 opt_ny = strtol(argv[optind+2], &endptr, 10);
303 endstr = argv[optind+2] + strlen(argv[optind+2]);
304 if (endptr != endstr) {
305 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
306 phm2sdf_usage(argv[0]);
310 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
311 if (opt_phmfilename[0]) {
312 strncat(str, "phantom=", sizeof(str));
313 strncat(str, opt_phmfilename, sizeof(str));
315 else if (opt_phmnum != -1) {
316 strncat(str, "phantom=", sizeof(str));
317 strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
319 else if (opt_filter != -1) {
320 strncat(str, "filter=", sizeof(str));
321 strncat(str, name_of_filter(opt_filter), sizeof(str));
322 strncat(str, " - ", sizeof(str));
323 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
326 strncat(str, ": ", sizeof(str));
327 strncat(str, opt_desc, sizeof(str));
329 strncpy(opt_desc, str, sizeof(opt_desc));
332 printf("Rasterize Phantom to Image\n\n");
338 mpi_t1 = MPI_Wtime();
339 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
340 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
341 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
342 MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
343 MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
344 MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
345 MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
346 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
347 MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
348 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
349 MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
352 mpi_t2 = MPI_Wtime();
353 mpi_t = mpi_t2 - mpi_t1;
354 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
355 if (mpi_ct.my_rank == 0)
356 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
359 mpi_ct_calc_work_units(opt_nx);
361 if (mpi_ct.my_rank == 0) {
362 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
363 im_global->adf.fileCreate();
366 im_local = new ImageFile (opt_nx, opt_ny);
368 im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
369 im_global->adf.fileCreate ();
373 phm = phm_create (opt_phmnum);
377 if (mpi_ct.my_rank == 0)
378 fprintf(stderr, "phmnum < 0\n");
383 ImageFileArray v = im_global->adf.getArray ();
387 if (phm->type == P_UNIT_PULSE) {
388 if (mpi_ct.my_rank == 0) {
389 v[opt_nx/2][opt_ny/2] = 1.;
392 } else if (opt_filter != -1) {
393 if (mpi_ct.my_rank == 0) {
394 image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
399 mpi_t1 = MPI_Wtime();
400 phm_to_imagefile (phm, *im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
401 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
403 mpi_t2 = MPI_Wtime();
404 mpi_t = mpi_t2 - mpi_t1;
405 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
406 if (mpi_ct.my_rank == 0)
407 printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
409 mpi_gather_image(im_global, im_local, opt_debug);
412 if (phm->type == P_UNIT_PULSE) {
413 v[opt_nx/2][opt_ny/2] = 1.;
415 } else if (opt_filter != -1) {
416 // image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
420 if (opt_trace >= TRACE_PHM)
423 phm_to_imagefile (phm, *im_global, 0, opt_nx, opt_nsample, opt_trace);
428 time_end = MPI_Wtime();
430 time_end = td_current_sec();
434 if (mpi_ct.my_rank == 0)
437 im_global->adf.arrayDataWrite ();
438 calctime = time_end - time_start;
439 im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
440 im_global->adf.fileClose ();
442 fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime);
445 if (opt_trace >= TRACE_PHM)
450 printf ("Enter display size scale (nominal = 1): ");
451 scanf ("%d", &nscale);
452 printf ("Enter minimum and maximum densities (min, max): ");
453 scanf ("%lf %lf", &dmin, &dmax);
454 // image_display_scale (im_global, nscale, dmin, dmax);
465 void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug)
471 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
472 for (iw = 0; iw <= end_work_unit; iw++) {
473 MPI_Send(im_local->v[iw], im_local->ny, im_local->getMPIDataType(), 0, 0, mpi_ct.comm);
476 if (mpi_ct.my_rank == 0) {
477 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
478 end_work_unit = mpi_ct.start_work_unit[iproc]
479 + mpi_ct.local_work_units[iproc]
483 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
484 mpi_ct.start_work_unit[iproc], end_work_unit);
489 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
492 MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
502 main (const int argc, char *const argv[])
504 return (phm2sdf_main(argc, argv));