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.6 2000/06/09 11:03:08 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 (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
98 static void print_raysum_info(const RAYSUM *rs);
101 ctrec_main (int argc, char * 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;
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);
129 time_start = MPI::Wtime();
131 time_start = td_current_sec();
135 if (mpiWorld.getRank() == 0) {
138 int c = getopt_long(argc, argv, "", my_options, NULL);
147 if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
148 ctrec_usage(argv[0]);
153 if ((opt_filter = opt_set_filter(optarg)) < 0) {
154 ctrec_usage(argv[0]);
159 if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
160 ctrec_usage(argv[0]);
165 opt_filter_param = strtod(optarg, &endptr);
166 if (endptr != optarg + strlen(optarg)) {
167 ctrec_usage(argv[0]);
177 if ((opt_trace = opt_set_trace(optarg)) < 0) {
178 ctrec_usage(argv[0]);
184 fprintf(stdout, "Version %s\n", VERSION);
186 fprintf(stderr, "Unknown version number");
191 ctrec_usage(argv[0]);
194 ctrec_usage(argv[0]);
199 if (optind + 4 != argc) {
200 ctrec_usage(argv[0]);
204 rs_name = argv[optind];
206 im_filename = argv[optind + 1];
208 nx = strtol(argv[optind + 2], &endptr, 10);
209 ny = strtol(argv[optind + 3], &endptr, 10);
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);
215 snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
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));
221 fprintf (stdout, "%s\n", remark);
227 if (mpiWorld.getRank() == 0) {
228 rs_global = raysum_open (rs_name);
229 raysum_read (rs_global);
231 print_raysum_info(rs_global);
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;
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);
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);
264 mpiWorld.setTotalWorkUnits (mpi_nview);
266 rs_local = raysum_create (NULL, mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
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;
275 mpi_t1 = MPI::Wtime();
276 mpi_scatter_rs(mpiWorld, rs_global, rs_local, opt_debug);
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);
285 if (mpiWorld.getRank() == 0) {
286 im_global = new ImageFile (im_filename, nx, ny);
287 im_global->fileCreate();
290 im_local = new ImageFile (nx, ny);
292 rs_global = raysum_open (rs_name);
293 raysum_read (rs_global);
295 print_raysum_info(rs_global);
297 im_global = new ImageFile (im_filename, nx, ny);
298 im_global->fileCreate();
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);
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);
312 proj_reconst (*im_global, rs_global, opt_filter, opt_filter_param,
313 opt_interp, opt_interp_param, opt_backproj, opt_trace);
318 mpi_t1 = MPI::Wtime();
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();
327 for (int ix = 0; ix < nxLocal; ix++) {
328 void *recvbuf = NULL;
329 if (mpiWorld.getRank() == 0)
330 recvbuf = vGlobal[ix];
332 mpiWorld.getComm().Reduce(vLocal[ix], recvbuf, nyLocal, im_local->getMPIDataType(), MPI::SUM, 0);
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);
343 if (mpiWorld.getRank() == 0)
344 time_end = MPI::Wtime();
346 time_end = td_current_sec();
351 if (mpiWorld.getRank() == 0)
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 ();
361 cout << "Time active = " << calctime << " sec" << endl;
373 static void mpi_scatter_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
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);
385 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
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);
392 rs_local->nview = mpiWorld.getMyLocalWorkUnits();
397 static void print_raysum_info(const RAYSUM *rs)
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);
411 main (int argc, char* argv[])
413 return (ctrec_main(argc, argv));