Exit on EOF
[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-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 #include "ct.h"
27 #include "timer.h"
28
29
30 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE, O_GEOMETRY, O_FOCAL_LENGTH, O_CENTER_DETECTOR_LENGTH,
31 O_VIEW_RATIO, O_SCAN_RATIO, O_OFFSETVIEW, O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
32
33 static struct option phm2pj_options[] =
34 {
35   {"phantom", 1, 0, O_PHANTOM},
36   {"phmfile", 1, 0, O_PHMFILE},
37   {"desc", 1, 0, O_DESC},
38   {"nray", 1, 0, O_NRAY},
39   {"rotangle", 1, 0, O_ROTANGLE},
40   {"geometry", 1, 0, O_GEOMETRY},
41   {"focal-length", 1, 0, O_FOCAL_LENGTH},
42   {"center-detector-length", 1, 0, O_CENTER_DETECTOR_LENGTH},
43   {"offsetview", 1, 0, O_OFFSETVIEW},
44   {"view-ratio", 1, 0, O_VIEW_RATIO},
45   {"scan-ratio", 1, 0, O_SCAN_RATIO},
46   {"trace", 1, 0, O_TRACE},
47   {"verbose", 0, 0, O_VERBOSE},
48   {"help", 0, 0, O_HELP},
49   {"debug", 0, 0, O_DEBUG},
50   {"version", 0, 0, O_VERSION},
51   {0, 0, 0, 0}
52 };
53
54 static const char* g_szIdStr = "$Id$";
55
56
57 void
58 phm2pj_usage (const char *program)
59 {
60   std::cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
61   std::cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
62   std::cout << "     outfile          Name of output file for projections\n";
63   std::cout << "     ndet             Number of detectors\n";
64   std::cout << "     nview            Number of rotated views\n";
65   std::cout << "     --phantom        Phantom to use for projection\n";
66   std::cout << "        herman        Herman head phantom\n";
67   std::cout << "        shepp-logan   Shepp-Logan head phantom\n";
68   std::cout << "        unit-pulse     Unit pulse phantom\n";
69   std::cout << "     --phmfile        Get Phantom from phantom file\n";
70   std::cout << "     --desc           Description of raysum\n";
71   std::cout << "     --nray           Number of rays per detector (default = 1)\n";
72   std::cout << "     --rotangle       Angle to rotate view through (fraction of a circle)\n";
73   std::cout << "                      (default = select appropriate for geometry)\n";
74   std::cout << "     --geometry       Geometry of scanning\n";
75   std::cout << "        parallel      Parallel scan beams (default)\n";
76   std::cout << "        equilinear    Equilinear divergent scan beams\n";
77   std::cout << "        equiangular   Equiangular divergent scan beams\n";
78   std::cout << "     --focal-length   Focal length ratio (ratio to radius of view area)\n";
79   std::cout << "                      (default = 2)\n";
80   std::cout << "     --center-detector-length  Distance from center of phantom to detector array\n";
81   std::cout << "                      (ratio to radius of view area) (default = 2)\n";
82   std::cout << "     --view-ratio     Length to view (view diameter to phantom diameter)\n";
83   std::cout << "                      (default = 1)\n";
84   std::cout << "     --scan-ratio     Length to scan (scan diameter to view diameter)\n";
85   std::cout << "                      (default = 1)\n";
86   std::cout << "     --offsetview     Initial gantry offset in 'views' (default = 0)\n";
87   std::cout << "     --trace          Trace level to use\n";
88   std::cout << "        none          No tracing (default)\n";
89   std::cout << "        console       Trace text level\n";
90   std::cout << "     --verbose        Verbose mode\n";
91   std::cout << "     --debug          Debug mode\n";
92   std::cout << "     --version        Print version\n";
93   std::cout << "     --help           Print this help message\n";
94 }
95
96 #ifdef HAVE_MPI
97 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
98 #endif
99
100 int
101 phm2pj_main (int argc, char* const argv[])
102 {
103   Phantom phm;
104   std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
105   char *opt_outfile = NULL;
106   std::string opt_desc;
107   std::string optPhmName;
108   std::string optPhmFileName;
109   int opt_ndet;
110   int opt_nview;
111   int opt_offsetview = 0;
112   int opt_nray = 1;
113   double dOptFocalLength = 2.;
114   double dOptCenterDetectorLength = 2.;
115   double dOptViewRatio = 1.;
116   double dOptScanRatio = 1.;
117   int opt_trace = Trace::TRACE_NONE;
118   int opt_verbose = 0;
119   int opt_debug = 0;
120   double opt_rotangle = -1;
121   char* endptr = NULL;
122   char* endstr;
123   UNUSED(opt_debug);
124   
125 #ifdef HAVE_MPI
126   MPIWorld mpiWorld (argc, argv);
127 #endif
128
129   Timer timerProgram;
130
131 #ifdef HAVE_MPI
132   if (mpiWorld.getRank() == 0) {
133 #endif
134     while (1) {
135       int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
136
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         opt_verbose = 1;
149         break;
150       case O_DEBUG:
151         opt_debug = 1;
152         break;
153         break;
154       case O_TRACE:
155         if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
156           phm2pj_usage(argv[0]);
157           return (1);
158         }
159         break;
160       case O_DESC:
161         opt_desc = optarg;
162         break;
163       case O_ROTANGLE:
164         opt_rotangle = strtod(optarg, &endptr);
165         endstr = optarg + strlen(optarg);
166         if (endptr != endstr) {
167           std::cerr << "Error setting --rotangle to " << optarg << std::endl;
168           phm2pj_usage(argv[0]);
169           return (1);
170         }
171         break;
172       case O_GEOMETRY:
173         optGeometryName = optarg;
174         break;
175       case O_FOCAL_LENGTH:
176         dOptFocalLength = strtod(optarg, &endptr);
177         endstr = optarg + strlen(optarg);
178         if (endptr != endstr) {
179           std::cerr << "Error setting --focal-length to " << optarg << std::endl;
180           phm2pj_usage(argv[0]);
181           return (1);
182         }
183         break;
184       case O_CENTER_DETECTOR_LENGTH:
185         dOptCenterDetectorLength = strtod(optarg, &endptr);
186         endstr = optarg + strlen(optarg);
187         if (endptr != endstr) {
188           std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
189           phm2pj_usage(argv[0]);
190           return (1);
191         }
192         break;
193       case O_VIEW_RATIO:
194         dOptViewRatio = strtod(optarg, &endptr);
195         endstr = optarg + strlen(optarg);
196         if (endptr != endstr) {
197           std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
198           phm2pj_usage(argv[0]);
199           return (1);
200         }
201         break;
202       case O_SCAN_RATIO:
203         dOptScanRatio = strtod(optarg, &endptr);
204         endstr = optarg + strlen(optarg);
205         if (endptr != endstr) {
206           std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
207           phm2pj_usage(argv[0]);
208           return (1);
209         }
210         break;
211       case O_NRAY:
212         opt_nray = strtol(optarg, &endptr, 10);
213         endstr = optarg + strlen(optarg);
214         if (endptr != endstr) {
215           std::cerr << "Error setting --nray to %s" << optarg << std::endl;
216           phm2pj_usage(argv[0]);
217           return (1);
218         }
219         break;
220           case O_OFFSETVIEW:
221                 opt_offsetview = strtol(optarg, &endptr, 10);
222                 endstr = optarg + strlen(optarg);
223                 if (endptr != endstr) {
224                   std::cerr << "Error setting --offsetview to %s" << optarg << std::endl;
225                   phm2pj_usage(argv[0]);
226                   return (1);
227                 }
228                 break;
229
230       case O_VERSION:
231 #ifdef VERSION
232         std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
233 #else
234         std::cout << "Unknown version number\n";
235 #endif
236         return (0);
237       case O_HELP:
238       case '?':
239         phm2pj_usage(argv[0]);
240         return (0);
241       default:
242         phm2pj_usage(argv[0]);
243         return (1);
244       }
245     }
246
247     if (optPhmName == "" && optPhmFileName == "") {
248       std::cerr << "No phantom defined\n" << std::endl;
249       phm2pj_usage(argv[0]);
250       return (1);
251     }
252     if (optind + 3 != argc) {
253       phm2pj_usage(argv[0]);
254       return (1);
255     }
256
257     opt_outfile = argv[optind];
258     opt_ndet = strtol(argv[optind+1], &endptr, 10);
259     endstr = argv[optind+1] + strlen(argv[optind+1]);
260     if (endptr != endstr) {
261       std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
262       phm2pj_usage(argv[0]);
263       return (1);
264     }
265     opt_nview = strtol(argv[optind+2], &endptr, 10);
266     endstr = argv[optind+2] + strlen(argv[optind+2]);
267     if (endptr != endstr) {
268       std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
269       phm2pj_usage(argv[0]);
270       return (1);
271     }
272
273     if (opt_rotangle < 0) {
274       if (optGeometryName.compare ("parallel") == 0)
275         opt_rotangle = 0.5;
276       else
277         opt_rotangle = 1.0;
278     }
279
280     std::ostringstream desc;
281     desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", OffsetView =" << opt_offsetview << ", Geometry=" << optGeometryName << ", ";
282     if (optPhmFileName.length()) {
283       desc << "PhantomFile=" << optPhmFileName;
284     } else if (optPhmName != "") {
285       desc << "Phantom=" << optPhmName;
286     }
287     if (opt_desc.length()) {
288       desc << ": " << opt_desc;
289     }
290     opt_desc = desc.str();
291
292     if (optPhmName != "") {
293       phm.createFromPhantom (optPhmName.c_str());
294       if (phm.fail()) {
295         std::cout << phm.failMessage() << std::endl << std::endl;
296         phm2pj_usage(argv[0]);
297         return (1);
298       }
299     }
300
301     if (optPhmFileName != "") {
302 #ifdef HAVE_MPI
303       std::cerr << "Can not read phantom from file in MPI mode\n";
304       return (1);
305 #endif
306       phm.createFromFile (optPhmFileName.c_str());
307     }
308
309 #ifdef HAVE_MPI
310   }
311 #endif
312
313 #ifdef HAVE_MPI
314   TimerCollectiveMPI timerBcast(mpiWorld.getComm());
315   mpiWorld.BcastString (optPhmName);
316   mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
317   mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
318   mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0);
319   mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
320   mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
321   mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
322   mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
323   mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
324   mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
325   mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
326   mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
327   if (opt_verbose)
328     timerBcast.timerEndAndReport ("Time to broadcast variables");
329
330   if (mpiWorld.getRank() > 0 && optPhmName != "")
331     phm.createFromPhantom (optPhmName.c_str());
332 #endif
333
334   opt_rotangle *= TWOPI;
335   Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_offsetview, opt_nray,
336                 opt_rotangle, dOptFocalLength, dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
337   if (scanner.fail()) {
338     std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
339     return (1);
340   }
341 #ifdef HAVE_MPI
342   mpiWorld.setTotalWorkUnits (opt_nview);
343
344   Projections pjGlobal;
345   if (mpiWorld.getRank() == 0)
346     pjGlobal.initFromScanner (scanner);
347
348   if (opt_verbose) {
349     std::ostringstream os;
350     pjGlobal.printScanInfo(os);
351     std::cout << os.str();
352   }
353
354   Projections pjLocal (scanner);
355   pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
356
357   if (opt_debug)
358     std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
359
360   TimerCollectiveMPI timerProject (mpiWorld.getComm());
361   scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
362   if (opt_verbose)
363     timerProject.timerEndAndReport ("Time to collect projections");
364
365   TimerCollectiveMPI timerGather (mpiWorld.getComm());
366   GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
367   if (opt_verbose)
368     timerGather.timerEndAndReport ("Time to gather projections");
369
370 #else
371   Projections pjGlobal (scanner);
372   scanner.collectProjections (pjGlobal, phm, 0, opt_nview, opt_offsetview, true, opt_trace);
373 #endif
374
375 #ifdef HAVE_MPI
376   if (mpiWorld.getRank() == 0)
377 #endif
378   {
379     pjGlobal.setCalcTime (timerProgram.timerEnd());
380     pjGlobal.setRemark (opt_desc);
381     pjGlobal.write (opt_outfile);
382     if (opt_verbose) {
383       phm.print (std::cout);
384       std::cout << std::endl;
385       std::ostringstream os;
386       pjGlobal.printScanInfo (os);
387       std::cout << os.str() << std::endl;
388       std::cout << "  Remark: " << pjGlobal.remark() << std::endl;
389       std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
390     }
391   }
392
393   return (0);
394 }
395
396
397 /* FUNCTION
398 *    GatherProjectionsMPI
399 *
400 * SYNOPSIS
401 *    Gather's raysums from all processes in pjLocal in pjGlobal in process 0
402 */
403
404 #ifdef HAVE_MPI
405 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
406 {
407   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
408     DetectorArray& detArray = pjLocal.getDetectorArray(iw);
409     double viewAngle = detArray.viewAngle();
410     int nDet = detArray.nDet();
411     DetectorValue* detval = detArray.detValues();
412
413     mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
414     mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
415     mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
416   }
417
418   if (mpiWorld.getRank() == 0) {
419     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
420       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
421         MPI::Status status;
422         double viewAngle;
423         int nDet;
424         DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
425         DetectorValue* detval = detArray.detValues();
426
427         mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
428         mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
429         mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
430         detArray.setViewAngle (viewAngle);
431       }
432     }
433   }
434 }
435 #endif
436
437
438 #ifndef NO_MAIN
439 int
440 main (int argc, char* argv[])
441 {
442   int retval = 1;
443
444   try {
445     retval = phm2pj_main(argc, argv);
446 #if HAVE_DMALLOC
447     //    dmalloc_shutdown();
448 #endif
449   } catch (exception e) {
450     std::cerr << "Exception: " << e.what() << std::endl;
451   } catch (...) {
452     std::cerr << "Unknown exception\n";
453   }
454
455   return (retval);
456 }
457 #endif
458