4cf3b794b3b1adcc96e93e177b9af4a1a6f48949
[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.25 2002/05/03 10:28:17 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.25 2002/05/03 10:28:17 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 << "     --debug         Debug mode\n";
93   std::cout << "     --verbose       Verbose mode\n";
94   std::cout << "     --version       Print version\n";
95   std::cout << "     --help          Print this help message\n";
96 }
97
98 #ifdef HAVE_MPI
99 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug);
100 #endif
101
102 int 
103 phm2if_main (int argc, char* const argv[])
104 {
105   ImageFile* pImGlobal = NULL;
106   Phantom phm;
107   std::string optPhmName;
108   std::string optFilterName;
109   std::string optDomainName (SignalFilter::convertDomainIDToName (SignalFilter::DOMAIN_SPATIAL));
110   std::string optOutFilename;
111   std::string optDesc;
112   std::string optPhmFilename;
113   int opt_nx = 0;
114   int opt_ny = 0;
115   int opt_nsample = 1;
116   double optViewRatio = 1.;
117   double optFilterParam = 1.;
118   double optFilterBW = 1.;
119   int optTrace = Trace::TRACE_NONE;
120   bool optVerbose = false;
121   bool optDebug = false;
122   char *endptr = NULL;
123   char *endstr;
124   Timer timerProgram;
125   
126 #ifdef HAVE_MPI
127   ImageFile* pImLocal = NULL;
128   MPIWorld mpiWorld (argc, argv);
129   if (mpiWorld.getRank() == 0) {
130 #endif
131     while (1) {
132       int c = getopt_long(argc, argv, "", my_options, NULL);
133       if (c == -1)
134         break;
135       
136       switch (c) {
137       case O_PHANTOM:
138         optPhmName = optarg;
139         break;
140       case O_PHMFILE:
141         optPhmFilename = optarg;
142         break;
143       case O_VERBOSE:
144         optVerbose = true;
145         break;
146       case O_DEBUG:
147         optDebug = true;
148         break;
149       case O_TRACE:
150         if ((optTrace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
151           phm2if_usage(argv[0]);
152           return (1);
153         }
154         break;
155       case O_FILTER:
156         optFilterName = optarg;
157         break;
158       case O_FILTER_DOMAIN:
159         optDomainName = optarg;
160         break;
161       case O_DESC:
162         optDesc =  optarg;
163         break;
164       case O_FILTER_BW:
165         optFilterBW = strtod(optarg, &endptr);
166         endstr = optarg + strlen(optarg);
167         if (endptr != endstr) {
168           sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
169           phm2if_usage(argv[0]);
170           return (1);
171         }
172         break;
173       case O_VIEW_RATIO:
174         optViewRatio = strtod(optarg, &endptr);
175         endstr = optarg + strlen(optarg);
176         if (endptr != endstr) {
177           sys_error(ERR_SEVERE,"Error setting --view-ratio to %s\n", optarg);
178           phm2if_usage(argv[0]);
179           return (1);
180         }
181         break;
182       case O_FILTER_PARAM:
183         optFilterParam = strtod(optarg, &endptr);
184         endstr = optarg + strlen(optarg);
185         if (endptr != endstr) {
186           sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
187           phm2if_usage(argv[0]);
188           return (1);
189         }
190         break;
191       case O_NSAMPLE:
192         opt_nsample = strtol(optarg, &endptr, 10);
193         endstr = optarg + strlen(optarg);
194         if (endptr != endstr) {
195           sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
196           phm2if_usage(argv[0]);
197           return (1);
198         }
199         break;
200       case O_VERSION:
201 #ifdef VERSION
202         std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
203 #else
204         std::cerr << "Unknown version number\n";
205 #endif
206       case O_HELP:
207       case '?':
208         phm2if_usage(argv[0]);
209         return (0);
210       default:
211         phm2if_usage(argv[0]);
212         return (1);
213       }
214     }
215     
216     if (optPhmName == "" && optFilterName == "" && optPhmFilename == "") {
217       std::cerr << "No phantom defined\n" << std::endl;
218       phm2if_usage(argv[0]);
219       return (1);
220     }
221     
222     if (optind + 3 != argc) {
223       phm2if_usage(argv[0]);
224       return (1);
225     }
226     optOutFilename = argv[optind];
227     opt_nx = strtol(argv[optind+1], &endptr, 10);
228     endstr = argv[optind+1] + strlen(argv[optind+1]);
229     if (endptr != endstr) {
230       sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
231       phm2if_usage(argv[0]);
232       return (1);
233     }
234     opt_ny = strtol(argv[optind+2], &endptr, 10);
235     endstr = argv[optind+2] + strlen(argv[optind+2]);
236     if (endptr != endstr) {
237       sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
238       phm2if_usage(argv[0]);
239       return (1);
240     }
241     
242     std::ostringstream oss;
243     oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", viewRatio=" << optViewRatio << ", nsample=" << opt_nsample << ", ";
244     if (optPhmFilename != "")
245       oss << "phantomFile=" << optPhmFilename;
246     else if (optPhmName != "")
247       oss << "phantom=" << optPhmName;
248     else if (optFilterName != "") {
249       oss << "filter=" << optFilterName << " - " << optDomainName;
250     }
251     if (optDesc != "")
252       oss << ": " << optDesc;
253     optDesc = oss.str();
254     
255     if (optPhmName != "") {
256       phm.createFromPhantom (optPhmName.c_str());
257       if (phm.fail()) {
258         std::cout << phm.failMessage() << std::endl << std::endl;
259         phm2if_usage(argv[0]);
260         return (1);
261       }
262     }
263     
264     if (optPhmFilename != "") {
265       phm.createFromFile(optPhmFilename.c_str());
266 #ifdef HAVE_MPI
267       if (mpiWorld.getRank() == 0) 
268         std::cerr << "Can't use phantom from file in MPI mode\n";
269       return (1);
270 #endif
271     }
272     
273     if (optVerbose)
274       std::cout << "Rasterize Phantom to Image\n" << std::endl;
275 #ifdef HAVE_MPI
276   }
277 #endif
278   
279 #ifdef HAVE_MPI
280   TimerCollectiveMPI timerBcast (mpiWorld.getComm());
281   mpiWorld.BcastString (optPhmName);
282   mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
283   mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
284   mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
285   mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
286   mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
287   mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
288   mpiWorld.getComm().Bcast (&optViewRatio, 1, MPI::DOUBLE, 0);
289   mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
290   mpiWorld.getComm().Bcast (&optFilterBW, 1, MPI::DOUBLE, 0);
291   
292   mpiWorld.BcastString (optFilterName);
293   mpiWorld.BcastString (optDomainName);
294   
295   if (optVerbose)
296     timerBcast.timerEndAndReport ("Time to broadcast variables");
297   
298   mpiWorld.setTotalWorkUnits (opt_nx);
299   
300   if (mpiWorld.getRank() > 0 && optPhmName != "")
301     phm.createFromPhantom (optPhmName.c_str());
302   
303   if (mpiWorld.getRank() == 0) {
304     pImGlobal = new ImageFile (opt_nx, opt_ny);
305   }
306   pImLocal = new ImageFile (opt_nx, opt_ny);
307 #else
308   pImGlobal = new ImageFile (opt_nx, opt_ny);
309 #endif
310   
311   ImageFileArray v = NULL;
312 #ifdef HAVE_MPI
313   if (mpiWorld.getRank() == 0)
314     v = pImGlobal->getArray ();
315   
316   if (phm.getComposition() == P_UNIT_PULSE) {
317     if (mpiWorld.getRank() == 0) {
318       v[opt_nx/2][opt_ny/2] = 1.;
319     }
320   } else if (optFilterName != "") {
321     if (mpiWorld.getRank() == 0) {
322       pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
323     }
324   } else {
325     TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
326     phm.convertToImagefile (*pImLocal, optViewRatio, opt_nsample, optTrace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false);
327     if (optVerbose)
328       timerRasterize.timerEndAndReport ("Time to rasterize phantom");
329     
330     TimerCollectiveMPI timerGather (mpiWorld.getComm());
331     mpi_gather_image (mpiWorld, pImGlobal, pImLocal, optDebug);
332     if (optVerbose)
333       timerGather.timerEndAndReport ("Time to gather image");
334   }
335 #else
336   v = pImGlobal->getArray ();
337   if (phm.getComposition() == P_UNIT_PULSE) {
338     v[opt_nx/2][opt_ny/2] = 1.;
339   } else if (optFilterName != "") {
340     pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
341   } else {
342     phm.convertToImagefile (*pImGlobal, optViewRatio, opt_nsample, optTrace);
343   }
344 #endif
345   
346 #ifdef HAVE_MPI
347   if (mpiWorld.getRank() == 0) 
348 #endif
349   {
350     double calctime = timerProgram.timerEnd ();
351     pImGlobal->labelAdd (Array2dFileLabel::L_HISTORY, optDesc.c_str(), calctime);
352     pImGlobal->fileWrite (optOutFilename.c_str());
353     if (optVerbose)
354       std::cout << "Time to rasterize phantom: " << calctime << " seconds\n";
355   }
356   
357   delete pImGlobal;
358 #ifdef HAVE_MPI
359   delete pImLocal;
360 #endif
361   
362   return (0);
363 }
364
365
366
367 #ifdef HAVE_MPI
368 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug)
369 {
370   ImageFileArray vLocal = pImLocal->getArray();
371   ImageFileArray vGlobal = NULL;
372   int nyLocal = pImLocal->ny();
373   
374   if (mpiWorld.getRank() == 0)
375     vGlobal = pImGlobal->getArray();
376   
377   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
378     mpiWorld.getComm().Send(vLocal[iw], nyLocal, pImLocal->getMPIDataType(), 0, 0);
379   
380   if (mpiWorld.getRank() == 0) {
381     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
382       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
383         MPI::Status status;
384         mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, pImLocal->getMPIDataType(), iProc, 0, status);
385       }
386     }
387   }
388   
389 }
390 #endif
391
392 #ifndef NO_MAIN
393 int 
394 main (int argc, char* argv[])
395 {
396   int retval = 1;
397   
398   try {
399     retval = phm2if_main(argc, argv);
400   } catch (exception e) {
401     std::cerr << "Exception: " << e.what() << std::endl;
402   } catch (...) {
403     std::cerr << "Unknown exception\n";
404   }
405   
406   return (retval);
407 }
408 #endif