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