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.11 2000/08/27 20:32:55 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* g_szIdStr = "$Id: phm2if.cpp,v 1.11 2000/08/27 20:32:55 kevin Exp $";
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 << " herman-b Herman head phantom (Bordered)" << endl;
67 cout << " shepp-logan Shepp-Logan head phantom" << endl;
68 cout << " shepp-logan-b Shepp-Logan head phantom (Bordered)" << 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 << " console Trace text level" << endl;
92 cout << " phantom Trace phantom" << endl;
93 cout << " proj Trace projections" << 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::TRACE_NONE;
123 bool optVerbose = false;
124 bool optDebug = false;
130 ImageFile* pImLocal = NULL;
131 MPIWorld mpiWorld (argc, argv);
132 if (mpiWorld.getRank() == 0) {
135 int c = getopt_long(argc, argv, "", my_options, NULL);
144 optPhmFilename = optarg;
153 if ((optTrace = Trace::convertTraceNameToID(optarg)) == Trace::TRACE_INVALID) {
154 phm2if_usage(argv[0]);
159 optFilterName = optarg;
161 case O_FILTER_DOMAIN:
162 optDomainName = optarg;
168 optFilterBW = strtod(optarg, &endptr);
169 endstr = optarg + strlen(optarg);
170 if (endptr != endstr) {
171 sys_error(ERR_SEVERE,"Error setting --filter-bw to %s\n", optarg);
172 phm2if_usage(argv[0]);
177 optFilterParam = strtod(optarg, &endptr);
178 endstr = optarg + strlen(optarg);
179 if (endptr != endstr) {
180 sys_error(ERR_SEVERE,"Error setting --filter-param to %s\n", optarg);
181 phm2if_usage(argv[0]);
186 opt_nsample = strtol(optarg, &endptr, 10);
187 endstr = optarg + strlen(optarg);
188 if (endptr != endstr) {
189 sys_error(ERR_SEVERE,"Error setting --nsample to %s\n", optarg);
190 phm2if_usage(argv[0]);
196 cout << "Version " << VERSION << endl << g_szIdStr << endl;
198 cerr << "Unknown version number" << endl;
202 phm2if_usage(argv[0]);
205 phm2if_usage(argv[0]);
210 if (optPhmName == "" && optFilterName == "" && optPhmFilename == "") {
211 cerr << "No phantom defined" << endl << endl;
212 phm2if_usage(argv[0]);
216 if (optind + 3 != argc) {
217 phm2if_usage(argv[0]);
220 optOutFilename = argv[optind];
221 opt_nx = strtol(argv[optind+1], &endptr, 10);
222 endstr = argv[optind+1] + strlen(argv[optind+1]);
223 if (endptr != endstr) {
224 sys_error(ERR_SEVERE,"Error setting nx to %s\n", argv[optind+1]);
225 phm2if_usage(argv[0]);
228 opt_ny = strtol(argv[optind+2], &endptr, 10);
229 endstr = argv[optind+2] + strlen(argv[optind+2]);
230 if (endptr != endstr) {
231 sys_error(ERR_SEVERE,"Error setting ny to %s\n", argv[optind+2]);
232 phm2if_usage(argv[0]);
237 oss << "phm2if: nx=" << opt_nx << ", ny=" << opt_ny << ", nsample=" << opt_nsample << ", ";
238 if (optPhmFilename != "")
239 oss << "phantomFile=" << optPhmFilename;
240 else if (optPhmName != "")
241 oss << "phantom=" << optPhmName;
242 else if (optFilterName != "") {
243 oss << "filter=" << optFilterName << " - " << optDomainName;
246 oss << ": " << optDesc;
249 if (optPhmName != "") {
250 phm.createFromPhantom (optPhmName.c_str());
252 cout << phm.failMessage() << endl << endl;
253 phm2if_usage(argv[0]);
258 if (optPhmFilename != "") {
259 phm.createFromFile(optPhmFilename.c_str());
261 if (mpiWorld.getRank() == 0)
262 cerr << "Can't use phantom from file in MPI mode" << endl;
268 cout << "Rasterize Phantom to Image" << endl << endl;
274 TimerCollectiveMPI timerBcast (mpiWorld.getComm());
275 mpiWorld.BcastString (optPhmName);
276 mpiWorld.getComm().Bcast (&optVerbose, 1, MPI::INT, 0);
277 mpiWorld.getComm().Bcast (&optDebug, 1, MPI::INT, 0);
278 mpiWorld.getComm().Bcast (&optTrace, 1, MPI::INT, 0);
279 mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
280 mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
281 mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
282 mpiWorld.getComm().Bcast (&optFilterParam, 1, MPI::DOUBLE, 0);
283 mpiWorld.getComm().Bcast (&optFilterBW, 1, MPI::DOUBLE, 0);
285 mpiWorld.BcastString (optFilterName);
286 mpiWorld.BcastString (optDomainName);
289 timerBcast.timerEndAndReport ("Time to broadcast variables");
291 mpiWorld.setTotalWorkUnits (opt_nx);
293 if (mpiWorld.getRank() > 0 && optPhmName != "")
294 phm.createFromPhantom (optPhmName.c_str());
296 if (mpiWorld.getRank() == 0) {
297 pImGlobal = new ImageFile (opt_nx, opt_ny);
299 pImLocal = new ImageFile (opt_nx, opt_ny);
301 pImGlobal = new ImageFile (opt_nx, opt_ny);
306 if (mpiWorld.getRank() == 0)
307 v = pImGlobal->getArray ();
309 if (phm.getComposition() == P_UNIT_PULSE) {
310 if (mpiWorld.getRank() == 0) {
311 v[opt_nx/2][opt_ny/2] = 1.;
313 } else if (optFilterName != "") {
314 if (mpiWorld.getRank() == 0) {
315 pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
318 TimerCollectiveMPI timerRasterize (mpiWorld.getComm());
319 phm.convertToImagefile (*pImLocal, opt_nsample, optTrace, mpiWorld.getMyStartWorkUnit(), mpiWorld.getMyLocalWorkUnits());
321 timerRasterize.timerEndAndReport ("Time to rasterize phantom");
323 TimerCollectiveMPI timerGather (mpiWorld.getComm());
324 mpi_gather_image (mpiWorld, pImGlobal, pImLocal, optDebug);
326 timerGather.timerEndAndReport ("Time to gather image");
329 v = pImGlobal->getArray ();
330 if (phm.getComposition() == P_UNIT_PULSE) {
331 v[opt_nx/2][opt_ny/2] = 1.;
332 } else if (optFilterName != "") {
333 pImGlobal->filterResponse (optDomainName.c_str(), optFilterBW, optFilterName.c_str(), optFilterParam);
336 if (optTrace >= Trace::TRACE_PHANTOM)
339 phm.convertToImagefile (*pImGlobal, opt_nsample, optTrace);
344 if (mpiWorld.getRank() == 0)
347 double calctime = timerProgram.timerEnd ();
348 pImGlobal->labelAdd (Array2dFileLabel::L_HISTORY, optDesc.c_str(), calctime);
349 pImGlobal->fileWrite (optOutFilename.c_str());
351 cout << "Time to rasterized phantom: " << calctime << " seconds" << endl;
353 if (optTrace >= Trace::TRACE_PHANTOM) {
357 printf ("Enter display size scale (nominal = 1): ");
358 scanf ("%d", &nscale);
359 printf ("Enter minimum and maximum densities (min, max): ");
360 scanf ("%lf %lf", &dmin, &dmax);
361 pImGlobal->displayScaling (nscale, dmin, dmax);
376 void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* pImGlobal, ImageFile* pImLocal, const int optDebug)
378 ImageFileArray vLocal = pImLocal->getArray();
379 ImageFileArray vGlobal = NULL;
380 int nyLocal = pImLocal->ny();
382 if (mpiWorld.getRank() == 0)
383 vGlobal = pImGlobal->getArray();
385 for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
386 mpiWorld.getComm().Send(vLocal[iw], nyLocal, pImLocal->getMPIDataType(), 0, 0);
388 if (mpiWorld.getRank() == 0) {
389 for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
390 for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
392 mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, pImLocal->getMPIDataType(), iProc, 0, status);
402 main (int argc, char* argv[])
407 retval = phm2if_main(argc, argv);
408 } catch (exception e) {
409 cerr << "Exception: " << e.what() << endl;
411 cerr << "Unknown exception" << endl;