r659: *** empty log message ***
[ctsim.git] / tools / 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.24 2001/04/02 03:49:52 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_VIEW_RATIO, 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   {"view-ratio", 1, 0, O_VIEW_RATIO},
47   {"verbose", 0, 0, O_VERBOSE},
48   {"debug", 0, 0, O_DEBUG},
49   {"help", 0, 0, O_HELP},
50   {"version", 0, 0, O_VERSION},
51   {0, 0, 0, 0}
52 };
53
54 static const char* g_szIdStr = "$Id: phm2if.cpp,v 1.24 2001/04/02 03:49:52 kevin Exp $";
55
56 void 
57 phm2if_usage (const char *program)
58 {
59   std::cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n";
60   std::cout << "Generate phantom image from a predefined --phantom or a --phmfile\n";
61   std::cout << std::endl;
62   std::cout << "     outfile         Name of output file for image\n";
63   std::cout << "     nx              Number of pixels X-axis\n";
64   std::cout << "     ny              Number of pixels Y-axis\n";
65   std::cout << "     --view-ratio    View diameter to phantom diameter ratio (default = 1)\n";
66   std::cout << "     --phantom       Phantom to use for projection\n";
67   std::cout << "        herman       Herman head phantom\n";
68   std::cout << "        shepp-logan  Shepp-Logan head phantom\n";
69   std::cout << "        unit-pulse    Unit pulse phantom\n";
70   std::cout << "     --phmfile       Generate Phantom from phantom file\n";
71   std::cout << "     --filter        Generate Phantom from a filter function\n";
72   std::cout << "       abs_bandlimit Abs * Bandlimiting\n";
73   std::cout << "       abs_sinc      Abs * Sinc\n";
74   std::cout << "       abs_cos       Abs * Cosine\n";
75   std::cout << "       abs_hamming   Abs * Hamming\n";
76   std::cout << "       shepp         Shepp-Logan\n";
77   std::cout << "       bandlimit     Bandlimiting\n";
78   std::cout << "       sinc          Sinc\n";
79   std::cout << "       cos           Cosine\n";
80   std::cout << "       triangle      Triangle\n";
81   std::cout << "       hamming       Hamming\n";
82   std::cout << "     --filter-param  Alpha level for Hamming filter\n";
83   std::cout << "     --filter-domain Set domain of filter\n";
84   std::cout << "         spatial     Spatial domain (default)\n";
85   std::cout << "         freq        Frequency domain\n";
86   std::cout << "     --filter-bw     Filter bandwidth (default = 1)\n";
87   std::cout << "     --desc          Description of raysum\n";
88   std::cout << "     --nsample       Number of samples per axis per pixel (default = 1)\n";
89   std::cout << "     --trace         Trace level to use\n";
90   std::cout << "        none         No tracing (default)\n";
91   std::cout << "        console      Trace text level\n";
92   std::cout << "        phantom      Trace phantom\n";
93   std::cout << "        proj         Trace projections\n";
94   std::cout << "        plot         Trace plot\n";
95   std::cout << "        clipping     Trace clipping\n";
96   std::cout << "     --debug         Debug mode\n";
97   std::cout << "     --verbose       Verbose mode\n";
98   std::cout << "     --version       Print version\n";
99   std::cout << "     --help          Print this help message\n";
100 }
101
102 #ifdef HAVE_MPI
103 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug);
104 #endif
105
106 int 
107 phm2if_main (int argc, char* const argv[])
108 {
109   ImageFile* pImGlobal = NULL;
110   Phantom phm;
111   std::string optPhmName;
112   std::string optFilterName;
113   std::string optDomainName (SignalFilter::convertDomainIDToName (SignalFilter::DOMAIN_SPATIAL));
114   std::string optOutFilename;
115   std::string optDesc;
116   std::string optPhmFilename;
117   int opt_nx = 0;
118   int opt_ny = 0;
119   int opt_nsample = 1;
120   double optViewRatio = 1.;
121   double optFilterParam = 1.;
122   double optFilterBW = 1.;
123   int optTrace = Trace::TRACE_NONE;
124   bool optVerbose = false;
125   bool optDebug = false;
126   char *endptr = NULL;
127   char *endstr;
128   Timer timerProgram;
129   
130 #ifdef HAVE_MPI
131   ImageFile* pImLocal = NULL;
132   MPIWorld mpiWorld (argc, argv);
133   if (mpiWorld.getRank() == 0) {
134 #endif
135     while (1) {
136       int c = getopt_long(argc, argv, "", my_options, NULL);
137       if (c == -1)
138         break;
139       
140       switch (c) {
141       case O_PHANTOM:
142         optPhmName = optarg;
143         break;
144       case O_PHMFILE:
145         optPhmFilename = optarg;
146         break;
147       case O_VERBOSE:
148         optVerbose = true;
149         break;
150       case O_DEBUG:
151         optDebug = true;
152         break;
153       case O_TRACE:
154         if ((optTrace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
155           phm2if_usage(argv[0]);
156           return (1);
157         }
158         break;
159       case O_FILTER:
160         optFilterName = optarg;
161         break;
162       case O_FILTER_DOMAIN:
163         optDomainName = optarg;
164         break;
165       case O_DESC:
166         optDesc =  optarg;
167         break;
168       case O_FILTER_BW:
169         optFilterBW = strtod(optarg, &endptr);
170         endstr = optarg + strlen(optarg);
171         if (endptr != endstr) {
172           sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
173           phm2if_usage(argv[0]);
174           return (1);
175         }
176         break;
177       case O_VIEW_RATIO:
178         optViewRatio = strtod(optarg, &endptr);
179         endstr = optarg + strlen(optarg);
180         if (endptr != endstr) {
181           sys_error(ERR_SEVERE,"Error setting --view-ratio to %s\n", optarg);
182           phm2if_usage(argv[0]);
183           return (1);
184         }
185         break;
186       case O_FILTER_PARAM:
187         optFilterParam = strtod(optarg, &endptr);
188         endstr = optarg + strlen(optarg);
189         if (endptr != endstr) {
190           sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
191           phm2if_usage(argv[0]);
192           return (1);
193         }
194         break;
195       case O_NSAMPLE:
196         opt_nsample = strtol(optarg, &endptr, 10);
197         endstr = optarg + strlen(optarg);
198         if (endptr != endstr) {
199           sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
200           phm2if_usage(argv[0]);
201           return (1);
202         }
203         break;
204       case O_VERSION:
205 #ifdef VERSION
206         std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
207 #else
208         std::cerr << "Unknown version number\n";
209 #endif
210       case O_HELP:
211       case '?':
212         phm2if_usage(argv[0]);
213         return (0);
214       default:
215         phm2if_usage(argv[0]);
216         return (1);
217       }
218     }
219     
220     if (optPhmName == "" && optFilterName == "" && optPhmFilename == "") {
221       std::cerr << "No phantom defined\n" << std::endl;
222       phm2if_usage(argv[0]);
223       return (1);
224     }
225     
226     if (optind + 3 != argc) {
227       phm2if_usage(argv[0]);
228       return (1);
229     }
230     optOutFilename = argv[optind];
231     opt_nx = strtol(argv[optind+1], &endptr, 10);
232     endstr = argv[optind+1] + strlen(argv[optind+1]);
233     if (endptr != endstr) {
234       sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
235       phm2if_usage(argv[0]);
236       return (1);
237     }
238     opt_ny = strtol(argv[optind+2], &endptr, 10);
239     endstr = argv[optind+2] + strlen(argv[optind+2]);
240     if (endptr != endstr) {
241       sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
242       phm2if_usage(argv[0]);
243       return (1);
244     }
245     
246     std::ostringstream oss;
247     oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", viewRatio=" << optViewRatio << ", nsample=" << opt_nsample << ", ";
248     if (optPhmFilename != "")
249       oss << "phantomFile=" << optPhmFilename;
250     else if (optPhmName != "")
251       oss << "phantom=" << optPhmName;
252     else if (optFilterName != "") {
253       oss << "filter=" << optFilterName << " - " << optDomainName;
254     }
255     if (optDesc != "")
256       oss << ": " << optDesc;
257     optDesc = oss.str();
258     
259     if (optPhmName != "") {
260       phm.createFromPhantom (optPhmName.c_str());
261       if (phm.fail()) {
262         std::cout << phm.failMessage() << std::endl << std::endl;
263         phm2if_usage(argv[0]);
264         return (1);
265       }
266     }
267     
268     if (optPhmFilename != "") {
269       phm.createFromFile(optPhmFilename.c_str());
270 #ifdef HAVE_MPI
271       if (mpiWorld.getRank() == 0) 
272         std::cerr << "Can't use phantom from file in MPI mode\n";
273       return (1);
274 #endif
275     }
276     
277     if (optVerbose)
278       std::cout << "Rasterize Phantom to Image\n" << std::endl;
279 #ifdef HAVE_MPI
280   }
281 #endif
282   
283 #ifdef HAVE_MPI
284   TimerCollectiveMPI timerBcast (mpiWorld.getComm());
285   mpiWorld.BcastString (optPhmName);
286   mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
287   mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
288   mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
289   mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
290   mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
291   mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
292   mpiWorld.getComm().Bcast (&optViewRatio, 1, MPI::DOUBLE, 0);
293   mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
294   mpiWorld.getComm().Bcast (&optFilterBW, 1, MPI::DOUBLE, 0);
295   
296   mpiWorld.BcastString (optFilterName);
297   mpiWorld.BcastString (optDomainName);
298   
299   if (optVerbose)
300     timerBcast.timerEndAndReport ("Time to broadcast variables");
301   
302   mpiWorld.setTotalWorkUnits (opt_nx);
303   
304   if (mpiWorld.getRank() > 0 && optPhmName != "")
305     phm.createFromPhantom (optPhmName.c_str());
306   
307   if (mpiWorld.getRank() == 0) {
308     pImGlobal = new ImageFile (opt_nx, opt_ny);
309   }
310   pImLocal = new ImageFile (opt_nx, opt_ny);
311 #else
312   pImGlobal = new ImageFile (opt_nx, opt_ny);
313 #endif
314   
315   ImageFileArray v = NULL;
316 #ifdef HAVE_MPI
317   if (mpiWorld.getRank() == 0)
318     v = pImGlobal->getArray ();
319   
320   if (phm.getComposition() == P_UNIT_PULSE) {
321     if (mpiWorld.getRank() == 0) {
322       v[opt_nx/2][opt_ny/2] = 1.;
323     }
324   } else if (optFilterName != "") {
325     if (mpiWorld.getRank() == 0) {
326       pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
327     }
328   } else {
329     TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
330     phm.convertToImagefile (*pImLocal, optViewRatio, opt_nsample, optTrace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false);
331     if (optVerbose)
332       timerRasterize.timerEndAndReport ("Time to rasterize phantom");
333     
334     TimerCollectiveMPI timerGather (mpiWorld.getComm());
335     mpi_gather_image (mpiWorld, pImGlobal, pImLocal, optDebug);
336     if (optVerbose)
337       timerGather.timerEndAndReport ("Time to gather image");
338   }
339 #else
340   v = pImGlobal->getArray ();
341   if (phm.getComposition() == P_UNIT_PULSE) {
342     v[opt_nx/2][opt_ny/2] = 1.;
343   } else if (optFilterName != "") {
344     pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
345   } else {
346     phm.convertToImagefile (*pImGlobal, optViewRatio, opt_nsample, optTrace);
347   }
348 #endif
349   
350 #ifdef HAVE_MPI
351   if (mpiWorld.getRank() == 0) 
352 #endif
353   {
354     double calctime = timerProgram.timerEnd ();
355     pImGlobal->labelAdd (Array2dFileLabel::L_HISTORY, optDesc.c_str(), calctime);
356     pImGlobal->fileWrite (optOutFilename.c_str());
357     if (optVerbose)
358       std::cout << "Time to rasterize phantom: " << calctime << " seconds\n";
359   }
360   
361   delete pImGlobal;
362 #ifdef HAVE_MPI
363   delete pImLocal;
364 #endif
365   
366   return (0);
367 }
368
369
370
371 #ifdef HAVE_MPI
372 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug)
373 {
374   ImageFileArray vLocal = pImLocal->getArray();
375   ImageFileArray vGlobal = NULL;
376   int nyLocal = pImLocal->ny();
377   
378   if (mpiWorld.getRank() == 0)
379     vGlobal = pImGlobal->getArray();
380   
381   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
382     mpiWorld.getComm().Send(vLocal[iw], nyLocal, pImLocal->getMPIDataType(), 0, 0);
383   
384   if (mpiWorld.getRank() == 0) {
385     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
386       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
387         MPI::Status status;
388         mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, pImLocal->getMPIDataType(), iProc, 0, status);
389       }
390     }
391   }
392   
393 }
394 #endif
395
396 #ifndef NO_MAIN
397 int 
398 main (int argc, char* argv[])
399 {
400   int retval = 1;
401   
402   try {
403     retval = phm2if_main(argc, argv);
404   } catch (exception e) {
405     std::cerr << "Exception: " << e.what() << std::endl;
406   } catch (...) {
407     std::cerr << "Unknown exception\n";
408   }
409   
410   return (retval);
411 }
412 #endif