r184: *** empty log message ***
[ctsim.git] / tools / phm2pj.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          phm2pj.cpp
5 **   Purpose:       Take projections of a phantom object
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: phm2pj.cpp,v 1.10 2000/08/25 15:59:13 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_NRAY, O_ROTANGLE, O_PHMFILE, O_GEOMETRY, O_FOCAL_LENGTH, O_FIELD_OF_VIEW,
33        O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
34
35 static struct option phm2pj_options[] = 
36 {
37   {"phantom", 1, 0, O_PHANTOM},
38   {"phmfile", 1, 0, O_PHMFILE},
39   {"desc", 1, 0, O_DESC},
40   {"nray", 1, 0, O_NRAY},
41   {"rotangle", 1, 0, O_ROTANGLE},
42   {"geometry", 1, 0, O_GEOMETRY},
43   {"focal-length", 1, 0, O_FOCAL_LENGTH},
44   {"field-of-view", 1, 0, O_FIELD_OF_VIEW},
45   {"trace", 1, 0, O_TRACE},
46   {"verbose", 0, 0, O_VERBOSE},
47   {"help", 0, 0, O_HELP},
48   {"debug", 0, 0, O_DEBUG},
49   {"version", 0, 0, O_VERSION},
50   {0, 0, 0, 0}
51 };
52
53 static const char* g_szIdStr = "$Id: phm2pj.cpp,v 1.10 2000/08/25 15:59:13 kevin Exp $";
54
55
56 void 
57 phm2pj_usage (const char *program)
58 {
59   cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
60   cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
61   cout << "     outfile          Name of output file for raysums\n";
62   cout << "     ndet             Number of detectors\n";
63   cout << "     nview            Number of rotated views\n";
64   cout << "     --phantom        Phantom to use for projection\n";
65   cout << "        herman        Herman head phantom\n";
66   cout << "        herman-b      Herman head phantom (Bordered)\n";
67   cout << "        shepp-logan   Shepp-Logan head phantom\n";
68   cout << "        shepp-logan-b Shepp-Logan head phantom (Bordered)\n";
69   cout << "        unitpulse     Unit pulse phantom\n";
70   cout << "     --phmfile        Get Phantom from phantom file\n";
71   cout << "     --desc           Description of raysum\n";
72   cout << "     --nray           Number of rays per detector (default = 1)\n";
73   cout << "     --rotangle       Degrees to rotate view through (multiple of PI)\n";
74   cout << "                      (default = 1)\n";
75   cout << "     --focal-length   Focal length ratio (ratio to radius of phantom)\n";
76   cout << "                      (default = 1)\n";
77   cout << "     --field-of-view  Field of view (ratio to diameter of phantom square)\n";
78   cout << "                      (default = 1)\n";
79   cout << "     --trace          Trace level to use\n";
80   cout << "        none          No tracing (default)\n";
81   cout << "        text          Trace text level\n";
82   cout << "        phm           Trace phantom image\n";
83   cout << "        rays          Trace rays\n";
84   cout << "        plot          Trace plot\n";
85   cout << "        clipping      Trace clipping\n";
86   cout << "     --verbose        Verbose mode\n";
87   cout << "     --debug          Debug mode\n";
88   cout << "     --version        Print version\n";
89   cout << "     --help           Print this help message\n";
90 }
91
92 #ifdef HAVE_MPI
93 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
94 #endif
95
96 int 
97 phm2pj_main (int argc, char* argv[])
98 {
99   Phantom phm;
100   string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
101   char *opt_outfile = NULL;
102   string opt_desc;
103   string optPhmFileName;
104   int opt_ndet;
105   int opt_nview;
106   int opt_nray = 1;
107   double dOptFocalLength = 1.;
108   double dOptFieldOfView = 1.;
109   int opt_trace = 0;
110   string optPhmName (Phantom::convertPhantomIDToName(Phantom::PHM_HERMAN));
111   int opt_verbose = 0;
112   int opt_debug = 0;
113   double opt_rotangle = 1;
114   char* endptr = NULL;
115   char* endstr;
116
117 #ifdef HAVE_MPI
118   MPIWorld mpiWorld (argc, argv);
119 #endif
120
121   Timer timerProgram;
122
123 #ifdef HAVE_MPI
124   if (mpiWorld.getRank() == 0) {
125 #endif
126     while (1) {
127       int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
128
129       if (c == -1)
130         break;
131       
132       switch (c) {
133       case O_PHANTOM:
134         optPhmName = optarg;
135         break;
136       case O_PHMFILE:
137         optPhmFileName = optarg;
138         break;
139       case O_VERBOSE:
140         opt_verbose = 1;
141         break;
142       case O_DEBUG:
143         opt_debug = 1;
144         break;
145         break;
146       case O_TRACE:
147         if ((opt_trace = TraceLevel::convertTraceNameToID(optarg)) == TRACE_INVALID) {
148           phm2pj_usage(argv[0]);
149           return (1);
150         }
151         break;
152       case O_DESC:
153         opt_desc = optarg;
154         break;
155       case O_ROTANGLE:
156         opt_rotangle = strtod(optarg, &endptr);
157         endstr = optarg + strlen(optarg);
158         if (endptr != endstr) {
159           cerr << "Error setting --rotangle to " << optarg << endl;
160           phm2pj_usage(argv[0]);
161           return (1);
162         }
163         break;
164       case O_GEOMETRY:
165         optGeometryName = optarg;
166         break;
167       case O_FOCAL_LENGTH:
168         dOptFocalLength = strtod(optarg, &endptr);
169         endstr = optarg + strlen(optarg);
170         if (endptr != endstr) {
171           cerr << "Error setting --focal-length to " << optarg << endl;
172           phm2pj_usage(argv[0]);
173           return (1);
174         }
175         break;
176       case O_FIELD_OF_VIEW:
177         dOptFieldOfView = strtod(optarg, &endptr);
178         endstr = optarg + strlen(optarg);
179         if (endptr != endstr) {
180           cerr << "Error setting --field-of-view to " << optarg << endl;
181           phm2pj_usage(argv[0]);
182           return (1);
183         }
184         break;
185       case O_NRAY:
186         opt_nray = strtol(optarg, &endptr, 10);
187         endstr = optarg + strlen(optarg);
188         if (endptr != endstr) {
189           cerr << "Error setting --nray to %s" << optarg << endl;
190           phm2pj_usage(argv[0]);
191           return (1);
192         }
193         break;
194         case O_VERSION:
195 #ifdef VERSION
196           cout << "Version: " << VERSION << endl << g_szIdStr << endl;
197 #else
198           cout << "Unknown version number\n";
199 #endif
200           return (0);
201       case O_HELP:
202       case '?':
203         phm2pj_usage(argv[0]);
204         return (0);
205       default:
206         phm2pj_usage(argv[0]);
207         return (1);
208       }
209     }
210   
211     if (optPhmName == "" && optPhmFileName == "") {
212       cerr << "No phantom defined\n" << endl;
213       phm2pj_usage(argv[0]);
214       return (1);
215     }
216     if (optind + 3 != argc) {
217       phm2pj_usage(argv[0]);
218       return (1);
219     }
220
221     opt_outfile = argv[optind];
222     opt_ndet = strtol(argv[optind+1], &endptr, 10);
223     endstr = argv[optind+1] + strlen(argv[optind+1]);
224     if (endptr != endstr) {
225       cerr << "Error setting --ndet to " << argv[optind+1] << endl;
226       phm2pj_usage(argv[0]);
227       return (1);
228     }
229     opt_nview = strtol(argv[optind+2], &endptr, 10);
230     endstr = argv[optind+2] + strlen(argv[optind+2]);
231     if (endptr != endstr) {
232       cerr << "Error setting --nview to " << argv[optind+2] << endl;
233       phm2pj_usage(argv[0]);
234       return (1);
235     }
236
237     ostringstream desc;
238     desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", ";
239     if (optPhmFileName.length()) {
240       desc << "PhantomFile=" << optPhmFileName;
241     } else if (optPhmName != "") {
242       desc << "Phantom=" << optPhmName;
243     }
244     if (opt_desc.length()) {
245       desc << ": " << opt_desc;
246     }
247     opt_desc = desc.str();
248
249     if (optPhmName != "") {
250       phm.createFromPhantom (optPhmName.c_str());
251       if (phm.fail()) {
252         cout << phm.failMessage() << endl << endl;
253         phm2pj_usage(argv[0]);
254         return (1);
255       }
256     }
257
258     if (optPhmFileName != "") {
259 #ifdef HAVE_MPI
260       cerr << "Can not read phantom from file in MPI mode\n";
261       return (1);
262 #endif
263       phm.createFromFile (optPhmFileName.c_str());
264     }
265
266 #ifdef HAVE_MPI
267   }
268 #endif
269
270 #ifdef HAVE_MPI
271   TimerCollectiveMPI timerBcast(mpiWorld.getComm());
272   mpiWorld.BcastString (optPhmName);
273   mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
274   mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
275   mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
276   mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
277   mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
278   mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
279   mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
280   timerBcast.timerEndAndReport ("Time to broadcast variables");
281
282   if (mpiWorld.getRank() > 0 && optPhmName != "")
283     phm.createFromPhantom (optPhmName.c_str());
284 #endif
285
286   opt_rotangle *= PI;
287   Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength, dOptFieldOfView);
288   if (scanner.fail()) {
289     cout << "Scanner Creation Error: " << scanner.failMessage() << endl;
290     return (1);
291   }
292 #ifdef HAVE_MPI
293   mpiWorld.setTotalWorkUnits (opt_nview);
294
295   Projections pjGlobal;
296   if (mpiWorld.getRank() == 0)
297     pjGlobal.initFromScanner (scanner);
298   
299   if (opt_verbose)
300     pjGlobal.printScanInfo();
301
302   Projections pjLocal (scanner);
303   pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
304
305   if (opt_debug)
306     cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
307
308   TimerCollectiveMPI timerProject (mpiWorld.getComm());
309   scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
310   if (opt_verbose)
311     timerProject.timerEndAndReport ("Time to collect projections");
312
313   TimerCollectiveMPI timerGather (mpiWorld.getComm());
314   GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
315   if (opt_verbose) 
316      timerGather.timerEndAndReport ("Time to gather projections");
317
318 #else
319   Projections pjGlobal (scanner);
320 #if HAVE_SGP
321   SGPDriver* pSGPDriver = NULL;
322   SGP* pSGP = NULL;
323   if (opt_trace >= TRACE_PHM) {
324     pSGPDriver = new SGPDriver ("phm2pj", 600, 600);
325     pSGP = new SGP (*pSGPDriver);
326   }
327   scanner.collectProjections (pjGlobal, phm, opt_trace, pSGP);
328   if (opt_trace >= TRACE_PHM) {
329     cout << "Press enter to continue\n";
330     cio_kb_getc();
331     delete pSGP;  pSGP = NULL;
332     delete pSGPDriver;  pSGPDriver = NULL;
333   }
334
335 #else
336   scanner.collectProjections (pjGlobal, phm, opt_trace);
337 #endif
338
339 #endif
340   
341 #ifdef HAVE_MPI
342   if (mpiWorld.getRank() == 0) 
343 #endif
344     {
345       pjGlobal.setCalcTime (timerProgram.timerEnd());
346       pjGlobal.setRemark (opt_desc);
347       pjGlobal.write (opt_outfile);
348       if (opt_verbose) {
349         phm.print();
350         cout << endl;
351         pjGlobal.printScanInfo();
352         cout << endl;
353         cout << "  Remark: " << pjGlobal.remark() << endl;
354         cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
355       }
356     }
357
358   return (0);
359 }
360
361
362 /* FUNCTION
363  *    GatherProjectionsMPI
364  *
365  * SYNOPSIS
366  *    Gather's raysums from all processes in pjLocal in pjGlobal in process 0
367  */
368
369 #ifdef HAVE_MPI
370 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
371 {
372   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
373     DetectorArray& detArray = pjLocal.getDetectorArray(iw);
374     double viewAngle = detArray.viewAngle();
375     int nDet = detArray.nDet();
376     DetectorValue* detval = detArray.detValues();
377
378     mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
379     mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
380     mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
381   }
382
383   if (mpiWorld.getRank() == 0) {
384     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
385       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
386         MPI::Status status;
387         double viewAngle;
388         int nDet;
389         DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
390         DetectorValue* detval = detArray.detValues();
391
392         mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
393         mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
394         mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
395         detArray.setViewAngle (viewAngle);
396       }
397     }
398   }
399 }
400 #endif
401
402
403 #ifndef NO_MAIN
404 int 
405 main (int argc, char* argv[])
406 {
407   int retval = 1;
408
409   try {
410     retval = phm2pj_main(argc, argv);
411 #if HAVE_DMALLOC
412     //    dmalloc_shutdown();
413 #endif
414   } catch (exception e) {
415     cerr << "Exception: " << e.what() << endl;
416   } catch (...) {
417     cerr << "Unknown exception\n";
418   }
419
420   return (retval);
421 }
422 #endif
423