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: phm2rs.cpp,v 1.6 2000/06/15 19:07:10 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,
33 O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
35 static struct option phm2rs_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 {"trace", 1, 0, O_TRACE},
43 {"verbose", 0, 0, O_VERBOSE},
44 {"help", 0, 0, O_HELP},
45 {"debug", 0, 0, O_DEBUG},
46 {"version", 0, 0, O_VERSION},
52 phm2rs_usage (const char *program)
54 cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]" << endl;
55 cout << "Calculate raysums (projections) through phantom object, either" << endl;
56 cout << "a predefined --phantom or a --phmfile" << endl;
58 cout << " outfile Name of output file for raysums" << endl;
59 cout << " ndet Number of detectors" << endl;
60 cout << " nview Number of rotated views" << endl;
61 cout << " --phantom Phantom to use for projection" << endl;
62 cout << " herman Herman head phantom" << endl;
63 cout << " rowland Rowland head phantom" << endl;
64 cout << " browland Bordered Rowland head phantom" << endl;
65 cout << " unitpulse Unit pulse phantom" << endl;
66 cout << " --phmfile Get Phantom from phantom file" << endl;
67 cout << " --desc Description of raysum" << endl;
68 cout << " --nray Number of rays per detector (default = 1)" << endl;
69 cout << " --rotangle Degrees to rotate view through, multiple of PI (default = 1)" << endl;
70 cout << " --trace Trace level to use" << endl;
71 cout << " none No tracing (default)" << endl;
72 cout << " text Trace text level" << endl;
73 cout << " phm Trace phantom image" << endl;
74 cout << " rays Trace rays" << endl;
75 cout << " plot Trace plot" << endl;
76 cout << " clipping Trace clipping" << endl;
77 cout << " --verbose Verbose mode" << endl;
78 cout << " --debug Debug mode" << endl;
79 cout << " --version Print version" << endl;
80 cout << " --help Print this help message" << endl;
84 void GatherProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug);
88 phm2rs_main (int argc, char* argv[])
92 RAYSUM *rsGlobal = NULL;
93 char *opt_outfile = NULL;
95 string opt_phmfilename;
96 char *endptr, *endstr;
97 int opt_ndet, opt_nview;
103 double opt_rotangle = 1;
107 MPIWorld mpiWorld (argc, argv);
113 if (mpiWorld.getRank() == 0) {
116 int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
125 if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
126 phm2rs_usage(argv[0]);
129 phm.create (opt_phmnum);
133 if (mpiWorld.getRank() == 0)
134 cerr << "Can not read phantom from file in MPI mode" << endl;
137 opt_phmfilename = optarg;
138 phm.createFromFile (opt_phmfilename.c_str());
148 if ((opt_trace = opt_set_trace(optarg)) < 0) {
149 phm2rs_usage(argv[0]);
157 opt_rotangle = strtod(optarg, &endptr);
158 endstr = optarg + strlen(optarg);
159 if (endptr != endstr) {
160 cerr << "Error setting --rotangle to " << optarg << endl;
161 phm2rs_usage(argv[0]);
166 opt_nray = strtol(optarg, &endptr, 10);
167 endstr = optarg + strlen(optarg);
168 if (endptr != endstr) {
169 cerr << "Error setting --nray to %s" << optarg << endl;
170 phm2rs_usage(argv[0]);
176 cout << "Version: " << VERSION << endl;
178 cout << "Unknown version number" << endl;
183 phm2rs_usage(argv[0]);
186 phm2rs_usage(argv[0]);
191 if (phm.nPElem() == 0) {
192 cerr << "No phantom defined" << endl;
193 phm2rs_usage(argv[0]);
196 if (optind + 3 != argc) {
197 phm2rs_usage(argv[0]);
201 opt_outfile = argv[optind];
202 opt_ndet = strtol(argv[optind+1], &endptr, 10);
203 endstr = argv[optind+1] + strlen(argv[optind+1]);
204 if (endptr != endstr) {
205 cerr << "Error setting --ndet to " << argv[optind+1] << endl;
206 phm2rs_usage(argv[0]);
209 opt_nview = strtol(argv[optind+2], &endptr, 10);
210 endstr = argv[optind+2] + strlen(argv[optind+2]);
211 if (endptr != endstr) {
212 cerr << "Error setting --nview to " << argv[optind+2] << endl;
213 phm2rs_usage(argv[0]);
218 desc << "Raysum_Collect: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", ";
219 if (opt_phmfilename.length()) {
220 desc << "PhantomFile=" << opt_phmfilename;
221 } else if (opt_phmnum != -1) {
222 desc << "Phantom=" << name_of_phantom(opt_phmnum);
224 if (opt_desc.length()) {
225 desc << ": " << opt_desc;
227 opt_desc = desc.str();
233 mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
234 mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
235 mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
236 mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
237 mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0);
238 mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
239 mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
240 mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
242 if (mpiWorld.getRank() > 0 && opt_phmnum >= 0)
243 phm.create (opt_phmnum);
247 det = new Detector (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
250 mpiWorld.setTotalWorkUnits (opt_nview);
252 if (mpiWorld.getRank() == 0) {
253 rsGlobal = raysum_create_from_det (opt_outfile, *det);
254 raysum_alloc_views(rsGlobal);
257 rsLocal = raysum_create_from_det (NULL, *det);
258 rsLocal->nview = mpiWorld.getMyLocalWorkUnits();
260 cout << "rsLocal->nview = " << rsLocal->nview << " (process " << mpiWorld.getRank() << ")" << endl;;
262 TimerMPI timerProject (mpiWorld.getComm());
263 raysum_collect (rsLocal, *det, phm, mpiWorld.getMyStartWorkUnit(), opt_trace, FALSE);
265 timerProject.timerEndAndReport ("Time to collect projections");
267 TimerMPI timerGather (mpiWorld.getComm());
268 GatherProjectionsMPI (mpiWorld, rsGlobal, rsLocal, opt_debug);
270 timerGather.timerEndAndReport ("Time to gather projections");
272 rsGlobal = raysum_create_from_det (opt_outfile, *det);
273 raysum_collect (rsGlobal, *det, phm, 0, opt_trace, FALSE);
277 if (mpiWorld.getRank() == 0)
280 rsGlobal->calctime = timerProgram.timerEnd();
281 rsGlobal->remark = opt_desc;
282 raysum_write (rsGlobal);
283 raysum_close (rsGlobal);
285 cout << "Remark: " << rsGlobal->remark << endl;
286 cout << "Run time: " << rsGlobal->calctime << " seconds" << endl;
297 * GatherProjectionsMPI
300 * Gather's raysums from all processes in rsLocal in rsGlobal in process 0
304 void GatherProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug)
306 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
307 mpiWorld.getComm().Send(&rsLocal->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0);
308 mpiWorld.getComm().Send(&rsLocal->view[iw]->ndet, 1, MPI::INT, 0, 0);
309 mpiWorld.getComm().Send(rsLocal->view[iw]->detval, rsLocal->ndet, MPI::FLOAT, 0, 0);
312 if (mpiWorld.getRank() == 0) {
313 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
314 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
317 mpiWorld.getComm().Recv(&rsGlobal->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0, status);
318 mpiWorld.getComm().Recv(&rsGlobal->view[iw]->ndet, 1, MPI::INT, iProc, 0, status);
319 mpiWorld.getComm().Recv(rsGlobal->view[iw]->detval, rsGlobal->ndet, MPI::FLOAT, iProc, 0, status);
330 main (int argc, char* argv[])
332 return (phm2rs_main(argc, argv));