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