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