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.26 2001/02/16 00:28:41 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_VIEW_RATIO, O_SCAN_RATIO,
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 {"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: phm2pj.cpp,v 1.26 2001/02/16 00:28:41 kevin Exp $";
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 << " herman-b Herman head phantom (Bordered)\n";
68 std::cout << " shepp-logan Shepp-Logan head phantom\n";
69 std::cout << " shepp-logan-b Shepp-Logan head phantom (Bordered)\n";
70 std::cout << " unitpulse 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 Degrees to rotate view through (multiple of PI)\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 phantom)\n";
81 std::cout << " (default = 1)\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 << " --trace Trace level to use\n";
87 std::cout << " none No tracing (default)\n";
88 std::cout << " console Trace text level\n";
89 std::cout << " phantom Trace phantom image\n";
90 std::cout << " proj Trace projections\n";
91 std::cout << " plot Trace plot\n";
92 std::cout << " clipping Trace clipping\n";
93 std::cout << " --verbose Verbose mode\n";
94 std::cout << " --debug Debug mode\n";
95 std::cout << " --version Print version\n";
96 std::cout << " --help Print this help message\n";
100 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
104 phm2pj_main (int argc, char* const argv[])
107 std::string optGeometryName = Scanner::convertGeometryIDToName(Scanner::GEOMETRY_PARALLEL);
108 char *opt_outfile = NULL;
109 std::string opt_desc;
110 std::string optPhmName;
111 std::string optPhmFileName;
115 double dOptFocalLength = 2.;
116 double dOptViewRatio = 1.;
117 double dOptScanRatio = 1.;
118 int opt_trace = Trace::TRACE_NONE;
121 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]);
185 dOptViewRatio = strtod(optarg, &endptr);
186 endstr = optarg + strlen(optarg);
187 if (endptr != endstr) {
188 std::cerr << "Error setting --view-ratio to " << optarg << std::endl;
189 phm2pj_usage(argv[0]);
194 dOptScanRatio = strtod(optarg, &endptr);
195 endstr = optarg + strlen(optarg);
196 if (endptr != endstr) {
197 std::cerr << "Error setting --scan-ratio to " << optarg << std::endl;
198 phm2pj_usage(argv[0]);
203 opt_nray = strtol(optarg, &endptr, 10);
204 endstr = optarg + strlen(optarg);
205 if (endptr != endstr) {
206 std::cerr << "Error setting --nray to %s" << optarg << std::endl;
207 phm2pj_usage(argv[0]);
213 std::cout << "Version: " << VERSION << std::endl << g_szIdStr << std::endl;
215 std::cout << "Unknown version number\n";
220 phm2pj_usage(argv[0]);
223 phm2pj_usage(argv[0]);
228 if (optPhmName == "" && optPhmFileName == "") {
229 std::cerr << "No phantom defined\n" << std::endl;
230 phm2pj_usage(argv[0]);
233 if (optind + 3 != argc) {
234 phm2pj_usage(argv[0]);
238 opt_outfile = argv[optind];
239 opt_ndet = strtol(argv[optind+1], &endptr, 10);
240 endstr = argv[optind+1] + strlen(argv[optind+1]);
241 if (endptr != endstr) {
242 std::cerr << "Error setting --ndet to " << argv[optind+1] << std::endl;
243 phm2pj_usage(argv[0]);
246 opt_nview = strtol(argv[optind+2], &endptr, 10);
247 endstr = argv[optind+2] + strlen(argv[optind+2]);
248 if (endptr != endstr) {
249 std::cerr << "Error setting --nview to " << argv[optind+2] << std::endl;
250 phm2pj_usage(argv[0]);
254 if (opt_rotangle < 0) {
255 if (optGeometryName.compare ("parallel") == 0)
261 std::ostringstream desc;
262 desc << "phm2pj: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", Geometry=" << optGeometryName << ", ";
263 if (optPhmFileName.length()) {
264 desc << "PhantomFile=" << optPhmFileName;
265 } else if (optPhmName != "") {
266 desc << "Phantom=" << optPhmName;
268 if (opt_desc.length()) {
269 desc << ": " << opt_desc;
271 opt_desc = desc.str();
273 if (optPhmName != "") {
274 phm.createFromPhantom (optPhmName.c_str());
276 std::cout << phm.failMessage() << std::endl << std::endl;
277 phm2pj_usage(argv[0]);
282 if (optPhmFileName != "") {
284 std::cerr << "Can not read phantom from file in MPI mode\n";
287 phm.createFromFile (optPhmFileName.c_str());
295 TimerCollectiveMPI timerBcast(mpiWorld.getComm());
296 mpiWorld.BcastString (optPhmName);
297 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
298 mpiWorld.getComm().Bcast (&dOptFocalLength, 1, MPI::DOUBLE, 0);
299 mpiWorld.getComm().Bcast (&dOptViewRatio, 1, MPI::DOUBLE, 0);
300 mpiWorld.getComm().Bcast (&dOptScanRatio, 1, MPI::DOUBLE, 0);
301 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
302 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
303 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
304 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
305 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
306 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
308 timerBcast.timerEndAndReport ("Time to broadcast variables");
310 if (mpiWorld.getRank() > 0 && optPhmName != "")
311 phm.createFromPhantom (optPhmName.c_str());
315 Scanner scanner (phm, optGeometryName.c_str(), opt_ndet, opt_nview, opt_nray, opt_rotangle, dOptFocalLength, dOptViewRatio, dOptScanRatio);
316 if (scanner.fail()) {
317 std::cout << "Scanner Creation Error: " << scanner.failMessage() << std::endl;
321 mpiWorld.setTotalWorkUnits (opt_nview);
323 Projections pjGlobal;
324 if (mpiWorld.getRank() == 0)
325 pjGlobal.initFromScanner (scanner);
328 std::ostringstream os;
329 pjGlobal.printScanInfo(os);
330 std::cout << os.str();
333 Projections pjLocal (scanner);
334 pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
337 std::cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")\n";;
339 TimerCollectiveMPI timerProject (mpiWorld.getComm());
340 scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits(), false, opt_trace);
342 timerProject.timerEndAndReport ("Time to collect projections");
344 TimerCollectiveMPI timerGather (mpiWorld.getComm());
345 GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
347 timerGather.timerEndAndReport ("Time to gather projections");
350 Projections pjGlobal (scanner);
351 scanner.collectProjections (pjGlobal, phm, opt_trace);
355 if (mpiWorld.getRank() == 0)
358 pjGlobal.setCalcTime (timerProgram.timerEnd());
359 pjGlobal.setRemark (opt_desc);
360 pjGlobal.write (opt_outfile);
362 phm.print (std::cout);
363 std::cout << std::endl;
364 std::ostringstream os;
365 pjGlobal.printScanInfo (os);
366 std::cout << os.str() << std::endl;
367 std::cout << " Remark: " << pjGlobal.remark() << std::endl;
368 std::cout << "Run time: " << pjGlobal.calcTime() << " seconds\n";
377 * GatherProjectionsMPI
380 * Gather's raysums from all processes in pjLocal in pjGlobal in process 0
384 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
386 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
387 DetectorArray& detArray = pjLocal.getDetectorArray(iw);
388 double viewAngle = detArray.viewAngle();
389 int nDet = detArray.nDet();
390 DetectorValue* detval = detArray.detValues();
392 mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
393 mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
394 mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
397 if (mpiWorld.getRank() == 0) {
398 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
399 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
403 DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
404 DetectorValue* detval = detArray.detValues();
406 mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
407 mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
408 mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
409 detArray.setViewAngle (viewAngle);
419 main (int argc, char* argv[])
424 retval = phm2pj_main(argc, argv);
426 // dmalloc_shutdown();
428 } catch (exception e) {
429 std::cerr << "Exception: " << e.what() << std::endl;
431 std::cerr << "Unknown exception\n";