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