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.16 2000/11/24 12:08:10 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, O_GEOMETRY, O_FOCAL_LENGTH, O_FIELD_OF_VIEW,
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 {"geometry", 1, 0, O_GEOMETRY},
43 {"focal-length", 1, 0, O_FOCAL_LENGTH},
44 {"field-of-view", 1, 0, O_FIELD_OF_VIEW},
45 {"trace", 1, 0, O_TRACE},
46 {"verbose", 0, 0, O_VERBOSE},
47 {"help", 0, 0, O_HELP},
48 {"debug", 0, 0, O_DEBUG},
49 {"version", 0, 0, O_VERSION},
53 static const char* g_szIdStr = "$Id: phm2pj.cpp,v 1.16 2000/11/24 12:08:10 kevin Exp $";
57 phm2pj_usage (const char *program)
59 cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
60 cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
61 cout << " outfile Name of output file for projections\n";
62 cout << " ndet Number of detectors\n";
63 cout << " nview Number of rotated views\n";
64 cout << " --phantom Phantom to use for projection\n";
65 cout << " herman Herman head phantom\n";
66 cout << " herman-b Herman head phantom (Bordered)\n";
67 cout << " shepp-logan Shepp-Logan head phantom\n";
68 cout << " shepp-logan-b Shepp-Logan head phantom (Bordered)\n";
69 cout << " unitpulse Unit pulse phantom\n";
70 cout << " --phmfile Get Phantom from phantom file\n";
71 cout << " --desc Description of raysum\n";
72 cout << " --nray Number of rays per detector (default = 1)\n";
73 cout << " --rotangle Degrees to rotate view through (multiple of PI)\n";
74 cout << " (default = 1)\n";
75 cout << " --geometry Geometry of scanning\n";
76 cout << " parallel Parallel scan beams (default)\n";
77 cout << " equilinear Equilinear divergent scan beams\n";
78 cout << " equiangular Equiangular divergent scan beams\n";
79 cout << " --focal-length Focal length ratio (ratio to radius of phantom)\n";
80 cout << " (default = 1)\n";
81 cout << " --field-of-view Field of view (ratio to diameter of phantom square)\n";
82 cout << " (default = 1)\n";
83 cout << " --trace Trace level to use\n";
84 cout << " none No tracing (default)\n";
85 cout << " console Trace text level\n";
86 cout << " phantom Trace phantom image\n";
87 cout << " proj Trace projections\n";
88 cout << " plot Trace plot\n";
89 cout << " clipping Trace clipping\n";
90 cout << " --verbose Verbose mode\n";
91 cout << " --debug Debug mode\n";
92 cout << " --version Print version\n";
93 cout << " --help Print this help message\n";
97 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
101 phm2pj_main (int argc, char* argv[])
104 string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
105 char *opt_outfile = NULL;
108 string optPhmFileName;
112 double dOptFocalLength = 1.;
113 double dOptFieldOfView = 1.;
114 int opt_trace = Trace::TRACE_NONE;
117 double opt_rotangle = 1;
122 MPIWorld mpiWorld (argc, argv);
128 if (mpiWorld.getRank() == 0) {
131 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
141 optPhmFileName = optarg;
151 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
152 phm2pj_usage(argv[0]);
160 opt_rotangle = strtod(optarg, &endptr);
161 endstr = optarg + strlen(optarg);
162 if (endptr != endstr) {
163 cerr << "Error setting --rotangle to " << optarg << endl;
164 phm2pj_usage(argv[0]);
169 optGeometryName = optarg;
172 dOptFocalLength = strtod(optarg, &endptr);
173 endstr = optarg + strlen(optarg);
174 if (endptr != endstr) {
175 cerr << "Error setting --focal-length to " << optarg << endl;
176 phm2pj_usage(argv[0]);
180 case O_FIELD_OF_VIEW:
181 dOptFieldOfView = strtod(optarg, &endptr);
182 endstr = optarg + strlen(optarg);
183 if (endptr != endstr) {
184 cerr << "Error setting --field-of-view to " << optarg << endl;
185 phm2pj_usage(argv[0]);
190 opt_nray = strtol(optarg, &endptr, 10);
191 endstr = optarg + strlen(optarg);
192 if (endptr != endstr) {
193 cerr << "Error setting --nray to %s" << optarg << endl;
194 phm2pj_usage(argv[0]);
200 cout << "Version: " << VERSION << endl << g_szIdStr << endl;
202 cout << "Unknown version number\n";
207 phm2pj_usage(argv[0]);
210 phm2pj_usage(argv[0]);
215 if (optPhmName == "" && optPhmFileName == "") {
216 cerr << "No phantom defined\n" << endl;
217 phm2pj_usage(argv[0]);
220 if (optind + 3 != argc) {
221 phm2pj_usage(argv[0]);
225 opt_outfile = argv[optind];
226 opt_ndet = strtol(argv[optind+1], &endptr, 10);
227 endstr = argv[optind+1] + strlen(argv[optind+1]);
228 if (endptr != endstr) {
229 cerr << "Error setting --ndet to " << argv[optind+1] << endl;
230 phm2pj_usage(argv[0]);
233 opt_nview = strtol(argv[optind+2], &endptr, 10);
234 endstr = argv[optind+2] + strlen(argv[optind+2]);
235 if (endptr != endstr) {
236 cerr << "Error setting --nview to " << argv[optind+2] << endl;
237 phm2pj_usage(argv[0]);
242 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
243 if (optPhmFileName.length()) {
244 desc << "PhantomFile=" << optPhmFileName;
245 } else if (optPhmName != "") {
246 desc << "Phantom=" << optPhmName;
248 if (opt_desc.length()) {
249 desc << ": " << opt_desc;
251 opt_desc = desc.str();
253 if (optPhmName != "") {
254 phm.createFromPhantom (optPhmName.c_str());
256 cout << phm.failMessage() << endl << endl;
257 phm2pj_usage(argv[0]);
262 if (optPhmFileName != "") {
264 cerr << "Can not read phantom from file in MPI mode\n";
267 phm.createFromFile (optPhmFileName.c_str());
275 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
276 mpiWorld.BcastString (optPhmName);
277 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
278 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
284 timerBcast.timerEndAndReport ("Time to broadcast variables");
286 if (mpiWorld.getRank() > 0 && optPhmName != "")
287 phm.createFromPhantom (optPhmName.c_str());
291 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength, dOptFieldOfView);
292 if (scanner.fail()) {
293 cout << "Scanner Creation Error: " << scanner.failMessage() << endl;
297 mpiWorld.setTotalWorkUnits (opt_nview);
299 Projections pjGlobal;
300 if (mpiWorld.getRank() == 0)
301 pjGlobal.initFromScanner (scanner);
305 pjGlobal.printScanInfo(os);
309 Projections pjLocal (scanner);
310 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
313 cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
315 TimerCollectiveMPI timerProject (mpiWorld.getComm());
316 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
318 timerProject.timerEndAndReport ("Time to collect projections");
320 TimerCollectiveMPI timerGather (mpiWorld.getComm());
321 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
323 timerGather.timerEndAndReport ("Time to gather projections");
326 Projections pjGlobal (scanner);
328 SGPDriver* pSGPDriver = NULL;
330 if (opt_trace >= Trace::TRACE_PHANTOM) {
331 pSGPDriver = new SGPDriver ("phm2pj", 600, 600);
332 pSGP = new SGP (*pSGPDriver);
334 scanner.collectProjections (pjGlobal, phm, opt_trace, pSGP);
335 if (opt_trace >= Trace::TRACE_PHANTOM) {
336 cout << "Press enter to continue\n";
338 delete pSGP; pSGP = NULL;
339 delete pSGPDriver; pSGPDriver = NULL;
343 scanner.collectProjections (pjGlobal, phm, opt_trace);
349 if (mpiWorld.getRank() == 0)
352 pjGlobal.setCalcTime (timerProgram.timerEnd());
353 pjGlobal.setRemark (opt_desc);
354 pjGlobal.write (opt_outfile);
359 pjGlobal.printScanInfo (os);
360 cout << os.str() << endl;
361 cout << " Remark: " << pjGlobal.remark() << endl;
362 cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
371 * GatherProjectionsMPI
374 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
378 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
380 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
381 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
382 double viewAngle = detArray.viewAngle();
383 int nDet = detArray.nDet();
384 DetectorValue* detval = detArray.detValues();
386 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
387 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
388 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
391 if (mpiWorld.getRank() == 0) {
392 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
393 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
397 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
398 DetectorValue* detval = detArray.detValues();
400 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
401 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
402 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
403 detArray.setViewAngle (viewAngle);
413 main (int argc, char* argv[])
418 retval = phm2pj_main(argc, argv);
420 // dmalloc_shutdown();
422 } catch (exception e) {
423 cerr << "Exception: " << e.what() << endl;
425 cerr << "Unknown exception\n";