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
ImGuiImageWindow< T > Class Template Reference

#include <ImGuiImageWindow.h>

Public Member Functions

 ImGuiImageWindow (const std::string &title, GLuint *tex_id, bool auto_render, int data_cmap, GIM_Flags flags=GIM_Flags::None, bool *visible=nullptr)
 
void set_data (int width, int height, std::vector< T > *data)
 
void render_window (bool b_redraw, int last_y, int render_update_offset, bool b_trigger_update)
 
void render_window (bool b_redraw, int last_y, bool b_trigger_update)
 
void reset_min_max ()
 
void set_nx_ny (int width, int height)
 

Public Attributes

bool * pb_open
 
ImGuiImageWindow< float > * fft_window
 
bool b_trigger_update
 

Detailed Description

template<class T>
class ImGuiImageWindow< T >

Definition at line 47 of file ImGuiImageWindow.h.

Constructor & Destructor Documentation

◆ ImGuiImageWindow()

template<typename T >
ImGuiImageWindow< T >::ImGuiImageWindow ( const std::string &  title,
GLuint *  tex_id,
bool  auto_render,
int  data_cmap,
GIM_Flags  flags = GIM_Flags::None,
bool *  visible = nullptr 
)

Definition at line 184 of file ImGuiImageWindow.cpp.

185 {
186 
187  this->title = title;
188  this->flags = flags;
189  this->tex_id = tex_id;
190  this->pb_open = visible;
191  this->auto_render = auto_render;
192  this->data_cmap = data_cmap;
193  this->last_y = 0;
194  this->last_idr = 0;
195  this->last_img = 0;
196  this->zoom = 1.0f;
197  this->power = 1.0f;
198  this->ny = 1;
199  this->nx = 1;
200  this->nxy = 1;
201  this->render_update_offset = 0;
202  this->b_trigger_update = false;
203  saveFileDialog = ImGui::FileBrowser(ImGuiFileBrowserFlags_EnterNewFilename | ImGuiFileBrowserFlags_CreateNewDir);
204  saveFileDialog.SetTitle("Save " + title + " image as .png");
205  saveDataDialog = ImGui::FileBrowser(ImGuiFileBrowserFlags_EnterNewFilename | ImGuiFileBrowserFlags_CreateNewDir);
206  saveDataDialog.SetTitle("Save " + title + "-data as numpy array (.npy)");
207 
208  uv_min = ImVec2(0.0f, 0.0f); // Top-left
209  uv_max = ImVec2(1.0f, 1.0f); // Lower-right
210  tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint
211  border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); // 50% opaque white
212  sdl_srf = SDL_CreateRGBSurface(0, this->nx, this->ny, 32, 0, 0, 0, 0);
213  if (sdl_srf == NULL)
214  {
215  std::cout << "Surface could not be created! SDL Error: " << SDL_GetError() << std::endl;
216  }
217  this->b_data_set = false;
218 }

Member Function Documentation

◆ render_window() [1/2]

template<typename T >
void ImGuiImageWindow< T >::render_window ( bool  b_redraw,
int  last_y,
bool  b_trigger_update 
)

Definition at line 345 of file ImGuiImageWindow.cpp.

346 {
347  ImGui::SetNextWindowSize(ImVec2{256, 256}, ImGuiCond_FirstUseEver);
348  bool t_open = ImGui::Begin(title.c_str(), pb_open, ImGuiWindowFlags_NoScrollbar);
349  if (t_open)
350  {
351  bool fr_switch = detect_frame_switch(fr_count);
352  b_trigger_update = b_trigger_update || b_trigger_ext || fr_switch;
353  if (b_trigger_update)
354  {
355  render_image();
356  }
357 
358  if (this->has(GIM_Flags::SaveImButton))
359  {
360  ImGui::SameLine();
361  if (ImGui::Button("Save Image as..."))
362  {
363  saveFileDialog.Open();
364  }
365  saveFileDialog.Display();
366  if (saveFileDialog.HasSelected())
367  {
368  std::string img_file = saveFileDialog.GetSelected().string();
369  saveFileDialog.ClearSelected();
370  save_image(&img_file, sdl_srf);
371  }
372  }
373 
374  if (this->has(GIM_Flags::SaveDataButton))
375  {
376  ImGui::SameLine();
377  if (ImGui::Button("Save Data as..."))
378  {
379  saveDataDialog.Open();
380  }
381  saveDataDialog.Display();
382  if (saveDataDialog.HasSelected())
383  {
384  std::string com_file = saveDataDialog.GetSelected().string();
385  saveDataDialog.ClearSelected();
386  save_numpy(&com_file, nx, ny, data);
387  }
388  }
389 
390  if (this->has(GIM_Flags::FftButton))
391  {
392  ImGui::SameLine();
393  bool fft_button_press = ImGui::Button("Compute FFT");
394  if (fft_button_press)
395  {
396  *fft_window->pb_open = true;
397  }
398  if (fft_button_press || (*fft_window->pb_open && b_trigger_update))
399  {
400  if (b_data_set)
401  {
402  compute_fft();
403  fft_window->set_data(nx, ny, &data_fft_f);
404  fft_window->b_trigger_update = true;
405  }
406  else
407  {
408  std::cout << "FFT was not performed, because no data was found in " + this->title + "!" << std::endl;
409  }
410  }
411  }
412 
413  if (this->has(GIM_Flags::PowerSlider))
414  {
415  ImGui::SameLine();
416  ImGui::SetNextItemWidth(64);
417  if (ImGui::DragFloat("Power", &power, 0.05f, 0.05f, 2.0f, "%.2f"))
418  {
419  this->last_idr = 0;
420  this->last_y = 0;
421  render_image((fr_count == 0) ? nxy : fr_count);
422  b_trigger_update = true;
423  }
424  }
425 
426  if (this->has(GIM_Flags::ColormapSelector))
427  {
428  ImGui::SameLine();
429  ImGui::SetNextItemWidth(-1);
430  if (ImGui::Combo("Colormap", &data_cmap, cmaps, IM_ARRAYSIZE(cmaps)))
431  {
432  this->last_idr = 0;
433  this->last_y = 0;
434  render_image((fr_count == 0) ? nxy : fr_count);
435  b_trigger_update = true;
436  }
437  }
438 
439  if (b_redraw && auto_render && fr_count > 0)
440  render_image(fr_count);
441 
442  ImGui::SetNextWindowBgAlpha(0.0f);
443  ImGui::BeginChildFrame(ImGui::GetID("ImageFrame"), ImVec2(0.0f, 0.0f), ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
444  ImVec2 vAvail = ImGui::GetContentRegionAvail();
445  float scale = (std::min)(vAvail.x / sdl_srf->w, vAvail.y / sdl_srf->h);
446  float tex_h = sdl_srf->h * scale;
447  float tex_w = sdl_srf->w * scale;
448  float tex_h_z = tex_h * zoom;
449  float tex_w_z = tex_w * zoom;
450  if (b_redraw || b_trigger_update)
451  {
452  glBindTexture(GL_TEXTURE_2D, (*tex_id));
453  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sdl_srf->w, sdl_srf->h, 0,
454  GL_BGRA, GL_UNSIGNED_BYTE, sdl_srf->pixels);
455  }
456  ImVec2 pos = ImGui::GetCursorScreenPos();
457  ImGui::Image((ImTextureID)(*tex_id), ImVec2(tex_w_z, tex_h_z), uv_min, uv_max, tint_col, border_col);
458  if (ImGui::IsItemHovered())
459  {
460 
461  // Get Mouse Inputs
462  float dz = (float)io.MouseWheel;
463  ImVec2 xy = ImGui::GetMousePos();
464 
465  // Compute relative cursor positions
466  float rel_x = xy.x - pos.x - ImGui::GetScrollX();
467  float rel_y = xy.y - pos.y - ImGui::GetScrollY();
468 
469  // Adjust Scroll positions
470  // Capture Start position of scroll or drag/pan
471  if ((std::abs(dz) > 0.0f) || ImGui::IsMouseClicked(ImGuiMouseButton_Left))
472  {
473  start_x = rel_x;
474  start_y = rel_y;
475  start_xs = ImGui::GetScrollX();
476  start_ys = ImGui::GetScrollY();
477  }
478 
479  // Panning
480  if (ImGui::IsMouseDown(ImGuiMouseButton_Left))
481  {
482  ImGui::SetScrollX(start_xs - (rel_x - start_x));
483  ImGui::SetScrollY(start_ys - (rel_y - start_y));
484  }
485 
486  // Zooming
487  if (std::abs(dz) > 0.0f)
488  {
489 
490  float zoom2 = zoom + dz * 0.1;
491  zoom2 = (std::max)(1.0f, zoom2);
492 
493  float dx = ((xy.x - pos.x) / tex_w_z) * tex_w * (zoom2 - zoom);
494  float dy = ((xy.y - pos.y) / tex_h_z) * tex_h * (zoom2 - zoom);
495 
496  ImGui::SetScrollX(start_xs + dx);
497  ImGui::SetScrollY(start_ys + dy);
498 
499  zoom = zoom2;
500  }
501 
502  // Value Popup
503  if (ImGui::IsMouseDown(ImGuiMouseButton_Right))
504  {
505  float scale_fct = scale * zoom;
506  int x = (int)std::floor((xy.x - pos.x) / scale_fct);
507  int y = (int)std::floor((xy.y - pos.y) / scale_fct);
508  value_tooltip(x, y, zoom);
509  }
510  if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
511  {
512  zoom = 1.0f;
513  ImGui::SetScrollX(0.0f);
514  ImGui::SetScrollY(0.0f);
515  }
516  }
517  ImGui::EndChildFrame();
518  }
519  ImGui::End();
520  b_trigger_update = false;
521 }
void save_numpy(std::string *path, int nx, int ny, std::vector< T > *data)
Definition: GuiUtils.cpp:17
void save_image(std::string *path, SDL_Surface *sdl_srf)
Definition: GuiUtils.cpp:30
@ ColormapSelector
ImGuiImageWindow< float > * fft_window

◆ render_window() [2/2]

template<typename T >
void ImGuiImageWindow< T >::render_window ( bool  b_redraw,
int  last_y,
int  render_update_offset,
bool  b_trigger_update 
)

Definition at line 265 of file ImGuiImageWindow.cpp.

266 {
267  this->render_update_offset = render_update_offset;
268  render_window(b_redraw, last_y, b_trigger_update);
269 }
void render_window(bool b_redraw, int last_y, int render_update_offset, bool b_trigger_update)

◆ reset_min_max()

template<typename T >
void ImGuiImageWindow< T >::reset_min_max

Definition at line 272 of file ImGuiImageWindow.cpp.

273 {
274  data_min = FLT_MAX;
275  data_max = -FLT_MAX;
276  data_range = FLT_MAX;
277 }

◆ set_data()

template<typename T >
void ImGuiImageWindow< T >::set_data ( int  width,
int  height,
std::vector< T > *  data 
)

Definition at line 221 of file ImGuiImageWindow.cpp.

222 {
223  this->data = data;
224  set_nx_ny(width, height);
225  reset_limits();
226  if (!auto_render)
227  {
228  render_image();
229  };
230  b_data_set = true;
231 }
void set_nx_ny(int width, int height)

◆ set_nx_ny()

template<typename T >
void ImGuiImageWindow< T >::set_nx_ny ( int  width,
int  height 
)

Definition at line 245 of file ImGuiImageWindow.cpp.

246 {
247  this->nx = width;
248  this->ny = height;
249  this->nxy = height * width;
250 
251  data_fft.resize(nxy);
252  data_fft_f.resize(nxy);
253  data_val.resize(nxy);
254 
255  reset_limits();
256 
257  sdl_srf = SDL_CreateRGBSurface(0, this->nx, this->ny, 32, 0, 0, 0, 0);
258  if (sdl_srf == NULL)
259  {
260  std::cout << "Surface could not be created! SDL Error: " << SDL_GetError() << std::endl;
261  }
262 }

Member Data Documentation

◆ b_trigger_update

template<class T >
bool ImGuiImageWindow< T >::b_trigger_update

Definition at line 58 of file ImGuiImageWindow.h.

◆ fft_window

template<class T >
ImGuiImageWindow<float>* ImGuiImageWindow< T >::fft_window

Definition at line 57 of file ImGuiImageWindow.h.

◆ pb_open

template<class T >
bool* ImGuiImageWindow< T >::pb_open

Definition at line 56 of file ImGuiImageWindow.h.


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