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