1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2sdf.c,v 1.9 2000/05/16 04:33:59 kevin Exp $
7 ** Revision 1.9 2000/05/16 04:33:59 kevin
8 ** Improved option processing
10 ** Revision 1.8 2000/05/11 01:06:30 kevin
11 ** Changed sprintf to snprintf
13 ** Revision 1.7 2000/05/08 20:02:32 kevin
16 ** Revision 1.6 2000/05/03 08:49:50 kevin
19 ** Revision 1.5 2000/04/30 19:17:54 kevin
20 ** *** empty log message ***
22 ** Revision 1.4 2000/04/30 04:06:13 kevin
23 ** Update Raysum i/o routines
24 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
26 ** Revision 1.3 2000/04/28 18:19:01 kevin
27 ** removed unused files
29 ** Revision 1.2 2000/04/28 13:50:45 kevin
30 ** Removed Makefile Makefile.in that are automatically generated by autoconf
32 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
33 ** Initial CVS import for first public release
37 ** This program is free software; you can redistribute it and/or modify
38 ** it under the terms of the GNU General Public License (version 2) as
39 ** published by the Free Software Foundation.
41 ** This program is distributed in the hope that it will be useful,
42 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
43 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 ** GNU General Public License for more details.
46 ** You should have received a copy of the GNU General Public License
47 ** along with this program; if not, write to the Free Software
48 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 ******************************************************************************/
52 * phm2sdf.c Generate a SDF image from a phantom
55 * 1984 - Final DOS verion
56 * 1999 - First UNIX version
61 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
62 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
64 static struct option my_options[] =
66 {"phantom", 1, 0, O_PHANTOM},
67 {"phmfile", 1, 0, O_PHMFILE},
68 {"desc", 1, 0, O_DESC},
69 {"nsample", 1, 0, O_NSAMPLE},
70 {"filter", 1, 0, O_FILTER},
71 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
72 {"filter-bw", 1, 0, O_FILTER_BW},
73 {"filter-param", 1, 0, O_FILTER_PARAM},
74 {"trace", 1, 0, O_TRACE},
75 {"verbose", 0, 0, O_VERBOSE},
76 {"debug", 0, 0, O_DEBUG},
77 {"help", 0, 0, O_HELP},
78 {"version", 0, 0, O_VERSION},
83 phm2sdf_usage (const char *program)
85 fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
86 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
88 fprintf(stdout," outfile Name of output file for image\n");
89 fprintf(stdout," nx Number of pixels X-axis\n");
90 fprintf(stdout," ny Number of pixels Y-axis\n");
91 fprintf(stdout," --phantom Phantom to use for projection\n");
92 fprintf(stdout," herman Herman head phantom\n");
93 fprintf(stdout," rowland Rowland head phantom\n");
94 fprintf(stdout," browland Bordered Rowland head phantom\n");
95 fprintf(stdout," unitpulse Unit pulse phantom\n");
96 fprintf(stdout," --phmfile Generate Phantom from phantom file\n");
97 fprintf(stdout," --filter Generate Phantom from a filter function\n");
98 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
99 fprintf(stdout," abs_sinc Abs * Sinc\n");
100 fprintf(stdout," abs_cos Abs * Cosine\n");
101 fprintf(stdout," abs_hamming Abs * Hamming\n");
102 fprintf(stdout," shepp Shepp-Logan\n");
103 fprintf(stdout," bandlimit Bandlimiting\n");
104 fprintf(stdout," sinc Sinc\n");
105 fprintf(stdout," cos Cosine\n");
106 fprintf(stdout," triangle Triangle\n");
107 fprintf(stdout," hamming Hamming\n");
108 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
109 fprintf(stdout," --filter-domain Set domain of filter\n");
110 fprintf(stdout," spatial Spatial domain (default)\n");
111 fprintf(stdout," freq Frequency domain\n");
112 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
113 fprintf(stdout," --desc Description of raysum\n");
114 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
115 fprintf(stdout," --trace Trace level to use\n");
116 fprintf(stdout," none No tracing (default)\n");
117 fprintf(stdout," text Trace text level\n");
118 fprintf(stdout," phm Trace phantom\n");
119 fprintf(stdout," rays Trace rays\n");
120 fprintf(stdout," plot Trace plot\n");
121 fprintf(stdout," clipping Trace clipping\n");
122 fprintf(stdout," --debug Debug mode\n");
123 fprintf(stdout," --verbose Verbose mode\n");
124 fprintf(stdout," --version Print version\n");
125 fprintf(stdout," --help Print this help message\n");
129 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
133 phm2sdf_main (const int argc, char *const argv[])
135 IMAGE *im_global = NULL;
137 int opt_nx = 0, opt_ny = 0;
141 int opt_filter_domain = D_SPATIAL;
142 char *opt_outfile = NULL;
145 char opt_desc[256], opt_phmfilename[256];
146 char *endstr, *endptr;
147 double opt_filter_param = 1;
148 double opt_filter_bw = 1.;
149 int opt_trace = TRACE_NONE;
151 double time_start=0, time_end=0;
153 IMAGE *im_local = NULL;
155 char **mpi_argv = (char **) argv;
156 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
158 MPI_Init(&mpi_argc, &mpi_argv);
159 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
160 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
161 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
163 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
164 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
165 mpi_ct.nproc, MPI_MAX_PROCESS);
170 time_start = MPI_Wtime();
172 time_start = td_current_sec();
176 if (mpi_ct.my_rank == 0) {
179 strcpy(opt_desc, "");
180 strcpy(opt_phmfilename, "");
182 int c = getopt_long(argc, argv, "", my_options, NULL);
191 if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
192 phm2sdf_usage(argv[0]);
197 strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
198 phm = phm_create_from_file(opt_phmfilename);
200 if (mpi_ct.my_rank == 0)
201 fprintf(stderr, "Can't use phantom from file in MPI mode\n");
212 if ((opt_trace = opt_set_trace(optarg)) < 0) {
213 phm2sdf_usage(argv[0]);
218 if ((opt_filter = opt_set_filter(optarg)) < 0) {
219 phm2sdf_usage (argv[0]);
223 case O_FILTER_DOMAIN:
224 if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
225 phm2sdf_usage (argv[0]);
230 strncpy(opt_desc, optarg, sizeof(opt_desc));
233 opt_filter_bw = strtod(optarg, &endptr);
234 endstr = optarg + strlen(optarg);
235 if (endptr != endstr) {
236 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
237 phm2sdf_usage(argv[0]);
242 opt_filter_param = strtod(optarg, &endptr);
243 endstr = optarg + strlen(optarg);
244 if (endptr != endstr) {
245 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
246 phm2sdf_usage(argv[0]);
251 opt_nsample = strtol(optarg, &endptr, 10);
252 endstr = optarg + strlen(optarg);
253 if (endptr != endstr) {
254 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
255 phm2sdf_usage(argv[0]);
261 fprintf(stdout, "Version %s\n", VERSION);
263 fprintf(stderr, "Unknown version number");
267 phm2sdf_usage(argv[0]);
270 phm2sdf_usage(argv[0]);
275 if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
276 fprintf(stderr, "No phantom defined\n");
277 phm2sdf_usage(argv[0]);
281 #if HAVE_INTERACTIVE_GRAPHICS
282 if (opt_trace >= TRACE_PHM)
286 if (optind + 3 != argc) {
287 phm2sdf_usage(argv[0]);
290 opt_outfile = argv[optind];
291 opt_nx = strtol(argv[optind+1], &endptr, 10);
292 endstr = argv[optind+1] + strlen(argv[optind+1]);
293 if (endptr != endstr) {
294 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
295 phm2sdf_usage(argv[0]);
298 opt_ny = strtol(argv[optind+2], &endptr, 10);
299 endstr = argv[optind+2] + strlen(argv[optind+2]);
300 if (endptr != endstr) {
301 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
302 phm2sdf_usage(argv[0]);
306 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
307 if (opt_phmfilename[0]) {
308 strncat(str, "phantom=", sizeof(str));
309 strncat(str, opt_phmfilename, sizeof(str));
311 else if (opt_phmnum != -1) {
312 strncat(str, "phantom=", sizeof(str));
313 strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
315 else if (opt_filter != -1) {
316 strncat(str, "filter=", sizeof(str));
317 strncat(str, name_of_filter(opt_filter), sizeof(str));
318 strncat(str, " - ", sizeof(str));
319 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
322 strncat(str, ": ", sizeof(str));
323 strncat(str, opt_desc, sizeof(str));
325 strncpy(opt_desc, str, sizeof(opt_desc));
328 printf("Compile Phantom to Image\n\n");
334 mpi_t1 = MPI_Wtime();
335 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
336 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
337 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
338 MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
339 MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
340 MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
341 MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
342 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
343 MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
344 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
345 MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
348 mpi_t2 = MPI_Wtime();
349 mpi_t = mpi_t2 - mpi_t1;
350 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
351 if (mpi_ct.my_rank == 0)
352 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
355 mpi_ct_calc_work_units(opt_nx);
357 if (mpi_ct.my_rank == 0)
358 im_global = image_create (opt_outfile, opt_nx, opt_ny);
360 im_local = image_create(NULL, opt_nx, opt_ny);
362 im_global = image_create (opt_outfile, opt_nx, opt_ny);
366 phm = phm_create (opt_phmnum);
370 if (mpi_ct.my_rank == 0)
371 fprintf(stderr, "phmnum < 0\n");
377 if (phm->type == P_UNIT_PULSE) {
378 if (mpi_ct.my_rank == 0) {
379 im_global->v[opt_nx/2][opt_ny/2] = 1.;
380 im_global->calctime = 0;
382 } else if (opt_filter != -1) {
383 if (mpi_ct.my_rank == 0) {
384 image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
385 im_global->calctime = 0;
389 mpi_t1 = MPI_Wtime();
390 phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
391 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
393 mpi_t2 = MPI_Wtime();
394 mpi_t = mpi_t2 - mpi_t1;
395 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
396 if (mpi_ct.my_rank == 0)
397 printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
399 mpi_gather_image(im_global, im_local, opt_debug);
402 if (phm->type == P_UNIT_PULSE) {
403 im_global->v[opt_nx/2][opt_ny/2] = 1.;
404 im_global->calctime = 0;
405 } else if (opt_filter != -1) {
406 image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
407 im_global->calctime = 0;
409 phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
414 time_end = MPI_Wtime();
416 time_end = td_current_sec();
420 if (mpi_ct.my_rank == 0)
423 im_global->calctime = time_end - time_start;
424 strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
425 image_save (im_global);
427 fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
430 if (opt_trace >= TRACE_PHM)
435 printf ("Enter size of cell (nx, ny): ");
436 scanf ("%d %d", &nx, &ny);
437 printf ("Enter minimum and maximum densities (min, max): ");
438 scanf ("%lf %lf", &dmin, &dmax);
439 // image_paint (CRTDEV, im_global, 0, 0, nx, ny, dmin, dmax, 0);
451 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
457 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
458 for (iw = 0; iw <= end_work_unit; iw++) {
459 MPI_Send(im_local->v[iw], im_local->ny, MPI_FLOAT, 0, 0, mpi_ct.comm);
462 if (mpi_ct.my_rank == 0) {
463 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
464 end_work_unit = mpi_ct.start_work_unit[iproc]
465 + mpi_ct.local_work_units[iproc]
469 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
470 mpi_ct.start_work_unit[iproc], end_work_unit);
475 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
478 MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
488 main (const int argc, char *const argv[])
490 return (phm2sdf_main(argc, argv));