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.18 2000/12/16 02:44:26 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_ZEROPAD, O_FILTER_PARAM, O_FILTER_GENERATION, O_BACKPROJ, O_PREINTERPOLATION_FACTOR, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
34 static struct option my_options[] =
36 {"interp", 1, 0, O_INTERP},
37 {"preinterpolation-factor", 1, 0, O_PREINTERPOLATION_FACTOR},
38 {"filter", 1, 0, O_FILTER},
39 {"filter-method", 1, 0, O_FILTER_METHOD},
40 {"zeropad", 1, 0, O_ZEROPAD},
41 {"filter-generation", 1, 0, O_FILTER_GENERATION},
42 {"filter-param", 1, 0, O_FILTER_PARAM},
43 {"backproj", 1, 0, O_BACKPROJ},
44 {"trace", 1, 0, O_TRACE},
45 {"debug", 0, 0, O_DEBUG},
46 {"verbose", 0, 0, O_VERBOSE},
47 {"help", 0, 0, O_HELP},
48 {"version", 0, 0, O_VERSION},
52 static const char* g_szIdStr = "$Id: pjrec.cpp,v 1.18 2000/12/16 02:44:26 kevin Exp $";
55 pjrec_usage (const char *program)
57 cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
58 cout << "Image reconstruction from raysum projections" << endl;
60 cout << " raysum-file Input raysum file" << endl;
61 cout << " image-file Output image file in SDF2D format" << endl;
62 cout << " nx-image Number of columns in output image" << endl;
63 cout << " ny-image Number of rows in output image" << endl;
64 cout << " --interp Interpolation method during backprojection" << endl;
65 cout << " nearest Nearest neighbor interpolation" << endl;
66 cout << " linear Linear interpolation" << endl;
67 #if HAVE_BSPLINE_INTERP
68 cout << " bspline B-spline interpolation" << endl;
70 cout << " --preinterpolate Preinterpolation factor (default = 1)\n";
71 cout << " Used only with frequency-based filtering\n";
72 cout << " --filter Filter name" << endl;
73 cout << " abs_bandlimit Abs * Bandlimiting (default)" << endl;
74 cout << " abs_sinc Abs * Sinc" << endl;
75 cout << " abs_cosine Abs * Cosine" << endl;
76 cout << " abs_hamming Abs * Hamming" << endl;
77 cout << " shepp Shepp-Logan" << endl;
78 cout << " bandlimit Bandlimiting" << endl;
79 cout << " sinc Sinc" << endl;
80 cout << " cosine Cosine" << endl;
81 cout << " triangle Triangle" << endl;
82 cout << " hamming Hamming" << endl;
83 cout << " --filter-method Filter method before backprojections\n";;
84 cout << " convolution Spatial filtering (default)\n";
85 cout << " fourier Frequency filtering with discete fourier\n";
86 cout << " fourier_table Frequency filtering with table lookup fourier\n";
87 cout << " fft Fast Fourier Transform\n";
89 cout << " fftw Fast Fourier Transform West library\n";
90 cout << " rfftw Fast Fourier Transform West (real-mode) library\n";
92 cout << " --zeropad n Set zeropad level (default = 0)\n";
93 cout << " set n to number of powers to two to pad\n";
94 cout << " --filter-generation Filter Generation mode\n";
95 cout << " direct Use direct filter in spatial or frequency domain (default)\n";
96 cout << " inverse_fourier Use inverse fourier transform of inverse filter\n";
97 cout << " --backproj Backprojection Method" << endl;
98 cout << " trig Trigometric functions at every point" << endl;
99 cout << " table Trigometric functions with precalculated table" << endl;
100 cout << " diff Difference method" << endl;
101 cout << " diff2 Optimized difference method (default)" << endl;
102 cout << " idiff2 Optimized difference method with integer math" << endl;
103 cout << " idiff3 Highly-optimized difference method with integer math" << endl;
104 cout << " --filter-param Alpha level for Hamming filter" << endl;
105 cout << " --trace Set tracing to level" << endl;
106 cout << " none No tracing (default)" << endl;
107 cout << " console Text level tracing" << endl;
108 cout << " phantom Trace phantom" << endl;
109 cout << " proj Trace allrays" << endl;
110 cout << " plot Trace plotting" << endl;
111 cout << " clipping Trace clipping" << endl;
112 cout << " --verbose Turn on verbose mode" << endl;
113 cout << " --debug Turn on debug mode" << endl;
114 cout << " --version Print version" << endl;
115 cout << " --help Print this help message" << endl;
120 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const bool bDebug);
121 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
126 pjrec_main (int argc, char * argv[])
128 Projections projGlobal;
129 ImageFile* imGlobal = NULL;
130 char* pszFilenameProj = NULL;
131 char* pszFilenameImage = NULL;
133 bool bOptVerbose = false;
136 int optTrace = Trace::TRACE_NONE;
137 double dOptFilterParam = -1;
138 string sOptFilterName (SignalFilter::convertFilterIDToName (SignalFilter::FILTER_ABS_BANDLIMIT));
139 string sOptFilterMethodName (ProcessSignal::convertFilterMethodIDToName (ProcessSignal::FILTER_METHOD_CONVOLUTION));
140 string sOptFilterGenerationName (ProcessSignal::convertFilterGenerationIDToName (ProcessSignal::FILTER_GENERATION_DIRECT));
141 string sOptInterpName (Backprojector::convertInterpIDToName (Backprojector::INTERP_LINEAR));
142 string sOptBackprojectName (Backprojector::convertBackprojectIDToName (Backprojector::BPROJ_IDIFF3));
143 int iOptPreinterpolationFactor = 1;
148 int mpi_nview, mpi_ndet;
149 double mpi_detinc, mpi_rotinc, mpi_phmlen;
150 MPIWorld mpiWorld (argc, argv);
156 if (mpiWorld.getRank() == 0) {
159 int c = getopt_long(argc, argv, "", my_options, NULL);
168 sOptInterpName = optarg;
170 case O_PREINTERPOLATION_FACTOR:
171 iOptPreinterpolationFactor = strtol(optarg, &endptr, 10);
172 if (endptr != optarg + strlen(optarg)) {
173 pjrec_usage(argv[0]);
178 sOptFilterName = optarg;
180 case O_FILTER_METHOD:
181 sOptFilterMethodName = optarg;
183 case O_FILTER_GENERATION:
184 sOptFilterGenerationName = optarg;
187 dOptFilterParam = strtod(optarg, &endptr);
188 if (endptr != optarg + strlen(optarg)) {
189 pjrec_usage(argv[0]);
194 iOptZeropad = strtol(optarg, &endptr, 10);
195 if (endptr != optarg + strlen(optarg)) {
196 pjrec_usage(argv[0]);
201 sOptBackprojectName = optarg;
210 if ((optTrace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
211 pjrec_usage(argv[0]);
217 cout << "Version " << VERSION << endl << g_szIdStr << endl;
219 cout << "Unknown version number" << endl;
224 pjrec_usage(argv[0]);
227 pjrec_usage(argv[0]);
232 if (optind + 4 != argc) {
233 pjrec_usage(argv[0]);
237 pszFilenameProj = argv[optind];
239 pszFilenameImage = argv[optind + 1];
241 nx = strtol(argv[optind + 2], &endptr, 10);
242 ny = strtol(argv[optind + 3], &endptr, 10);
244 ostringstream filterDesc;
245 if (dOptFilterParam >= 0)
246 filterDesc << sOptFilterName << ": alpha=" << dOptFilterParam;
248 filterDesc << sOptFilterName;
251 label << "pjrec: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << sOptInterpName << ", preinterpolationFactor=" << iOptPreinterpolationFactor << ", " << sOptBackprojectName;
252 sRemark = label.str();
255 cout << "SRemark: " << sRemark << endl;
261 if (mpiWorld.getRank() == 0) {
262 projGlobal.read (pszFilenameProj);
265 projGlobal.printScanInfo (os);
269 mpi_ndet = projGlobal.nDet();
270 mpi_nview = projGlobal.nView();
271 mpi_detinc = projGlobal.detInc();
272 mpi_phmlen = projGlobal.phmLen();
273 mpi_rotinc = projGlobal.rotInc();
276 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
277 mpiWorld.BcastString (sOptBackprojectName);
278 mpiWorld.BcastString (sOptFilterName);
279 mpiWorld.BcastString (sOptFilterMethodName);
280 mpiWorld.BcastString (sOptInterpName);
281 mpiWorld.getComm().Bcast (&bOptVerbose, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&bOptDebug, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
284 mpiWorld.getComm().Bcast (&dOptFilterParam, 1, MPI::DOUBLE, 0);
285 mpiWorld.getComm().Bcast (&iOptZeropad, 1, MPI::INT, 0);
286 mpiWorld.getComm().Bcast (&iOptPreinterpolationFactor, 1, MPI::INT, 0);
287 mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
288 mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
289 mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
290 mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
291 mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
292 mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
293 mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
295 timerBcast.timerEndAndReport ("Time to broadcast variables");
297 mpiWorld.setTotalWorkUnits (mpi_nview);
299 Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
300 projLocal.setDetInc (mpi_detinc);
301 projLocal.setPhmLen (mpi_phmlen);
302 projLocal.setRotInc (mpi_rotinc);
304 TimerCollectiveMPI timerScatter (mpiWorld.getComm());
305 ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, bOptDebug);
307 timerScatter.timerEndAndReport ("Time to scatter projections");
309 if (mpiWorld.getRank() == 0) {
310 imGlobal = new ImageFile (nx, ny);
313 imLocal = new ImageFile (nx, ny);
315 projGlobal.read (pszFilenameProj);
318 projGlobal.printScanInfo(os);
322 imGlobal = new ImageFile (nx, ny);
326 TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
328 Reconstructor reconstruct (projLocal, *imLocal, sOptFilterName.c_str(), dOptFilterParam, sOptFilterMethodName.c_str(), iOptZeropad, sOptFilterGenerationName.c_str(), sOptInterpName.c_str(), iOptPreinterpolationFactor, sOptBackprojectName.c_str(), optTrace);
329 if (reconstruct.fail()) {
330 cout << reconstruct.failMessage();
333 reconstruct.reconstructAllViews();
336 timerReconstruct.timerEndAndReport ("Time to reconstruct");
338 TimerCollectiveMPI timerReduce (mpiWorld.getComm());
339 ReduceImageMPI (mpiWorld, imLocal, imGlobal);
341 timerReduce.timerEndAndReport ("Time to reduce image");
343 Reconstructor reconstruct (projGlobal, *imGlobal, sOptFilterName.c_str(), dOptFilterParam, sOptFilterMethodName.c_str(), iOptZeropad, sOptFilterGenerationName.c_str(), sOptInterpName.c_str(), iOptPreinterpolationFactor, sOptBackprojectName.c_str(), optTrace);
344 if (reconstruct.fail()) {
345 cout << reconstruct.failMessage();
348 reconstruct.reconstructAllViews();
352 if (mpiWorld.getRank() == 0)
355 double dCalcTime = timerProgram.timerEnd();
356 imGlobal->labelAdd (projGlobal.getLabel());
357 imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, sRemark.c_str(), dCalcTime);
358 imGlobal->fileWrite (pszFilenameImage);
360 cout << "Run time: " << dCalcTime << " seconds" << endl;
370 //////////////////////////////////////////////////////////////////////////////////////
371 // MPI Support Routines
373 //////////////////////////////////////////////////////////////////////////////////////
376 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const bool bOptDebug)
378 if (mpiWorld.getRank() == 0) {
379 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
380 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
381 DetectorArray& detarray = projGlobal.getDetectorArray( iw );
382 int nDet = detarray.nDet();
383 DetectorValue* detval = detarray.detValues();
385 double viewAngle = detarray.viewAngle();
386 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, iProc, 0);
387 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, iProc, 0);
388 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, iProc, 0);
393 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
397 DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
399 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, 0, 0, status);
400 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, 0, 0, status);
401 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, 0, 0, status);
402 projLocal.getDetectorArray(iw).setViewAngle( viewAngle );
407 ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
409 ImageFileArray vLocal = imLocal->getArray();
411 for (unsigned int ix = 0; ix < imLocal->nx(); ix++) {
412 void *recvbuf = NULL;
413 if (mpiWorld.getRank() == 0) {
414 ImageFileArray vGlobal = imGlobal->getArray();
415 recvbuf = vGlobal[ix];
417 mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
426 main (int argc, char* argv[])
431 retval = pjrec_main(argc, argv);
432 } catch (exception e) {
433 cerr << "Exception: " << e.what() << endl;
435 cerr << "Unknown exception" << endl;