c29cb8314b62dc9ba6f27bc5b2b2d927da003176
[ctsim.git] / tools / linogram.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          linogram.cpp
5 **   Purpose:       Display linogram sampling
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  April 2003
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: linogram.cpp,v 1.4 2003/04/04 20:04:42 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30 enum { O_XY, O_POLAR_RT, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
31
32 static struct option my_options[] =
33 {
34   {"xy", 0, 0, O_XY},
35   {"polar-rt", 0, 0, O_POLAR_RT},
36   {"debug", 0, 0, O_DEBUG},
37   {"verbose", 0, 0, O_VERBOSE},
38   {"help", 0, 0, O_HELP},
39   {"version", 0, 0, O_VERSION},
40   {0, 0, 0, 0}
41 };
42
43 static const char* g_szIdStr = "$Id: linogram.cpp,v 1.4 2003/04/04 20:04:42 kevin Exp $";
44
45
46 void 
47 linogram_usage (const char *program)
48 {
49   std::cout << "usage: " << fileBasename(program) << " n d [OPTIONS]\n";
50   std::cout << "Imagefile information\n";
51   std::cout << std::endl;
52   std::cout << "     n            Linogram N\n";
53   std::cout << "     d            Max detector spacing\n";
54   std::cout << "     --xy         Output x,y pairs\n";
55   std::cout << "     --polar-rt   Output r,t pairs\n";
56   std::cout << "     --debug      Debug mode\n";
57   std::cout << "     --verbose    Verbose mode\n";
58   std::cout << "     --version    Print version\n";
59   std::cout << "     --help       Print this help message\n";
60 }
61
62 int 
63 linogram_main (int argc, char *const argv[])
64 {
65   int opt_polar_rt = 0;
66   int opt_xy = 0;
67   int opt_verbose = 0;
68   int opt_debug = 0;
69
70   while (1)
71     {
72       int c = getopt_long (argc, argv, "", my_options, NULL);
73       
74       if (c == -1)
75         break;
76       
77       switch (c)
78         {
79         case O_XY:
80           opt_xy = 1;
81           break;
82         case O_POLAR_RT:
83           opt_polar_rt = 1;
84           break;
85         case O_VERBOSE:
86           opt_verbose = 1;
87           break;
88         case O_DEBUG:
89           opt_debug = 0;
90           break;
91         case O_VERSION:
92 #ifdef VERSION
93           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
94 #else
95           std::cout << "Unknown version number\n";
96 #endif
97           return (0);
98         case O_HELP:
99         case '?':
100           linogram_usage(argv[0]);
101           return (0);
102         default:
103           linogram_usage(argv[0]);
104           return (1);
105         }
106     }
107
108   if (optind + 2 != argc) {
109     linogram_usage (argv[0]);
110     return (1);
111   }
112   
113   const char* in_n = argv[optind];
114   const char* in_d = argv[optind+1];
115
116   int n = atol (in_n);
117   double d = atof (in_d);
118   int size = 4 * n + 3;
119   int max = 2 * n + 1;
120   int min = -max;
121   double theta_base = PI/4;
122   //  theta_base = 0;
123   
124   double theta_vec [size];
125   for (int i = 0; i < size; i++) {
126     int m = i - (2 * n + 1);
127     theta_vec[i] = atan (static_cast<double>(2 * m) / size);
128   }
129
130   if (opt_xy) {
131     int m;
132     for (m = 0; m < size; m++) {
133       double step = d * cos(theta_vec[m]);
134       for (int id = min; id <= max; id++) {
135         double r = id * step;
136         double x = r * cos(theta_vec[m] + theta_base);
137         double y = r * sin(theta_vec[m] + theta_base);
138         printf ("%lf,%lf ", x, y);
139       }
140       printf ("\n");
141     }
142     
143     for (m = 0; m < size; m++) {
144       double step = d * cos(theta_vec[m]);
145       for (int id = min; id <= max; id++) {
146         double r = id * step;
147         double x = r * cos(theta_vec[m] + PI/2 + theta_base);
148         double y = r * sin(theta_vec[m] + PI/2 + theta_base);
149         printf ("%lf,%lf ", x, y);
150       }
151       printf ("\n");
152     }
153   } else {
154     int m;
155     for (m = 0; m < size; m++) {
156       if (! opt_polar_rt)
157         printf ("%lf: ", theta_vec[m] + theta_base);
158       double step = d * cos(theta_vec[m]);
159       for (int id = min; id <= max; id++) {
160         if (opt_polar_rt)
161           printf ("%lf,", theta_vec[m] + theta_base);
162         printf ("%lf ", id * step);
163       }
164       printf ("\n");
165     }
166     
167     for (m = 0; m < size; m++) {
168       if (! opt_polar_rt)
169         printf ("%lf: ", theta_vec[m] + PI/2 + theta_base);
170       double step = d * cos(theta_vec[m]);
171       for (int id = min; id <= max; id++) {
172         if (opt_polar_rt)
173           printf ("%lf,", theta_vec[m] + PI/2 + theta_base);
174         printf ("%lf ", id * step);
175       }
176       printf ("\n");
177     }
178   }
179   
180   return (0);
181 }
182
183 #ifndef NO_MAIN
184 int 
185 main (int argc, char *const argv[])
186 {
187   int retval = 1;
188
189   try {
190     retval = linogram_main(argc, argv);
191   } catch (exception e) {
192           std::cerr << "Exception: " << e.what() << std::endl;
193   } catch (...) {
194           std::cerr << "Unknown exception\n";
195   }
196
197   return (retval);
198 }
199 #endif