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.6 2000/07/04 22:21:01 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_METHOD, 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-method", 1, 0, O_FILTER_METHOD},
39 {"filter-param", 1, 0, O_FILTER_PARAM},
40 {"backproj", 1, 0, O_BACKPROJ},
41 {"trace", 1, 0, O_TRACE},
42 {"debug", 0, 0, O_DEBUG},
43 {"verbose", 0, 0, O_VERBOSE},
44 {"help", 0, 0, O_HELP},
45 {"version", 0, 0, O_VERSION},
51 pjrec_usage (const char *program)
53 cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
54 cout << "Image reconstruction from raysum projections" << endl;
56 cout << " raysum-file Input raysum file" << endl;
57 cout << " image-file Output image file in SDF2D format" << endl;
58 cout << " nx-image Number of columns in output image" << endl;
59 cout << " ny-image Number of rows in output image" << endl;
60 cout << " --interp Interpolation method during backprojection" << endl;
61 cout << " nearest Nearest neighbor interpolation" << endl;
62 cout << " linear Linear interpolation" << endl;
63 #if HAVE_BSPLINE_INTERP
64 cout << " bspline B-spline interpolation" << endl;
66 cout << " --filter Filter name" << endl;
67 cout << " abs_bandlimit Abs * Bandlimiting (default)" << endl;
68 cout << " abs_sinc Abs * Sinc" << endl;
69 cout << " abs_cos Abs * Cosine" << endl;
70 cout << " abs_hamming Abs * Hamming" << endl;
71 cout << " shepp Shepp-Logan" << endl;
72 cout << " bandlimit Bandlimiting" << endl;
73 cout << " sinc Sinc" << endl;
74 cout << " cos Cosine" << endl;
75 cout << " triangle Triangle" << endl;
76 cout << " hamming Hamming" << endl;
77 cout << " --filter-method Filter method before backprojections\n";;
78 cout << " convolution Spatial filtering (default)\n";
79 cout << " fourier Frequency filtering with discete fourier\n";
80 cout << " --backproj Backprojection Method" << endl;
81 cout << " trig Trigometric functions at every point" << endl;
82 cout << " table Trigometric functions with precalculated table" << endl;
83 cout << " diff Difference method" << endl;
84 cout << " diff2 Optimized difference method (default)" << endl;
85 cout << " idiff2 Optimized difference method with integer math" << endl;
86 cout << " --filter-param Alpha level for Hamming filter" << endl;
87 cout << " --trace Set tracing to level" << endl;
88 cout << " none No tracing (default)" << endl;
89 cout << " text Text level tracing" << endl;
90 cout << " phm Trace phantom" << endl;
91 cout << " rays Trace allrays" << endl;
92 cout << " plot Trace plotting" << endl;
93 cout << " clipping Trace clipping" << endl;
94 cout << " --verbose Turn on verbose mode" << endl;
95 cout << " --debug Turn on debug mode" << endl;
96 cout << " --version Print version" << endl;
97 cout << " --help Print this help message" << endl;
102 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int debug);
103 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
108 pjrec_main (int argc, char * argv[])
110 Projections projGlobal;
111 ImageFile* imGlobal = NULL;
112 char* filenameProj = NULL;
113 char* filenameImage = NULL;
118 int optTrace = TRACE_NONE;
119 double optFilterParam = -1;
120 string optFilterName = SignalFilter::FILTER_ABS_BANDLIMIT_STR;
121 string optFilterMethodName = SignalFilter::FILTER_METHOD_CONVOLUTION_STR;
122 string optInterpName = Backprojector::INTERP_LINEAR_STR;
123 string optBackprojName = Backprojector::BPROJ_IDIFF2_STR;
124 // string optFilterMethodName = SignalFilter::FILTER_METHOD_CONVOLUTION_STR;
125 int optInterpParam = 1;
129 int mpi_nview, mpi_ndet;
130 double mpi_detinc, mpi_rotinc, mpi_phmlen;
131 MPIWorld mpiWorld (argc, argv);
137 if (mpiWorld.getRank() == 0) {
140 int c = getopt_long(argc, argv, "", my_options, NULL);
149 optInterpName = optarg;
152 optFilterName = optarg;
154 case O_FILTER_METHOD:
155 optFilterMethodName = optarg;
158 optBackprojName = optarg;
161 optFilterParam = strtod(optarg, &endptr);
162 if (endptr != optarg + strlen(optarg)) {
163 pjrec_usage(argv[0]);
173 if ((optTrace = convertTraceNameToID(optarg)) == TRACE_INVALID) {
174 pjrec_usage(argv[0]);
180 cout << "Version " << VERSION << endl;
182 cout << "Unknown version number" << endl;
187 pjrec_usage(argv[0]);
190 pjrec_usage(argv[0]);
195 if (optind + 4 != argc) {
196 pjrec_usage(argv[0]);
200 filenameProj = argv[optind];
202 filenameImage = argv[optind + 1];
204 nx = strtol(argv[optind + 2], &endptr, 10);
205 ny = strtol(argv[optind + 3], &endptr, 10);
207 ostringstream filterDesc;
208 if (optFilterParam >= 0)
209 filterDesc << optFilterName << ": alpha=" << optFilterParam;
211 filterDesc << optFilterName;
214 label << "pjrec: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << optInterpName << ", " << optBackprojName;
215 remark = label.str();
218 cout << "Remark: " << remark << endl;
224 if (mpiWorld.getRank() == 0) {
225 projGlobal.read (filenameProj);
227 projGlobal.printScanInfo();
229 mpi_ndet = projGlobal.nDet();
230 mpi_nview = projGlobal.nView();
231 mpi_detinc = projGlobal.detInc();
232 mpi_phmlen = projGlobal.phmLen();
233 mpi_rotinc = projGlobal.rotInc();
236 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
237 mpiWorld.BcastString (optBackprojName);
238 mpiWorld.BcastString (optFilterName);
239 mpiWorld.BcastString (optInterpName);
240 mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
241 mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
242 mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
243 mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
244 mpiWorld.getComm().Bcast (&optInterpParam, 1, MPI::INT, 0);
245 mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
246 mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
247 mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
248 mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
249 mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
250 mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
251 mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
253 timerBcast.timerEndAndReport ("Time to broadcast variables");
255 mpiWorld.setTotalWorkUnits (mpi_nview);
257 Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
258 projLocal.setDetInc (mpi_detinc);
259 projLocal.setPhmLen (mpi_phmlen);
260 projLocal.setRotInc (mpi_rotinc);
262 TimerCollectiveMPI timerScatter (mpiWorld.getComm());
263 ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, optDebug);
265 timerScatter.timerEndAndReport ("Time to scatter projections");
267 if (mpiWorld.getRank() == 0) {
268 imGlobal = new ImageFile (nx, ny);
271 imLocal = new ImageFile (nx, ny);
273 projGlobal.read (filenameProj);
275 projGlobal.printScanInfo();
277 imGlobal = new ImageFile (nx, ny);
281 TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
282 projLocal.reconstruct (*imLocal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
284 timerReconstruct.timerEndAndReport ("Time to reconstruct");
286 TimerCollectiveMPI timerReduce (mpiWorld.getComm());
287 ReduceImageMPI (mpiWorld, imLocal, imGlobal);
289 timerReduce.timerEndAndReport ("Time to reduce image");
291 projGlobal.reconstruct (*imGlobal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
295 if (mpiWorld.getRank() == 0)
298 double calcTime = timerProgram.timerEnd();
299 imGlobal->labelAdd (projGlobal.getLabel());
300 imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark.c_str(), calcTime);
301 imGlobal->fileWrite (filenameImage);
303 cout << "Run time: " << calcTime << " seconds" << endl;
313 //////////////////////////////////////////////////////////////////////////////////////
314 // MPI Support Routines
316 //////////////////////////////////////////////////////////////////////////////////////
319 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int optDebug)
321 if (mpiWorld.getRank() == 0) {
322 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
323 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
324 DetectorArray& detarray = projGlobal.getDetectorArray( iw );
325 int nDet = detarray.nDet();
326 DetectorValue* detval = detarray.detValues();
328 double viewAngle = detarray.viewAngle();
329 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, iProc, 0);
330 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, iProc, 0);
331 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, iProc, 0);
336 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
340 DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
342 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, 0, 0, status);
343 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, 0, 0, status);
344 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, 0, 0, status);
345 projLocal.getDetectorArray(iw).setViewAngle( viewAngle );
350 ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
352 ImageFileArray vLocal = imLocal->getArray();
354 for (int ix = 0; ix < imLocal->nx(); ix++) {
355 void *recvbuf = NULL;
356 if (mpiWorld.getRank() == 0) {
357 ImageFileArray vGlobal = imGlobal->getArray();
358 recvbuf = vGlobal[ix];
360 mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
369 main (int argc, char* argv[])
374 retval = pjrec_main(argc, argv);
375 } catch (exception e) {
376 cerr << "Exception: " << e.what() << endl;
378 cerr << "Unknown exception" << endl;