r72: Initial C++ versions
[ctsim.git] / src / ctrec.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: ctrec.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: ctrec.cpp,v $
7 **  Revision 1.1  2000/06/07 02:29:05  kevin
8 **  Initial C++ versions
9 **
10 **  Revision 1.13  2000/06/05 01:33:25  kevin
11 **  BSpline changes
12 **
13 **  Revision 1.12  2000/05/24 22:50:04  kevin
14 **  Added support for new SGP library
15 **
16 **  Revision 1.11  2000/05/16 04:33:59  kevin
17 **  Improved option processing
18 **
19 **  Revision 1.10  2000/05/11 01:06:30  kevin
20 **  Changed sprintf to snprintf
21 **
22 **  Revision 1.9  2000/05/08 20:02:32  kevin
23 **  ANSI C changes
24 **
25 **  Revision 1.8  2000/05/04 18:16:34  kevin
26 **  renamed filter definitions
27 **
28 **  Revision 1.7  2000/05/03 08:49:50  kevin
29 **  Code cleanup
30 **
31 **  Revision 1.6  2000/05/02 15:31:47  kevin
32 **  code cleaning
33 **
34 **  Revision 1.5  2000/04/30 11:41:06  kevin
35 **  Cleaned up debugging code
36 **
37 **  Revision 1.4  2000/04/30 10:13:27  kevin
38 **  Fixed MPI bugs
39 **
40 **  Revision 1.3  2000/04/30 04:06:13  kevin
41 **  Update Raysum i/o routines
42 **  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
43 **
44 **  Revision 1.2  2000/04/29 23:24:56  kevin
45 **  *** empty log message ***
46 **
47 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
48 **  Initial CVS import for first public release
49 **
50 **
51 **
52 **  This program is free software; you can redistribute it and/or modify
53 **  it under the terms of the GNU General Public License (version 2) as
54 **  published by the Free Software Foundation.
55 **
56 **  This program is distributed in the hope that it will be useful,
57 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
58 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59 **  GNU General Public License for more details.
60 **
61 **  You should have received a copy of the GNU General Public License
62 **  along with this program; if not, write to the Free Software
63 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
64 ******************************************************************************/
65
66 /* FILE
67  *   ctrec.c            Reconstruct an image from raysums
68  *
69  * DATE
70  *   Aug 84
71  *   Jul 99  -- Converted to ANSI C
72  *              Added MPI parallel processing
73  */
74
75 #include "ct.h"
76
77 enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
78
79 static struct option my_options[] =
80 {
81   {"interp", 1, 0, O_INTERP},
82   {"filter", 1, 0, O_FILTER},
83   {"filter-param", 1, 0, O_FILTER_PARAM},
84   {"backproj", 1, 0, O_BACKPROJ},
85   {"trace", 1, 0, O_TRACE},
86   {"debug", 0, 0, O_DEBUG},
87   {"verbose", 0, 0, O_VERBOSE},
88   {"help", 0, 0, O_HELP},
89   {"version", 0, 0, O_VERSION},
90   {0, 0, 0, 0}
91 };
92
93 void 
94 ctrec_usage (const char *program)
95 {
96   fprintf(stdout,"usage: %s raysum-file image-file nx-image ny-image [OPTIONS]\n", kbasename(program));
97   fprintf(stdout,"Image reconstruction from raysum projections\n");
98   fprintf(stdout,"\n");
99   fprintf(stdout,"   raysum-file     Input raysum file\n");
100   fprintf(stdout,"   image-file      Output image file in SDF2D format\n");
101   fprintf(stdout,"   nx-image        Number of columns in output image\n");
102   fprintf(stdout,"   ny-image        Number of rows in output image\n");
103   fprintf(stdout,"   --interp        Interpolation method during backprojection\n");
104   fprintf(stdout,"       nearest     Nearest neighbor interpolation\n");
105   fprintf(stdout,"       linear      Linear interpolation\n");
106 #if HAVE_BSPLINE_INTERP
107   fprintf(stdout,"       bspline     B-spline interpolation\n");
108 #endif
109   fprintf(stdout,"    --filter       Filter name\n");
110   fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting (default)\n");
111   fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
112   fprintf(stdout,"       abs_cos       Abs * Cosine\n");
113   fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
114   fprintf(stdout,"       shepp         Shepp-Logan\n");
115   fprintf(stdout,"       bandlimit     Bandlimiting\n");
116   fprintf(stdout,"       sinc          Sinc\n");
117   fprintf(stdout,"       cos           Cosine\n");
118   fprintf(stdout,"       triangle      Triangle\n");
119   fprintf(stdout,"       hamming       Hamming\n");
120   fprintf(stdout,"    --backproj     Backprojection Method\n");
121   fprintf(stdout,"       trig        Trigometric functions at every point\n");
122   fprintf(stdout,"       table       Trigometric functions with precalculated table\n");
123   fprintf(stdout,"       diff        Difference method\n");
124   fprintf(stdout,"       diff2       Optimized difference method (default)\n");
125   fprintf(stdout,"       idiff2      Optimized difference method with integer math\n");
126   fprintf(stdout,"    --filter-param Alpha level for Hamming filter\n");
127   fprintf(stdout,"    --trace        Set tracing to level\n");
128   fprintf(stdout,"         none      No tracing (default)\n");
129   fprintf(stdout,"         text      Text level tracing\n");
130   fprintf(stdout,"         phm       Trace phantom\n");
131   fprintf(stdout,"         rays      Trace allrays\n");
132   fprintf(stdout,"         plot      Trace plotting\n");
133   fprintf(stdout,"         clipping  Trace clipping\n");
134   fprintf(stdout,"    --verbose      Turn on verbose mode\n");
135   fprintf(stdout,"    --debug        Turn on debug mode\n");
136   fprintf(stdout,"    --version      Print version\n");
137   fprintf(stdout,"    --help         Print this help message\n");
138 }
139
140
141 #ifdef MPI_CT
142 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
143 #endif
144
145 static void print_raysum_info(const RAYSUM *rs);
146
147 int 
148 ctrec_main (const int argc, char *const argv[])
149 {
150   IMAGE *im_global = NULL;
151   RAYSUM *rs_global = NULL;
152   char *rs_name, *im_filename = NULL;
153   char remark[MAXREMARK];
154   char filt_name[80];
155   int nx, ny;
156   double time_start = 0, time_end = 0;
157   char *endptr;
158   int opt_verbose = 0;
159   int opt_debug = 0;
160   int opt_trace = TRACE_NONE;
161   double opt_filter_param = 1;
162   FilterType opt_filter = FILTER_ABS_BANDLIMIT;
163   InterpolationType opt_interp = I_LINEAR;
164   int opt_interp_param = 1;
165   BackprojType opt_backproj = O_BPROJ_DIFF2;
166 #ifdef MPI_CT
167   IMAGE *im_local;
168   RAYSUM *rs_local;
169   int mpi_argc = argc;
170   char **mpi_argv = (char **) argv;
171   int mpi_nview, mpi_ndet;
172   double mpi_detinc, mpi_rotinc, mpi_phmlen;
173   double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
174   int ix;
175
176   MPI_Init(&mpi_argc, &mpi_argv);
177   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
178   MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
179   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
180
181   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
182     sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
183               mpi_ct.nproc, MPI_MAX_PROCESS);
184     exit(1);
185   }
186 #endif
187
188 #ifdef MPI_CT
189   time_start = MPI_Wtime();
190 #else
191   time_start = td_current_sec();
192 #endif
193
194 #ifdef MPI_CT
195   if (mpi_ct.my_rank == 0) {
196 #endif
197     while (1) {
198       int c = getopt_long(argc, argv, "", my_options, NULL);
199       char *endptr = NULL;
200       
201       if (c == -1)
202         break;
203       
204       switch (c)
205         {
206         case O_INTERP:
207           if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
208             ctrec_usage(argv[0]);
209             return (1);
210           }
211           break;
212         case O_FILTER:
213           if ((opt_filter = opt_set_filter(optarg)) < 0) {
214             ctrec_usage(argv[0]);
215             return (1);
216           }
217           break;
218         case O_BACKPROJ:
219           if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
220               ctrec_usage(argv[0]);
221               return (1);
222           }
223           break;
224         case O_FILTER_PARAM:
225           opt_filter_param = strtod(optarg, &endptr);
226           if (endptr != optarg + strlen(optarg)) {
227             ctrec_usage(argv[0]);
228           }
229           break;
230         case O_VERBOSE:
231           opt_verbose = 1;
232           break;
233         case O_DEBUG:
234           opt_debug = 1;
235           break;
236         case O_TRACE:
237           if ((opt_trace = opt_set_trace(optarg)) < 0) {
238             ctrec_usage(argv[0]);
239             return (1);
240           }
241           break;
242         case O_VERSION:
243 #ifdef VERSION
244           fprintf(stdout, "Version %s\n", VERSION);
245 #else
246           fprintf(stderr, "Unknown version number");
247 #endif
248           exit(0);
249         case O_HELP:
250         case '?':
251           ctrec_usage(argv[0]);
252           return (0);
253         default:
254           ctrec_usage(argv[0]);
255           return (1);
256         }
257     }
258   
259     if (optind + 4 != argc) {
260       ctrec_usage(argv[0]);
261       return (1);
262     }
263
264     rs_name = argv[optind];
265   
266     im_filename = argv[optind + 1];
267   
268     nx = strtol(argv[optind + 2], &endptr, 10);
269     ny = strtol(argv[optind + 3], &endptr, 10);
270   
271     if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
272       snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
273                name_of_filter (opt_filter), opt_filter_param); 
274     else
275       snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
276   
277     snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
278              nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
279   
280     if (opt_verbose)
281       fprintf (stdout, "%s\n", remark);
282 #ifdef MPI_CT
283   }
284 #endif
285
286 #ifdef MPI_CT
287   if (mpi_ct.my_rank == 0) {
288     rs_global = raysum_open (rs_name);
289     raysum_read (rs_global);
290     if (opt_verbose)
291       print_raysum_info(rs_global);
292
293     mpi_ndet = rs_global->ndet;
294     mpi_nview = rs_global->nview;
295     mpi_detinc = rs_global->det_inc;
296     mpi_phmlen = rs_global->phmlen;
297     mpi_rotinc = rs_global->rot_inc;
298   }
299
300   mpi_t1 = MPI_Wtime();
301   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
302   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
303   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
304   MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
305   MPI_Bcast(&opt_interp, 1, MPI_INT, 0, mpi_ct.comm);
306   MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
307   MPI_Bcast(&opt_interp_param, 1, MPI_INT, 0, mpi_ct.comm);
308   MPI_Bcast(&opt_backproj, 1, MPI_INT, 0, mpi_ct.comm);
309   MPI_Bcast(&mpi_ndet, 1, MPI_INT, 0, mpi_ct.comm);
310   MPI_Bcast(&mpi_nview, 1, MPI_INT, 0, mpi_ct.comm);
311   MPI_Bcast(&mpi_detinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
312   MPI_Bcast(&mpi_phmlen, 1, MPI_DOUBLE, 0, mpi_ct.comm);
313   MPI_Bcast(&mpi_rotinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
314   MPI_Bcast(&nx, 1, MPI_INT, 0, mpi_ct.comm);
315   MPI_Bcast(&ny, 1, MPI_INT, 0, mpi_ct.comm);
316   if (opt_verbose) {
317     mpi_t2 = MPI_Wtime();
318     mpi_t = mpi_t2 - mpi_t1;
319     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
320     if (mpi_ct.my_rank == 0)
321       printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
322   }
323
324   mpi_ct_calc_work_units(mpi_nview);
325
326   rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
327
328   rs_local->ndet = mpi_ndet;
329   rs_local->nview = mpi_nview;
330   rs_local->det_inc = mpi_detinc;
331   rs_local->phmlen = mpi_phmlen;
332   rs_local->rot_inc = mpi_rotinc;
333
334   if (opt_verbose)
335     mpi_t1 = MPI_Wtime();
336   mpi_scatter_rs(rs_global, rs_local, opt_debug);
337   if (opt_verbose) {
338     mpi_t2 = MPI_Wtime();
339     mpi_t = mpi_t2 - mpi_t1;
340     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
341     if (mpi_ct.my_rank == 0)
342       printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
343   }
344
345   if (mpi_ct.my_rank == 0) {
346     im_global = image_create (im_filename, nx, ny);
347     sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
348     sdf_add_empty_label (im_global->dfp_2d->dfp);
349   }
350   im_local = image_create (NULL, nx, ny);
351
352 #else
353   rs_global = raysum_open (rs_name);
354   raysum_read (rs_global);
355   if (opt_verbose)
356     print_raysum_info(rs_global);
357
358   im_global = image_create (im_filename, nx, ny);
359   sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
360   sdf_add_empty_label (im_global->dfp_2d->dfp);
361 #endif
362
363 #ifdef MPI_CT
364   mpi_t1 = MPI_Wtime();
365   image_reconst (im_local, rs_local, opt_filter, opt_filter_param, 
366                  opt_interp, opt_interp_param, opt_backproj, opt_trace);
367   mpi_t2 = MPI_Wtime();
368   mpi_t = mpi_t2 - mpi_t1;
369   MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
370   if (mpi_ct.my_rank == 0 && opt_verbose)
371     printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
372 #else
373   image_reconst (im_global, rs_global, opt_filter, opt_filter_param, 
374                  opt_interp, opt_interp_param, opt_backproj, opt_trace);
375 #endif
376
377 #ifdef MPI_CT
378   if (opt_verbose)
379     mpi_t1 = MPI_Wtime();
380
381   for (ix = 0; ix < im_local->nx; ix++) {
382     void *recvbuf = NULL;
383     if (mpi_ct.my_rank == 0)
384       recvbuf = im_global->v[ix];
385
386     MPI_Reduce(im_local->v[ix], recvbuf, im_local->ny, MPI_FLOAT, MPI_SUM, 0, mpi_ct.comm);
387   }
388
389   if (opt_verbose) {
390     mpi_t2 = MPI_Wtime();
391     mpi_t = mpi_t2 - mpi_t1;
392     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
393     if (mpi_ct.my_rank == 0)
394       printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
395   }
396
397   if (mpi_ct.my_rank == 0)
398       time_end = MPI_Wtime();
399 #else
400   time_end = td_current_sec();
401 #endif
402
403
404 #ifdef MPI_CT
405   if (mpi_ct.my_rank == 0)
406 #endif
407     {
408       raysum_close (rs_global);
409       strncpy (im_global->remark, remark, sizeof(im_global->remark));
410       im_global->calctime = time_end - time_start;
411       image_save (im_global);
412       if (opt_verbose)
413           fprintf (stdout, "Time active = %.2f\n", im_global->calctime);
414     }
415
416 #ifdef MPI_CT
417         MPI_Finalize();
418 #endif
419
420         return (0);
421 }
422
423
424 #ifdef MPI_CT
425 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
426 {
427   int iproc;
428   int end_work_unit;
429   int iw = 0;
430
431   if (mpi_ct.my_rank == 0) {
432     for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
433       end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc]  - 1;
434
435       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
436         MPI_Send(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm);
437         MPI_Send(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm);
438         MPI_Send(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm);
439       }
440     }
441   }
442
443   if (opt_debug)
444     fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
445
446   end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
447   for (iw = 0; iw <= end_work_unit; iw++) {
448     MPI_Status status;
449
450     MPI_Recv(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm, &status);
451     MPI_Recv(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm, &status);
452     MPI_Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm, &status);
453   }
454   rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
455   if (opt_debug) {
456     MPI_Barrier(MPI_COMM_WORLD);
457     fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
458   }
459 }
460
461 #endif
462
463 static void print_raysum_info(const RAYSUM *rs)
464 {
465   printf ("Number of detectors: %d\n", rs->ndet);
466   printf ("    Number of views: %d\n", rs->nview);
467   printf ("             Remark: %s\n", rs->remark);
468   printf ("             phmlen: %f\n", rs->phmlen);
469   printf ("          det_start: %f\n", rs->det_start);
470   printf ("            det_inc: %f\n", rs->det_inc);
471   printf ("          rot_start: %f\n", rs->rot_start);
472   printf ("            rot_inc: %f\n", rs->rot_inc);
473 }
474
475 #ifndef NO_MAIN
476 int 
477 main (const int argc, char *const argv[])
478 {
479   return (ctrec_main(argc, argv));
480 }
481 #endif
482