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.23 2001/01/17 06:25:15 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_FIELD_OF_VIEW,
33 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 {"field-of-view", 1, 0, O_FIELD_OF_VIEW},
45 {"trace", 1, 0, O_TRACE},
46 {"verbose", 0, 0, O_VERBOSE},
47 {"help", 0, 0, O_HELP},
48 {"debug", 0, 0, O_DEBUG},
49 {"version", 0, 0, O_VERSION},
53 static const char* g_szIdStr = "$Id: phm2pj.cpp,v 1.23 2001/01/17 06:25:15 kevin Exp $";
57 phm2pj_usage (const char *program)
59 std::cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n";
60 std::cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile\n\n";
61 std::cout << " outfile Name of output file for projections\n";
62 std::cout << " ndet Number of detectors\n";
63 std::cout << " nview Number of rotated views\n";
64 std::cout << " --phantom Phantom to use for projection\n";
65 std::cout << " herman Herman head phantom\n";
66 std::cout << " herman-b Herman head phantom (Bordered)\n";
67 std::cout << " shepp-logan Shepp-Logan head phantom\n";
68 std::cout << " shepp-logan-b Shepp-Logan head phantom (Bordered)\n";
69 std::cout << " unitpulse 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 Degrees to rotate view through (multiple of PI)\n";
74 std::cout << " (default = 1)\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 phantom)\n";
80 std::cout << " (default = 1)\n";
81 std::cout << " --field-of-view Field of view (ratio to diameter of phantom square)\n";
82 std::cout << " (default = 1)\n";
83 std::cout << " --trace Trace level to use\n";
84 std::cout << " none No tracing (default)\n";
85 std::cout << " console Trace text level\n";
86 std::cout << " phantom Trace phantom image\n";
87 std::cout << " proj Trace projections\n";
88 std::cout << " plot Trace plot\n";
89 std::cout << " clipping Trace clipping\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;
112 double dOptFocalLength = 1.;
113 double dOptFieldOfView = 1.;
114 int opt_trace = Trace::TRACE_NONE;
117 double opt_rotangle = 1;
122 MPIWorld mpiWorld (argc, argv);
128 if (mpiWorld.getRank() == 0) {
131 int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
141 optPhmFileName = optarg;
151 if ((opt_trace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
152 phm2pj_usage(argv[0]);
160 opt_rotangle = strtod(optarg, &endptr);
161 endstr = optarg + strlen(optarg);
162 if (endptr != endstr) {
163 std::cerr << "Error setting --rotangle to " << optarg << std::endl;
164 phm2pj_usage(argv[0]);
169 optGeometryName = optarg;
172 dOptFocalLength = strtod(optarg, &endptr);
173 endstr = optarg + strlen(optarg);
174 if (endptr != endstr) {
175 std::cerr << "Error setting --focal-length to " << optarg << std::endl;
176 phm2pj_usage(argv[0]);
180 case O_FIELD_OF_VIEW:
181 dOptFieldOfView = strtod(optarg, &endptr);
182 endstr = optarg + strlen(optarg);
183 if (endptr != endstr) {
184 std::cerr << "Error setting --field-of-view to " << optarg << std::endl;
185 phm2pj_usage(argv[0]);
190 opt_nray = strtol(optarg, &endptr, 10);
191 endstr = optarg + strlen(optarg);
192 if (endptr != endstr) {
193 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
194 phm2pj_usage(argv[0]);
200 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
202 std::cout << "Unknown version number\n";
207 phm2pj_usage(argv[0]);
210 phm2pj_usage(argv[0]);
215 if (optPhmName == "" && optPhmFileName == "") {
216 std::cerr << "No phantom defined\n" << std::endl;
217 phm2pj_usage(argv[0]);
220 if (optind + 3 != argc) {
221 phm2pj_usage(argv[0]);
225 opt_outfile = argv[optind];
226 opt_ndet = strtol(argv[optind+1], &endptr, 10);
227 endstr = argv[optind+1] + strlen(argv[optind+1]);
228 if (endptr != endstr) {
229 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
230 phm2pj_usage(argv[0]);
233 opt_nview = strtol(argv[optind+2], &endptr, 10);
234 endstr = argv[optind+2] + strlen(argv[optind+2]);
235 if (endptr != endstr) {
236 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
237 phm2pj_usage(argv[0]);
241 std::ostringstream desc;
242 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
243 if (optPhmFileName.length()) {
244 desc << "PhantomFile=" << optPhmFileName;
245 } else if (optPhmName != "") {
246 desc << "Phantom=" << optPhmName;
248 if (opt_desc.length()) {
249 desc << ": " << opt_desc;
251 opt_desc = desc.str();
253 if (optPhmName != "") {
254 phm.createFromPhantom (optPhmName.c_str());
256 std::cout << phm.failMessage() << std::endl << std::endl;
257 phm2pj_usage(argv[0]);
262 if (optPhmFileName != "") {
264 std::cerr << "Can not read phantom from file in MPI mode\n";
267 phm.createFromFile (optPhmFileName.c_str());
275 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
276 mpiWorld.BcastString (optPhmName);
277 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
278 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
285 timerBcast.timerEndAndReport ("Time to broadcast variables");
287 if (mpiWorld.getRank() > 0 && optPhmName != "")
288 phm.createFromPhantom (optPhmName.c_str());
292 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength, dOptFieldOfView);
293 if (scanner.fail()) {
294 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
298 mpiWorld.setTotalWorkUnits (opt_nview);
300 Projections pjGlobal;
301 if (mpiWorld.getRank() == 0)
302 pjGlobal.initFromScanner (scanner);
305 std::ostringstream os;
306 pjGlobal.printScanInfo(os);
307 std::cout << os.str();
310 Projections pjLocal (scanner);
311 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
314 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
316 TimerCollectiveMPI timerProject (mpiWorld.getComm());
317 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
319 timerProject.timerEndAndReport ("Time to collect projections");
321 TimerCollectiveMPI timerGather (mpiWorld.getComm());
322 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
324 timerGather.timerEndAndReport ("Time to gather projections");
327 Projections pjGlobal (scanner);
328 scanner.collectProjections (pjGlobal, phm, opt_trace);
332 if (mpiWorld.getRank() == 0)
335 pjGlobal.setCalcTime (timerProgram.timerEnd());
336 pjGlobal.setRemark (opt_desc);
337 pjGlobal.write (opt_outfile);
339 phm.print (std::cout);
340 std::cout << std::endl;
341 std::ostringstream os;
342 pjGlobal.printScanInfo (os);
343 std::cout << os.str() << std::endl;
344 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
345 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
354 * GatherProjectionsMPI
357 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
361 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
363 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
364 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
365 double viewAngle = detArray.viewAngle();
366 int nDet = detArray.nDet();
367 DetectorValue* detval = detArray.detValues();
369 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
370 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
371 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
374 if (mpiWorld.getRank() == 0) {
375 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
376 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
380 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
381 DetectorValue* detval = detArray.detValues();
383 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
384 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
385 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
386 detArray.setViewAngle (viewAngle);
396 main (int argc, char* argv[])
401 retval = phm2pj_main(argc, argv);
403 // dmalloc_shutdown();
405 } catch (exception e) {
406 std::cerr << "Exception: " << e.what() << std::endl;
408 std::cerr << "Unknown exception\n";