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: pjrec.cpp,v 1.4 2000/06/29 13:21:14 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 ******************************************************************************/
32 enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
34 static struct option my_options[] =
36 {"interp", 1, 0, O_INTERP},
37 {"filter", 1, 0, O_FILTER},
38 {"filter-param", 1, 0, O_FILTER_PARAM},
39 {"backproj", 1, 0, O_BACKPROJ},
40 {"trace", 1, 0, O_TRACE},
41 {"debug", 0, 0, O_DEBUG},
42 {"verbose", 0, 0, O_VERBOSE},
43 {"help", 0, 0, O_HELP},
44 {"version", 0, 0, O_VERSION},
50 pjrec_usage (const char *program)
52 cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
53 cout << "Image reconstruction from raysum projections" << endl;
55 cout << " raysum-file Input raysum file" << endl;
56 cout << " image-file Output image file in SDF2D format" << endl;
57 cout << " nx-image Number of columns in output image" << endl;
58 cout << " ny-image Number of rows in output image" << endl;
59 cout << " --interp Interpolation method during backprojection" << endl;
60 cout << " nearest Nearest neighbor interpolation" << endl;
61 cout << " linear Linear interpolation" << endl;
62 #if HAVE_BSPLINE_INTERP
63 cout << " bspline B-spline interpolation" << endl;
65 cout << " --filter Filter name" << endl;
66 cout << " abs_bandlimit Abs * Bandlimiting (default)" << endl;
67 cout << " abs_sinc Abs * Sinc" << endl;
68 cout << " abs_cos Abs * Cosine" << endl;
69 cout << " abs_hamming Abs * Hamming" << endl;
70 cout << " shepp Shepp-Logan" << endl;
71 cout << " bandlimit Bandlimiting" << endl;
72 cout << " sinc Sinc" << endl;
73 cout << " cos Cosine" << endl;
74 cout << " triangle Triangle" << endl;
75 cout << " hamming Hamming" << endl;
76 cout << " --backproj Backprojection Method" << endl;
77 cout << " trig Trigometric functions at every point" << endl;
78 cout << " table Trigometric functions with precalculated table" << endl;
79 cout << " diff Difference method" << endl;
80 cout << " diff2 Optimized difference method (default)" << endl;
81 cout << " idiff2 Optimized difference method with integer math" << endl;
82 cout << " --filter-param Alpha level for Hamming filter" << endl;
83 cout << " --trace Set tracing to level" << endl;
84 cout << " none No tracing (default)" << endl;
85 cout << " text Text level tracing" << endl;
86 cout << " phm Trace phantom" << endl;
87 cout << " rays Trace allrays" << endl;
88 cout << " plot Trace plotting" << endl;
89 cout << " clipping Trace clipping" << endl;
90 cout << " --verbose Turn on verbose mode" << endl;
91 cout << " --debug Turn on debug mode" << endl;
92 cout << " --version Print version" << endl;
93 cout << " --help Print this help message" << endl;
98 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int debug);
99 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
104 pjrec_main (int argc, char * argv[])
106 Projections projGlobal;
107 ImageFile* imGlobal = NULL;
108 char* filenameProj = NULL;
109 char* filenameImage = NULL;
114 int optTrace = TRACE_NONE;
115 double optFilterParam = -1;
116 string optFilterName = SignalFilter::FILTER_ABS_BANDLIMIT_STR;
117 string optFilterMethodName = SignalFilter::FILTER_METHOD_CONVOLUTION_STR;
118 string optInterpName = Backprojector::INTERP_LINEAR_STR;
119 string optBackprojName = Backprojector::BPROJ_IDIFF2_STR;
120 // string optFilterMethodName = SignalFilter::FILTER_METHOD_CONVOLUTION_STR;
121 int optInterpParam = 1;
125 int mpi_nview, mpi_ndet;
126 double mpi_detinc, mpi_rotinc, mpi_phmlen;
127 MPIWorld mpiWorld (argc, argv);
133 if (mpiWorld.getRank() == 0) {
136 int c = getopt_long(argc, argv, "", my_options, NULL);
145 optInterpName = optarg;
148 optFilterName = optarg;
151 optBackprojName = optarg;
154 optFilterParam = strtod(optarg, &endptr);
155 if (endptr != optarg + strlen(optarg)) {
156 pjrec_usage(argv[0]);
166 if ((optTrace = opt_set_trace(optarg)) < 0) {
167 pjrec_usage(argv[0]);
173 cout << "Version " << VERSION << endl;
175 cout << "Unknown version number" << endl;
180 pjrec_usage(argv[0]);
183 pjrec_usage(argv[0]);
188 if (optind + 4 != argc) {
189 pjrec_usage(argv[0]);
193 filenameProj = argv[optind];
195 filenameImage = argv[optind + 1];
197 nx = strtol(argv[optind + 2], &endptr, 10);
198 ny = strtol(argv[optind + 3], &endptr, 10);
200 ostringstream filterDesc;
201 if (optFilterParam >= 0)
202 filterDesc << optFilterName << ": alpha=" << optFilterParam;
204 filterDesc << optFilterName;
207 label << "pjrec: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << optInterpName << ", " << optBackprojName;
208 remark = label.str();
211 cout << "Remark: " << remark << endl;
217 if (mpiWorld.getRank() == 0) {
218 projGlobal.read (filenameProj);
220 projGlobal.printScanInfo();
222 mpi_ndet = projGlobal.nDet();
223 mpi_nview = projGlobal.nView();
224 mpi_detinc = projGlobal.detInc();
225 mpi_phmlen = projGlobal.phmLen();
226 mpi_rotinc = projGlobal.rotInc();
229 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
230 mpiWorld.BcastString (optBackprojName);
231 mpiWorld.BcastString (optFilterName);
232 mpiWorld.BcastString (optInterpName);
233 mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
234 mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
235 mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
236 mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
237 mpiWorld.getComm().Bcast (&optInterpParam, 1, MPI::INT, 0);
238 mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
239 mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
240 mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
241 mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
242 mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
243 mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
244 mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
246 timerBcast.timerEndAndReport ("Time to broadcast variables");
248 mpiWorld.setTotalWorkUnits (mpi_nview);
250 Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
251 projLocal.setDetInc (mpi_detinc);
252 projLocal.setPhmLen (mpi_phmlen);
253 projLocal.setRotInc (mpi_rotinc);
255 TimerCollectiveMPI timerScatter (mpiWorld.getComm());
256 ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, optDebug);
258 timerScatter.timerEndAndReport ("Time to scatter projections");
260 if (mpiWorld.getRank() == 0) {
261 imGlobal = new ImageFile (nx, ny);
264 imLocal = new ImageFile (nx, ny);
266 projGlobal.read (filenameProj);
268 projGlobal.printScanInfo();
270 imGlobal = new ImageFile (nx, ny);
274 TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
275 projLocal.reconstruct (*imLocal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
277 timerReconstruct.timerEndAndReport ("Time to reconstruct");
279 TimerCollectiveMPI timerReduce (mpiWorld.getComm());
280 ReduceImageMPI (mpiWorld, imLocal, imGlobal);
282 timerReduce.timerEndAndReport ("Time to reduce image");
284 projGlobal.reconstruct (*imGlobal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
288 if (mpiWorld.getRank() == 0)
291 double calcTime = timerProgram.timerEnd();
292 imGlobal->labelAdd (projGlobal.getLabel());
293 imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark.c_str(), calcTime);
294 imGlobal->fileWrite (filenameImage);
296 cout << "Run time: " << calcTime << " seconds" << endl;
306 //////////////////////////////////////////////////////////////////////////////////////
307 // MPI Support Routines
309 //////////////////////////////////////////////////////////////////////////////////////
312 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int optDebug)
314 if (mpiWorld.getRank() == 0) {
315 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
316 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
317 DetectorArray& detarray = projGlobal.getDetectorArray( iw );
318 int nDet = detarray.nDet();
319 DetectorValue* detval = detarray.detValues();
321 double viewAngle = detarray.viewAngle();
322 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, iProc, 0);
323 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, iProc, 0);
324 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, iProc, 0);
329 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
333 DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
335 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, 0, 0, status);
336 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, 0, 0, status);
337 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, 0, 0, status);
338 projLocal.getDetectorArray(iw).setViewAngle( viewAngle );
343 ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
345 ImageFileArray vLocal = imLocal->getArray();
347 for (int ix = 0; ix < imLocal->nx(); ix++) {
348 void *recvbuf = NULL;
349 if (mpiWorld.getRank() == 0) {
350 ImageFileArray vGlobal = imGlobal->getArray();
351 recvbuf = vGlobal[ix];
353 mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
362 main (int argc, char* argv[])
367 retval = pjrec_main(argc, argv);
368 } catch (exception e) {
369 cerr << "Exception: " << e.what() << endl;
371 cerr << "Unknown exception" << endl;