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.7 2000/06/10 22:33:11 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 cout << "usage: " << kbasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
50 cout << "Image reconstruction from raysum projections" << 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;
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;
95 static void mpi_scatter_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
100 ctrec_main (int argc, char * argv[])
102 ImageFile *im_global = NULL;
103 RAYSUM *rs_global = NULL;
104 char *rs_name, *im_filename = NULL;
105 char remark[MAXREMARK];
107 double time_start = 0, time_end = 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;
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);
128 time_start = MPI::Wtime();
130 time_start = td_current_sec();
134 if (mpiWorld.getRank() == 0) {
137 int c = getopt_long(argc, argv, "", my_options, NULL);
146 if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
147 ctrec_usage(argv[0]);
152 if ((opt_filter = opt_set_filter(optarg)) < 0) {
153 ctrec_usage(argv[0]);
158 if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
159 ctrec_usage(argv[0]);
164 opt_filter_param = strtod(optarg, &endptr);
165 if (endptr != optarg + strlen(optarg)) {
166 ctrec_usage(argv[0]);
176 if ((opt_trace = opt_set_trace(optarg)) < 0) {
177 ctrec_usage(argv[0]);
183 cout << "Version " << VERSION << endl;
185 cout << "Unknown version number" << endl;
190 ctrec_usage(argv[0]);
193 ctrec_usage(argv[0]);
198 if (optind + 4 != argc) {
199 ctrec_usage(argv[0]);
203 rs_name = argv[optind];
205 im_filename = argv[optind + 1];
207 nx = strtol(argv[optind + 2], &endptr, 10);
208 ny = strtol(argv[optind + 3], &endptr, 10);
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);
214 snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
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));
220 fprintf (stdout, "%s\n", remark);
226 if (mpiWorld.getRank() == 0) {
227 rs_global = raysum_open (rs_name);
228 raysum_read (rs_global);
230 raysum_print_info(rs_global);
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;
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);
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);
263 mpiWorld.setTotalWorkUnits (mpi_nview);
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;
273 mpi_t1 = MPI::Wtime();
274 mpi_scatter_rs(mpiWorld, rs_global, rs_local, opt_debug);
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);
283 if (mpiWorld.getRank() == 0) {
284 im_global = new ImageFile (im_filename, nx, ny);
285 im_global->fileCreate();
288 im_local = new ImageFile (nx, ny);
290 rs_global = raysum_open (rs_name);
291 raysum_read (rs_global);
293 raysum_print_info(rs_global);
295 im_global = new ImageFile (im_filename, nx, ny);
296 im_global->fileCreate();
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);
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);
310 proj_reconst (*im_global, rs_global, opt_filter, opt_filter_param,
311 opt_interp, opt_interp_param, opt_backproj, opt_trace);
316 mpi_t1 = MPI::Wtime();
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();
325 for (int ix = 0; ix < nxLocal; ix++) {
326 void *recvbuf = NULL;
327 if (mpiWorld.getRank() == 0)
328 recvbuf = vGlobal[ix];
330 mpiWorld.getComm().Reduce(vLocal[ix], recvbuf, nyLocal, im_local->getMPIDataType(), MPI::SUM, 0);
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);
341 if (mpiWorld.getRank() == 0)
342 time_end = MPI::Wtime();
344 time_end = td_current_sec();
348 if (mpiWorld.getRank() == 0)
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 ();
358 cout << "Time active = " << calctime << " sec" << endl;
369 static void mpi_scatter_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
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);
381 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
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);
388 rs_local->nview = mpiWorld.getMyLocalWorkUnits();
395 main (int argc, char* argv[])
397 return (ctrec_main(argc, argv));