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.28 2001/03/01 07:30:49 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.28 2001/03/01 07:30:49 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 << " herman-b Herman head phantom (Bordered)\n";
69 std::cout << " shepp-logan Shepp-Logan head phantom\n";
70 std::cout << " shepp-logan-b Shepp-Logan head phantom (Bordered)\n";
71 std::cout << " unitpulse Unit pulse phantom\n";
72 std::cout << " --phmfile Get Phantom from phantom file\n";
73 std::cout << " --desc Description of raysum\n";
74 std::cout << " --nray Number of rays per detector (default = 1)\n";
75 std::cout << " --rotangle Angle to rotate view through (fraction of a circle)\n";
76 std::cout << " (default = select appropriate for geometry)\n";
77 std::cout << " --geometry Geometry of scanning\n";
78 std::cout << " parallel Parallel scan beams (default)\n";
79 std::cout << " equilinear Equilinear divergent scan beams\n";
80 std::cout << " equiangular Equiangular divergent scan beams\n";
81 std::cout << " --focal-length Focal length ratio (ratio to radius of view area)\n";
82 std::cout << " (default = 2)\n";
83 std::cout << " --center-detector-length Distance from center of phantom to detector array\n";
84 std::cout << " (ratio to radius of view area) (default = 2)\n";
85 std::cout << " --view-ratio Length to view (view diameter to phantom diameter)\n";
86 std::cout << " (default = 1)\n";
87 std::cout << " --scan-ratio Length to scan (scan diameter to view diameter)\n";
88 std::cout << " (default = 1)\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;
118 double dOptFocalLength = 2.;
119 double dOptCenterDetectorLength = 2.;
120 double dOptViewRatio = 1.;
121 double dOptScanRatio = 1.;
122 int opt_trace = Trace::TRACE_NONE;
125 double opt_rotangle = -1;
130 MPIWorld mpiWorld (argc, argv);
136 if (mpiWorld.getRank() == 0) {
139 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
149 optPhmFileName = optarg;
159 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
160 phm2pj_usage(argv[0]);
168 opt_rotangle = strtod(optarg, &endptr);
169 endstr = optarg + strlen(optarg);
170 if (endptr != endstr) {
171 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
172 phm2pj_usage(argv[0]);
177 optGeometryName = optarg;
180 dOptFocalLength = strtod(optarg, &endptr);
181 endstr = optarg + strlen(optarg);
182 if (endptr != endstr) {
183 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
184 phm2pj_usage(argv[0]);
188 case O_CENTER_DETECTOR_LENGTH:
189 dOptCenterDetectorLength = strtod(optarg, &endptr);
190 endstr = optarg + strlen(optarg);
191 if (endptr != endstr) {
192 std::cerr << "Error setting --center-detector-length to " << optarg << std::endl;
193 phm2pj_usage(argv[0]);
198 dOptViewRatio = strtod(optarg, &endptr);
199 endstr = optarg + strlen(optarg);
200 if (endptr != endstr) {
201 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
202 phm2pj_usage(argv[0]);
207 dOptScanRatio = strtod(optarg, &endptr);
208 endstr = optarg + strlen(optarg);
209 if (endptr != endstr) {
210 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
211 phm2pj_usage(argv[0]);
216 opt_nray = strtol(optarg, &endptr, 10);
217 endstr = optarg + strlen(optarg);
218 if (endptr != endstr) {
219 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
220 phm2pj_usage(argv[0]);
226 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
228 std::cout << "Unknown version number\n";
233 phm2pj_usage(argv[0]);
236 phm2pj_usage(argv[0]);
241 if (optPhmName == "" && optPhmFileName == "") {
242 std::cerr << "No phantom defined\n" << std::endl;
243 phm2pj_usage(argv[0]);
246 if (optind + 3 != argc) {
247 phm2pj_usage(argv[0]);
251 opt_outfile = argv[optind];
252 opt_ndet = strtol(argv[optind+1], &endptr, 10);
253 endstr = argv[optind+1] + strlen(argv[optind+1]);
254 if (endptr != endstr) {
255 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
256 phm2pj_usage(argv[0]);
259 opt_nview = strtol(argv[optind+2], &endptr, 10);
260 endstr = argv[optind+2] + strlen(argv[optind+2]);
261 if (endptr != endstr) {
262 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
263 phm2pj_usage(argv[0]);
267 if (opt_rotangle < 0) {
268 if (optGeometryName.compare ("parallel") == 0)
274 std::ostringstream desc;
275 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
276 if (optPhmFileName.length()) {
277 desc << "PhantomFile=" << optPhmFileName;
278 } else if (optPhmName != "") {
279 desc << "Phantom=" << optPhmName;
281 if (opt_desc.length()) {
282 desc << ": " << opt_desc;
284 opt_desc = desc.str();
286 if (optPhmName != "") {
287 phm.createFromPhantom (optPhmName.c_str());
289 std::cout << phm.failMessage() << std::endl << std::endl;
290 phm2pj_usage(argv[0]);
295 if (optPhmFileName != "") {
297 std::cerr << "Can not read phantom from file in MPI mode\n";
300 phm.createFromFile (optPhmFileName.c_str());
308 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
309 mpiWorld.BcastString (optPhmName);
310 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
311 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
312 mpiWorld.getComm().Bcast (&dOptCenterDetectorLength, 1, MPI::DOUBLE, 0)
313 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
314 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
315 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
316 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
317 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
318 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
319 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
320 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
322 timerBcast.timerEndAndReport ("Time to broadcast variables");
324 if (mpiWorld.getRank() > 0 && optPhmName != "")
325 phm.createFromPhantom (optPhmName.c_str());
328 opt_rotangle *= TWOPI;
329 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength,
330 dOptCenterDetectorLength, dOptViewRatio, dOptScanRatio);
331 if (scanner.fail()) {
332 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
336 mpiWorld.setTotalWorkUnits (opt_nview);
338 Projections pjGlobal;
339 if (mpiWorld.getRank() == 0)
340 pjGlobal.initFromScanner (scanner);
343 std::ostringstream os;
344 pjGlobal.printScanInfo(os);
345 std::cout << os.str();
348 Projections pjLocal (scanner);
349 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
352 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
354 TimerCollectiveMPI timerProject (mpiWorld.getComm());
355 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
357 timerProject.timerEndAndReport ("Time to collect projections");
359 TimerCollectiveMPI timerGather (mpiWorld.getComm());
360 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
362 timerGather.timerEndAndReport ("Time to gather projections");
365 Projections pjGlobal (scanner);
366 scanner.collectProjections (pjGlobal, phm, opt_trace);
370 if (mpiWorld.getRank() == 0)
373 pjGlobal.setCalcTime (timerProgram.timerEnd());
374 pjGlobal.setRemark (opt_desc);
375 pjGlobal.write (opt_outfile);
377 phm.print (std::cout);
378 std::cout << std::endl;
379 std::ostringstream os;
380 pjGlobal.printScanInfo (os);
381 std::cout << os.str() << std::endl;
382 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
383 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
392 * GatherProjectionsMPI
395 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
399 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
401 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
402 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
403 double viewAngle = detArray.viewAngle();
404 int nDet = detArray.nDet();
405 DetectorValue* detval = detArray.detValues();
407 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
408 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
409 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
412 if (mpiWorld.getRank() == 0) {
413 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
414 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
418 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
419 DetectorValue* detval = detArray.detValues();
421 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
422 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
423 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
424 detArray.setViewAngle (viewAngle);
434 main (int argc, char* argv[])
439 retval = phm2pj_main(argc, argv);
441 // dmalloc_shutdown();
443 } catch (exception e) {
444 std::cerr << "Exception: " << e.what() << std::endl;
446 std::cerr << "Unknown exception\n";