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.31 2001/09/24 09:40:42 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_CENTER_DETECTOR_LENGTH,
33 O_VIEW_RATIO, O_SCAN_RATIO, O_OFFSETVIEW, 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 {"center-detector-length", 1, 0, O_CENTER_DETECTOR_LENGTH},
45 {"offsetview", 1, 0, O_OFFSETVIEW},
46 {"view-ratio", 1, 0, O_VIEW_RATIO},
47 {"scan-ratio", 1, 0, O_SCAN_RATIO},
48 {"trace", 1, 0, O_TRACE},
49 {"verbose", 0, 0, O_VERBOSE},
50 {"help", 0, 0, O_HELP},
51 {"debug", 0, 0, O_DEBUG},
52 {"version", 0, 0, O_VERSION},
56 static const char* g_szIdStr = "$Id: phm2pj.cpp,v 1.31 2001/09/24 09:40:42 kevin Exp $";
60 phm2pj_usage (const char *program)
62 std::cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
63 std::cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
64 std::cout << " outfile Name of output file for projections\n";
65 std::cout << " ndet Number of detectors\n";
66 std::cout << " nview Number of rotated views\n";
67 std::cout << " --phantom Phantom to use for projection\n";
68 std::cout << " herman Herman head phantom\n";
69 std::cout << " shepp-logan Shepp-Logan head phantom\n";
70 std::cout << " unit-pulse Unit pulse phantom\n";
71 std::cout << " --phmfile Get Phantom from phantom file\n";
72 std::cout << " --desc Description of raysum\n";
73 std::cout << " --nray Number of rays per detector (default = 1)\n";
74 std::cout << " --rotangle Angle to rotate view through (fraction of a circle)\n";
75 std::cout << " (default = select appropriate for geometry)\n";
76 std::cout << " --geometry Geometry of scanning\n";
77 std::cout << " parallel Parallel scan beams (default)\n";
78 std::cout << " equilinear Equilinear divergent scan beams\n";
79 std::cout << " equiangular Equiangular divergent scan beams\n";
80 std::cout << " --focal-length Focal length ratio (ratio to radius of view area)\n";
81 std::cout << " (default = 2)\n";
82 std::cout << " --center-detector-length Distance from center of phantom to detector array\n";
83 std::cout << " (ratio to radius of view area) (default = 2)\n";
84 std::cout << " --view-ratio Length to view (view diameter to phantom diameter)\n";
85 std::cout << " (default = 1)\n";
86 std::cout << " --scan-ratio Length to scan (scan diameter to view diameter)\n";
87 std::cout << " (default = 1)\n";
88 std::cout << " --offsetview Initial gantry offset in 'views' (default = 0)\n";
89 std::cout << " --trace Trace level to use\n";
90 std::cout << " none No tracing (default)\n";
91 std::cout << " console Trace text level\n";
92 std::cout << " phantom Trace phantom image\n";
93 std::cout << " proj Trace projections\n";
94 std::cout << " plot Trace plot\n";
95 std::cout << " clipping Trace clipping\n";
96 std::cout << " --verbose Verbose mode\n";
97 std::cout << " --debug Debug mode\n";
98 std::cout << " --version Print version\n";
99 std::cout << " --help Print this help message\n";
103 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
107 phm2pj_main (int argc, char* const argv[])
110 std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
111 char *opt_outfile = NULL;
112 std::string opt_desc;
113 std::string optPhmName;
114 std::string optPhmFileName;
117 int opt_offsetview = 0;
119 double dOptFocalLength = 2.;
120 double dOptCenterDetectorLength = 2.;
121 double dOptViewRatio = 1.;
122 double dOptScanRatio = 1.;
123 int opt_trace = Trace::TRACE_NONE;
126 double opt_rotangle = -1;
131 MPIWorld mpiWorld (argc, argv);
137 if (mpiWorld.getRank() == 0) {
140 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
150 optPhmFileName = optarg;
160 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
161 phm2pj_usage(argv[0]);
169 opt_rotangle = strtod(optarg, &endptr);
170 endstr = optarg + strlen(optarg);
171 if (endptr != endstr) {
172 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
173 phm2pj_usage(argv[0]);
178 optGeometryName = optarg;
181 dOptFocalLength = strtod(optarg, &endptr);
182 endstr = optarg + strlen(optarg);
183 if (endptr != endstr) {
184 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
185 phm2pj_usage(argv[0]);
189 case O_CENTER_DETECTOR_LENGTH:
190 dOptCenterDetectorLength = strtod(optarg, &endptr);
191 endstr = optarg + strlen(optarg);
192 if (endptr != endstr) {
193 std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
194 phm2pj_usage(argv[0]);
199 dOptViewRatio = strtod(optarg, &endptr);
200 endstr = optarg + strlen(optarg);
201 if (endptr != endstr) {
202 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
203 phm2pj_usage(argv[0]);
208 dOptScanRatio = strtod(optarg, &endptr);
209 endstr = optarg + strlen(optarg);
210 if (endptr != endstr) {
211 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
212 phm2pj_usage(argv[0]);
217 opt_nray = strtol(optarg, &endptr, 10);
218 endstr = optarg + strlen(optarg);
219 if (endptr != endstr) {
220 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
221 phm2pj_usage(argv[0]);
226 opt_offsetview = strtol(optarg, &endptr, 10);
227 endstr = optarg + strlen(optarg);
228 if (endptr != endstr) {
229 std::cerr << "Error setting --offsetview to %s" << optarg << std::endl;
230 phm2pj_usage(argv[0]);
237 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
239 std::cout << "Unknown version number\n";
244 phm2pj_usage(argv[0]);
247 phm2pj_usage(argv[0]);
252 if (optPhmName == "" && optPhmFileName == "") {
253 std::cerr << "No phantom defined\n" << std::endl;
254 phm2pj_usage(argv[0]);
257 if (optind + 3 != argc) {
258 phm2pj_usage(argv[0]);
262 opt_outfile = argv[optind];
263 opt_ndet = strtol(argv[optind+1], &endptr, 10);
264 endstr = argv[optind+1] + strlen(argv[optind+1]);
265 if (endptr != endstr) {
266 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
267 phm2pj_usage(argv[0]);
270 opt_nview = strtol(argv[optind+2], &endptr, 10);
271 endstr = argv[optind+2] + strlen(argv[optind+2]);
272 if (endptr != endstr) {
273 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
274 phm2pj_usage(argv[0]);
278 if (opt_rotangle < 0) {
279 if (optGeometryName.compare ("parallel") == 0)
285 std::ostringstream desc;
286 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << "OffsetView =" << opt_offsetview << ", Geometry=" << optGeometryName << ", ";
287 if (optPhmFileName.length()) {
288 desc << "PhantomFile=" << optPhmFileName;
289 } else if (optPhmName != "") {
290 desc << "Phantom=" << optPhmName;
292 if (opt_desc.length()) {
293 desc << ": " << opt_desc;
295 opt_desc = desc.str();
297 if (optPhmName != "") {
298 phm.createFromPhantom (optPhmName.c_str());
300 std::cout << phm.failMessage() << std::endl << std::endl;
301 phm2pj_usage(argv[0]);
306 if (optPhmFileName != "") {
308 std::cerr << "Can not read phantom from file in MPI mode\n";
311 phm.createFromFile (optPhmFileName.c_str());
319 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
320 mpiWorld.BcastString (optPhmName);
321 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
322 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
323 mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0);
324 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
325 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
326 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
327 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
328 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
329 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
330 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
331 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
333 timerBcast.timerEndAndReport ("Time to broadcast variables");
335 if (mpiWorld.getRank() > 0 && optPhmName != "")
336 phm.createFromPhantom (optPhmName.c_str());
339 opt_rotangle *= TWOPI;
340 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_offsetview, opt_nray,
341 opt_rotangle, dOptFocalLength, dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
342 if (scanner.fail()) {
343 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
347 mpiWorld.setTotalWorkUnits (opt_nview);
349 Projections pjGlobal;
350 if (mpiWorld.getRank() == 0)
351 pjGlobal.initFromScanner (scanner);
354 std::ostringstream os;
355 pjGlobal.printScanInfo(os);
356 std::cout << os.str();
359 Projections pjLocal (scanner);
360 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
363 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
365 TimerCollectiveMPI timerProject (mpiWorld.getComm());
366 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
368 timerProject.timerEndAndReport ("Time to collect projections");
370 TimerCollectiveMPI timerGather (mpiWorld.getComm());
371 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
373 timerGather.timerEndAndReport ("Time to gather projections");
376 Projections pjGlobal (scanner);
377 scanner.collectProjections (pjGlobal, phm, 0, opt_nview, opt_offsetview, true, opt_trace);
381 if (mpiWorld.getRank() == 0)
384 pjGlobal.setCalcTime (timerProgram.timerEnd());
385 pjGlobal.setRemark (opt_desc);
386 pjGlobal.write (opt_outfile);
388 phm.print (std::cout);
389 std::cout << std::endl;
390 std::ostringstream os;
391 pjGlobal.printScanInfo (os);
392 std::cout << os.str() << std::endl;
393 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
394 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
403 * GatherProjectionsMPI
406 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
410 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
412 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
413 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
414 double viewAngle = detArray.viewAngle();
415 int nDet = detArray.nDet();
416 DetectorValue* detval = detArray.detValues();
418 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
419 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
420 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
423 if (mpiWorld.getRank() == 0) {
424 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
425 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
429 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
430 DetectorValue* detval = detArray.detValues();
432 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
433 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
434 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
435 detArray.setViewAngle (viewAngle);
445 main (int argc, char* argv[])
450 retval = phm2pj_main(argc, argv);
452 // dmalloc_shutdown();
454 } catch (exception e) {
455 std::cerr << "Exception: " << e.what() << std::endl;
457 std::cerr << "Unknown exception\n";