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