1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2sdf.c,v 1.2 2000/04/28 13:50:45 kevin Exp $
7 ** Revision 1.2 2000/04/28 13:50:45 kevin
8 ** Removed Makefile Makefile.in that are automatically generated by autoconf
10 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
11 ** Initial CVS import for first public release
15 ** This program is free software; you can redistribute it and/or modify
16 ** it under the terms of the GNU General Public License (version 2) as
17 ** published by the Free Software Foundation.
19 ** This program is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ** GNU General Public License for more details.
24 ** You should have received a copy of the GNU General Public License
25 ** along with this program; if not, write to the Free Software
26 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ******************************************************************************/
29 * phm2sdf.c Generate a SDF image from a phantom
32 * 1984 - Final DOS verion
33 * 1999 - First UNIX version
46 #define O_FILTER_DOMAIN 9
47 #define O_FILTER_BW 10
48 #define O_FILTER_PARAM 11
52 static struct option my_options[] =
54 {"phantom", 1, 0, O_PHANTOM},
55 {"picfile", 1, 0, O_PICFILE},
56 {"desc", 1, 0, O_DESC},
57 {"nsample", 1, 0, O_NSAMPLE},
58 {"filter", 1, 0, O_FILTER},
59 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
60 {"filter-bw", 1, 0, O_FILTER_BW},
61 {"filter-param", 1, 0, O_FILTER_PARAM},
62 {"trace", 1, 0, O_TRACE},
63 {"verbose", 0, 0, O_VERBOSE},
64 {"debug", 0, 0, O_DEBUG},
65 {"help", 0, 0, O_HELP},
66 {"version", 0, 0, O_VERSION},
71 usage (const char *program)
73 fprintf(stdout,"usage: %s outfile nx ny [--phantom phantom-name] [--picfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
74 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --picfile\n");
76 fprintf(stdout," outfile Name of output file for image\n");
77 fprintf(stdout," nx Number of pixels X-axis\n");
78 fprintf(stdout," ny Number of pixels Y-axis\n");
79 fprintf(stdout," --phantom Phantom to use for projection\n");
80 fprintf(stdout," herman Herman head phantom\n");
81 fprintf(stdout," rowland Rowland head phantom\n");
82 fprintf(stdout," browland Bordered Rowland head phantom\n");
83 fprintf(stdout," unitpulse Unit pulse phantom\n");
84 fprintf(stdout," --picfile Generate Phantom from picture file\n");
85 fprintf(stdout," --filter Generate Phantom from a filter function\n");
86 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
87 fprintf(stdout," abs_sinc Abs * Sinc\n");
88 fprintf(stdout," abs_cos Abs * Cosine\n");
89 fprintf(stdout," abs_hamming Abs * Hamming\n");
90 fprintf(stdout," shepp Shepp-Logan\n");
91 fprintf(stdout," bandlimit Bandlimiting\n");
92 fprintf(stdout," sinc Sinc\n");
93 fprintf(stdout," cos Cosine\n");
94 fprintf(stdout," triangle Triangle\n");
95 fprintf(stdout," hamming Hamming\n");
96 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
97 fprintf(stdout," --filter-domain Set domain of filter\n");
98 fprintf(stdout," spatial Spatial domain (default)\n");
99 fprintf(stdout," freq Frequency domain\n");
100 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
101 fprintf(stdout," --desc Description of raysum\n");
102 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
103 fprintf(stdout," --trace Trace level to use\n");
104 fprintf(stdout," none No tracing (default)\n");
105 fprintf(stdout," text Trace text level\n");
106 fprintf(stdout," pic Trace picture\n");
107 fprintf(stdout," rays Trace rays\n");
108 fprintf(stdout," plot Trace plot\n");
109 fprintf(stdout," clipping Trace clipping\n");
110 fprintf(stdout," --debug Debug mode\n");
111 fprintf(stdout," --verbose Verbose mode\n");
112 fprintf(stdout," --version Print version\n");
113 fprintf(stdout," --help Print this help message\n");
118 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
122 main (const int argc, char *const argv[])
124 IMAGE *im_global = NULL;
126 int opt_nx = 0, opt_ny = 0;
130 int opt_filter_domain = D_SPATIAL;
134 char opt_desc[256], opt_picfilename[256];
135 char *endstr, *endptr;
136 double opt_filter_param;
137 double opt_filter_bw = 1.;
138 int opt_trace = TRACE_NONE;
140 double time_start, time_end;
142 IMAGE *im_local = NULL;
144 char **mpi_argv = (char **) argv;
145 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
147 MPI_Init(&mpi_argc, &mpi_argv);
148 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
149 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
150 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
152 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
153 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
154 mpi_ct.nproc, MPI_MAX_PROCESS);
160 time_start = MPI_Wtime();
162 time_start = td_current_sec();
166 if (mpi_ct.my_rank == 0) {
169 strcpy(opt_desc, "");
170 strcpy(opt_picfilename, "");
172 int c = getopt_long(argc, argv, "", my_options, NULL);
181 opt_picnum = opt_set_picture(optarg, argv[0]);
184 strncpy(opt_picfilename, optarg, sizeof(opt_picfilename));
185 pic = create_pic_from_file(opt_picfilename);
187 if (mpi_ct.my_rank == 0)
188 fprintf(stderr, "Can't use picture from file in MPI mode\n");
199 opt_trace = opt_set_trace(optarg, argv[0]);
202 opt_filter = opt_set_filter(optarg, argv[0]);
204 case O_FILTER_DOMAIN:
205 opt_filter_domain = opt_set_filter_domain(optarg, argv[0]);
208 strncpy(opt_desc, optarg, sizeof(opt_desc));
211 opt_filter_bw = strtod(optarg, &endptr);
212 endstr = optarg + strlen(optarg);
213 if (endptr != endstr) {
214 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
220 opt_filter_param = strtod(optarg, &endptr);
221 endstr = optarg + strlen(optarg);
222 if (endptr != endstr) {
223 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
229 opt_nsample = strtol(optarg, &endptr, 10);
230 endstr = optarg + strlen(optarg);
231 if (endptr != endstr) {
232 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
239 fprintf(stdout, "Version %s\n", VERSION);
241 fprintf(stderr, "Unknown version number");
253 if (pic == NULL && opt_picnum == -1 && opt_filter == -1) {
254 fprintf(stderr, "No phantom defined\n");
259 if (opt_trace >= TRACE_PIC)
262 if (optind + 3 != argc) {
266 opt_outfile = argv[optind];
267 opt_nx = strtol(argv[optind+1], &endptr, 10);
268 endstr = argv[optind+1] + strlen(argv[optind+1]);
269 if (endptr != endstr) {
270 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
274 opt_ny = strtol(argv[optind+2], &endptr, 10);
275 endstr = argv[optind+2] + strlen(argv[optind+2]);
276 if (endptr != endstr) {
277 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
282 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
283 if (opt_picfilename[0]) {
284 strncat(str, "phantom=", sizeof(str));
285 strncat(str, opt_picfilename, sizeof(str));
287 else if (opt_picnum != -1) {
288 strncat(str, "phantom=", sizeof(str));
289 strncat(str, name_of_picture(opt_picnum), sizeof(str));
291 else if (opt_filter != -1) {
292 strncat(str, "filter=", sizeof(str));
293 strncat(str, name_of_filter(opt_filter), sizeof(str));
294 strncat(str, " - ", sizeof(str));
295 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
298 strncat(str, ": ", sizeof(str));
299 strncat(str, opt_desc, sizeof(str));
301 strncpy(opt_desc, str, sizeof(opt_desc));
304 printf("Compile Phantom to Image\n\n");
310 mpi_t1 = MPI_Wtime();
311 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
312 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
313 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
314 MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
315 MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
316 MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
317 MPI_Bcast(&opt_picnum, 1, MPI_INT, 0, mpi_ct.comm);
318 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
319 MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
320 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
321 MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
324 mpi_t2 = MPI_Wtime();
325 mpi_t = mpi_t2 - mpi_t1;
326 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
327 if (mpi_ct.my_rank == 0)
328 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
331 mpi_ct_calc_work_units(opt_nx);
333 if (mpi_ct.my_rank == 0)
334 im_global = image_create (opt_outfile, opt_nx, opt_ny);
336 im_local = image_create(NULL, opt_nx, opt_ny);
338 im_global = image_create (opt_outfile, opt_nx, opt_ny);
342 pic = create_pic(opt_picnum);
345 if (mpi_ct.my_rank == 0)
346 fprintf(stderr, "picnum < 0\n");
352 if (pic->type == P_UNIT_PULSE) {
353 if (mpi_ct.my_rank == 0) {
354 im_global->v[opt_nx/2][opt_ny/2] = 1.;
355 im_global->calctime = 0;
357 } else if (opt_filter != -1) {
358 if (mpi_ct.my_rank == 0) {
359 image_filter_init (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
360 im_global->calctime = 0;
364 mpi_t1 = MPI_Wtime();
365 pic_to_image (pic, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
366 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
368 mpi_t2 = MPI_Wtime();
369 mpi_t = mpi_t2 - mpi_t1;
370 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
371 if (mpi_ct.my_rank == 0)
372 printf("Time to compile pic = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
374 mpi_gather_image(im_global, im_local, opt_debug);
377 if (pic->type == P_UNIT_PULSE) {
378 im_global->v[opt_nx/2][opt_ny/2] = 1.;
379 im_global->calctime = 0;
380 } else if (opt_filter != -1) {
381 image_filter_init (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
382 im_global->calctime = 0;
384 pic_to_image (pic, im_global, 0, opt_nx, opt_nsample, opt_trace);
389 time_end = MPI_Wtime();
390 if (mpi_ct.my_rank == 0) {
391 im_global->calctime = time_end - time_start;
392 strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
395 if (mpi_ct.my_rank == 0) {
396 image_save (im_global);
398 fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
401 time_end = td_current_sec();
402 im_global->calctime = time_end - time_start;
403 strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
404 image_save (im_global);
406 if (opt_trace >= TRACE_PIC)
412 fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
415 if (opt_trace >= TRACE_PIC)
420 printf ("Enter size of cell (nx, ny): ");
421 scanf ("%d %d", &nx, &ny);
422 printf ("Enter minimum and maximum densities (min, max): ");
423 scanf ("%lf %lf", &dmin, &dmax);
424 // image_paint (CRTDEV, im_global, 0, 0, nx, ny, dmin, dmax, 0);
427 crt_set_palette (0, crt_calc_color (0., 0., 2.));
428 crt_put_stra ("Finished", 8 + C_WHITE);
438 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
444 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
445 for (iw = 0; iw <= end_work_unit; iw++) {
446 MPI_Send(im_local->v[iw], im_local->ny, MPI_FLOAT, 0, 0, mpi_ct.comm);
449 if (mpi_ct.my_rank == 0) {
450 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
451 end_work_unit = mpi_ct.start_work_unit[iproc]
452 + mpi_ct.local_work_units[iproc]
456 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
457 mpi_ct.start_work_unit[iproc], end_work_unit);
462 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
465 MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);