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