r129: *** empty log message ***
[ctsim.git] / src / pjrec.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          pjrec.cpp
5 **   Purpose:       Reconstruct an image from projections
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Aug 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: pjrec.cpp,v 1.5 2000/07/04 18:33:35 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29 #include "timer.h"
30
31
32 enum {O_INTERP, O_FILTER, O_FILTER_METHOD, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
33
34 static struct option my_options[] =
35 {
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},
46   {0, 0, 0, 0}
47 };
48
49
50 void 
51 pjrec_usage (const char *program)
52 {
53   cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
54   cout << "Image reconstruction from raysum projections" << endl;
55   cout << 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;
65 #endif
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;
98 }
99
100
101 #ifdef HAVE_MPI
102 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int debug);
103 static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
104 #endif
105
106
107 int 
108 pjrec_main (int argc, char * argv[])
109 {
110   Projections projGlobal;
111   ImageFile* imGlobal = NULL;
112   char* filenameProj = NULL;
113   char* filenameImage = NULL;
114   string remark;
115   char *endptr;
116   int optVerbose = 0;
117   int optDebug = 0;
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;
126   int nx, ny;
127 #ifdef HAVE_MPI
128   ImageFile* imLocal;
129   int mpi_nview, mpi_ndet;
130   double mpi_detinc, mpi_rotinc, mpi_phmlen;
131   MPIWorld mpiWorld (argc, argv);
132 #endif
133
134   Timer timerProgram;
135
136 #ifdef HAVE_MPI
137   if (mpiWorld.getRank() == 0) {
138 #endif
139     while (1) {
140       int c = getopt_long(argc, argv, "", my_options, NULL);
141       char *endptr = NULL;
142       
143       if (c == -1)
144         break;
145       
146       switch (c)
147         {
148         case O_INTERP:
149           optInterpName = optarg;
150           break;
151         case O_FILTER:
152           optFilterName = optarg;
153           break;
154         case O_FILTER_METHOD:
155           optFilterMethodName = optarg;
156           break;
157         case O_BACKPROJ:
158           optBackprojName = optarg;
159           break;
160         case O_FILTER_PARAM:
161           optFilterParam = strtod(optarg, &endptr);
162           if (endptr != optarg + strlen(optarg)) {
163             pjrec_usage(argv[0]);
164           }
165           break;
166         case O_VERBOSE:
167           optVerbose = 1;
168           break;
169         case O_DEBUG:
170           optDebug = 1;
171           break;
172         case O_TRACE:
173           if ((optTrace = opt_set_trace(optarg)) < 0) {
174             pjrec_usage(argv[0]);
175             return (1);
176           }
177           break;
178         case O_VERSION:
179 #ifdef VERSION
180           cout <<  "Version " <<  VERSION << endl;
181 #else
182           cout << "Unknown version number" << endl;
183 #endif
184           return (0);
185         case O_HELP:
186         case '?':
187           pjrec_usage(argv[0]);
188           return (0);
189         default:
190           pjrec_usage(argv[0]);
191           return (1);
192         }
193     }
194   
195     if (optind + 4 != argc) {
196       pjrec_usage(argv[0]);
197       return (1);
198     }
199
200     filenameProj = argv[optind];
201   
202     filenameImage = argv[optind + 1];
203   
204     nx = strtol(argv[optind + 2], &endptr, 10);
205     ny = strtol(argv[optind + 3], &endptr, 10);
206   
207     ostringstream filterDesc;
208     if (optFilterParam >= 0)
209       filterDesc << optFilterName << ": alpha=" << optFilterParam; 
210     else
211       filterDesc << optFilterName;
212
213     ostringstream label;
214     label << "pjrec: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << optInterpName << ", " << optBackprojName;
215     remark = label.str();
216   
217     if (optVerbose)
218       cout << "Remark: " << remark << endl;
219 #ifdef HAVE_MPI
220   }
221 #endif
222
223 #ifdef HAVE_MPI
224   if (mpiWorld.getRank() == 0) {
225     projGlobal.read (filenameProj);
226     if (optVerbose)
227       projGlobal.printScanInfo();
228
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();
234   }
235
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);
252   if (optVerbose)
253       timerBcast.timerEndAndReport ("Time to broadcast variables");
254
255   mpiWorld.setTotalWorkUnits (mpi_nview);
256
257   Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
258   projLocal.setDetInc (mpi_detinc);
259   projLocal.setPhmLen (mpi_phmlen);
260   projLocal.setRotInc (mpi_rotinc);
261
262   TimerCollectiveMPI timerScatter (mpiWorld.getComm());
263   ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, optDebug);
264   if (optVerbose)
265       timerScatter.timerEndAndReport ("Time to scatter projections");
266
267   if (mpiWorld.getRank() == 0) {
268     imGlobal = new ImageFile (nx, ny);
269   }
270
271   imLocal = new ImageFile (nx, ny);
272 #else
273   projGlobal.read (filenameProj);
274   if (optVerbose)
275     projGlobal.printScanInfo();
276
277   imGlobal = new ImageFile (nx, ny);
278 #endif
279
280 #ifdef HAVE_MPI
281   TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
282   projLocal.reconstruct (*imLocal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
283   if (optVerbose)
284       timerReconstruct.timerEndAndReport ("Time to reconstruct");
285
286   TimerCollectiveMPI timerReduce (mpiWorld.getComm());
287   ReduceImageMPI (mpiWorld, imLocal, imGlobal);
288   if (optVerbose)
289       timerReduce.timerEndAndReport ("Time to reduce image");
290 #else
291   projGlobal.reconstruct (*imGlobal, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optInterpName.c_str(), optInterpParam, optBackprojName.c_str(), optTrace);
292 #endif
293
294 #ifdef HAVE_MPI
295   if (mpiWorld.getRank() == 0)
296 #endif
297     {
298       double calcTime = timerProgram.timerEnd();
299       imGlobal->labelAdd (projGlobal.getLabel());
300       imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark.c_str(), calcTime);
301       imGlobal->fileWrite (filenameImage);
302       if (optVerbose)
303         cout << "Run time: " << calcTime << " seconds" << endl;
304     }
305 #ifdef HAVE_MPI
306   MPI::Finalize();
307 #endif
308
309   return (0);
310 }
311
312
313 //////////////////////////////////////////////////////////////////////////////////////
314 // MPI Support Routines
315 //
316 //////////////////////////////////////////////////////////////////////////////////////
317
318 #ifdef HAVE_MPI
319 static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int optDebug)
320 {
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();
327
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);
332       }
333     }
334   }
335
336   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
337     MPI::Status status;
338     int nDet;
339     double viewAngle;
340     DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
341
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 );
346   }
347 }
348
349 static void
350 ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
351 {
352   ImageFileArray vLocal = imLocal->getArray();
353
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];
359     }
360     mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
361   }
362 }
363
364 #endif
365
366
367 #ifndef NO_MAIN
368 int 
369 main (int argc, char* argv[])
370 {
371   int retval = 1;
372
373   try {
374     retval = pjrec_main(argc, argv);
375   } catch (exception e) {
376     cerr << "Exception: " << e.what() << endl;
377   } catch (...) {
378     cerr << "Unknown exception" << endl;
379   }
380
381   return (retval);
382 }
383 #endif
384