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