1 /*****************************************************************************
5 ** Purpose: Take projections of a phantom object
6 ** Programmer: Kevin Rosenberg
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: phm2pj.cpp,v 1.3 2000/06/22 10:17:28 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_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
33 O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
35 static struct option phm2pj_options[] =
37 {"phantom", 1, 0, O_PHANTOM},
38 {"phmfile", 1, 0, O_PHMFILE},
39 {"desc", 1, 0, O_DESC},
40 {"nray", 1, 0, O_NRAY},
41 {"rotangle", 1, 0, O_ROTANGLE},
42 {"trace", 1, 0, O_TRACE},
43 {"verbose", 0, 0, O_VERBOSE},
44 {"help", 0, 0, O_HELP},
45 {"debug", 0, 0, O_DEBUG},
46 {"version", 0, 0, O_VERSION},
52 phm2pj_usage (const char *program)
54 cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]" << endl;
55 cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile" << endl;
57 cout << " outfile Name of output file for raysums" << endl;
58 cout << " ndet Number of detectors" << endl;
59 cout << " nview Number of rotated views" << endl;
60 cout << " --phantom Phantom to use for projection" << endl;
61 cout << " herman Herman head phantom" << endl;
62 cout << " bherman Bordered herman head phantom" << endl;
63 cout << " rowland Rowland head phantom" << endl;
64 cout << " browland Bordered Rowland head phantom" << endl;
65 cout << " unitpulse Unit pulse phantom" << endl;
66 cout << " --phmfile Get Phantom from phantom file" << endl;
67 cout << " --desc Description of raysum" << endl;
68 cout << " --nray Number of rays per detector (default = 1)" << endl;
69 cout << " --rotangle Degrees to rotate view through, multiple of PI (default = 1)" << endl;
70 cout << " --trace Trace level to use" << endl;
71 cout << " none No tracing (default)" << endl;
72 cout << " text Trace text level" << endl;
73 cout << " phm Trace phantom image" << endl;
74 cout << " rays Trace rays" << endl;
75 cout << " plot Trace plot" << endl;
76 cout << " clipping Trace clipping" << endl;
77 cout << " --verbose Verbose mode" << endl;
78 cout << " --debug Debug mode" << endl;
79 cout << " --version Print version" << endl;
80 cout << " --help Print this help message" << endl;
84 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
88 phm2pj_main (int argc, char* argv[])
91 ScannerGeometry opt_geometry = DETECTOR_PARALLEL;
92 char *opt_outfile = NULL;
94 string opt_phmfilename;
102 double opt_rotangle = 1;
107 MPIWorld mpiWorld (argc, argv);
113 if (mpiWorld.getRank() == 0) {
116 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
124 if (! phm.createFromPhantom (optPhmName.c_str())) {
125 cout << "ERROR: Invalid phantom name " << optPhmName << endl << endl;
126 phm2pj_usage(argv[0]);
132 if (mpiWorld.getRank() == 0)
133 cerr << "Can not read phantom from file in MPI mode" << endl;
136 opt_phmfilename = optarg;
137 phm.createFromFile (opt_phmfilename.c_str());
147 if ((opt_trace = opt_set_trace(optarg)) < 0) {
148 phm2pj_usage(argv[0]);
156 opt_rotangle = strtod(optarg, &endptr);
157 endstr = optarg + strlen(optarg);
158 if (endptr != endstr) {
159 cerr << "Error setting --rotangle to " << optarg << endl;
160 phm2pj_usage(argv[0]);
165 opt_nray = strtol(optarg, &endptr, 10);
166 endstr = optarg + strlen(optarg);
167 if (endptr != endstr) {
168 cerr << "Error setting --nray to %s" << optarg << endl;
169 phm2pj_usage(argv[0]);
175 cout << "Version: " << VERSION << endl;
177 cout << "Unknown version number" << endl;
182 phm2pj_usage(argv[0]);
185 phm2pj_usage(argv[0]);
190 if (phm.nPElem() == 0) {
191 cerr << "No phantom defined" << endl;
192 phm2pj_usage(argv[0]);
195 if (optind + 3 != argc) {
196 phm2pj_usage(argv[0]);
200 opt_outfile = argv[optind];
201 opt_ndet = strtol(argv[optind+1], &endptr, 10);
202 endstr = argv[optind+1] + strlen(argv[optind+1]);
203 if (endptr != endstr) {
204 cerr << "Error setting --ndet to " << argv[optind+1] << endl;
205 phm2pj_usage(argv[0]);
208 opt_nview = strtol(argv[optind+2], &endptr, 10);
209 endstr = argv[optind+2] + strlen(argv[optind+2]);
210 if (endptr != endstr) {
211 cerr << "Error setting --nview to " << argv[optind+2] << endl;
212 phm2pj_usage(argv[0]);
217 desc << "Raysum_Collect: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", ";
218 if (opt_phmfilename.length()) {
219 desc << "PhantomFile=" << opt_phmfilename;
220 } else if (optPhmName != "") {
221 desc << "Phantom=" << optPhmName;
223 if (opt_desc.length()) {
224 desc << ": " << opt_desc;
226 opt_desc = desc.str();
232 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
233 mpiWorld.BcastString (optPhmName);
234 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
235 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
236 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
237 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
238 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
239 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
240 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
241 timerBcast.timerEndAndReport ("Time to broadcast variables");
243 if (mpiWorld.getRank() > 0 && optPhmName != "")
244 phm.createFromPhantom (optPhmName.c_str());
248 Scanner scanner (phm, opt_geometry, opt_ndet, opt_nview, opt_nray, opt_rotangle);
251 mpiWorld.setTotalWorkUnits (opt_nview);
253 Projections pjGlobal;
254 if (mpiWorld.getRank() == 0)
255 pjGlobal.initFromScanner (scanner);
258 pjGlobal.printScanInfo();
260 Projections pjLocal (scanner);
261 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
264 cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")" << endl;;
266 TimerCollectiveMPI timerProject (mpiWorld.getComm());
267 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), opt_trace);
269 timerProject.timerEndAndReport ("Time to collect projections");
271 TimerCollectiveMPI timerGather (mpiWorld.getComm());
272 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
274 timerGather.timerEndAndReport ("Time to gather projections");
277 Projections pjGlobal (scanner);
278 scanner.collectProjections (pjGlobal, phm, 0, opt_trace);
282 if (mpiWorld.getRank() == 0)
285 pjGlobal.setCalcTime (timerProgram.timerEnd());
286 pjGlobal.setRemark (opt_desc);
287 pjGlobal.write (opt_outfile);
291 pjGlobal.printScanInfo();
293 cout << " Remark: " << pjGlobal.remark() << endl;
294 cout << "Run time: " << pjGlobal.calcTime() << " seconds" << endl;
303 * GatherProjectionsMPI
306 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
310 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
312 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
313 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
314 double viewAngle = detArray.viewAngle();
315 int nDet = detArray.nDet();
316 DetectorValue* detval = detArray.detValues();
318 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
319 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
320 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
323 if (mpiWorld.getRank() == 0) {
324 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
325 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
329 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
330 DetectorValue* detval = detArray.detValues();
332 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
333 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
334 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
335 detArray.setViewAngle (viewAngle);
345 main (int argc, char* argv[])
350 retval = phm2pj_main(argc, argv);
351 } catch (exception e) {
352 cerr << "Exception: " << e.what() << endl;
354 cerr << "Unknown exception" << endl;