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.32 2002/05/03 10:28:17 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.32 2002/05/03 10:28:17 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 << " --verbose Verbose mode\n";
93 std::cout << " --debug Debug mode\n";
94 std::cout << " --version Print version\n";
95 std::cout << " --help Print this help message\n";
99 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
103 phm2pj_main (int argc, char* const argv[])
106 std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
107 char *opt_outfile = NULL;
108 std::string opt_desc;
109 std::string optPhmName;
110 std::string optPhmFileName;
113 int opt_offsetview = 0;
115 double dOptFocalLength = 2.;
116 double dOptCenterDetectorLength = 2.;
117 double dOptViewRatio = 1.;
118 double dOptScanRatio = 1.;
119 int opt_trace = Trace::TRACE_NONE;
122 double opt_rotangle = -1;
127 MPIWorld mpiWorld (argc, argv);
133 if (mpiWorld.getRank() == 0) {
136 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
146 optPhmFileName = optarg;
156 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
157 phm2pj_usage(argv[0]);
165 opt_rotangle = strtod(optarg, &endptr);
166 endstr = optarg + strlen(optarg);
167 if (endptr != endstr) {
168 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
169 phm2pj_usage(argv[0]);
174 optGeometryName = optarg;
177 dOptFocalLength = strtod(optarg, &endptr);
178 endstr = optarg + strlen(optarg);
179 if (endptr != endstr) {
180 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
181 phm2pj_usage(argv[0]);
185 case O_CENTER_DETECTOR_LENGTH:
186 dOptCenterDetectorLength = strtod(optarg, &endptr);
187 endstr = optarg + strlen(optarg);
188 if (endptr != endstr) {
189 std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
190 phm2pj_usage(argv[0]);
195 dOptViewRatio = strtod(optarg, &endptr);
196 endstr = optarg + strlen(optarg);
197 if (endptr != endstr) {
198 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
199 phm2pj_usage(argv[0]);
204 dOptScanRatio = strtod(optarg, &endptr);
205 endstr = optarg + strlen(optarg);
206 if (endptr != endstr) {
207 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
208 phm2pj_usage(argv[0]);
213 opt_nray = strtol(optarg, &endptr, 10);
214 endstr = optarg + strlen(optarg);
215 if (endptr != endstr) {
216 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
217 phm2pj_usage(argv[0]);
222 opt_offsetview = strtol(optarg, &endptr, 10);
223 endstr = optarg + strlen(optarg);
224 if (endptr != endstr) {
225 std::cerr << "Error setting --offsetview to %s" << optarg << std::endl;
226 phm2pj_usage(argv[0]);
233 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
235 std::cout << "Unknown version number\n";
240 phm2pj_usage(argv[0]);
243 phm2pj_usage(argv[0]);
248 if (optPhmName == "" && optPhmFileName == "") {
249 std::cerr << "No phantom defined\n" << std::endl;
250 phm2pj_usage(argv[0]);
253 if (optind + 3 != argc) {
254 phm2pj_usage(argv[0]);
258 opt_outfile = argv[optind];
259 opt_ndet = strtol(argv[optind+1], &endptr, 10);
260 endstr = argv[optind+1] + strlen(argv[optind+1]);
261 if (endptr != endstr) {
262 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
263 phm2pj_usage(argv[0]);
266 opt_nview = strtol(argv[optind+2], &endptr, 10);
267 endstr = argv[optind+2] + strlen(argv[optind+2]);
268 if (endptr != endstr) {
269 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
270 phm2pj_usage(argv[0]);
274 if (opt_rotangle < 0) {
275 if (optGeometryName.compare ("parallel") == 0)
281 std::ostringstream desc;
282 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << "OffsetView =" << opt_offsetview << ", Geometry=" << optGeometryName << ", ";
283 if (optPhmFileName.length()) {
284 desc << "PhantomFile=" << optPhmFileName;
285 } else if (optPhmName != "") {
286 desc << "Phantom=" << optPhmName;
288 if (opt_desc.length()) {
289 desc << ": " << opt_desc;
291 opt_desc = desc.str();
293 if (optPhmName != "") {
294 phm.createFromPhantom (optPhmName.c_str());
296 std::cout << phm.failMessage() << std::endl << std::endl;
297 phm2pj_usage(argv[0]);
302 if (optPhmFileName != "") {
304 std::cerr << "Can not read phantom from file in MPI mode\n";
307 phm.createFromFile (optPhmFileName.c_str());
315 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
316 mpiWorld.BcastString (optPhmName);
317 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
318 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
319 mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0);
320 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
321 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
322 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
323 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
324 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
325 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
326 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
327 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
329 timerBcast.timerEndAndReport ("Time to broadcast variables");
331 if (mpiWorld.getRank() > 0 && optPhmName != "")
332 phm.createFromPhantom (optPhmName.c_str());
335 opt_rotangle *= TWOPI;
336 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_offsetview, opt_nray,
337 opt_rotangle, dOptFocalLength, dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
338 if (scanner.fail()) {
339 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
343 mpiWorld.setTotalWorkUnits (opt_nview);
345 Projections pjGlobal;
346 if (mpiWorld.getRank() == 0)
347 pjGlobal.initFromScanner (scanner);
350 std::ostringstream os;
351 pjGlobal.printScanInfo(os);
352 std::cout << os.str();
355 Projections pjLocal (scanner);
356 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
359 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
361 TimerCollectiveMPI timerProject (mpiWorld.getComm());
362 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
364 timerProject.timerEndAndReport ("Time to collect projections");
366 TimerCollectiveMPI timerGather (mpiWorld.getComm());
367 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
369 timerGather.timerEndAndReport ("Time to gather projections");
372 Projections pjGlobal (scanner);
373 scanner.collectProjections (pjGlobal, phm, 0, opt_nview, opt_offsetview, true, opt_trace);
377 if (mpiWorld.getRank() == 0)
380 pjGlobal.setCalcTime (timerProgram.timerEnd());
381 pjGlobal.setRemark (opt_desc);
382 pjGlobal.write (opt_outfile);
384 phm.print (std::cout);
385 std::cout << std::endl;
386 std::ostringstream os;
387 pjGlobal.printScanInfo (os);
388 std::cout << os.str() << std::endl;
389 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
390 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
399 * GatherProjectionsMPI
402 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
406 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
408 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
409 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
410 double viewAngle = detArray.viewAngle();
411 int nDet = detArray.nDet();
412 DetectorValue* detval = detArray.detValues();
414 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
415 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
416 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
419 if (mpiWorld.getRank() == 0) {
420 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
421 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
425 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
426 DetectorValue* detval = detArray.detValues();
428 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
429 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
430 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
431 detArray.setViewAngle (viewAngle);
441 main (int argc, char* argv[])
446 retval = phm2pj_main(argc, argv);
448 // dmalloc_shutdown();
450 } catch (exception e) {
451 std::cerr << "Exception: " << e.what() << std::endl;
453 std::cerr << "Unknown exception\n";