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