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