r41: ANSI C changes
[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.7 2000/05/08 20:02:32 kevin Exp $
6 **  $Log: phm2sdf.c,v $
7 **  Revision 1.7  2000/05/08 20:02:32  kevin
8 **  ANSI C changes
9 **
10 **  Revision 1.6  2000/05/03 08:49:50  kevin
11 **  Code cleanup
12 **
13 **  Revision 1.5  2000/04/30 19:17:54  kevin
14 **  *** empty log message ***
15 **
16 **  Revision 1.4  2000/04/30 04:06:13  kevin
17 **  Update Raysum i/o routines
18 **  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
19 **
20 **  Revision 1.3  2000/04/28 18:19:01  kevin
21 **  removed unused files
22 **
23 **  Revision 1.2  2000/04/28 13:50:45  kevin
24 **  Removed Makefile Makefile.in that are automatically generated by autoconf
25 **
26 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
27 **  Initial CVS import for first public release
28 **
29 **
30 **
31 **  This program is free software; you can redistribute it and/or modify
32 **  it under the terms of the GNU General Public License (version 2) as
33 **  published by the Free Software Foundation.
34 **
35 **  This program is distributed in the hope that it will be useful,
36 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
37 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38 **  GNU General Public License for more details.
39 **
40 **  You should have received a copy of the GNU General Public License
41 **  along with this program; if not, write to the Free Software
42 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
43 ******************************************************************************/
44 /* FILE
45  *   phm2sdf.c                  Generate a SDF image from a phantom
46  *
47  * HISTORY
48  *   1984 - Final DOS verion
49  *   1999 - First UNIX version
50  */
51
52 #include "ct.h"
53
54 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP, 
55        O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
56
57 static struct option my_options[] = 
58 {
59   {"phantom", 1, 0, O_PHANTOM},
60   {"phmfile", 1, 0, O_PHMFILE},
61   {"desc", 1, 0, O_DESC},
62   {"nsample", 1, 0, O_NSAMPLE},
63   {"filter", 1, 0, O_FILTER},
64   {"filter-domain", 1, 0, O_FILTER_DOMAIN},
65   {"filter-bw", 1, 0, O_FILTER_BW},
66   {"filter-param", 1, 0, O_FILTER_PARAM},
67   {"trace", 1, 0, O_TRACE},
68   {"verbose", 0, 0, O_VERBOSE},
69   {"debug", 0, 0, O_DEBUG},
70   {"help", 0, 0, O_HELP},
71   {"version", 0, 0, O_VERSION},
72   {0, 0, 0, 0}
73 };
74
75 void 
76 usage (const char *program)
77 {
78   fprintf(stdout,"usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program)); 
79   fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
80   fprintf(stdout,"\n");
81   fprintf(stdout,"     outfile         Name of output file for image\n");
82   fprintf(stdout,"     nx              Number of pixels X-axis\n");
83   fprintf(stdout,"     ny              Number of pixels Y-axis\n");
84   fprintf(stdout,"     --phantom       Phantom to use for projection\n");
85   fprintf(stdout,"        herman       Herman head phantom\n");
86   fprintf(stdout,"        rowland      Rowland head phantom\n");
87   fprintf(stdout,"        browland     Bordered Rowland head phantom\n");
88   fprintf(stdout,"        unitpulse    Unit pulse phantom\n");
89   fprintf(stdout,"     --phmfile       Generate Phantom from phantom file\n");
90   fprintf(stdout,"     --filter        Generate Phantom from a filter function\n");
91   fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting\n");
92   fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
93   fprintf(stdout,"       abs_cos       Abs * Cosine\n");
94   fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
95   fprintf(stdout,"       shepp         Shepp-Logan\n");
96   fprintf(stdout,"       bandlimit     Bandlimiting\n");
97   fprintf(stdout,"       sinc          Sinc\n");
98   fprintf(stdout,"       cos           Cosine\n");
99   fprintf(stdout,"       triangle      Triangle\n");
100   fprintf(stdout,"       hamming       Hamming\n");
101   fprintf(stdout,"     --filter-param  Alpha level for Hamming filter\n");
102   fprintf(stdout,"     --filter-domain Set domain of filter\n");
103   fprintf(stdout,"         spatial     Spatial domain (default)\n");
104   fprintf(stdout,"         freq        Frequency domain\n");
105   fprintf(stdout,"     --filter-bw     Filter bandwidth (default = 1)\n");
106   fprintf(stdout,"     --desc          Description of raysum\n");
107   fprintf(stdout,"     --nsample       Number of samples per axis per pixel (default = 1)\n");
108   fprintf(stdout,"     --trace         Trace level to use\n");
109   fprintf(stdout,"        none         No tracing (default)\n");
110   fprintf(stdout,"        text         Trace text level\n");
111   fprintf(stdout,"        phm          Trace phantom\n");
112   fprintf(stdout,"        rays         Trace rays\n");
113   fprintf(stdout,"        plot         Trace plot\n");
114   fprintf(stdout,"        clipping     Trace clipping\n");
115   fprintf(stdout,"     --debug         Debug mode\n");
116   fprintf(stdout,"     --verbose       Verbose mode\n");
117   fprintf(stdout,"     --version       Print version\n");
118   fprintf(stdout,"     --help          Print this help message\n");
119   exit(1);
120 }
121
122 #ifdef MPI_CT
123 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
124 #endif
125
126 int 
127 main (const int argc, char *const argv[])
128 {
129   IMAGE *im_global = NULL;
130   PHANTOM *phm = NULL;
131   int opt_nx = 0, opt_ny = 0;
132   int opt_nsample = 1;
133   int opt_phmnum = -1;
134   int opt_filter = -1;
135   int opt_filter_domain = D_SPATIAL;
136   char *opt_outfile = NULL;
137   int opt_debug = 0;
138   char str[256];
139   char opt_desc[256], opt_phmfilename[256];
140   char *endstr, *endptr;
141   double opt_filter_param = 1;
142   double opt_filter_bw = 1.;
143   int opt_trace = TRACE_NONE;
144   int opt_verbose = 0;
145   double time_start=0, time_end=0;
146 #ifdef MPI_CT
147   IMAGE *im_local = NULL;
148   int mpi_argc = argc;
149   char **mpi_argv = (char **) argv;
150   double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
151
152   MPI_Init(&mpi_argc, &mpi_argv);
153   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
154   MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
155   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
156
157   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
158     sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
159               mpi_ct.nproc, MPI_MAX_PROCESS);
160     exit(1);
161   }
162 #endif
163
164 #ifdef MPI_CT
165   time_start = MPI_Wtime();
166 #else
167   time_start = td_current_sec();
168 #endif
169   
170 #ifdef MPI_CT
171   if (mpi_ct.my_rank == 0) {
172 #endif
173
174     strcpy(opt_desc, "");
175     strcpy(opt_phmfilename, "");
176     while (1) {
177       int c = getopt_long(argc, argv, "", my_options, NULL);
178       char *endptr = NULL;
179       char *endstr;
180       
181       if (c == -1)
182         break;
183       
184       switch (c) {
185       case O_PHANTOM:
186         opt_phmnum = opt_set_phantom(optarg, argv[0]);
187         break;
188       case O_PHMFILE:
189         strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
190         phm = phm_create_from_file(opt_phmfilename);
191 #ifdef MPI_CT
192         if (mpi_ct.my_rank == 0) 
193           fprintf(stderr, "Can't use phantom from file in MPI mode\n");
194         exit(1);
195 #endif
196         break;
197       case O_VERBOSE:
198         opt_verbose = 1;
199         break;
200       case O_DEBUG:
201         opt_debug = 1;
202         break;
203       case O_TRACE:
204         opt_trace = opt_set_trace(optarg, argv[0]);
205         break;
206       case O_FILTER:
207         opt_filter = opt_set_filter(optarg, argv[0]);
208         break;
209       case O_FILTER_DOMAIN:
210         opt_filter_domain = opt_set_filter_domain(optarg, argv[0]);
211         break;
212       case O_DESC:
213         strncpy(opt_desc, optarg, sizeof(opt_desc));
214         break;
215       case O_FILTER_BW:
216         opt_filter_bw = strtod(optarg, &endptr);
217         endstr = optarg + strlen(optarg);
218         if (endptr != endstr) {
219           fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
220           usage(argv[0]);
221           exit(1);
222         }
223         break;
224       case O_FILTER_PARAM:
225         opt_filter_param = strtod(optarg, &endptr);
226         endstr = optarg + strlen(optarg);
227         if (endptr != endstr) {
228           fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
229           usage(argv[0]);
230           exit(1);
231         }
232         break;
233       case O_NSAMPLE:
234         opt_nsample = strtol(optarg, &endptr, 10);
235         endstr = optarg + strlen(optarg);
236         if (endptr != endstr) {
237           fprintf(stderr,"Error setting --nsample to %s\n", optarg);
238           usage(argv[0]);
239           exit(1);
240         }
241         break;
242         case O_VERSION:
243 #ifdef VERSION
244           fprintf(stdout, "Version %s\n", VERSION);
245 #else
246           fprintf(stderr, "Unknown version number");
247 #endif
248       case O_HELP:
249       case '?':
250         usage(argv[0]);
251         exit(0);
252       default:
253         usage(argv[0]);
254         exit(1);
255       }
256     }
257     
258     if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
259       fprintf(stderr, "No phantom defined\n");
260       usage(argv[0]);
261       exit(1);
262     }
263
264 #if HAVE_INTERACTIVE_GRAPHICS
265     if (opt_trace >= TRACE_PHM)
266       phm_show(phm);
267 #endif
268
269     if (optind + 3 != argc) {
270       usage(argv[0]);
271       exit(1);
272     }
273     opt_outfile = argv[optind];
274     opt_nx = strtol(argv[optind+1], &endptr, 10);
275     endstr = argv[optind+1] + strlen(argv[optind+1]);
276     if (endptr != endstr) {
277       fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
278       usage(argv[0]);
279       exit(1);
280     }
281     opt_ny = strtol(argv[optind+2], &endptr, 10);
282     endstr = argv[optind+2] + strlen(argv[optind+2]);
283     if (endptr != endstr) {
284       fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
285       usage(argv[0]);
286       exit(1);
287     }
288     
289     snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
290     if (opt_phmfilename[0]) {
291       strncat(str, "phantom=", sizeof(str));
292       strncat(str, opt_phmfilename, sizeof(str));
293     }
294     else if (opt_phmnum != -1) {
295       strncat(str, "phantom=", sizeof(str));
296       strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
297     }
298     else if (opt_filter != -1) {
299       strncat(str, "filter=", sizeof(str));
300       strncat(str, name_of_filter(opt_filter), sizeof(str));
301       strncat(str, " - ", sizeof(str));
302       strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
303     }
304     if (opt_desc[0]) {
305       strncat(str, ": ", sizeof(str));
306       strncat(str, opt_desc, sizeof(str));
307     }
308     strncpy(opt_desc, str, sizeof(opt_desc));
309     
310     if (opt_verbose)
311       printf("Compile Phantom to Image\n\n");
312 #ifdef MPI_CT
313   }
314 #endif
315
316 #ifdef MPI_CT
317   mpi_t1 = MPI_Wtime();
318   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
319   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
320   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
321   MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
322   MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
323   MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
324   MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
325   MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
326   MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
327   MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
328   MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
329
330   if (opt_verbose) {
331     mpi_t2 = MPI_Wtime();
332     mpi_t = mpi_t2 - mpi_t1;
333     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
334     if (mpi_ct.my_rank == 0)
335       printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
336   }
337
338   mpi_ct_calc_work_units(opt_nx);
339
340   if (mpi_ct.my_rank == 0)
341     im_global = image_create (opt_outfile, opt_nx, opt_ny);
342
343   im_local = image_create(NULL, opt_nx, opt_ny);
344 #else
345   im_global = image_create (opt_outfile, opt_nx, opt_ny);
346 #endif
347   
348   if (opt_phmnum > 0)
349     phm = phm_create (opt_phmnum);
350 #ifdef MPI_CT
351   else {
352     if (mpi_ct.my_rank == 0)
353       fprintf(stderr, "phmnum < 0\n");
354     exit(1);
355   }
356 #endif
357
358 #ifdef MPI_CT
359   if (phm->type == P_UNIT_PULSE) {
360     if (mpi_ct.my_rank == 0) {
361       im_global->v[opt_nx/2][opt_ny/2] = 1.;
362       im_global->calctime = 0;
363     }
364   } else if (opt_filter != -1) {
365     if (mpi_ct.my_rank == 0) {
366       image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
367       im_global->calctime = 0;
368     }
369   } else {
370     if (opt_verbose)
371       mpi_t1 = MPI_Wtime();
372     phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
373                   mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
374     if (opt_verbose) {
375       mpi_t2 = MPI_Wtime();
376       mpi_t = mpi_t2 - mpi_t1;
377       MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
378       if (mpi_ct.my_rank == 0)
379         printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
380     }
381     mpi_gather_image(im_global, im_local, opt_debug);
382   }
383 #else
384   if (phm->type == P_UNIT_PULSE) {
385     im_global->v[opt_nx/2][opt_ny/2] = 1.;
386     im_global->calctime = 0;
387   } else if (opt_filter != -1) {
388     image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
389     im_global->calctime = 0;
390   } else {
391     phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
392   }
393 #endif
394
395 #ifdef MPI_CT
396   time_end = MPI_Wtime();
397   if (mpi_ct.my_rank == 0) {
398     im_global->calctime = time_end - time_start;
399     strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
400   }
401
402   if (mpi_ct.my_rank == 0) {
403     image_save (im_global);
404     if (opt_verbose)
405       fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
406   }
407 #else
408   time_end = td_current_sec();
409   im_global->calctime = time_end - time_start;
410   strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
411   image_save (im_global);
412
413   if (opt_verbose)
414     fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
415 #endif
416
417   if (opt_trace >= TRACE_PHM)
418     {
419       double dmin, dmax;
420       int nx, ny;
421
422       printf ("Enter size of cell (nx, ny): ");
423       scanf ("%d %d", &nx, &ny);
424       printf ("Enter minimum and maximum densities (min, max): ");
425       scanf ("%lf %lf", &dmin, &dmax);
426       //      image_paint (CRTDEV, im_global, 0, 0, nx, ny, dmin, dmax, 0);
427
428     }
429
430   return (0);
431 }
432
433
434
435 #ifdef MPI_CT
436 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
437 {
438   int iproc;
439   int end_work_unit;
440   int iw;
441
442   end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
443   for (iw = 0; iw <= end_work_unit; iw++) {
444     MPI_Send(im_local->v[iw], im_local->ny, MPI_FLOAT, 0, 0, mpi_ct.comm);
445   }
446
447   if (mpi_ct.my_rank == 0) {
448     for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
449       end_work_unit = mpi_ct.start_work_unit[iproc] 
450         + mpi_ct.local_work_units[iproc] 
451         - 1;
452
453       if (opt_debug) {
454         fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
455                 mpi_ct.start_work_unit[iproc], end_work_unit);
456         fflush(stdout);
457       }
458
459
460       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
461         MPI_Status status;
462
463         MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
464       }
465     }
466   }
467
468 }
469 #endif