r94: finished c++ conversions
[ctsim.git] / src / ctrec.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctrec.cpp
5 **   Purpose:       Reconstruct an image from projections
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Aug 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctrec.cpp,v 1.8 2000/06/13 16:20:31 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_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
33
34 static struct option my_options[] =
35 {
36   {"interp", 1, 0, O_INTERP},
37   {"filter", 1, 0, O_FILTER},
38   {"filter-param", 1, 0, O_FILTER_PARAM},
39   {"backproj", 1, 0, O_BACKPROJ},
40   {"trace", 1, 0, O_TRACE},
41   {"debug", 0, 0, O_DEBUG},
42   {"verbose", 0, 0, O_VERBOSE},
43   {"help", 0, 0, O_HELP},
44   {"version", 0, 0, O_VERSION},
45   {0, 0, 0, 0}
46 };
47
48
49 void 
50 ctrec_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
53   cout << "Image reconstruction from raysum projections" << endl;
54   cout << endl;
55   cout << "   raysum-file     Input raysum file" << endl;
56   cout << "   image-file      Output image file in SDF2D format" << endl;
57   cout << "   nx-image        Number of columns in output image" << endl;
58   cout << "   ny-image        Number of rows in output image" << endl;
59   cout << "   --interp        Interpolation method during backprojection" << endl;
60   cout << "       nearest     Nearest neighbor interpolation" << endl;
61   cout << "       linear      Linear interpolation" << endl;
62 #if HAVE_BSPLINE_INTERP
63   cout << "       bspline     B-spline interpolation" << endl;
64 #endif
65   cout << "    --filter       Filter name" << endl;
66   cout << "       abs_bandlimit Abs * Bandlimiting (default)" << endl;
67   cout << "       abs_sinc      Abs * Sinc" << endl;
68   cout << "       abs_cos       Abs * Cosine" << endl;
69   cout << "       abs_hamming   Abs * Hamming" << endl;
70   cout << "       shepp         Shepp-Logan" << endl;
71   cout << "       bandlimit     Bandlimiting" << endl;
72   cout << "       sinc          Sinc" << endl;
73   cout << "       cos           Cosine" << endl;
74   cout << "       triangle      Triangle" << endl;
75   cout << "       hamming       Hamming" << endl;
76   cout << "    --backproj     Backprojection Method" << endl;
77   cout << "       trig        Trigometric functions at every point" << endl;
78   cout << "       table       Trigometric functions with precalculated table" << endl;
79   cout << "       diff        Difference method" << endl;
80   cout << "       diff2       Optimized difference method (default)" << endl;
81   cout << "       idiff2      Optimized difference method with integer math" << endl;
82   cout << "    --filter-param Alpha level for Hamming filter" << endl;
83   cout << "    --trace        Set tracing to level" << endl;
84   cout << "         none      No tracing (default)" << endl;
85   cout << "         text      Text level tracing" << endl;
86   cout << "         phm       Trace phantom" << endl;
87   cout << "         rays      Trace allrays" << endl;
88   cout << "         plot      Trace plotting" << endl;
89   cout << "         clipping  Trace clipping" << endl;
90   cout << "    --verbose      Turn on verbose mode" << endl;
91   cout << "    --debug        Turn on debug mode" << endl;
92   cout << "    --version      Print version" << endl;
93   cout << "    --help         Print this help message" << endl;
94 }
95
96
97 #ifdef HAVE_MPI
98 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int debug);
99 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
100 #endif
101
102
103 int 
104 ctrec_main (int argc, char * argv[])
105 {
106   ImageFile *imGlobal = NULL;
107   RAYSUM *rsGlobal = NULL;
108   char *rs_name, *im_filename = NULL;
109   char remark[MAXREMARK];
110   char filt_name[80];
111   char *endptr;
112   int opt_verbose = 0;
113   int opt_debug = 0;
114   int opt_trace = TRACE_NONE;
115   double opt_filter_param = 1;
116   FilterType opt_filter = FILTER_ABS_BANDLIMIT;
117   InterpolationType opt_interp = I_LINEAR;
118   int opt_interp_param = 1;
119   BackprojType opt_backproj = O_BPROJ_DIFF2;
120   int nx, ny;
121 #ifdef HAVE_MPI
122   ImageFile *imLocal;
123   RAYSUM *rsLocal;
124   int mpi_nview, mpi_ndet;
125   double mpi_detinc, mpi_rotinc, mpi_phmlen;
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, "", my_options, NULL);
136       char *endptr = NULL;
137       
138       if (c == -1)
139         break;
140       
141       switch (c)
142         {
143         case O_INTERP:
144           if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
145             ctrec_usage(argv[0]);
146             return (1);
147           }
148           break;
149         case O_FILTER:
150           if ((opt_filter = opt_set_filter(optarg)) < 0) {
151             ctrec_usage(argv[0]);
152             return (1);
153           }
154           break;
155         case O_BACKPROJ:
156           if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
157               ctrec_usage(argv[0]);
158               return (1);
159           }
160           break;
161         case O_FILTER_PARAM:
162           opt_filter_param = strtod(optarg, &endptr);
163           if (endptr != optarg + strlen(optarg)) {
164             ctrec_usage(argv[0]);
165           }
166           break;
167         case O_VERBOSE:
168           opt_verbose = 1;
169           break;
170         case O_DEBUG:
171           opt_debug = 1;
172           break;
173         case O_TRACE:
174           if ((opt_trace = opt_set_trace(optarg)) < 0) {
175             ctrec_usage(argv[0]);
176             return (1);
177           }
178           break;
179         case O_VERSION:
180 #ifdef VERSION
181         cout <<  "Version " <<  VERSION << endl;
182 #else
183         cout << "Unknown version number" << endl;
184 #endif
185           exit(0);
186         case O_HELP:
187         case '?':
188           ctrec_usage(argv[0]);
189           return (0);
190         default:
191           ctrec_usage(argv[0]);
192           return (1);
193         }
194     }
195   
196     if (optind + 4 != argc) {
197       ctrec_usage(argv[0]);
198       return (1);
199     }
200
201     rs_name = argv[optind];
202   
203     im_filename = argv[optind + 1];
204   
205     nx = strtol(argv[optind + 2], &endptr, 10);
206     ny = strtol(argv[optind + 3], &endptr, 10);
207   
208     if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
209       snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
210                name_of_filter (opt_filter), opt_filter_param); 
211     else
212       snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
213   
214     snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
215              nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
216   
217     if (opt_verbose)
218       cout << "Remark: " << remark << endl;
219 #ifdef HAVE_MPI
220   }
221 #endif
222
223 #ifdef HAVE_MPI
224   if (mpiWorld.getRank() == 0) {
225     rsGlobal = raysum_open (rs_name);
226     raysum_read (rsGlobal);
227     if (opt_verbose)
228       raysum_print_info(rsGlobal);
229
230     mpi_ndet = rsGlobal->ndet;
231     mpi_nview = rsGlobal->nview;
232     mpi_detinc = rsGlobal->det_inc;
233     mpi_phmlen = rsGlobal->phmlen;
234     mpi_rotinc = rsGlobal->rot_inc;
235   }
236
237   TimerCollectiveMPI timerBcast (mpiWorld.getComm());
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   mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
242   mpiWorld.getComm().Bcast (&opt_interp, 1, MPI::INT, 0);
243   mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
244   mpiWorld.getComm().Bcast (&opt_interp_param, 1, MPI::INT, 0);
245   mpiWorld.getComm().Bcast (&opt_backproj, 1, MPI::INT, 0);
246   mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
247   mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
248   mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
249   mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
250   mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
251   mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
252   mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
253   if (opt_verbose)
254       timerBcast.timerEndAndReport ("Time to broadcast variables");
255
256   mpiWorld.setTotalWorkUnits (mpi_nview);
257
258   rsLocal = raysum_create (NULL, mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
259   rsLocal->ndet = mpi_ndet;
260   rsLocal->nview = mpi_nview;
261   rsLocal->det_inc = mpi_detinc;
262   rsLocal->phmlen = mpi_phmlen;
263   rsLocal->rot_inc = mpi_rotinc;
264
265   TimerCollectiveMPI timerScatter (mpiWorld.getComm());
266   ScatterProjectionsMPI (mpiWorld, rsGlobal, rsLocal, opt_debug);
267   if (opt_verbose)
268       timerScatter.timerEndAndReport ("Time to scatter projections");
269
270   if (mpiWorld.getRank() == 0) {
271     imGlobal = new ImageFile (im_filename, nx, ny);
272     imGlobal->fileCreate();
273   }
274
275   imLocal = new ImageFile (nx, ny);
276 #else
277   rsGlobal = raysum_open (rs_name);
278   raysum_read (rsGlobal);
279   if (opt_verbose)
280     raysum_print_info(rsGlobal);
281
282   imGlobal = new ImageFile (im_filename, nx, ny);
283   imGlobal->fileCreate();
284 #endif
285
286 #ifdef HAVE_MPI
287   TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
288   proj_reconst (*imLocal, rsLocal, opt_filter, opt_filter_param, opt_interp, opt_interp_param, opt_backproj, opt_trace);
289   if (opt_verbose)
290       timerReconstruct.timerEndAndReport ("Time to reconstruct");
291
292   TimerCollectiveMPI timerReduce (mpiWorld.getComm());
293   ReduceImageMPI (mpiWorld, imLocal, imGlobal);
294   if (opt_verbose)
295       timerReduce.timerEndAndReport ("Time to reduce image");
296 #else
297   proj_reconst (*imGlobal, rsGlobal, opt_filter, opt_filter_param, opt_interp, opt_interp_param, opt_backproj, opt_trace);
298 #endif
299
300 #ifdef HAVE_MPI
301   if (mpiWorld.getRank() == 0)
302 #endif
303     {
304       raysum_close (rsGlobal);
305       double calctime = timerProgram.timerEnd();
306       imGlobal->arrayDataWrite ();
307       imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, rsGlobal->remark, rsGlobal->calctime);
308       imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark, calctime);
309       imGlobal->fileClose ();
310       if (opt_verbose)
311         cout << "Run time: " << calctime << " seconds" << endl;
312     }
313 #ifdef HAVE_MPI
314   MPI::Finalize();
315 #endif
316
317   return (0);
318 }
319
320
321 //////////////////////////////////////////////////////////////////////////////////////
322 // MPI Support Routines
323 //
324 //////////////////////////////////////////////////////////////////////////////////////
325
326 #ifdef HAVE_MPI
327 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug)
328 {
329   if (mpiWorld.getRank() == 0) {
330     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
331       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
332         mpiWorld.getComm().Send(&rsGlobal->view[iw]->ndet, 1, MPI::INT, iProc, 0);
333         mpiWorld.getComm().Send(&rsGlobal->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0);
334         mpiWorld.getComm().Send(rsGlobal->view[iw]->detval, rsGlobal->ndet, MPI::FLOAT, iProc, 0);
335       }
336     }
337   }
338
339   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
340     MPI::Status status;
341     mpiWorld.getComm().Recv(&rsLocal->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
342     mpiWorld.getComm().Recv(&rsLocal->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
343     mpiWorld.getComm().Recv(rsLocal->view[iw]->detval, rsLocal->ndet, MPI::FLOAT, 0, 0, status);
344   }
345   rsLocal->nview = mpiWorld.getMyLocalWorkUnits();
346 }
347
348 static void
349 ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
350 {
351   ImageFileArray vLocal = imLocal->getArray();
352
353   for (int ix = 0; ix < imLocal->nx(); ix++) {
354     void *recvbuf = NULL;
355     if (mpiWorld.getRank() == 0) {
356       ImageFileArray vGlobal = imGlobal->getArray();
357       recvbuf = vGlobal[ix];
358     }
359     mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
360   }
361 }
362
363 #endif
364
365
366 #ifndef NO_MAIN
367 int 
368 main (int argc, char* argv[])
369 {
370   return (ctrec_main(argc, argv));
371 }
372 #endif
373