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.13 2000/11/09 00:12:25 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.13 2000/11/09 00:12:25 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 << " --focal-length Focal length ratio (ratio to radius of phantom)\n";
76 cout << " (default = 1)\n";
77 cout << " --field-of-view Field of view (ratio to diameter of phantom square)\n";
78 cout << " (default = 1)\n";
79 cout << " --trace Trace level to use\n";
80 cout << " none No tracing (default)\n";
81 cout << " console Trace text level\n";
82 cout << " phantom Trace phantom image\n";
83 cout << " proj Trace projections\n";
84 cout << " plot Trace plot\n";
85 cout << " clipping Trace clipping\n";
86 cout << " --verbose Verbose mode\n";
87 cout << " --debug Debug mode\n";
88 cout << " --version Print version\n";
89 cout << " --help Print this help message\n";
93 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
97 phm2pj_main (int argc, char* argv[])
100 string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
101 char *opt_outfile = NULL;
104 string optPhmFileName;
108 double dOptFocalLength = 1.;
109 double dOptFieldOfView = 1.;
110 int opt_trace = Trace::TRACE_NONE;
113 double opt_rotangle = 1;
118 MPIWorld mpiWorld (argc, argv);
124 if (mpiWorld.getRank() == 0) {
127 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
137 optPhmFileName = optarg;
147 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
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 optGeometryName = optarg;
168 dOptFocalLength = strtod(optarg, &endptr);
169 endstr = optarg + strlen(optarg);
170 if (endptr != endstr) {
171 cerr << "Error setting --focal-length to " << optarg << endl;
172 phm2pj_usage(argv[0]);
176 case O_FIELD_OF_VIEW:
177 dOptFieldOfView = strtod(optarg, &endptr);
178 endstr = optarg + strlen(optarg);
179 if (endptr != endstr) {
180 cerr << "Error setting --field-of-view to " << optarg << endl;
181 phm2pj_usage(argv[0]);
186 opt_nray = strtol(optarg, &endptr, 10);
187 endstr = optarg + strlen(optarg);
188 if (endptr != endstr) {
189 cerr << "Error setting --nray to %s" << optarg << endl;
190 phm2pj_usage(argv[0]);
196 cout << "Version: " << VERSION << endl << g_szIdStr << endl;
198 cout << "Unknown version number\n";
203 phm2pj_usage(argv[0]);
206 phm2pj_usage(argv[0]);
211 if (optPhmName == "" && optPhmFileName == "") {
212 cerr << "No phantom defined\n" << endl;
213 phm2pj_usage(argv[0]);
216 if (optind + 3 != argc) {
217 phm2pj_usage(argv[0]);
221 opt_outfile = argv[optind];
222 opt_ndet = strtol(argv[optind+1], &endptr, 10);
223 endstr = argv[optind+1] + strlen(argv[optind+1]);
224 if (endptr != endstr) {
225 cerr << "Error setting --ndet to " << argv[optind+1] << endl;
226 phm2pj_usage(argv[0]);
229 opt_nview = strtol(argv[optind+2], &endptr, 10);
230 endstr = argv[optind+2] + strlen(argv[optind+2]);
231 if (endptr != endstr) {
232 cerr << "Error setting --nview to " << argv[optind+2] << endl;
233 phm2pj_usage(argv[0]);
238 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
239 if (optPhmFileName.length()) {
240 desc << "PhantomFile=" << optPhmFileName;
241 } else if (optPhmName != "") {
242 desc << "Phantom=" << optPhmName;
244 if (opt_desc.length()) {
245 desc << ": " << opt_desc;
247 opt_desc = desc.str();
249 if (optPhmName != "") {
250 phm.createFromPhantom (optPhmName.c_str());
252 cout << phm.failMessage() << endl << endl;
253 phm2pj_usage(argv[0]);
258 if (optPhmFileName != "") {
260 cerr << "Can not read phantom from file in MPI mode\n";
263 phm.createFromFile (optPhmFileName.c_str());
271 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
272 mpiWorld.BcastString (optPhmName);
273 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
274 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
275 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
276 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
277 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
278 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
280 timerBcast.timerEndAndReport ("Time to broadcast variables");
282 if (mpiWorld.getRank() > 0 && optPhmName != "")
283 phm.createFromPhantom (optPhmName.c_str());
287 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength, dOptFieldOfView);
288 if (scanner.fail()) {
289 cout << "Scanner Creation Error: " << scanner.failMessage() << endl;
293 mpiWorld.setTotalWorkUnits (opt_nview);
295 Projections pjGlobal;
296 if (mpiWorld.getRank() == 0)
297 pjGlobal.initFromScanner (scanner);
301 pjGlobal.printScanInfo(os);
305 Projections pjLocal (scanner);
306 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
309 cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
311 TimerCollectiveMPI timerProject (mpiWorld.getComm());
312 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
314 timerProject.timerEndAndReport ("Time to collect projections");
316 TimerCollectiveMPI timerGather (mpiWorld.getComm());
317 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
319 timerGather.timerEndAndReport ("Time to gather projections");
322 Projections pjGlobal (scanner);
324 SGPDriver* pSGPDriver = NULL;
326 if (opt_trace >= Trace::TRACE_PHANTOM) {
327 pSGPDriver = new SGPDriver ("phm2pj", 600, 600);
328 pSGP = new SGP (*pSGPDriver);
330 scanner.collectProjections (pjGlobal, phm, opt_trace, pSGP);
331 if (opt_trace >= Trace::TRACE_PHANTOM) {
332 cout << "Press enter to continue\n";
334 delete pSGP; pSGP = NULL;
335 delete pSGPDriver; pSGPDriver = NULL;
339 scanner.collectProjections (pjGlobal, phm, opt_trace);
345 if (mpiWorld.getRank() == 0)
348 pjGlobal.setCalcTime (timerProgram.timerEnd());
349 pjGlobal.setRemark (opt_desc);
350 pjGlobal.write (opt_outfile);
355 pjGlobal.printScanInfo (os);
356 cout << os.str() << endl;
357 cout << " Remark: " << pjGlobal.remark() << endl;
358 cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
367 * GatherProjectionsMPI
370 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
374 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
376 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
377 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
378 double viewAngle = detArray.viewAngle();
379 int nDet = detArray.nDet();
380 DetectorValue* detval = detArray.detValues();
382 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
383 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
384 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
387 if (mpiWorld.getRank() == 0) {
388 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
389 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
393 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
394 DetectorValue* detval = detArray.detValues();
396 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
397 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
398 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
399 detArray.setViewAngle (viewAngle);
409 main (int argc, char* argv[])
414 retval = phm2pj_main(argc, argv);
416 // dmalloc_shutdown();
418 } catch (exception e) {
419 cerr << "Exception: " << e.what() << endl;
421 cerr << "Unknown exception\n";