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