1 /*****************************************************************************
5 ** Purpose: Reconstruct an image from projections
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Aug 1984
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: ctrec.cpp,v 1.4 2000/06/08 16:43:10 kevin Exp $
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.
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.
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 ******************************************************************************/
30 enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
32 static struct option my_options[] =
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},
47 ctrec_usage (const char *program)
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");
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");
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");
95 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
98 static void print_raysum_info(const RAYSUM *rs);
101 ctrec_main (const int argc, char *const argv[])
103 ImageFile *im_global = NULL;
104 RAYSUM *rs_global = NULL;
105 char *rs_name, *im_filename = NULL;
106 char remark[MAXREMARK];
108 double time_start = 0, time_end = 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;
123 char **mpi_argv = (char **) argv;
124 int mpi_nview, mpi_ndet;
125 double mpi_detinc, mpi_rotinc, mpi_phmlen;
126 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
128 MPI::Init (mpi_argc, mpi_argv);
129 mpi_ct.comm = MPI::COMM_WORLD.Dup();
130 mpi_ct.nproc = mpi_ct.comm.Get_size();
131 mpi_ct.my_rank = mpi_ct.comm.Get_rank();
133 if (mpi_ct.nproc > CT_MPI_MAX_PROCESS) {
134 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
135 mpi_ct.nproc, CT_MPI_MAX_PROCESS);
141 time_start = MPI::Wtime();
143 time_start = td_current_sec();
147 if (mpi_ct.my_rank == 0) {
150 int c = getopt_long(argc, argv, "", my_options, NULL);
159 if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
160 ctrec_usage(argv[0]);
165 if ((opt_filter = opt_set_filter(optarg)) < 0) {
166 ctrec_usage(argv[0]);
171 if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
172 ctrec_usage(argv[0]);
177 opt_filter_param = strtod(optarg, &endptr);
178 if (endptr != optarg + strlen(optarg)) {
179 ctrec_usage(argv[0]);
189 if ((opt_trace = opt_set_trace(optarg)) < 0) {
190 ctrec_usage(argv[0]);
196 fprintf(stdout, "Version %s\n", VERSION);
198 fprintf(stderr, "Unknown version number");
203 ctrec_usage(argv[0]);
206 ctrec_usage(argv[0]);
211 if (optind + 4 != argc) {
212 ctrec_usage(argv[0]);
216 rs_name = argv[optind];
218 im_filename = argv[optind + 1];
220 nx = strtol(argv[optind + 2], &endptr, 10);
221 ny = strtol(argv[optind + 3], &endptr, 10);
223 if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
224 snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
225 name_of_filter (opt_filter), opt_filter_param);
227 snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
229 snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
230 nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
233 fprintf (stdout, "%s\n", remark);
239 if (mpi_ct.my_rank == 0) {
240 rs_global = raysum_open (rs_name);
241 raysum_read (rs_global);
243 print_raysum_info(rs_global);
245 mpi_ndet = rs_global->ndet;
246 mpi_nview = rs_global->nview;
247 mpi_detinc = rs_global->det_inc;
248 mpi_phmlen = rs_global->phmlen;
249 mpi_rotinc = rs_global->rot_inc;
252 mpi_t1 = MPI::Wtime();
253 mpi_ct.comm.Bcast (&opt_verbose, 1, MPI::INT, 0);
254 mpi_ct.comm.Bcast (&opt_debug, 1, MPI::INT, 0);
255 mpi_ct.comm.Bcast (&opt_trace, 1, MPI::INT, 0);
256 mpi_ct.comm.Bcast (&opt_filter, 1, MPI::INT, 0);
257 mpi_ct.comm.Bcast (&opt_interp, 1, MPI::INT, 0);
258 mpi_ct.comm.Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
259 mpi_ct.comm.Bcast (&opt_interp_param, 1, MPI::INT, 0);
260 mpi_ct.comm.Bcast (&opt_backproj, 1, MPI::INT, 0);
261 mpi_ct.comm.Bcast (&mpi_ndet, 1, MPI::INT, 0);
262 mpi_ct.comm.Bcast (&mpi_nview, 1, MPI::INT, 0);
263 mpi_ct.comm.Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
264 mpi_ct.comm.Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
265 mpi_ct.comm.Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
266 mpi_ct.comm.Bcast (&nx, 1, MPI::INT, 0);
267 mpi_ct.comm.Bcast (&ny, 1, MPI::INT, 0);
269 mpi_t2 = MPI::Wtime();
270 mpi_t = mpi_t2 - mpi_t1;
271 mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
272 if (mpi_ct.my_rank == 0)
273 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
276 mpi_ct_calc_work_units(mpi_nview);
278 rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
280 rs_local->ndet = mpi_ndet;
281 rs_local->nview = mpi_nview;
282 rs_local->det_inc = mpi_detinc;
283 rs_local->phmlen = mpi_phmlen;
284 rs_local->rot_inc = mpi_rotinc;
287 mpi_t1 = MPI::Wtime();
288 mpi_scatter_rs(rs_global, rs_local, opt_debug);
290 mpi_t2 = MPI::Wtime();
291 mpi_t = mpi_t2 - mpi_t1;
292 mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
293 if (mpi_ct.my_rank == 0)
294 printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
297 if (mpi_ct.my_rank == 0) {
298 im_global = new ImageFile (im_filename, nx, ny);
299 im_global->adf.fileCreate();
302 im_local = new ImageFile (nx, ny);
304 rs_global = raysum_open (rs_name);
305 raysum_read (rs_global);
307 print_raysum_info(rs_global);
309 im_global = new ImageFile (im_filename, nx, ny);
310 im_global->adf.fileCreate();
314 mpi_t1 = MPI::Wtime();
315 proj_reconst (*im_local, rs_local, opt_filter, opt_filter_param,
316 opt_interp, opt_interp_param, opt_backproj, opt_trace);
318 mpi_t2 = MPI::Wtime();
319 mpi_t = mpi_t2 - mpi_t1;
320 mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
321 if (mpi_ct.my_rank == 0 && opt_verbose)
322 printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
324 proj_reconst (*im_global, rs_global, opt_filter, opt_filter_param,
325 opt_interp, opt_interp_param, opt_backproj, opt_trace);
330 mpi_t1 = MPI::Wtime();
332 int nxLocal = im_local->adf.nx();
333 int nyLocal = im_local->adf.ny();
334 ImageFileArray vLocal = im_local->getArray();
335 ImageFileArray vGlobal = NULL;
336 if (mpi_ct.my_rank == 0)
337 vGlobal = im_global->getArray();
339 for (int ix = 0; ix < nxLocal; ix++) {
340 void *recvbuf = NULL;
341 if (mpi_ct.my_rank == 0)
342 recvbuf = vGlobal[ix];
344 mpi_ct.comm.Reduce(vLocal[ix], recvbuf, nyLocal, im_local->getMPIDataType(), MPI::SUM, 0);
348 mpi_t2 = MPI::Wtime();
349 mpi_t = mpi_t2 - mpi_t1;
350 mpi_ct.comm.Reduce (&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
351 if (mpi_ct.my_rank == 0)
352 printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
355 if (mpi_ct.my_rank == 0)
356 time_end = MPI::Wtime();
358 time_end = td_current_sec();
363 if (mpi_ct.my_rank == 0)
366 raysum_close (rs_global);
367 double calctime = time_end - time_start;
368 im_global->adf.arrayDataWrite ();
369 im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, rs_global->remark, rs_global->calctime);
370 im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, remark, calctime);
371 im_global->adf.fileClose ();
373 cout << "Time active = " << calctime << " sec" << endl;
385 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
391 if (mpi_ct.my_rank == 0) {
392 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
393 end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc] - 1;
395 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
396 mpi_ct.comm.Send(&rs_global->view[iw]->ndet, 1, MPI::INT, iproc, 0);
397 mpi_ct.comm.Send(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iproc, 0);
398 mpi_ct.comm.Send(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iproc, 0);
404 fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
406 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
407 for (iw = 0; iw <= end_work_unit; iw++) {
410 mpi_ct.comm.Recv(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
411 mpi_ct.comm.Recv(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
412 mpi_ct.comm.Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0, status);
414 rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
416 mpi_ct.comm.Barrier();
417 fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
423 static void print_raysum_info(const RAYSUM *rs)
425 printf ("Number of detectors: %d\n", rs->ndet);
426 printf (" Number of views: %d\n", rs->nview);
427 printf (" Remark: %s\n", rs->remark);
428 printf (" phmlen: %f\n", rs->phmlen);
429 printf (" det_start: %f\n", rs->det_start);
430 printf (" det_inc: %f\n", rs->det_inc);
431 printf (" rot_start: %f\n", rs->rot_start);
432 printf (" rot_inc: %f\n", rs->rot_inc);
437 main (const int argc, char *const argv[])
439 return (ctrec_main(argc, argv));