riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Ricom.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #ifndef RICOM_H
15 #define RICOM_H
16 
17 #ifdef _WIN32
18 #include <io.h>
19 #pragma warning(disable : 4005 4333 34)
20 #else
21 #include <unistd.h>
22 #endif
23 
24 #define _USE_MATH_DEFINES
25 #include <cmath>
26 
27 #include <stdio.h>
28 #include <complex>
29 #include <cfloat>
30 #include <vector>
31 #include <string>
32 #include <SDL.h>
33 #include <mutex>
34 #include <future>
35 #include <thread>
36 #include <fftw3.h>
37 #include <chrono>
38 #include <algorithm>
39 #include <ctime>
40 
41 #include "BoundedThreadPool.hpp"
42 #include "tinycolormap.hpp"
43 #include "fft2d.hpp"
44 #include "SocketConnector.h"
45 #include "ProgressMonitor.h"
46 #include "Cheetah.hpp"
47 #include "Timepix.hpp"
48 #include "Advapix.hpp"
49 #include "GuiUtils.h"
50 
51 namespace chc = std::chrono;
52 
54 {
55 public:
56  // Properties
57  uint16_t nx_cam;
58  uint16_t ny_cam;
60  bool b_filter;
61  std::array<int, 2> kernel_filter_frequency;
63  int k_area;
64  float rotation;
65  std::vector<float> kernel_x;
66  std::vector<float> kernel_y;
67  std::vector<float> kernel_filter;
68  std::vector<float> f_approx;
69  SDL_Surface *srf_kx;
70  SDL_Surface *srf_ky;
71  // Methods
72  void compute_kernel();
73  void compute_filter();
74  void include_filter();
75  std::vector<int> fftshift_map(int x, int y);
76  // Constructor
78  b_filter(false),
80  k_width_sym(0),
81  k_area(0),
82  rotation(0.0),
83  kernel_x(),
84  kernel_y(),
85  kernel_filter(),
86  f_approx(),
87  srf_kx(), srf_ky()
88  {
90  };
91  void approximate_frequencies(size_t n_im);
92  void draw_surfaces();
93  // Destructor
95 };
96 
98 // Helper class for ricom data indexing //
100 class id_x_y
101 {
102 public:
103  int id;
104  bool valid;
105  id_x_y() : id(0), valid(false){};
106  id_x_y(int id, bool valid);
107 };
108 
110 {
111 private:
112  // Properties
113  Ricom_kernel kernel;
114  int nx;
115  int ny;
116 
117 public:
118  // Properties
119  std::vector<id_x_y> ids;
120  // Methods
121  void init(Ricom_kernel kernel, int nx_ricom, int ny_ricom);
122  inline void shift(id_x_y &id_sft, int id, int shift);
123  // Constructor
124  Update_list() : kernel(), nx(0), ny(0){};
125 };
126 
128 {
129 public:
130  // Properties
131  std::array<float, 2> radius;
132  std::array<float, 2> radius2;
133  std::vector<int> id_list;
134 
135  // Methods
136  void compute_detector(int nx_cam, int ny_cam, std::array<float, 2> &offset);
137  // Constructor
138  Ricom_detector() : radius{0, 0}, radius2{0, 0}, id_list(){};
139  // Destructor
141 };
142 
143 namespace RICOM
144 {
145  enum cameras
146  {
147  // MERLIN,
149  CHEETAH
150  };
151 }
152 
153 class Ricom
154 {
155 private:
156  // vSTEM Variables
157  float stem_max;
158  float stem_min;
159 
160  // ricom variables
161  std::vector<int> u;
162  std::vector<int> v;
163 
164  Update_list update_list;
165 
166  // Electric field magnitude
167  float e_mag_max;
168  float e_mag_min;
169 
170  // Variables for potting in the SDL2 frame
171  float ricom_max;
172  float ricom_min;
173  std::vector<float> cbed_log;
174 
175  // Thread Synchronization Variables
176  std::mutex ricom_mutex;
177  std::mutex stem_mutex;
178  std::mutex counter_mutex;
179  std::mutex e_field_mutex;
180 
181  // Private Methods - General
182  void init_surface();
183  template <typename T>
184  inline void update_surfaces(int iy, std::vector<T> &frame);
185  void reinit_vectors_limits();
186  void reset_limits();
187  void reset_file();
188  void calculate_update_list();
189  inline void rescales_recomputes();
190  // template <typename T, class CameraInterface>
191  // inline void skip_frames(int n_skip, std::vector<T> &data, CAMERA::Camera<CameraInterface, CAMERA::FRAME_BASED> *camera_fr);
192  template <typename T>
193  inline void swap_endianess(T &val);
194 
195  void line_processor(
196  size_t &img_num,
197  size_t &first_frame,
198  size_t &end_frame,
199  ProgressMonitor *prog_mon,
200  size_t &fr_total_u,
201  BoundedThreadPool *pool
202  );
203 
204  void icom_group_decompose(int idxx);
205  void icom_group_classical(int idxx);
206 
207  // Private Methods - riCOM
208  inline void set_ricom_pixel(int idx, int idy);
209 
210  // Private Methods - vSTEM
211  template <typename T>
212  inline void stem(std::vector<T> *data, size_t id_stem);
213  inline void set_stem_pixel(size_t idx, size_t idy);
214 
215  // Private Methods electric field
216  inline void compute_electric_field(std::array<float, 2> &p_com_xy, size_t id);
217  inline void set_e_field_pixel(size_t idx, size_t idy);
218 
219 public:
221  std::string file_path;
224  std::atomic<int> last_y;
226  bool b_busy;
228  bool b_continuous=false;
229  bool b_cumulative=false;
231  std::array<std::atomic<size_t>, 3> frame_id_plot_cbed = {0, 1, 0};
232 
233  bool b_vSTEM;
234  bool b_e_mag;
235  bool b_ricom;
236  bool b_airpi;
237 
243  std::array<float, 2> offset;
244  std::array<float, 2> com_public;
245  std::vector<float> comx_image;
246  std::vector<float> comy_image;
247  std::vector<float> ricom_image;
248  std::vector<float> stem_image;
249  std::vector<float> airpi_image;
250  std::vector<size_t> dose_data[2];
251  std::vector<size_t> sumx_data[2];
252  std::vector<size_t> sumy_data[2];
253  std::vector<size_t> stem_data[2];
254  std::vector<size_t> frame;
255  std::vector<std::complex<float>> e_field_data;
256 
257  // settings
258  int mode; // 0:file, 1:tcp
260 
261  // Scan Variables
262  int nx;
263  int ny;
264  int nxy;
265  int n_cam;
266  int dt;
267  int rep;
268  int fr_total;
269  int skip_row;
270  int skip_img;
273  int id_image;
274 
275  // Variables for progress and performance
279  float fr_freq; // Frequncy per frame
280  float fr_count; // Count all Frames processed in an image
281  float fr_count_total; // Count all Frames in a scanning session
282  std::atomic<bool> rescale_ricom;
283  std::atomic<bool> rescale_stem;
284  std::atomic<bool> rescale_e_mag;
285  bool rc_quit;
286 
287  SDL_Surface *srf_ricom; // Surface for the ricom window;
289  SDL_Surface *srf_stem; // Surface for the vSTEM window;
291  SDL_Surface *srf_cbed; // Surface for the CBED window;
293  SDL_Surface *srf_e_mag; // Surface for the E-Field window;
295 
296  // Public Methods
297  void draw_ricom_image();
298  void draw_ricom_image(int y0, int ye);
299  void draw_stem_image();
300  void draw_stem_image(int y0, int ye);
301  void draw_e_field_image();
302  void draw_e_field_image(int y0, int ye);
303  // template <class CameraInterface>
304  void run(int mode);
305  void reset();
306  void process_data();
307 
308  // Constructor
309  Ricom();
310 
311  // Destructor
312  ~Ricom();
313 };
314 
315 #endif // __RICOM_H__
std::array< float, 2 > radius2
Definition: Ricom.h:132
~Ricom_detector()
Definition: Ricom.h:140
std::array< float, 2 > radius
Definition: Ricom.h:131
std::vector< int > id_list
Definition: Ricom.h:133
Ricom_detector()
Definition: Ricom.h:138
void compute_detector(int nx_cam, int ny_cam, std::array< float, 2 > &offset)
Definition: Ricom.cpp:160
~Ricom_kernel()
Definition: Ricom.h:94
int k_area
Definition: Ricom.h:63
Ricom_kernel()
Definition: Ricom.h:77
void include_filter()
Definition: Ricom.cpp:87
std::vector< float > kernel_x
Definition: Ricom.h:65
uint16_t ny_cam
Definition: Ricom.h:58
SDL_Surface * srf_kx
Definition: Ricom.h:69
void compute_kernel()
Definition: Ricom.cpp:21
std::vector< float > kernel_y
Definition: Ricom.h:66
int k_width_sym
Definition: Ricom.h:62
void draw_surfaces()
Definition: Ricom.cpp:129
bool b_filter
Definition: Ricom.h:60
std::vector< float > f_approx
Definition: Ricom.h:68
void approximate_frequencies(size_t n_im)
Definition: Ricom.cpp:111
float rotation
Definition: Ricom.h:64
SDL_Surface * srf_ky
Definition: Ricom.h:70
uint16_t nx_cam
Definition: Ricom.h:57
std::array< int, 2 > kernel_filter_frequency
Definition: Ricom.h:61
std::vector< float > kernel_filter
Definition: Ricom.h:67
void compute_filter()
Definition: Ricom.cpp:66
std::vector< int > fftshift_map(int x, int y)
int kernel_size
Definition: Ricom.h:59
Definition: Ricom.h:154
std::vector< float > stem_image
Definition: Ricom.h:248
std::vector< size_t > frame
Definition: Ricom.h:254
std::vector< float > comy_image
Definition: Ricom.h:246
int skip_img
Definition: Ricom.h:270
void run(int mode)
Definition: Ricom.cpp:661
std::vector< size_t > sumy_data[2]
Definition: Ricom.h:252
std::array< float, 2 > com_public
Definition: Ricom.h:244
void process_data()
Definition: Ricom.cpp:471
SocketConnector socket
Definition: Ricom.h:220
int nxy
Definition: Ricom.h:264
int redraw_interval
Definition: Ricom.h:223
bool b_busy
Definition: Ricom.h:226
int mode
Definition: Ricom.h:258
int cbed_cmap
Definition: Ricom.h:292
bool b_recompute_detector
Definition: Ricom.h:239
int e_mag_cmap
Definition: Ricom.h:294
int n_threads
Definition: Ricom.h:276
int processor_line
Definition: Ricom.h:271
void draw_stem_image()
Definition: Ricom.cpp:313
void draw_e_field_image()
Definition: Ricom.cpp:357
int rep
Definition: Ricom.h:267
bool update_offset
Definition: Ricom.h:227
bool b_plot2SDL
Definition: Ricom.h:238
bool b_airpi
Definition: Ricom.h:236
SDL_Surface * srf_e_mag
Definition: Ricom.h:293
int id_image
Definition: Ricom.h:273
std::atomic< bool > rescale_ricom
Definition: Ricom.h:282
float fr_count_total
Definition: Ricom.h:281
int n_threads_max
Definition: Ricom.h:277
std::vector< float > airpi_image
Definition: Ricom.h:249
float fr_count
Definition: Ricom.h:280
std::vector< float > comx_image
Definition: Ricom.h:245
std::vector< std::complex< float > > e_field_data
Definition: Ricom.h:255
std::string file_path
Definition: Ricom.h:221
int preprocessor_line
Definition: Ricom.h:272
void draw_ricom_image()
Definition: Ricom.cpp:266
int nx
Definition: Ricom.h:262
RICOM::cameras camera
Definition: Ricom.h:259
Ricom_detector detector
Definition: Ricom.h:241
bool rc_quit
Definition: Ricom.h:285
int queue_size
Definition: Ricom.h:278
std::vector< size_t > sumx_data[2]
Definition: Ricom.h:251
bool b_e_mag
Definition: Ricom.h:234
std::vector< size_t > stem_data[2]
Definition: Ricom.h:253
SDL_Surface * srf_ricom
Definition: Ricom.h:287
SDL_Surface * srf_cbed
Definition: Ricom.h:291
int fr_total
Definition: Ricom.h:268
ProgressMonitor * p_prog_mon
Definition: Ricom.h:225
int ricom_cmap
Definition: Ricom.h:288
void reset()
Definition: Ricom.cpp:767
bool b_plot_cbed
Definition: Ricom.h:230
float fr_freq
Definition: Ricom.h:279
SDL_Surface * srf_stem
Definition: Ricom.h:289
Ricom()
Definition: Ricom.cpp:222
bool b_recompute_kernel
Definition: Ricom.h:240
std::vector< size_t > dose_data[2]
Definition: Ricom.h:250
std::array< std::atomic< size_t >, 3 > frame_id_plot_cbed
Definition: Ricom.h:231
std::vector< float > ricom_image
Definition: Ricom.h:247
bool b_continuous
Definition: Ricom.h:228
std::atomic< int > last_y
Definition: Ricom.h:224
bool b_cumulative
Definition: Ricom.h:229
int skip_row
Definition: Ricom.h:269
int n_cam
Definition: Ricom.h:265
std::atomic< bool > rescale_e_mag
Definition: Ricom.h:284
bool b_ricom
Definition: Ricom.h:235
bool b_vSTEM
Definition: Ricom.h:233
int ny
Definition: Ricom.h:263
int dt
Definition: Ricom.h:266
~Ricom()
Definition: Ricom.cpp:263
bool b_print2file
Definition: Ricom.h:222
std::array< float, 2 > offset
Definition: Ricom.h:243
std::atomic< bool > rescale_stem
Definition: Ricom.h:283
int stem_cmap
Definition: Ricom.h:290
Ricom_kernel kernel
Definition: Ricom.h:242
void init(Ricom_kernel kernel, int nx_ricom, int ny_ricom)
std::vector< id_x_y > ids
Definition: Ricom.h:119
Update_list()
Definition: Ricom.h:124
void shift(id_x_y &id_sft, int id, int shift)
Definition: Ricom.h:101
int id
Definition: Ricom.h:103
id_x_y(int id, bool valid)
bool valid
Definition: Ricom.h:104
id_x_y()
Definition: Ricom.h:105
Definition: Ricom.h:144
cameras
Definition: Ricom.h:146
@ ADVAPIX
Definition: Ricom.h:148
@ CHEETAH
Definition: Ricom.h:149