r56: Improved option processing
[ctsim.git] / src / phm2sdf.c
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: phm2sdf.c,v 1.9 2000/05/16 04:33:59 kevin Exp $
6 **  $Log: phm2sdf.c,v $
7 **  Revision 1.9  2000/05/16 04:33:59  kevin
8 **  Improved option processing
9 **
10 **  Revision 1.8  2000/05/11 01:06:30  kevin
11 **  Changed sprintf to snprintf
12 **
13 **  Revision 1.7  2000/05/08 20:02:32  kevin
14 **  ANSI C changes
15 **
16 **  Revision 1.6  2000/05/03 08:49:50  kevin
17 **  Code cleanup
18 **
19 **  Revision 1.5  2000/04/30 19:17:54  kevin
20 **  *** empty log message ***
21 **
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
25 **
26 **  Revision 1.3  2000/04/28 18:19:01  kevin
27 **  removed unused files
28 **
29 **  Revision 1.2  2000/04/28 13:50:45  kevin
30 **  Removed Makefile Makefile.in that are automatically generated by autoconf
31 **
32 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
33 **  Initial CVS import for first public release
34 **
35 **
36 **
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.
40 **
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.
45 **
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 ******************************************************************************/
50
51 /* FILE
52  *   phm2sdf.c                  Generate a SDF image from a phantom
53  *
54  * HISTORY
55  *   1984 - Final DOS verion
56  *   1999 - First UNIX version
57  */
58
59 #include "ct.h"
60
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 };
63
64 static struct option my_options[] = 
65 {
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},
79   {0, 0, 0, 0}
80 };
81
82 void 
83 phm2sdf_usage (const char *program)
84 {
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");
87   fprintf(stdout,"\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");
126 }
127
128 #ifdef MPI_CT
129 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
130 #endif
131
132 int 
133 phm2sdf_main (const int argc, char *const argv[])
134 {
135   IMAGE *im_global = NULL;
136   PHANTOM *phm = NULL;
137   int opt_nx = 0, opt_ny = 0;
138   int opt_nsample = 1;
139   int opt_phmnum = -1;
140   int opt_filter = -1;
141   int opt_filter_domain = D_SPATIAL;
142   char *opt_outfile = NULL;
143   int opt_debug = 0;
144   char str[256];
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;
150   int opt_verbose = 0;
151   double time_start=0, time_end=0;
152 #ifdef MPI_CT
153   IMAGE *im_local = NULL;
154   int mpi_argc = argc;
155   char **mpi_argv = (char **) argv;
156   double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
157
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);
162
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);
166   }
167 #endif
168
169 #ifdef MPI_CT
170   time_start = MPI_Wtime();
171 #else
172   time_start = td_current_sec();
173 #endif
174   
175 #ifdef MPI_CT
176   if (mpi_ct.my_rank == 0) {
177 #endif
178
179     strcpy(opt_desc, "");
180     strcpy(opt_phmfilename, "");
181     while (1) {
182       int c = getopt_long(argc, argv, "", my_options, NULL);
183       char *endptr = NULL;
184       char *endstr;
185       
186       if (c == -1)
187         break;
188       
189       switch (c) {
190       case O_PHANTOM:
191         if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
192           phm2sdf_usage(argv[0]);
193           return (1);
194         }
195         break;
196       case O_PHMFILE:
197         strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
198         phm = phm_create_from_file(opt_phmfilename);
199 #ifdef MPI_CT
200         if (mpi_ct.my_rank == 0) 
201           fprintf(stderr, "Can't use phantom from file in MPI mode\n");
202         return (1);
203 #endif
204         break;
205       case O_VERBOSE:
206         opt_verbose = 1;
207         break;
208       case O_DEBUG:
209         opt_debug = 1;
210         break;
211       case O_TRACE:
212         if ((opt_trace = opt_set_trace(optarg)) < 0) {
213           phm2sdf_usage(argv[0]);
214           return (1);
215         }
216         break;
217       case O_FILTER:
218         if ((opt_filter = opt_set_filter(optarg)) < 0) {
219           phm2sdf_usage (argv[0]);
220           return (1);
221         }
222         break;
223       case O_FILTER_DOMAIN:
224         if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
225           phm2sdf_usage (argv[0]);
226           return (1);
227         }
228         break;
229       case O_DESC:
230         strncpy(opt_desc, optarg, sizeof(opt_desc));
231         break;
232       case O_FILTER_BW:
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]);
238           return (1);
239         }
240         break;
241       case O_FILTER_PARAM:
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]);
247           return (1);
248         }
249         break;
250       case O_NSAMPLE:
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]);
256           return (1);
257         }
258         break;
259         case O_VERSION:
260 #ifdef VERSION
261           fprintf(stdout, "Version %s\n", VERSION);
262 #else
263           fprintf(stderr, "Unknown version number");
264 #endif
265       case O_HELP:
266       case '?':
267         phm2sdf_usage(argv[0]);
268         return (0);
269       default:
270         phm2sdf_usage(argv[0]);
271         return (1);
272       }
273     }
274     
275     if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
276       fprintf(stderr, "No phantom defined\n");
277       phm2sdf_usage(argv[0]);
278       return (1);
279     }
280
281 #if HAVE_INTERACTIVE_GRAPHICS
282     if (opt_trace >= TRACE_PHM)
283       phm_show(phm);
284 #endif
285
286     if (optind + 3 != argc) {
287       phm2sdf_usage(argv[0]);
288       return (1);
289     }
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]);
296       return (1);
297     }
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]);
303       return (1);
304     }
305     
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));
310     }
311     else if (opt_phmnum != -1) {
312       strncat(str, "phantom=", sizeof(str));
313       strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
314     }
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));
320     }
321     if (opt_desc[0]) {
322       strncat(str, ": ", sizeof(str));
323       strncat(str, opt_desc, sizeof(str));
324     }
325     strncpy(opt_desc, str, sizeof(opt_desc));
326     
327     if (opt_verbose)
328       printf("Compile Phantom to Image\n\n");
329 #ifdef MPI_CT
330   }
331 #endif
332
333 #ifdef MPI_CT
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);
346
347   if (opt_verbose) {
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);
353   }
354
355   mpi_ct_calc_work_units(opt_nx);
356
357   if (mpi_ct.my_rank == 0)
358     im_global = image_create (opt_outfile, opt_nx, opt_ny);
359
360   im_local = image_create(NULL, opt_nx, opt_ny);
361 #else
362   im_global = image_create (opt_outfile, opt_nx, opt_ny);
363 #endif
364
365   if (opt_phmnum >= 0)
366       phm = phm_create (opt_phmnum);
367
368 #ifdef MPI_CT
369   else {
370     if (mpi_ct.my_rank == 0)
371       fprintf(stderr, "phmnum < 0\n");
372     exit(1);
373   }
374 #endif
375
376 #ifdef MPI_CT
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;
381     }
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;
386     }
387   } else {
388     if (opt_verbose)
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);
392     if (opt_verbose) {
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);
398     }
399     mpi_gather_image(im_global, im_local, opt_debug);
400   }
401 #else
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;
408   } else {
409     phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
410   }
411 #endif
412
413 #ifdef MPI_CT
414   time_end = MPI_Wtime();
415 #else
416   time_end = td_current_sec();
417 #endif
418
419 #ifdef MPI_CT
420   if (mpi_ct.my_rank == 0) 
421 #endif
422   {
423     im_global->calctime = time_end - time_start;
424     strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
425     image_save (im_global);
426     if (opt_verbose)
427       fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
428   }
429
430   if (opt_trace >= TRACE_PHM)
431     {
432       double dmin, dmax;
433       int nx, ny;
434
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);
440
441     }
442
443   phm_free (phm);
444
445   return (0);
446 }
447
448
449
450 #ifdef MPI_CT
451 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
452 {
453   int iproc;
454   int end_work_unit;
455   int iw;
456
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);
460   }
461
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] 
466         - 1;
467
468       if (opt_debug) {
469         fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
470                 mpi_ct.start_work_unit[iproc], end_work_unit);
471         fflush(stdout);
472       }
473
474
475       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
476         MPI_Status status;
477
478         MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
479       }
480     }
481   }
482
483 }
484 #endif
485
486 #ifndef NO_MAIN
487 int 
488 main (const int argc, char *const argv[])
489 {
490   return (phm2sdf_main(argc, argv));
491 }
492 #endif