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