r98: finished object-orient convern of Scanner & Phantom
[ctsim.git] / src / phm2if.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          phm2if.cpp
5 **   Purpose:       Convert an phantom object to an image file
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: phm2if.cpp,v 1.10 2000/06/18 10:27:11 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29 #include "timer.h"
30
31
32 enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP, 
33        O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
34
35 static struct option my_options[] = 
36 {
37   {"phantom", 1, 0, O_PHANTOM},
38   {"phmfile", 1, 0, O_PHMFILE},
39   {"desc", 1, 0, O_DESC},
40   {"nsample", 1, 0, O_NSAMPLE},
41   {"filter", 1, 0, O_FILTER},
42   {"filter-domain", 1, 0, O_FILTER_DOMAIN},
43   {"filter-bw", 1, 0, O_FILTER_BW},
44   {"filter-param", 1, 0, O_FILTER_PARAM},
45   {"trace", 1, 0, O_TRACE},
46   {"verbose", 0, 0, O_VERBOSE},
47   {"debug", 0, 0, O_DEBUG},
48   {"help", 0, 0, O_HELP},
49   {"version", 0, 0, O_VERSION},
50   {0, 0, 0, 0}
51 };
52
53 void 
54 phm2if_usage (const char *program)
55 {
56   cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]" << endl;
57   cout << "Generate phantom image from a predefined --phantom or a --phmfile" << endl;
58   cout << endl;
59   cout << "     outfile         Name of output file for image" << endl;
60   cout << "     nx              Number of pixels X-axis" << endl;
61   cout << "     ny              Number of pixels Y-axis" << endl;
62   cout << "     --phantom       Phantom to use for projection" << endl;
63   cout << "        herman       Herman head phantom" << endl;
64   cout << "        rowland      Rowland head phantom" << endl;
65   cout << "        browland     Bordered Rowland head phantom" << endl;
66   cout << "        unitpulse    Unit pulse phantom" << endl;
67   cout << "     --phmfile       Generate Phantom from phantom file" << endl;
68   cout << "     --filter        Generate Phantom from a filter function" << endl;
69   cout << "       abs_bandlimit Abs * Bandlimiting" << endl;
70   cout << "       abs_sinc      Abs * Sinc" << endl;
71   cout << "       abs_cos       Abs * Cosine" << endl;
72   cout << "       abs_hamming   Abs * Hamming" << endl;
73   cout << "       shepp         Shepp-Logan" << endl;
74   cout << "       bandlimit     Bandlimiting" << endl;
75   cout << "       sinc          Sinc" << endl;
76   cout << "       cos           Cosine" << endl;
77   cout << "       triangle      Triangle" << endl;
78   cout << "       hamming       Hamming" << endl;
79   cout << "     --filter-param  Alpha level for Hamming filter" << endl;
80   cout << "     --filter-domain Set domain of filter" << endl;
81   cout << "         spatial     Spatial domain (default)" << endl;
82   cout << "         freq        Frequency domain" << endl;
83   cout << "     --filter-bw     Filter bandwidth (default = 1)" << endl;
84   cout << "     --desc          Description of raysum" << endl;
85   cout << "     --nsample       Number of samples per axis per pixel (default = 1)" << endl;
86   cout << "     --trace         Trace level to use" << endl;
87   cout << "        none         No tracing (default)" << endl;
88   cout << "        text         Trace text level" << endl;
89   cout << "        phm          Trace phantom" << endl;
90   cout << "        rays         Trace rays" << endl;
91   cout << "        plot         Trace plot" << endl;
92   cout << "        clipping     Trace clipping" << endl;
93   cout << "     --debug         Debug mode" << endl;
94   cout << "     --verbose       Verbose mode" << endl;
95   cout << "     --version       Print version" << endl;
96   cout << "     --help          Print this help message" << endl;
97 }
98
99 #ifdef HAVE_MPI
100 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug);
101 #endif
102
103 int 
104 phm2if_main (int argc, char* argv[])
105 {
106   ImageFile* imGlobal = NULL;
107   Phantom phm;
108   int opt_nx = 0, opt_ny = 0;
109   int opt_nsample = 1;
110   int opt_phmnum = -1;
111   FilterType opt_filter = static_cast<FilterType>(-1);
112   DomainType opt_filter_domain = D_SPATIAL;
113   char *opt_outfile = NULL;
114   int opt_debug = 0;
115   string opt_desc;
116   string opt_phmfilename;
117   char *endstr, *endptr;
118   double opt_filter_param = 1;
119   double opt_filter_bw = 1.;
120   int opt_trace = TRACE_NONE;
121   bool opt_verbose = false;
122 #ifdef HAVE_MPI
123   ImageFile* imLocal = NULL;
124   MPIWorld mpiWorld (argc, argv);
125 #endif
126
127   Timer timerProgram;
128   
129 #ifdef HAVE_MPI
130   if (mpiWorld.getRank() == 0) {
131 #endif
132     while (1) {
133       int c = getopt_long(argc, argv, "", my_options, NULL);
134       char *endptr = NULL;
135       char *endstr;
136       
137       if (c == -1)
138         break;
139       
140       switch (c) {
141       case O_PHANTOM:
142         if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
143           phm2if_usage(argv[0]);
144           return (1);
145         }
146         break;
147       case O_PHMFILE:
148         opt_phmfilename = optarg;
149         phm.createFromFile(opt_phmfilename.c_str());
150 #ifdef HAVE_MPI
151         if (mpiWorld.getRank() == 0) 
152           cerr << "Can't use phantom from file in MPI mode" << endl;
153         return (1);
154 #endif
155         break;
156       case O_VERBOSE:
157         opt_verbose = true;
158         break;
159       case O_DEBUG:
160         opt_debug = 1;
161         break;
162       case O_TRACE:
163         if ((opt_trace = opt_set_trace(optarg)) < 0) {
164           phm2if_usage(argv[0]);
165           return (1);
166         }
167         break;
168       case O_FILTER:
169         if ((opt_filter = opt_set_filter(optarg)) < 0) {
170           phm2if_usage (argv[0]);
171           return (1);
172         }
173         break;
174       case O_FILTER_DOMAIN:
175         if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
176           phm2if_usage (argv[0]);
177           return (1);
178         }
179         break;
180       case O_DESC:
181         opt_desc =  optarg;
182         break;
183       case O_FILTER_BW:
184         opt_filter_bw = strtod(optarg, &endptr);
185         endstr = optarg + strlen(optarg);
186         if (endptr != endstr) {
187           sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
188           phm2if_usage(argv[0]);
189           return (1);
190         }
191         break;
192       case O_FILTER_PARAM:
193         opt_filter_param = strtod(optarg, &endptr);
194         endstr = optarg + strlen(optarg);
195         if (endptr != endstr) {
196           sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
197           phm2if_usage(argv[0]);
198           return (1);
199         }
200         break;
201       case O_NSAMPLE:
202         opt_nsample = strtol(optarg, &endptr, 10);
203         endstr = optarg + strlen(optarg);
204         if (endptr != endstr) {
205           sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
206           phm2if_usage(argv[0]);
207           return (1);
208         }
209         break;
210         case O_VERSION:
211 #ifdef VERSION
212           cout <<  "Version " << VERSION << endl;
213 #else
214           cerr << "Unknown version number" << endl;
215 #endif
216       case O_HELP:
217       case '?':
218         phm2if_usage(argv[0]);
219         return (0);
220       default:
221         phm2if_usage(argv[0]);
222         return (1);
223       }
224     }
225     
226     if (phm.nPElem() == 0 && opt_phmnum == -1 && opt_filter == -1) {
227       cerr << "No phantom defined" << endl;
228       phm2if_usage(argv[0]);
229       return (1);
230     }
231
232     if (optind + 3 != argc) {
233       phm2if_usage(argv[0]);
234       return (1);
235     }
236     opt_outfile = argv[optind];
237     opt_nx = strtol(argv[optind+1], &endptr, 10);
238     endstr = argv[optind+1] + strlen(argv[optind+1]);
239     if (endptr != endstr) {
240       sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
241       phm2if_usage(argv[0]);
242       return (1);
243     }
244     opt_ny = strtol(argv[optind+2], &endptr, 10);
245     endstr = argv[optind+2] + strlen(argv[optind+2]);
246     if (endptr != endstr) {
247       sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
248       phm2if_usage(argv[0]);
249       return (1);
250     }
251     
252     ostringstream oss;
253     oss << "nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
254     if (opt_phmfilename.length())
255       oss << "phantom=" << opt_phmfilename;
256     else if (opt_phmnum != -1)
257       oss << "phantom=" << name_of_phantom(opt_phmnum);
258     else if (opt_filter != -1) {
259       oss << "filter=" << name_of_filter(opt_filter);
260       oss << " - " << name_of_filter_domain(opt_filter_domain);
261     }
262     if (opt_desc.length())
263       oss << ": " << opt_desc;
264     opt_desc = oss.str();
265     
266     if (opt_verbose)
267       cout << "Rasterize Phantom to Image" << endl << endl;
268 #ifdef HAVE_MPI
269   }
270 #endif
271
272 #ifdef HAVE_MPI
273   TimerMPI timerBcast (mpiWorld.getComm());
274   mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
275   mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
276   mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
277   mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
278   mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
279   mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
280   mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0);
281   mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
282   mpiWorld.getComm().Bcast (&opt_filter_domain, 1, MPI::INT, 0);
283   mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
284   mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
285
286   if (opt_verbose)
287     timerBcast.timerEndAndReport ("Time to broadcast variables");
288
289   mpiWorld.setTotalWorkUnits (opt_nx);
290
291   if (mpiWorld.getRank() == 0) {
292     imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
293     imGlobal->fileCreate();
294   }
295   imLocal = new ImageFile (opt_nx, opt_ny);
296 #else
297   imGlobal = new ImageFile (opt_outfile, opt_nx, opt_ny);
298   imGlobal->fileCreate ();
299 #endif
300
301   if (opt_phmnum >= 0)
302       phm.create (opt_phmnum);
303
304 #ifdef HAVE_MPI
305   else {
306     if (mpiWorld.getRank() == 0)
307       cerr << "phmnum < 0" << endl;
308     exit(1);
309   }
310 #endif
311
312   ImageFileArray v;
313 #ifdef HAVE_MPI
314   if (mpiWorld.getRank() == 0)
315     v = imGlobal->getArray ();
316
317   if (phm.getComposition() == P_UNIT_PULSE) {
318     if (mpiWorld.getRank() == 0) {
319       v[opt_nx/2][opt_ny/2] = 1.;
320     }
321   } else if (opt_filter != -1) {
322     if (mpiWorld.getRank() == 0) {
323       image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
324     }
325   } else {
326     TimerMPI timerRasterize (mpiWorld.getComm());
327     phm_to_imagefile (phm, *imLocal, mpiWorld.getMyStartWorkUnit(),  mpiWorld.getMyLocalWorkUnits(), opt_nsample, opt_trace);
328     if (opt_verbose)
329       timerRasterize.timerEndAndReport ("Time to rasterize phantom");
330
331     TimerMPI timerGather (mpiWorld.getComm());
332     mpi_gather_image (mpiWorld, imGlobal, imLocal, opt_debug);
333     if (opt_verbose)
334       timerGather.timerEndAndReport ("Time to gather image");
335   }
336 #else
337   v = imGlobal->getArray ();
338   if (phm.getComposition() == P_UNIT_PULSE) {
339     v[opt_nx/2][opt_ny/2] = 1.;
340   } else if (opt_filter != -1) {
341     image_filter_response (*imGlobal, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
342   } else {
343 #if HAVE_SGP
344       if (opt_trace >= TRACE_PHM)
345         phm.show();
346 #endif
347       phm_to_imagefile (phm, *imGlobal, 0, opt_nx, opt_nsample, opt_trace);
348   }
349 #endif
350
351 #ifdef HAVE_MPI
352   if (mpiWorld.getRank() == 0) 
353 #endif
354   {
355     imGlobal->arrayDataWrite ();
356     double calctime = timerProgram.timerEnd ();
357     imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, opt_desc.c_str(), calctime);
358     imGlobal->fileClose ();
359     if (opt_verbose)
360       cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
361
362     if (opt_trace >= TRACE_PHM) {
363       double dmin, dmax;
364       int nscale;
365       
366       printf ("Enter display size scale (nominal = 1): ");
367       scanf ("%d", &nscale);
368       printf ("Enter minimum and maximum densities (min, max): ");
369       scanf ("%lf %lf", &dmin, &dmax);
370       image_display_scale (*imGlobal, nscale, dmin, dmax);
371     }
372   }
373
374   return (0);
375 }
376
377
378
379 #ifdef HAVE_MPI
380 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* imGlobal, ImageFile* imLocal, const int opt_debug)
381 {
382   ImageFileArray vLocal = imLocal->getArray();
383   ImageFileArray vGlobal = NULL;
384   int nyLocal = imLocal->ny();
385
386   if (mpiWorld.getRank() == 0)
387     vGlobal = imGlobal->getArray();
388   
389   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
390     mpiWorld.getComm().Send(vLocal[iw], nyLocal, imLocal->getMPIDataType(), 0, 0);
391
392   if (mpiWorld.getRank() == 0) {
393     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
394       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
395         MPI::Status status;
396         mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, imLocal->getMPIDataType(), iProc, 0, status);
397       }
398     }
399   }
400
401 }
402 #endif
403
404 #ifndef NO_MAIN
405 int 
406 main (int argc, char* argv[])
407 {
408   return (phm2if_main(argc, argv));
409 }
410 #endif