45c45f1508d7a64906baf8de90e7ab12f0eb0071
[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-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 #include "ct.h"
27
28 enum { O_XY, O_POLAR_RT, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
29
30 static struct option my_options[] =
31 {
32   {"xy", 0, 0, O_XY},
33   {"polar-rt", 0, 0, O_POLAR_RT},
34   {"debug", 0, 0, O_DEBUG},
35   {"verbose", 0, 0, O_VERBOSE},
36   {"help", 0, 0, O_HELP},
37   {"version", 0, 0, O_VERSION},
38   {0, 0, 0, 0}
39 };
40
41 static const char* g_szIdStr = "$Id$";
42
43
44 void
45 linogram_usage (const char *program)
46 {
47   std::cout << "usage: " << fileBasename(program) << " n d [OPTIONS]\n";
48   std::cout << "Imagefile information\n";
49   std::cout << std::endl;
50   std::cout << "     n            Linogram N\n";
51   std::cout << "     d            Max detector spacing\n";
52   std::cout << "     --xy         Output x,y pairs\n";
53   std::cout << "     --polar-rt   Output r,t pairs\n";
54   std::cout << "     --debug      Debug mode\n";
55   std::cout << "     --verbose    Verbose mode\n";
56   std::cout << "     --version    Print version\n";
57   std::cout << "     --help       Print this help message\n";
58 }
59
60 int
61 linogram_main (int argc, char *const argv[])
62 {
63   int opt_polar_rt = 0;
64   int opt_xy = 0;
65   int opt_verbose = 0;
66   int opt_debug = 0;
67
68   while (1)
69     {
70       int c = getopt_long (argc, argv, "", my_options, NULL);
71
72       if (c == -1)
73         break;
74
75       switch (c)
76         {
77         case O_XY:
78           opt_xy = 1;
79           break;
80         case O_POLAR_RT:
81           opt_polar_rt = 1;
82           break;
83         case O_VERBOSE:
84           opt_verbose = 1;
85           break;
86         case O_DEBUG:
87           opt_debug = 0;
88           break;
89         case O_VERSION:
90 #ifdef VERSION
91           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
92 #else
93           std::cout << "Unknown version number\n";
94 #endif
95           return (0);
96         case O_HELP:
97         case '?':
98           linogram_usage(argv[0]);
99           return (0);
100         default:
101           linogram_usage(argv[0]);
102           return (1);
103         }
104     }
105
106   if (optind + 2 != argc) {
107     linogram_usage (argv[0]);
108     return (1);
109   }
110
111   const char* in_n = argv[optind];
112   const char* in_d = argv[optind+1];
113
114   int n = atol (in_n);
115   double d = atof (in_d);
116   int size = 4 * n + 3;
117   int max = 2 * n + 1;
118   int min = -max;
119   double theta_base = PI/4;
120   //  theta_base = 0;
121
122   double theta_vec [size];
123   for (int i = 0; i < size; i++) {
124     int m = i - (2 * n + 1);
125     theta_vec[i] = atan (static_cast<double>(2 * m) / size);
126   }
127
128   if (opt_xy) {
129     int m;
130     for (m = 0; m < size; m++) {
131       double step = d * cos(theta_vec[m]);
132       for (int id = min; id <= max; id++) {
133         double r = id * step;
134         double x = r * cos(theta_vec[m] + theta_base);
135         double y = r * sin(theta_vec[m] + theta_base);
136         printf ("%lf,%lf ", x, y);
137       }
138       printf ("\n");
139     }
140
141     for (m = 0; m < size; m++) {
142       double step = d * cos(theta_vec[m]);
143       for (int id = min; id <= max; id++) {
144         double r = id * step;
145         double x = r * cos(theta_vec[m] + PI/2 + theta_base);
146         double y = r * sin(theta_vec[m] + PI/2 + theta_base);
147         printf ("%lf,%lf ", x, y);
148       }
149       printf ("\n");
150     }
151   } else {
152     int m;
153     for (m = 0; m < size; m++) {
154       if (! opt_polar_rt)
155         printf ("%lf: ", theta_vec[m] + theta_base);
156       double step = d * cos(theta_vec[m]);
157       for (int id = min; id <= max; id++) {
158         if (opt_polar_rt)
159           printf ("%lf,", theta_vec[m] + theta_base);
160         printf ("%lf ", id * step);
161       }
162       printf ("\n");
163     }
164
165     for (m = 0; m < size; m++) {
166       if (! opt_polar_rt)
167         printf ("%lf: ", theta_vec[m] + PI/2 + theta_base);
168       double step = d * cos(theta_vec[m]);
169       for (int id = min; id <= max; id++) {
170         if (opt_polar_rt)
171           printf ("%lf,", theta_vec[m] + PI/2 + theta_base);
172         printf ("%lf ", id * step);
173       }
174       printf ("\n");
175     }
176   }
177
178   return (0);
179 }
180
181 #ifndef NO_MAIN
182 int
183 main (int argc, char *const argv[])
184 {
185   int retval = 1;
186
187   try {
188     retval = linogram_main(argc, argv);
189   } catch (exception e) {
190           std::cerr << "Exception: " << e.what() << std::endl;
191   } catch (...) {
192           std::cerr << "Unknown exception\n";
193   }
194
195   return (retval);
196 }
197 #endif