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.17 2001/01/02 05:34:57 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.17 2001/01/02 05:34:57 kevin Exp $";
56 phm2if_usage (const char *program)
58 std::cout << "phm2if_usage: " << fileBasename(program) << " outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n";
59 std::cout << "Generate phantom image from a predefined --phantom or a --phmfile\n";
60 std::cout << std::endl;
61 std::cout << " outfile Name of output file for image\n";
62 std::cout << " nx Number of pixels X-axis\n";
63 std::cout << " ny Number of pixels Y-axis\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 Generate Phantom from phantom file\n";
71 std::cout << " --filter Generate Phantom from a filter function\n";
72 std::cout << " abs_bandlimit Abs * Bandlimiting\n";
73 std::cout << " abs_sinc Abs * Sinc\n";
74 std::cout << " abs_cos Abs * Cosine\n";
75 std::cout << " abs_hamming Abs * Hamming\n";
76 std::cout << " shepp Shepp-Logan\n";
77 std::cout << " bandlimit Bandlimiting\n";
78 std::cout << " sinc Sinc\n";
79 std::cout << " cos Cosine\n";
80 std::cout << " triangle Triangle\n";
81 std::cout << " hamming Hamming\n";
82 std::cout << " --filter-param Alpha level for Hamming filter\n";
83 std::cout << " --filter-domain Set domain of filter\n";
84 std::cout << " spatial Spatial domain (default)\n";
85 std::cout << " freq Frequency domain\n";
86 std::cout << " --filter-bw Filter bandwidth (default = 1)\n";
87 std::cout << " --desc Description of raysum\n";
88 std::cout << " --nsample Number of samples per axis per pixel (default = 1)\n";
89 std::cout << " --trace Trace level to use\n";
90 std::cout << " none No tracing (default)\n";
91 std::cout << " console Trace text level\n";
92 std::cout << " phantom Trace phantom\n";
93 std::cout << " proj Trace projections\n";
94 std::cout << " plot Trace plot\n";
95 std::cout << " clipping Trace clipping\n";
96 std::cout << " --debug Debug mode\n";
97 std::cout << " --verbose Verbose mode\n";
98 std::cout << " --version Print version\n";
99 std::cout << " --help Print this help message\n";
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 std::string optPhmName;
112 std::string optFilterName;
113 std::string optDomainName (SignalFilter::convertDomainIDToName (SignalFilter::DOMAIN_SPATIAL));
114 std::string optOutFilename;
116 std::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 std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
198 std::cerr << "Unknown version number\n";
202 phm2if_usage(argv[0]);
205 phm2if_usage(argv[0]);
210 if (optPhmName == "" && optFilterName == "" && optPhmFilename == "") {
211 std::cerr << "No phantom defined\n" << std::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]);
236 std::ostringstream oss;
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 std::cout << phm.failMessage() << std::endl << std::endl;
253 phm2if_usage(argv[0]);
258 if (optPhmFilename != "") {
259 phm.createFromFile(optPhmFilename.c_str());
261 if (mpiWorld.getRank() == 0)
262 std::cerr << "Can't use phantom from file in MPI mode\n";
268 std::cout << "Rasterize Phantom to Image\n" << std::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);
304 ImageFileArray v = NULL;
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(), false);
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 std::cout << "Time to rasterized phantom: " << calctime << " seconds\n";
353 if (optTrace >= Trace::TRACE_PHANTOM) {
357 std::cout << "Enter display size scale (nominal = 1): ";
359 std::cout ("Enter minimum and maximum densities (min, max): ";
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 std::cerr << "Exception: " << e.what() << std::endl;
412 std::cerr << "Unknown exception\n";