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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
30 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE, O_GEOMETRY, O_FOCAL_LENGTH, O_CENTER_DETECTOR_LENGTH,
31 O_VIEW_RATIO, O_SCAN_RATIO, O_OFFSETVIEW, O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
33 static struct option phm2pj_options[] =
35 {"phantom", 1, 0, O_PHANTOM},
36 {"phmfile", 1, 0, O_PHMFILE},
37 {"desc", 1, 0, O_DESC},
38 {"nray", 1, 0, O_NRAY},
39 {"rotangle", 1, 0, O_ROTANGLE},
40 {"geometry", 1, 0, O_GEOMETRY},
41 {"focal-length", 1, 0, O_FOCAL_LENGTH},
42 {"center-detector-length", 1, 0, O_CENTER_DETECTOR_LENGTH},
43 {"offsetview", 1, 0, O_OFFSETVIEW},
44 {"view-ratio", 1, 0, O_VIEW_RATIO},
45 {"scan-ratio", 1, 0, O_SCAN_RATIO},
46 {"trace", 1, 0, O_TRACE},
47 {"verbose", 0, 0, O_VERBOSE},
48 {"help", 0, 0, O_HELP},
49 {"debug", 0, 0, O_DEBUG},
50 {"version", 0, 0, O_VERSION},
54 static const char* g_szIdStr = "$Id$";
58 phm2pj_usage (const char *program)
60 std::cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
61 std::cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
62 std::cout << " outfile Name of output file for projections\n";
63 std::cout << " ndet Number of detectors\n";
64 std::cout << " nview Number of rotated views\n";
65 std::cout << " --phantom Phantom to use for projection\n";
66 std::cout << " herman Herman head phantom\n";
67 std::cout << " shepp-logan Shepp-Logan head phantom\n";
68 std::cout << " unit-pulse Unit pulse phantom\n";
69 std::cout << " --phmfile Get Phantom from phantom file\n";
70 std::cout << " --desc Description of raysum\n";
71 std::cout << " --nray Number of rays per detector (default = 1)\n";
72 std::cout << " --rotangle Angle to rotate view through (fraction of a circle)\n";
73 std::cout << " (default = select appropriate for geometry)\n";
74 std::cout << " --geometry Geometry of scanning\n";
75 std::cout << " parallel Parallel scan beams (default)\n";
76 std::cout << " equilinear Equilinear divergent scan beams\n";
77 std::cout << " equiangular Equiangular divergent scan beams\n";
78 std::cout << " --focal-length Focal length ratio (ratio to radius of view area)\n";
79 std::cout << " (default = 2)\n";
80 std::cout << " --center-detector-length Distance from center of phantom to detector array\n";
81 std::cout << " (ratio to radius of view area) (default = 2)\n";
82 std::cout << " --view-ratio Length to view (view diameter to phantom diameter)\n";
83 std::cout << " (default = 1)\n";
84 std::cout << " --scan-ratio Length to scan (scan diameter to view diameter)\n";
85 std::cout << " (default = 1)\n";
86 std::cout << " --offsetview Initial gantry offset in 'views' (default = 0)\n";
87 std::cout << " --trace Trace level to use\n";
88 std::cout << " none No tracing (default)\n";
89 std::cout << " console Trace text level\n";
90 std::cout << " --verbose Verbose mode\n";
91 std::cout << " --debug Debug mode\n";
92 std::cout << " --version Print version\n";
93 std::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* const argv[])
104 std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
105 char *opt_outfile = NULL;
106 std::string opt_desc;
107 std::string optPhmName;
108 std::string optPhmFileName;
111 int opt_offsetview = 0;
113 double dOptFocalLength = 2.;
114 double dOptCenterDetectorLength = 2.;
115 double dOptViewRatio = 1.;
116 double dOptScanRatio = 1.;
117 int opt_trace = Trace::TRACE_NONE;
120 double opt_rotangle = -1;
126 MPIWorld mpiWorld (argc, argv);
132 if (mpiWorld.getRank() == 0) {
135 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
145 optPhmFileName = optarg;
155 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
156 phm2pj_usage(argv[0]);
164 opt_rotangle = strtod(optarg, &endptr);
165 endstr = optarg + strlen(optarg);
166 if (endptr != endstr) {
167 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
168 phm2pj_usage(argv[0]);
173 optGeometryName = optarg;
176 dOptFocalLength = strtod(optarg, &endptr);
177 endstr = optarg + strlen(optarg);
178 if (endptr != endstr) {
179 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
180 phm2pj_usage(argv[0]);
184 case O_CENTER_DETECTOR_LENGTH:
185 dOptCenterDetectorLength = strtod(optarg, &endptr);
186 endstr = optarg + strlen(optarg);
187 if (endptr != endstr) {
188 std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
189 phm2pj_usage(argv[0]);
194 dOptViewRatio = strtod(optarg, &endptr);
195 endstr = optarg + strlen(optarg);
196 if (endptr != endstr) {
197 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
198 phm2pj_usage(argv[0]);
203 dOptScanRatio = strtod(optarg, &endptr);
204 endstr = optarg + strlen(optarg);
205 if (endptr != endstr) {
206 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
207 phm2pj_usage(argv[0]);
212 opt_nray = strtol(optarg, &endptr, 10);
213 endstr = optarg + strlen(optarg);
214 if (endptr != endstr) {
215 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
216 phm2pj_usage(argv[0]);
221 opt_offsetview = strtol(optarg, &endptr, 10);
222 endstr = optarg + strlen(optarg);
223 if (endptr != endstr) {
224 std::cerr << "Error setting --offsetview to %s" << optarg << std::endl;
225 phm2pj_usage(argv[0]);
232 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
234 std::cout << "Unknown version number\n";
239 phm2pj_usage(argv[0]);
242 phm2pj_usage(argv[0]);
247 if (optPhmName == "" && optPhmFileName == "") {
248 std::cerr << "No phantom defined\n" << std::endl;
249 phm2pj_usage(argv[0]);
252 if (optind + 3 != argc) {
253 phm2pj_usage(argv[0]);
257 opt_outfile = argv[optind];
258 opt_ndet = strtol(argv[optind+1], &endptr, 10);
259 endstr = argv[optind+1] + strlen(argv[optind+1]);
260 if (endptr != endstr) {
261 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
262 phm2pj_usage(argv[0]);
265 opt_nview = strtol(argv[optind+2], &endptr, 10);
266 endstr = argv[optind+2] + strlen(argv[optind+2]);
267 if (endptr != endstr) {
268 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
269 phm2pj_usage(argv[0]);
273 if (opt_rotangle < 0) {
274 if (optGeometryName.compare ("parallel") == 0)
280 std::ostringstream desc;
281 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", OffsetView =" << opt_offsetview << ", Geometry=" << optGeometryName << ", ";
282 if (optPhmFileName.length()) {
283 desc << "PhantomFile=" << optPhmFileName;
284 } else if (optPhmName != "") {
285 desc << "Phantom=" << optPhmName;
287 if (opt_desc.length()) {
288 desc << ": " << opt_desc;
290 opt_desc = desc.str();
292 if (optPhmName != "") {
293 phm.createFromPhantom (optPhmName.c_str());
295 std::cout << phm.failMessage() << std::endl << std::endl;
296 phm2pj_usage(argv[0]);
301 if (optPhmFileName != "") {
303 std::cerr << "Can not read phantom from file in MPI mode\n";
306 phm.createFromFile (optPhmFileName.c_str());
314 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
315 mpiWorld.BcastString (optPhmName);
316 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
317 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
318 mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0);
319 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
320 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
321 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
322 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
323 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
324 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
325 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
326 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
328 timerBcast.timerEndAndReport ("Time to broadcast variables");
330 if (mpiWorld.getRank() > 0 && optPhmName != "")
331 phm.createFromPhantom (optPhmName.c_str());
334 opt_rotangle *= TWOPI;
335 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_offsetview, opt_nray,
336 opt_rotangle, dOptFocalLength, dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
337 if (scanner.fail()) {
338 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
342 mpiWorld.setTotalWorkUnits (opt_nview);
344 Projections pjGlobal;
345 if (mpiWorld.getRank() == 0)
346 pjGlobal.initFromScanner (scanner);
349 std::ostringstream os;
350 pjGlobal.printScanInfo(os);
351 std::cout << os.str();
354 Projections pjLocal (scanner);
355 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
358 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
360 TimerCollectiveMPI timerProject (mpiWorld.getComm());
361 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
363 timerProject.timerEndAndReport ("Time to collect projections");
365 TimerCollectiveMPI timerGather (mpiWorld.getComm());
366 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
368 timerGather.timerEndAndReport ("Time to gather projections");
371 Projections pjGlobal (scanner);
372 scanner.collectProjections (pjGlobal, phm, 0, opt_nview, opt_offsetview, true, opt_trace);
376 if (mpiWorld.getRank() == 0)
379 pjGlobal.setCalcTime (timerProgram.timerEnd());
380 pjGlobal.setRemark (opt_desc);
381 pjGlobal.write (opt_outfile);
383 phm.print (std::cout);
384 std::cout << std::endl;
385 std::ostringstream os;
386 pjGlobal.printScanInfo (os);
387 std::cout << os.str() << std::endl;
388 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
389 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
398 * GatherProjectionsMPI
401 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
405 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
407 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
408 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
409 double viewAngle = detArray.viewAngle();
410 int nDet = detArray.nDet();
411 DetectorValue* detval = detArray.detValues();
413 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
414 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
415 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
418 if (mpiWorld.getRank() == 0) {
419 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
420 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
424 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
425 DetectorValue* detval = detArray.detValues();
427 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
428 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
429 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
430 detArray.setViewAngle (viewAngle);
440 main (int argc, char* argv[])
445 retval = phm2pj_main(argc, argv);
447 // dmalloc_shutdown();
449 } catch (exception e) {
450 std::cerr << "Exception: " << e.what() << std::endl;
452 std::cerr << "Unknown exception\n";