| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kernel.cpp
Go to the documentation of this file.
1
9 #include "kernel.h"
10 #include "trigonometry.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
22 RSI_Kernel::RSI_Kernel(int kernel_width, int kernel_power)
23 {
24 int i, j, ik, width_2;
25 double s1, s2, c, arg, arg1, arg2, iwidth, piwidth, incr;
26
27 power = kernel_power;
28
29 if ( power < 0 ) width = 1;
30 else width = kernel_width;
31
32 half_width = width_2 = (width - 1) / 2;
33 ndiv = 512;
34 size = (ndiv + 1) * width;
35 data = new float[size];
36
37 for ( i = 0; i < size; i++ ) data[i] = 0;
38
39 if ( power < 0 ) {
40 for ( i = 0; i < size; i++ ) data[i] = 1;
41 } else {
42 iwidth = 1.0L / width;
43 piwidth = M_PI * iwidth;
44 incr = 1.0L / ndiv;
45 arg = incr;
46 data[width_2] = 1;
47 for ( i = 1, ik = width; i < ndiv; i++ ) {
48 arg1 = M_PI * (arg + width_2);
49 s1 = iwidth * bsin (arg1);
50 arg2 = arg1 * iwidth;
51 for ( j = 0; j < width; j++, ik++ ) {
52 s2 = bsin (arg2);
53 c = bcos (arg2);
54 data[ik] = (s1 / s2) * pow (c, power);
55 arg2 -= piwidth;
56 s1 = -s1;
57 }
58 arg += incr;
59 }
60 ik += width_2 + 1;
61 data[ik] = 1;
62 }
63
64 if ( verbose & VERB_PROCESS ) {
65 cout << "Kernel parameters:" << endl;
66 cout << "Width: " << width << endl;
67 cout << "Half width: " << half_width << endl;
68 cout << "Power: " << power << endl;
69 cout << "Divisions: " << ndiv << endl;
70 cout << "Data size: " << size << endl << endl;
71 }
72
73 if ( verbose & VERB_FULL ) {
74 cout << "Kernel content:" << endl;
75 for ( i=ik=0; i<=ndiv; i++ ) {
76 cout << i;
77 for ( j=0; j<width; j++, ik++ ) {
78 cout << tab << data[ik];
79 }
80 cout << endl;
81 }
82 cout << endl;
83 }
84 }
85
96 Complex<double> RSI_Kernel::interpolate(Bimage* p, unsigned long img_num, Vector3<float> m)
97 {
98 Complex<double> value(0,0);
99
100 if ( p->compound_type() != TComplex ) return value;
101
102 long x, y, z, kx, ky, kz, mx, my, mz, jx, jy, jz, nx, ny, nz;
103 long ix = (long) floor(m.x());
104 long iy = (long) floor(m.y());
105 long iz = (long) floor(m.z());
106 long ikx = half_width;
107 long iky = half_width;
108 long ikz = half_width;
109 double wyz;
110
111 nx = ny = nz = width;
112
113 if ( p->sizeX() > 1 ) {
114 ikx = ((long) ((m.x() - ix)*ndiv + 0.5))*width;
115 ix -= half_width;
116 } else nx = 1;
117
118 if ( p->sizeY() > 1 ) {
119 iky = ((long) ((m.y() - iy)*ndiv + 0.5))*width;
120 iy -= half_width;
121 } else ny = 1;
122
123 if ( p->sizeZ() > 1 ) {
124 ikz = ((long) ((m.z() - iz)*ndiv + 0.5))*width;
125 iz -= half_width;
126 } else nz = 1;
127
128 for ( z=0, kz=ikz, mz=iz; z<nz; z++, kz++, mz++ ) {
129 jz = mz;
130 while ( jz < 0 ) jz += p->sizeZ();
131 while ( jz >= p->sizeZ() ) jz -= p->sizeZ();
132 jz = (img_num*p->sizeZ() + jz)*p->sizeY();
133 for ( y=0, ky=iky, my=iy; y<ny; y++, ky++, my++ ) {
134 jy = my;
135 while ( jy < 0 ) jy += p->sizeY();
136 while ( jy >= p->sizeY() ) jy -= p->sizeY();
137 jy = (jz + jy)*p->sizeX();
138 wyz = data[ky]*data[kz];
139 for ( x=0, kx=ikx, mx=ix; x<nx; x++, kx++, mx++ ) {
140 jx = mx;
141 while ( jx < 0 ) jx += p->sizeX();
142 while ( jx >= p->sizeX() ) jx -= p->sizeX();
143 value += p->complex(jy + jx) * (data[kx]*wyz);
144 }
145 }
146 }
147
148 if ( !value.is_finite() )
149 cerr << "Warning: Interpolated complex value not finite! (" << value << ")" << endl;
150
151 return value;
152 }
153
Vector3::z
Type z()
Definition: Vector3.h:141
Bimage::sizeZ
void sizeZ(size_t nz)
Definition: Bimage.h:311
RSI_Kernel::RSI_Kernel
RSI_Kernel()
Definition: kernel.h:23
bcos
double bcos(double x)
Avoid non-zero values for cos(PI/2) and cos(-PI/2).
Definition: trigonometry.cpp:27
utilities.h
Header file for general utilities.
kernel.h
Header file for filter kernel functions.
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:286
Vector3< float >
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:100
TComplex
Definition: Bimage.h:51
verbose
int verbose
bsin
double bsin(double x)
Avoid non-zero values for sin(PI) and sin(-PI).
Definition: trigonometry.cpp:48
Bimage
General image parameter class.
Definition: Bimage.h:132
M_PI
#define M_PI
Definition: utilities.h:52
RSI_Kernel::interpolate
Complex< double > interpolate(Bimage *p, unsigned long img_num, Vector3< float > m)
Calculates the complex value at an image location by kernel-based interpolation.
Definition: kernel.cpp:96
Bimage::complex
Complex< double > complex(size_t j)
Returns a complex value at the given index.
Definition: Bimage.cpp:862
Vector3::x
Type x()
Definition: Vector3.h:139
trigonometry.h
Header file for trigonometry functions.
Complex::is_finite
bool is_finite()
Definition: Complex.h:100
Complex
Definition: Complex.h:23
Bimage::sizeX
void sizeX(size_t nx)
Definition: Bimage.h:309
Bimage::sizeY
void sizeY(size_t ny)
Definition: Bimage.h:310
VERB_FULL
#define VERB_FULL
Definition: utilities.h:102
Vector3::y
Type y()
Definition: Vector3.h:140
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21