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