r96: *** empty log message ***
[ctsim.git] / src / phm2rs.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          phm2rs.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: phm2rs.cpp,v 1.6 2000/06/15 19:07:10 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 phm2rs_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 phm2rs_usage (const char *program)
53 {
54   cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]" << endl;
55   cout << "Calculate raysums (projections) through phantom object, either" << endl;
56   cout << "a predefined --phantom or a --phmfile" << endl;
57   cout << "" << endl;
58   cout << "     outfile      Name of output file for raysums" << endl;
59   cout << "     ndet         Number of detectors" << endl;
60   cout << "     nview        Number of rotated views" << endl;
61   cout << "     --phantom    Phantom to use for projection" << endl;
62   cout << "        herman    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, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug);
85 #endif
86
87 int 
88 phm2rs_main (int argc, char* argv[])
89 {
90   Detector *det;
91   Phantom phm;
92   RAYSUM *rsGlobal = NULL;
93   char *opt_outfile = NULL;
94   string opt_desc;
95   string opt_phmfilename;
96   char *endptr, *endstr;
97   int opt_ndet, opt_nview;
98   int opt_nray = 1;
99   int opt_trace = 0;
100   int opt_phmnum = -1;
101   int opt_verbose = 0;
102   int opt_debug = 0;
103   double opt_rotangle = 1;
104
105 #ifdef HAVE_MPI
106   RAYSUM *rsLocal;
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, "", phm2rs_options, NULL);
117       char *endptr = NULL;
118       char *endstr;
119       
120       if (c == -1)
121         break;
122       
123       switch (c) {
124       case O_PHANTOM:
125         if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
126           phm2rs_usage(argv[0]);
127           return (1);
128         }
129         phm.create (opt_phmnum);
130         break;
131       case O_PHMFILE:
132 #ifdef HAVE_MPI
133         if (mpiWorld.getRank() == 0)
134           cerr << "Can not read phantom from file in MPI mode" << endl;
135         return (1);
136 #endif
137         opt_phmfilename = optarg;
138         phm.createFromFile (opt_phmfilename.c_str());
139         break;
140       case O_VERBOSE:
141         opt_verbose = 1;
142         break;
143       case O_DEBUG:
144         opt_debug = 1;
145         break;
146         break;
147       case O_TRACE:
148         if ((opt_trace = opt_set_trace(optarg)) < 0) {
149           phm2rs_usage(argv[0]);
150           return (1);
151         }
152         break;
153       case O_DESC:
154         opt_desc = optarg;
155         break;
156       case O_ROTANGLE:
157         opt_rotangle = strtod(optarg, &endptr);
158         endstr = optarg + strlen(optarg);
159         if (endptr != endstr) {
160           cerr << "Error setting --rotangle to " << optarg << endl;
161           phm2rs_usage(argv[0]);
162           return (1);
163         }
164         break;
165       case O_NRAY:
166         opt_nray = strtol(optarg, &endptr, 10);
167         endstr = optarg + strlen(optarg);
168         if (endptr != endstr) {
169           cerr << "Error setting --nray to %s" << optarg << endl;
170           phm2rs_usage(argv[0]);
171           return (1);
172         }
173         break;
174         case O_VERSION:
175 #ifdef VERSION
176         cout << "Version: " << VERSION << endl;
177 #else
178         cout << "Unknown version number" << endl;
179 #endif
180           exit(0);
181       case O_HELP:
182       case '?':
183         phm2rs_usage(argv[0]);
184         return (0);
185       default:
186         phm2rs_usage(argv[0]);
187         return (1);
188       }
189     }
190   
191     if (phm.nPElem() == 0) {
192       cerr << "No phantom defined" << endl;
193       phm2rs_usage(argv[0]);
194       return (1);
195     }
196     if (optind + 3 != argc) {
197       phm2rs_usage(argv[0]);
198       return (1);
199     }
200
201     opt_outfile = argv[optind];
202     opt_ndet = strtol(argv[optind+1], &endptr, 10);
203     endstr = argv[optind+1] + strlen(argv[optind+1]);
204     if (endptr != endstr) {
205       cerr << "Error setting --ndet to " << argv[optind+1] << endl;
206       phm2rs_usage(argv[0]);
207       return (1);
208     }
209     opt_nview = strtol(argv[optind+2], &endptr, 10);
210     endstr = argv[optind+2] + strlen(argv[optind+2]);
211     if (endptr != endstr) {
212       cerr << "Error setting --nview to " << argv[optind+2] << endl;
213       phm2rs_usage(argv[0]);
214       return (1);
215     }
216
217     ostringstream desc;
218     desc << "Raysum_Collect: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", ";
219     if (opt_phmfilename.length()) {
220       desc << "PhantomFile=" << opt_phmfilename;
221     } else if (opt_phmnum != -1) {
222       desc << "Phantom=" << name_of_phantom(opt_phmnum);
223     }
224     if (opt_desc.length()) {
225       desc << ": " << opt_desc;
226     }
227     opt_desc = desc.str();
228 #ifdef HAVE_MPI
229   }
230 #endif
231
232 #ifdef HAVE_MPI
233   mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
234   mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
235   mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
236   mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
237   mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0);
238   mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
239   mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
240   mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
241
242   if (mpiWorld.getRank() > 0 && opt_phmnum >= 0)
243     phm.create (opt_phmnum);
244 #endif
245
246   opt_rotangle *= PI;
247   det = new Detector (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
248
249 #ifdef HAVE_MPI
250   mpiWorld.setTotalWorkUnits (opt_nview);
251
252   if (mpiWorld.getRank() == 0) {
253     rsGlobal = raysum_create_from_det (opt_outfile, *det);
254     raysum_alloc_views(rsGlobal);
255   }
256   
257   rsLocal = raysum_create_from_det (NULL, *det);
258   rsLocal->nview = mpiWorld.getMyLocalWorkUnits();
259   if (opt_debug)
260     cout << "rsLocal->nview = " << rsLocal->nview << " (process " << mpiWorld.getRank() << ")" << endl;;
261
262   TimerMPI timerProject (mpiWorld.getComm());
263   raysum_collect (rsLocal, *det, phm, mpiWorld.getMyStartWorkUnit(), opt_trace, FALSE);
264   if (opt_verbose)
265     timerProject.timerEndAndReport ("Time to collect projections");
266
267   TimerMPI timerGather (mpiWorld.getComm());
268   GatherProjectionsMPI (mpiWorld, rsGlobal, rsLocal, opt_debug);
269   if (opt_verbose)
270     timerGather.timerEndAndReport ("Time to gather projections");
271 #else
272   rsGlobal = raysum_create_from_det (opt_outfile, *det);
273   raysum_collect (rsGlobal, *det, phm, 0, opt_trace, FALSE);
274 #endif
275   
276 #ifdef HAVE_MPI
277   if (mpiWorld.getRank() == 0) 
278 #endif
279     {
280       rsGlobal->calctime = timerProgram.timerEnd();
281       rsGlobal->remark = opt_desc;
282       raysum_write (rsGlobal);
283       raysum_close (rsGlobal);
284       if (opt_verbose) {
285         cout << "Remark: " << rsGlobal->remark << endl;
286         cout << "Run time: " << rsGlobal->calctime << " seconds" << endl;
287       }
288     }
289
290   delete det;
291
292   return (0);
293 }
294
295
296 /* FUNCTION
297  *    GatherProjectionsMPI
298  *
299  * SYNOPSIS
300  *    Gather's raysums from all processes in rsLocal in rsGlobal in process 0
301  */
302
303 #ifdef HAVE_MPI
304 void GatherProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug)
305 {
306   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
307     mpiWorld.getComm().Send(&rsLocal->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0);
308     mpiWorld.getComm().Send(&rsLocal->view[iw]->ndet, 1, MPI::INT, 0, 0);
309     mpiWorld.getComm().Send(rsLocal->view[iw]->detval, rsLocal->ndet, MPI::FLOAT, 0, 0);
310   }
311
312   if (mpiWorld.getRank() == 0) {
313     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
314       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
315         MPI::Status status;
316
317         mpiWorld.getComm().Recv(&rsGlobal->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0, status);
318         mpiWorld.getComm().Recv(&rsGlobal->view[iw]->ndet, 1, MPI::INT, iProc, 0, status);
319         mpiWorld.getComm().Recv(rsGlobal->view[iw]->detval, rsGlobal->ndet, MPI::FLOAT, iProc, 0, status);
320       }
321     }
322   }
323
324 }
325 #endif
326
327
328 #ifndef NO_MAIN
329 int 
330 main (int argc, char* argv[])
331 {
332   return (phm2rs_main(argc, argv));
333 }
334 #endif
335