Bsoft  2.1.0
Bernard's software package
proj_tool.cpp
Go to the documentation of this file.
1 
11 #include "proj_tool.h"
12 #include "radon_util.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
24 LUTable *
25 create_table (int ncol, int sizeT)
26 {
27  int i;
28 
29  LUTable *lut = (LUTable *) malloc (sizeof (LUTable));
30  lut->ncol = ncol;
31  lut->sizeT = sizeT;
32 
33  int size_tab_1 = (ncol * sizeT + 1) * (sizeT + 1);
34  int size_tab_2 = size_tab_1 * ncol / 2;
35 
36  int **tab1;
37  float **tab2;
38 
39  if ((tab1 = (int **) calloc (size_tab_1, sizeof (int *))) == 0)
40  {
41  perror ("create_table: tab1 error calloc");
42  exit (0);
43  }
44 
45  for (i = 0; i < size_tab_1; i++)
46  if ((tab1[i] = (int *) calloc (2, sizeof (int))) == 0)
47  {
48  perror ("create_table: tab1 error calloc");
49  exit (0);
50  }
51 
52  if ((tab2 = (float **) calloc (size_tab_2, sizeof (float *))) == 0)
53  {
54  perror ("create_table: tab2 error calloc");
55  exit (0);
56  }
57 
58  for (i = 0; i < size_tab_2; i++)
59  if ((tab2[i] = (float *) calloc (3, sizeof (float))) == 0)
60  {
61  perror ("create_table: tab1 error calloc");
62  exit (0);
63  }
64 
65  lut->tab1 = tab1;
66  lut->tab2 = tab2;
67 
68  double fat, a, b, a1, b1, a2, b2, d;
69  double alpha, beta, ang, arg;
70  float w2, weight, stepb;
71  int n, n1, n2, n_theta, n_theta_2, n_theta_4, step;
72 
73  w2 = 9.2023;
74 
75  n_theta = 2 * ncol;
76  n_theta_2 = n_theta / 2;
77  n_theta_4 = n_theta / 4;
78  step = sizeT;
79  fat = 2. * M_PI / n_theta;
80 // delta = 1. / fat;
81 // soglia = fat * 180. / M_PI;
82 
83  n1 = n2 = 0;
84  stepb = 1. / (float) step;
85  for (beta = -(float) n_theta_4; beta <= (float) n_theta_4 + .001; beta += stepb)
86  {
87  for (alpha = 0; alpha <= 1.001; alpha += stepb)
88  {
89  b1 = (int) floor (beta);
90  b2 = b1 + 1;
91  n = 0;
92  for (b = b1; b <= b2; b += 1)
93  {
94  if (b != 0 && b != n_theta_2)
95  {
96  d = 1.0 / sin (b * fat);
97  d = fabs (d);
98  a1 = (int) floor (alpha - d);
99  a2 = (int) floor (alpha + d) + 1;
100  }
101  else
102  {
103  a1 = 0;
104  a2 = n_theta - 1;
105  }
106  for (a = a1; a <= a2; a += 1.0)
107  {
108  ang = ang_one_two (alpha * fat, beta * fat, a * fat, b * fat);
109  if (ang < fat - 0.001)
110  {
111  arg = ang * n_theta / 2.;
112  if (arg >= 0.001)
113  weight = (sin (arg) / arg) * exp (-(arg * arg) / w2);
114  else
115  weight = 1;
116  ang *= 180. / M_PI;
117  tab2[n2][0] = a;
118  tab2[n2][1] = b + n_theta_4;
119  tab2[n2][2] = weight;
120  n2++;
121  n++;
122  }
123  }
124  }
125  tab1[n1][0] = n;
126  tab1[n1][1] = n2;
127  n1++;
128  }
129  }
130 
131  if (verbose & VERB_FULL)
132  cout << "LUTable sizes: " << n1 << " (" << size_tab_1 <<
133  ") " << n2 << " (" << size_tab_2 << ")" << endl;
134 
135  return (lut);
136 }
137 
138 int
140 {
141  int i;
142  int size_tab_1 = (lut->ncol * lut->sizeT + 1) * (lut->sizeT + 1);
143  int size_tab_2 = size_tab_1 * lut->ncol / 2;
144 
145  int **tab1 = lut->tab1;
146  float **tab2 = lut->tab2;
147 
148  for (i = 0; i < size_tab_1; i++)
149  free (tab1[i]);
150 
151  free (tab1);
152 
153  for (i = 0; i < size_tab_2; i++)
154  free (tab2[i]);
155 
156  free (tab2);
157 
158  free (lut);
159 
160  return 0;
161 }
162 
163 
176 int
177 write_line (Bimage * p, Bimage * prec, int angolo, float csi, float eta, Bimage * pmask, LUTable * lut, float *priga)
178 {
179  int i, j, k, l, memo, memo0;
180  float *pg, *pg1, *pg2, *pv1, *pr1;
181  int eta_in_pix, csi_in_pix;
182  int place, nlines, ind_eta, ind_csi, ind;
183  float eta0, eta_in_fl, csi_in_fl, weight;
184 
185  int sizeT = lut->sizeT;
186  int **ptab1 = lut->tab1;
187  float **ptab2 = lut->tab2;
188 
189  float *pr = (float *) p->data_pointer();
190  float *pv = (float *) prec->data_pointer();
191  float *pw1 = (float *) pmask->data_pointer();
192 
193  pg = pr;
194  pr1 = priga;
195 
196  pg1 = pg + angolo;
197  for (l = 0; l < p->sizeY(); l++)
198  {
199  pg2 = pg1 + l * p->sizeX();
200  pr1[l] = *pg2;
201  }
202 
203  memo0 = 0;
204  if (csi < 0)
205  {
206  csi = -csi;
207  eta -= M_PI;
208  memo0 = 1;
209  }
210  while (eta < 0)
211  eta += 2. * M_PI;
212 
213  eta_in_fl = eta * (float) p->sizeX() / M_PI;
214  csi_in_fl = csi * (float) p->sizeX() / M_PI;
215  eta0 = floor (eta_in_fl);
216 
217  ind_eta = (int) rint ((eta_in_fl - eta0) * (float) sizeT);
218  ind_csi = (int) rint (csi_in_fl * (float) sizeT);
219  ind = ind_eta + ind_csi * (sizeT + 1);
220 
221  nlines = ptab1[ind][0];
222  place = ptab1[ind][1];
223 
224  for (j = 0; j < nlines; j++)
225  {
226  k = place + j;
227  eta_in_pix = (int) ptab2[k][0] + (int) eta0;
228  csi_in_pix = (int) ptab2[k][1];
229  weight = ptab2[k][2];
230 
231  memo = memo0;
232  while (eta_in_pix < 0)
233  eta_in_pix += 2 * p->sizeX();
234  if (csi_in_pix < 0)
235  {
236  csi_in_pix *= -1;
237  eta_in_pix += p->sizeX();
238  memo += 1;
239  }
240  if (eta_in_pix >= 2 * p->sizeX())
241  eta_in_pix -= 2 * p->sizeX();
242  if (eta_in_pix >= p->sizeX() && csi_in_pix != 0)
243  {
244  eta_in_pix -= p->sizeX();
245  csi_in_pix = (p->sizeX() - csi_in_pix) % p->sizeX();
246  }
247  if (eta_in_pix >= p->sizeX() && csi_in_pix == 0)
248  {
249  eta_in_pix -= p->sizeX();
250  csi_in_pix = (p->sizeX() - csi_in_pix) % p->sizeX();
251  memo += 1;
252  }
253 
254  memo = memo % 2;
255  pw1[eta_in_pix * p->sizeX() + csi_in_pix] += weight;
256 
257  pv1 = pv + eta_in_pix * p->sizeX() + csi_in_pix;
258  pv1[0] += pr1[0] * weight;
259  pv1 += p->sizeX() * p->sizeY();
260  if (memo == 0)
261  for (i = 1; i < p->sizeY(); i++)
262  {
263  *pv1 += pr1[i] * weight;
264  pv1 += p->sizeX() * p->sizeY();
265  }
266  else
267  for (i = p->sizeY() - 1; i > 0; i--)
268  {
269  *pv1 += pr1[i] * weight;
270  pv1 += p->sizeX() * p->sizeY();
271  }
272  }
273  return 0;
274 }
275 
288 int
290 {
291  float *pw1, *pv1, *pv2, *pv3;
292  int j, k;
293  float soglia, val, min;
294 
295  float *pr = (float *) p->data_pointer();
296  float *pw = (float *) pmask->data_pointer();
297 
298  min = 1000;
299  soglia = 0.90; // Threshold to accept a voxel
300  pw1 = pw;
301  for (j = 0; j < p->sizeX() * p->sizeY(); j++)
302  { // Finds minimum and sets values below threshold to zero
303  val = *pw1;
304  if (val < min)
305  min = val;
306  if (val > soglia)
307  *pw1 = 1. / val;
308  else
309  *pw1 = 0.;
310  pw1++;
311  }
312 
313  if (verbose & VERB_PROCESS)
314  {
315  cout << "Minimum weight: " << min << endl;
316  if (min > .01)
317  cout << "Radon transform filled" << endl;
318  else
319  cout << "Radon transform unfilled" << endl;
320  }
321 
322  for (k = 0; k < p->sizeZ(); k++) // Weighs each z-plane with mask
323  {
324  pv1 = pr + p->sizeX() * p->sizeY() * k;
325  pv2 = pv1 + p->sizeX() * p->sizeY();
326  for (pv3 = pv1, pw1 = pw; pv3 < pv2; pv3++, pw1++)
327  *pv3 *= *pw1;
328  }
329 
330  for (j = 0, pw1 = pw; j < p->sizeX() * p->sizeY(); j++, pw1++) // Sets the mask values to zero and one
331  if (*pw1)
332  *pw1 = 1;
333 
334  return 0;
335 }
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
LUTable::ncol
int ncol
Definition: proj_tool.h:15
LUTable::sizeT
int sizeT
Definition: proj_tool.h:15
LUTable
Definition: proj_tool.h:14
proj_tool.h
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
weigh_radon_transf
int weigh_radon_transf(Bimage *p, Bimage *pmask)
Weighs a radon transform with a mask.
Definition: proj_tool.cpp:289
create_table
LUTable * create_table(int ncol, int sizeT)
Definition: proj_tool.cpp:25
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
ang_one_two
double ang_one_two(double a1, double b1, double a2, double b2)
Calculates the angle (in radians) from 2 vectors.
Definition: radon_util.cpp:89
Bimage
General image parameter class.
Definition: Bimage.h:177
LUTable::tab1
int ** tab1
Definition: proj_tool.h:16
write_line
int write_line(Bimage *p, Bimage *prec, int angolo, float csi, float eta, Bimage *pmask, LUTable *lut, float *priga)
Definition: proj_tool.cpp:177
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
LUTable::tab2
float ** tab2
Definition: proj_tool.h:17
beta
double beta(double volt)
Calculates the relative electron velocity.
Definition: ctf.cpp:500
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
radon_util.h
verbose
int verbose
kill_table
int kill_table(LUTable *lut)
Definition: proj_tool.cpp:139