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.30 2001/04/02 03:49:52 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_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 {"view-ratio", 1, 0, O_VIEW_RATIO},
46 {"scan-ratio", 1, 0, O_SCAN_RATIO},
47 {"trace", 1, 0, O_TRACE},
48 {"verbose", 0, 0, O_VERBOSE},
49 {"help", 0, 0, O_HELP},
50 {"debug", 0, 0, O_DEBUG},
51 {"version", 0, 0, O_VERSION},
55 static const char* g_szIdStr = "$Id: phm2pj.cpp,v 1.30 2001/04/02 03:49:52 kevin Exp $";
59 phm2pj_usage (const char *program)
61 std::cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
62 std::cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
63 std::cout << " outfile Name of output file for projections\n";
64 std::cout << " ndet Number of detectors\n";
65 std::cout << " nview Number of rotated views\n";
66 std::cout << " --phantom Phantom to use for projection\n";
67 std::cout << " herman Herman head phantom\n";
68 std::cout << " shepp-logan Shepp-Logan head phantom\n";
69 std::cout << " unit-pulse Unit pulse phantom\n";
70 std::cout << " --phmfile Get Phantom from phantom file\n";
71 std::cout << " --desc Description of raysum\n";
72 std::cout << " --nray Number of rays per detector (default = 1)\n";
73 std::cout << " --rotangle Angle to rotate view through (fraction of a circle)\n";
74 std::cout << " (default = select appropriate for geometry)\n";
75 std::cout << " --geometry Geometry of scanning\n";
76 std::cout << " parallel Parallel scan beams (default)\n";
77 std::cout << " equilinear Equilinear divergent scan beams\n";
78 std::cout << " equiangular Equiangular divergent scan beams\n";
79 std::cout << " --focal-length Focal length ratio (ratio to radius of view area)\n";
80 std::cout << " (default = 2)\n";
81 std::cout << " --center-detector-length Distance from center of phantom to detector array\n";
82 std::cout << " (ratio to radius of view area) (default = 2)\n";
83 std::cout << " --view-ratio Length to view (view diameter to phantom diameter)\n";
84 std::cout << " (default = 1)\n";
85 std::cout << " --scan-ratio Length to scan (scan diameter to view diameter)\n";
86 std::cout << " (default = 1)\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 << " phantom Trace phantom image\n";
91 std::cout << " proj Trace projections\n";
92 std::cout << " plot Trace plot\n";
93 std::cout << " clipping Trace clipping\n";
94 std::cout << " --verbose Verbose mode\n";
95 std::cout << " --debug Debug mode\n";
96 std::cout << " --version Print version\n";
97 std::cout << " --help Print this help message\n";
101 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
105 phm2pj_main (int argc, char* const argv[])
108 std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
109 char *opt_outfile = NULL;
110 std::string opt_desc;
111 std::string optPhmName;
112 std::string optPhmFileName;
116 double dOptFocalLength = 2.;
117 double dOptCenterDetectorLength = 2.;
118 double dOptViewRatio = 1.;
119 double dOptScanRatio = 1.;
120 int opt_trace = Trace::TRACE_NONE;
123 double opt_rotangle = -1;
128 MPIWorld mpiWorld (argc, argv);
134 if (mpiWorld.getRank() == 0) {
137 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
147 optPhmFileName = optarg;
157 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
158 phm2pj_usage(argv[0]);
166 opt_rotangle = strtod(optarg, &endptr);
167 endstr = optarg + strlen(optarg);
168 if (endptr != endstr) {
169 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
170 phm2pj_usage(argv[0]);
175 optGeometryName = optarg;
178 dOptFocalLength = strtod(optarg, &endptr);
179 endstr = optarg + strlen(optarg);
180 if (endptr != endstr) {
181 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
182 phm2pj_usage(argv[0]);
186 case O_CENTER_DETECTOR_LENGTH:
187 dOptCenterDetectorLength = strtod(optarg, &endptr);
188 endstr = optarg + strlen(optarg);
189 if (endptr != endstr) {
190 std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
191 phm2pj_usage(argv[0]);
196 dOptViewRatio = strtod(optarg, &endptr);
197 endstr = optarg + strlen(optarg);
198 if (endptr != endstr) {
199 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
200 phm2pj_usage(argv[0]);
205 dOptScanRatio = strtod(optarg, &endptr);
206 endstr = optarg + strlen(optarg);
207 if (endptr != endstr) {
208 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
209 phm2pj_usage(argv[0]);
214 opt_nray = strtol(optarg, &endptr, 10);
215 endstr = optarg + strlen(optarg);
216 if (endptr != endstr) {
217 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
218 phm2pj_usage(argv[0]);
224 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
226 std::cout << "Unknown version number\n";
231 phm2pj_usage(argv[0]);
234 phm2pj_usage(argv[0]);
239 if (optPhmName == "" && optPhmFileName == "") {
240 std::cerr << "No phantom defined\n" << std::endl;
241 phm2pj_usage(argv[0]);
244 if (optind + 3 != argc) {
245 phm2pj_usage(argv[0]);
249 opt_outfile = argv[optind];
250 opt_ndet = strtol(argv[optind+1], &endptr, 10);
251 endstr = argv[optind+1] + strlen(argv[optind+1]);
252 if (endptr != endstr) {
253 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
254 phm2pj_usage(argv[0]);
257 opt_nview = strtol(argv[optind+2], &endptr, 10);
258 endstr = argv[optind+2] + strlen(argv[optind+2]);
259 if (endptr != endstr) {
260 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
261 phm2pj_usage(argv[0]);
265 if (opt_rotangle < 0) {
266 if (optGeometryName.compare ("parallel") == 0)
272 std::ostringstream desc;
273 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
274 if (optPhmFileName.length()) {
275 desc << "PhantomFile=" << optPhmFileName;
276 } else if (optPhmName != "") {
277 desc << "Phantom=" << optPhmName;
279 if (opt_desc.length()) {
280 desc << ": " << opt_desc;
282 opt_desc = desc.str();
284 if (optPhmName != "") {
285 phm.createFromPhantom (optPhmName.c_str());
287 std::cout << phm.failMessage() << std::endl << std::endl;
288 phm2pj_usage(argv[0]);
293 if (optPhmFileName != "") {
295 std::cerr << "Can not read phantom from file in MPI mode\n";
298 phm.createFromFile (optPhmFileName.c_str());
306 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
307 mpiWorld.BcastString (optPhmName);
308 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
309 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
310 mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0);
311 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
312 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
313 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
314 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
315 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
316 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
317 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
318 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
320 timerBcast.timerEndAndReport ("Time to broadcast variables");
322 if (mpiWorld.getRank() > 0 && optPhmName != "")
323 phm.createFromPhantom (optPhmName.c_str());
326 opt_rotangle *= TWOPI;
327 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength,
328 dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
329 if (scanner.fail()) {
330 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
334 mpiWorld.setTotalWorkUnits (opt_nview);
336 Projections pjGlobal;
337 if (mpiWorld.getRank() == 0)
338 pjGlobal.initFromScanner (scanner);
341 std::ostringstream os;
342 pjGlobal.printScanInfo(os);
343 std::cout << os.str();
346 Projections pjLocal (scanner);
347 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
350 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
352 TimerCollectiveMPI timerProject (mpiWorld.getComm());
353 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
355 timerProject.timerEndAndReport ("Time to collect projections");
357 TimerCollectiveMPI timerGather (mpiWorld.getComm());
358 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
360 timerGather.timerEndAndReport ("Time to gather projections");
363 Projections pjGlobal (scanner);
364 scanner.collectProjections (pjGlobal, phm, opt_trace);
368 if (mpiWorld.getRank() == 0)
371 pjGlobal.setCalcTime (timerProgram.timerEnd());
372 pjGlobal.setRemark (opt_desc);
373 pjGlobal.write (opt_outfile);
375 phm.print (std::cout);
376 std::cout << std::endl;
377 std::ostringstream os;
378 pjGlobal.printScanInfo (os);
379 std::cout << os.str() << std::endl;
380 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
381 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
390 * GatherProjectionsMPI
393 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
397 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
399 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
400 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
401 double viewAngle = detArray.viewAngle();
402 int nDet = detArray.nDet();
403 DetectorValue* detval = detArray.detValues();
405 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
406 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
407 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
410 if (mpiWorld.getRank() == 0) {
411 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
412 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
416 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
417 DetectorValue* detval = detArray.detValues();
419 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
420 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
421 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
422 detArray.setViewAngle (viewAngle);
432 main (int argc, char* argv[])
437 retval = phm2pj_main(argc, argv);
439 // dmalloc_shutdown();
441 } catch (exception e) {
442 std::cerr << "Exception: " << e.what() << std::endl;
444 std::cerr << "Unknown exception\n";