riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Public Member Functions | Public Attributes | List of all members
Ricom_kernel Class Reference

#include <Ricom.h>

Public Member Functions

void compute_kernel ()
 
void compute_filter ()
 
void include_filter ()
 
std::vector< int > fftshift_map (int x, int y)
 
 Ricom_kernel ()
 
void approximate_frequencies (size_t n_im)
 
void draw_surfaces ()
 
 ~Ricom_kernel ()
 

Public Attributes

int kernel_size
 
bool b_filter
 
std::array< int, 2 > kernel_filter_frequency
 
int k_width_sym
 
int k_area
 
float rotation
 
std::vector< float > kernel_x
 
std::vector< float > kernel_y
 
std::vector< float > kernel_filter
 
std::vector< float > f_approx
 
SDL_Surface * srf_kx
 
SDL_Surface * srf_ky
 

Detailed Description

Definition at line 52 of file Ricom.h.

Constructor & Destructor Documentation

◆ Ricom_kernel()

Ricom_kernel::Ricom_kernel ( )
inline

Definition at line 74 of file Ricom.h.

74  : kernel_size(5),
75  b_filter(false),
77  k_width_sym(0),
78  k_area(0),
79  rotation(0.0),
80  kernel_x(),
81  kernel_y(),
82  kernel_filter(),
83  f_approx(),
84  srf_kx(), srf_ky()
85  {
87  };
int k_area
Definition: Ricom.h:60
std::vector< float > kernel_x
Definition: Ricom.h:62
SDL_Surface * srf_kx
Definition: Ricom.h:66
void compute_kernel()
Definition: Ricom.cpp:21
std::vector< float > kernel_y
Definition: Ricom.h:63
int k_width_sym
Definition: Ricom.h:59
bool b_filter
Definition: Ricom.h:57
std::vector< float > f_approx
Definition: Ricom.h:65
float rotation
Definition: Ricom.h:61
SDL_Surface * srf_ky
Definition: Ricom.h:67
std::array< int, 2 > kernel_filter_frequency
Definition: Ricom.h:58
std::vector< float > kernel_filter
Definition: Ricom.h:64
int kernel_size
Definition: Ricom.h:56

◆ ~Ricom_kernel()

Ricom_kernel::~Ricom_kernel ( )
inline

Definition at line 91 of file Ricom.h.

91 {};

Member Function Documentation

◆ approximate_frequencies()

void Ricom_kernel::approximate_frequencies ( size_t  n_im)

Definition at line 111 of file Ricom.cpp.

112 {
113  f_approx.resize(nx_im);
114  float f_max = 0;
115  float k = kernel_size * 2;
116  for (size_t i = 0; i < nx_im; i++)
117  {
118  float x = 2 * i * M_PI;
119  f_approx[i] = (nx_im / x) * (1 - cos(k / 2 * (x / nx_im)));
120  if (f_approx[i] > f_max)
121  {
122  f_max = f_approx[i];
123  }
124  }
125  std::for_each(f_approx.begin(), f_approx.end(), [f_max](float &x) { x/=f_max; });
126 }

◆ compute_filter()

void Ricom_kernel::compute_filter ( )

Definition at line 66 of file Ricom.cpp.

67 {
68  kernel_filter.assign(k_area, 0);
69  float lb = pow(kernel_filter_frequency[0], 2);
70  float ub = pow(kernel_filter_frequency[1], 2);
71 
72  for (int iy = 0; iy < k_width_sym; iy++)
73  {
74  for (int ix = 0; ix < k_width_sym; ix++)
75  {
76  float dist = pow(ix - kernel_size, 2) + pow(iy - kernel_size, 2);
77  int ic = iy * k_width_sym + ix;
78  if (dist <= ub && dist > lb)
79  {
80  kernel_filter[ic] = 1;
81  }
82  }
83  }
84 }

◆ compute_kernel()

void Ricom_kernel::compute_kernel ( )

Definition at line 21 of file Ricom.cpp.

22 {
23  float rot_rad = M_PI * rotation / 180;
24  float cos_rot = cos(rot_rad);
25  float sin_rot = sin(rot_rad);
26 
27  k_width_sym = kernel_size * 2 + 1;
29  kernel_x.assign(k_area, 0);
30  kernel_y.assign(k_area, 0);
31 
32  for (int iy = 0; iy < k_width_sym; iy++)
33  {
34  int iy_e = (iy + 1) * k_width_sym - 1;
35  for (int ix = 0; ix < k_width_sym; ix++)
36  {
37  int ix_s = ix - kernel_size;
38  int iy_s = iy - kernel_size;
39  float d = ix_s * ix_s + iy_s * iy_s;
40  int ix_e = k_area - iy_e + ix - 1;
41 
42  if (d > 0)
43  {
44  float ix_sd = (ix_s / d);
45  float iy_sd = (iy_s / d);
46  kernel_x[ix_e] = cos_rot * ix_sd - sin_rot * iy_sd;
47  kernel_y[ix_e] = sin_rot * ix_sd + cos_rot * iy_sd;
48  }
49  else
50  {
51  kernel_y[ix_e] = 0;
52  kernel_x[ix_e] = 0;
53  }
54  }
55  }
56 
57  // Add filter
58  if (b_filter)
59  {
62  }
63 }
void include_filter()
Definition: Ricom.cpp:87
void compute_filter()
Definition: Ricom.cpp:66

◆ draw_surfaces()

void Ricom_kernel::draw_surfaces ( )

Definition at line 128 of file Ricom.cpp.

129 {
130  // Creating SDL Surface (holding the ricom image in CPU memory)
131  srf_kx = SDL_CreateRGBSurface(0, k_width_sym, k_width_sym, 32, 0, 0, 0, 0);
132  if (srf_kx == NULL)
133  {
134  std::cout << "Surface could not be created! SDL Error: " << SDL_GetError() << std::endl;
135  }
136  srf_ky = SDL_CreateRGBSurface(0, k_width_sym, k_width_sym, 32, 0, 0, 0, 0);
137  if (srf_ky == NULL)
138  {
139  std::cout << "Surface could not be created! SDL Error: " << SDL_GetError() << std::endl;
140  }
141  // determine location index of value in memory
142  std::pair kx_min_max = std::minmax_element(kernel_x.begin(), kernel_x.end());
143  std::pair ky_min_max = std::minmax_element(kernel_y.begin(), kernel_y.end());
144  // Update pixel at location
145 
146  for (int y = 0; y < k_width_sym; y++)
147  {
148  int ic = y * k_width_sym;
149  for (int x = 0; x < k_width_sym; x++)
150  {
151  float valx = (kernel_x[ic + x] - kx_min_max.first[0]) / (kx_min_max.second[0] - kx_min_max.first[0]);
152  SDL_Utils::draw_pixel(srf_kx, x, y, valx, 5);
153  float valy = (kernel_y[ic + x] - ky_min_max.first[0]) / (ky_min_max.second[0] - ky_min_max.first[0]);
154  SDL_Utils::draw_pixel(srf_ky, x, y, valy, 5);
155  }
156  }
157 }
void draw_pixel(SDL_Surface *surface, int x, int y, float val, int col_map)
Definition: GuiUtils.cpp:97

◆ fftshift_map()

std::vector<int> Ricom_kernel::fftshift_map ( int  x,
int  y 
)

◆ include_filter()

void Ricom_kernel::include_filter ( )

Definition at line 87 of file Ricom.cpp.

88 {
89  FFT2D fft2d(k_width_sym, k_width_sym);
90  std::vector<std::complex<float>> x2c = FFT2D::r2c(kernel_x);
91  std::vector<std::complex<float>> y2c = FFT2D::r2c(kernel_y);
92  fft2d.fft(x2c, x2c);
93  fft2d.fft(y2c, y2c);
94  for (int id = 0; id < k_area; id++)
95  {
96  if (kernel_filter[id] == 0.0f)
97  {
98  x2c[id] = {0, 0};
99  y2c[id] = {0, 0};
100  }
101  }
102  fft2d.ifft(x2c, x2c);
103  fft2d.ifft(y2c, y2c);
104  for (int id = 0; id < k_area; id++)
105  {
106  kernel_x[id] = x2c[id].real();
107  kernel_y[id] = y2c[id].real();
108  }
109 }

Member Data Documentation

◆ b_filter

bool Ricom_kernel::b_filter

Definition at line 57 of file Ricom.h.

◆ f_approx

std::vector<float> Ricom_kernel::f_approx

Definition at line 65 of file Ricom.h.

◆ k_area

int Ricom_kernel::k_area

Definition at line 60 of file Ricom.h.

◆ k_width_sym

int Ricom_kernel::k_width_sym

Definition at line 59 of file Ricom.h.

◆ kernel_filter

std::vector<float> Ricom_kernel::kernel_filter

Definition at line 64 of file Ricom.h.

◆ kernel_filter_frequency

std::array<int, 2> Ricom_kernel::kernel_filter_frequency

Definition at line 58 of file Ricom.h.

◆ kernel_size

int Ricom_kernel::kernel_size

Definition at line 56 of file Ricom.h.

◆ kernel_x

std::vector<float> Ricom_kernel::kernel_x

Definition at line 62 of file Ricom.h.

◆ kernel_y

std::vector<float> Ricom_kernel::kernel_y

Definition at line 63 of file Ricom.h.

◆ rotation

float Ricom_kernel::rotation

Definition at line 61 of file Ricom.h.

◆ srf_kx

SDL_Surface* Ricom_kernel::srf_kx

Definition at line 66 of file Ricom.h.

◆ srf_ky

SDL_Surface* Ricom_kernel::srf_ky

Definition at line 67 of file Ricom.h.


The documentation for this class was generated from the following files: