1 /*****************************************************************************
5 ** Purpose: Convert an phantom object to an image file
6 ** Programmer: Kevin Rosenberg
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: phm2if.cpp,v 1.5 2000/08/02 18:06:00 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_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP,
33 O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
35 static struct option my_options[] =
37 {"phantom", 1, 0, O_PHANTOM},
38 {"phmfile", 1, 0, O_PHMFILE},
39 {"desc", 1, 0, O_DESC},
40 {"nsample", 1, 0, O_NSAMPLE},
41 {"filter", 1, 0, O_FILTER},
42 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
43 {"filter-bw", 1, 0, O_FILTER_BW},
44 {"filter-param", 1, 0, O_FILTER_PARAM},
45 {"trace", 1, 0, O_TRACE},
46 {"verbose", 0, 0, O_VERBOSE},
47 {"debug", 0, 0, O_DEBUG},
48 {"help", 0, 0, O_HELP},
49 {"version", 0, 0, O_VERSION},
53 static const char* szIdStr = "$Id";
56 phm2if_usage (const char *program)
58 cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]" << endl;
59 cout << "Generate phantom image from a predefined --phantom or a --phmfile" << endl;
61 cout << " outfile Name of output file for image" << endl;
62 cout << " nx Number of pixels X-axis" << endl;
63 cout << " ny Number of pixels Y-axis" << endl;
64 cout << " --phantom Phantom to use for projection" << endl;
65 cout << " herman Herman head phantom" << endl;
66 cout << " bherman Bordered Herman head phantom" << endl;
67 cout << " rowland Rowland head phantom" << endl;
68 cout << " browland Bordered Rowland head phantom" << endl;
69 cout << " unitpulse Unit pulse phantom" << endl;
70 cout << " --phmfile Generate Phantom from phantom file" << endl;
71 cout << " --filter Generate Phantom from a filter function" << endl;
72 cout << " abs_bandlimit Abs * Bandlimiting" << endl;
73 cout << " abs_sinc Abs * Sinc" << endl;
74 cout << " abs_cos Abs * Cosine" << endl;
75 cout << " abs_hamming Abs * Hamming" << endl;
76 cout << " shepp Shepp-Logan" << endl;
77 cout << " bandlimit Bandlimiting" << endl;
78 cout << " sinc Sinc" << endl;
79 cout << " cos Cosine" << endl;
80 cout << " triangle Triangle" << endl;
81 cout << " hamming Hamming" << endl;
82 cout << " --filter-param Alpha level for Hamming filter" << endl;
83 cout << " --filter-domain Set domain of filter" << endl;
84 cout << " spatial Spatial domain (default)" << endl;
85 cout << " freq Frequency domain" << endl;
86 cout << " --filter-bw Filter bandwidth (default = 1)" << endl;
87 cout << " --desc Description of raysum" << endl;
88 cout << " --nsample Number of samples per axis per pixel (default = 1)" << endl;
89 cout << " --trace Trace level to use" << endl;
90 cout << " none No tracing (default)" << endl;
91 cout << " text Trace text level" << endl;
92 cout << " phm Trace phantom" << endl;
93 cout << " rays Trace rays" << endl;
94 cout << " plot Trace plot" << endl;
95 cout << " clipping Trace clipping" << endl;
96 cout << " --debug Debug mode" << endl;
97 cout << " --verbose Verbose mode" << endl;
98 cout << " --version Print version" << endl;
99 cout << " --help Print this help message" << endl;
103 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug);
107 phm2if_main (int argc, char* argv[])
109 ImageFile* pImGlobal = NULL;
111 string optPhmName (Phantom::convertPhantomIDToName(Phantom::PHM_HERMAN));
112 string optFilterName;
113 string optDomainName (SignalFilter::convertDomainIDToName (SignalFilter::DOMAIN_SPATIAL));
114 string optOutFilename;
116 string optPhmFilename;
120 double optFilterParam = 1.;
121 double optFilterBW = 1.;
122 int optTrace = TRACE_NONE;
123 bool optVerbose = false;
124 bool optDebug = false;
128 ImageFile* pImLocal = NULL;
129 MPIWorld mpiWorld (argc, argv);
130 if (mpiWorld.getRank() == 0) {
133 int c = getopt_long(argc, argv, "", my_options, NULL);
145 optPhmFilename = optarg;
154 if ((optTrace = TraceLevel::convertTraceNameToID(optarg)) == TRACE_INVALID) {
155 phm2if_usage(argv[0]);
160 optFilterName = optarg;
162 case O_FILTER_DOMAIN:
163 optDomainName = optarg;
169 optFilterBW = strtod(optarg, &endptr);
170 endstr = optarg + strlen(optarg);
171 if (endptr != endstr) {
172 sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
173 phm2if_usage(argv[0]);
178 optFilterParam = strtod(optarg, &endptr);
179 endstr = optarg + strlen(optarg);
180 if (endptr != endstr) {
181 sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
182 phm2if_usage(argv[0]);
187 opt_nsample = strtol(optarg, &endptr, 10);
188 endstr = optarg + strlen(optarg);
189 if (endptr != endstr) {
190 sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
191 phm2if_usage(argv[0]);
197 cout << "Version " << VERSION << endl;
199 cerr << "Unknown version number" << endl;
203 phm2if_usage(argv[0]);
206 phm2if_usage(argv[0]);
211 if (optPhmName == "" && optFilterName == "" && optPhmFilename == "") {
212 cerr << "No phantom defined" << endl << endl;
213 phm2if_usage(argv[0]);
217 if (optind + 3 != argc) {
218 phm2if_usage(argv[0]);
221 optOutFilename = argv[optind];
222 opt_nx = strtol(argv[optind+1], &endptr, 10);
223 endstr = argv[optind+1] + strlen(argv[optind+1]);
224 if (endptr != endstr) {
225 sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
226 phm2if_usage(argv[0]);
229 opt_ny = strtol(argv[optind+2], &endptr, 10);
230 endstr = argv[optind+2] + strlen(argv[optind+2]);
231 if (endptr != endstr) {
232 sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
233 phm2if_usage(argv[0]);
238 oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
239 if (optPhmFilename != "")
240 oss << "phantomFile=" << optPhmFilename;
241 else if (optPhmName != "")
242 oss << "phantom=" << optPhmName;
243 else if (optFilterName != "") {
244 oss << "filter=" << optFilterName << " - " << optDomainName;
247 oss << ": " << optDesc;
250 if (optPhmName != "") {
251 phm.createFromPhantom (optPhmName.c_str());
253 cout << phm.failMessage() << endl << endl;
254 phm2if_usage(argv[0]);
259 if (optPhmFilename != "") {
260 phm.createFromFile(optPhmFilename.c_str());
262 if (mpiWorld.getRank() == 0)
263 cerr << "Can't use phantom from file in MPI mode" << endl;
269 cout << "Rasterize Phantom to Image" << endl << endl;
275 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
276 mpiWorld.BcastString (optPhmName);
277 mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
278 mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
283 mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
284 mpiWorld.getComm().Bcast (&optFilterBW, 1, MPI::DOUBLE, 0);
286 mpiWorld.BcastString (optFilterName);
287 mpiWorld.BcastString (optDomainName);
290 timerBcast.timerEndAndReport ("Time to broadcast variables");
292 mpiWorld.setTotalWorkUnits (opt_nx);
294 if (mpiWorld.getRank() > 0 && optPhmName != "")
295 phm.createFromPhantom (optPhmName.c_str());
297 if (mpiWorld.getRank() == 0) {
298 pImGlobal = new ImageFile (opt_nx, opt_ny);
300 pImLocal = new ImageFile (opt_nx, opt_ny);
302 pImGlobal = new ImageFile (opt_nx, opt_ny);
307 if (mpiWorld.getRank() == 0)
308 v = pImGlobal->getArray ();
310 if (phm.getComposition() == P_UNIT_PULSE) {
311 if (mpiWorld.getRank() == 0) {
312 v[opt_nx/2][opt_ny/2] = 1.;
314 } else if (optFilterName != "") {
315 if (mpiWorld.getRank() == 0) {
316 pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
319 TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
320 phm.convertToImagefile (*pImLocal, opt_nsample, optTrace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits());
322 timerRasterize.timerEndAndReport ("Time to rasterize phantom");
324 TimerCollectiveMPI timerGather (mpiWorld.getComm());
325 mpi_gather_image (mpiWorld, pImGlobal, pImLocal, optDebug);
327 timerGather.timerEndAndReport ("Time to gather image");
330 v = pImGlobal->getArray ();
331 if (phm.getComposition() == P_UNIT_PULSE) {
332 v[opt_nx/2][opt_ny/2] = 1.;
333 } else if (optFilterName != "") {
334 pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
337 if (optTrace >= TRACE_PHM)
340 phm.convertToImagefile (*pImGlobal, opt_nsample, optTrace);
345 if (mpiWorld.getRank() == 0)
348 double calctime = timerProgram.timerEnd ();
349 pImGlobal->labelAdd (Array2dFileLabel::L_HISTORY, optDesc.c_str(), calctime);
350 pImGlobal->fileWrite (optOutFilename.c_str());
352 cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
354 if (optTrace >= TRACE_PHM) {
358 printf ("Enter display size scale (nominal = 1): ");
359 scanf ("%d", &nscale);
360 printf ("Enter minimum and maximum densities (min, max): ");
361 scanf ("%lf %lf", &dmin, &dmax);
362 pImGlobal->displayScaling (nscale, dmin, dmax);
377 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug)
379 ImageFileArray vLocal = pImLocal->getArray();
380 ImageFileArray vGlobal = NULL;
381 int nyLocal = pImLocal->ny();
383 if (mpiWorld.getRank() == 0)
384 vGlobal = pImGlobal->getArray();
386 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
387 mpiWorld.getComm().Send(vLocal[iw], nyLocal, pImLocal->getMPIDataType(), 0, 0);
389 if (mpiWorld.getRank() == 0) {
390 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
391 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
393 mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, pImLocal->getMPIDataType(), iProc, 0, status);
403 main (int argc, char* argv[])
408 retval = phm2if_main(argc, argv);
409 } catch (exception e) {
410 cerr << "Exception: " << e.what() << endl;
412 cerr << "Unknown exception" << endl;