riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Loading...
Searching...
No Matches
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 247 of file ImGuiImageWindow.cpp.

248{
249
250 this->title = title;
251 this->flags = flags;
252 this->tex_id = tex_id;
253 this->pb_open = visible;
254 this->auto_render = auto_render;
255 this->data_cmap = data_cmap;
256 this->last_y = 0;
257 this->last_idr = 0;
258 this->last_img = 0;
259 this->zoom = 1.0f;
260 this->power = 1.0f;
261 this->ny = 1;
262 this->nx = 1;
263 this->nxy = 1;
264 this->render_update_offset = 0;
265 this->b_trigger_update = false;
266 this->bin_factor = 1;
267 this->bin_factor_idx = 0;
268 saveFileDialog = ImGui::FileBrowser(ImGuiFileBrowserFlags_EnterNewFilename | ImGuiFileBrowserFlags_CreateNewDir);
269 saveFileDialog.SetTitle("Save " + title + " image as .png");
270 saveDataDialog = ImGui::FileBrowser(ImGuiFileBrowserFlags_EnterNewFilename | ImGuiFileBrowserFlags_CreateNewDir);
271 saveDataDialog.SetTitle("Save " + title + "-data as numpy array (.npy)");
272
273 uv_min = ImVec2(0.0f, 0.0f); // Top-left
274 uv_max = ImVec2(1.0f, 1.0f); // Lower-right
275 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint
276 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); // 50% opaque white
277 sdl_srf = SDL_CreateRGBSurface(0, this->nx, this->ny, 32, 0, 0, 0, 0);
278 if (sdl_srf == NULL)
279 {
280 std::cout << "Surface could not be created! SDL Error: " << SDL_GetError() << std::endl;
281 }
282 this->b_data_set = false;
283}

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 421 of file ImGuiImageWindow.cpp.

422{
423 ImGui::SetNextWindowSize(ImVec2{256, 256}, ImGuiCond_FirstUseEver);
424 bool t_open = ImGui::Begin(title.c_str(), pb_open, ImGuiWindowFlags_NoScrollbar);
425 if (t_open)
426 {
427 bool fr_switch = detect_frame_switch(fr_count);
428 b_trigger_update = b_trigger_update || b_trigger_ext || fr_switch;
430 {
431 render_image();
432 }
433
434 if (this->has(GIM_Flags::SaveImButton))
435 {
436 ImGui::SameLine();
437 if (ImGui::Button("Save Image as..."))
438 {
439 saveFileDialog.Open();
440 }
441 saveFileDialog.Display();
442 if (saveFileDialog.HasSelected())
443 {
444 std::string img_file = saveFileDialog.GetSelected().string();
445 saveFileDialog.ClearSelected();
446 save_image(&img_file, sdl_srf);
447 }
448 }
449
450 if (this->has(GIM_Flags::SaveDataButton))
451 {
452 ImGui::SameLine();
453 if (ImGui::Button("Save Data as..."))
454 {
455 saveDataDialog.Open();
456 }
457 saveDataDialog.Display();
458 if (saveDataDialog.HasSelected())
459 {
460 std::string com_file = saveDataDialog.GetSelected().string();
461 saveDataDialog.ClearSelected();
462 save_numpy(&com_file, nx, ny, data);
463 }
464 }
465
466 if (this->has(GIM_Flags::FftButton))
467 {
468 ImGui::SameLine();
469 bool fft_button_press = ImGui::Button("Compute FFT");
470 if (fft_button_press)
471 {
472 *fft_window->pb_open = true;
473 }
474 if (fft_button_press || (*fft_window->pb_open && b_trigger_update))
475 {
476 if (b_data_set)
477 {
478 compute_fft();
479 fft_window->set_data(nx, ny, &data_fft_f);
480 fft_window->b_trigger_update = true;
481 }
482 else
483 {
484 std::cout << "FFT was not performed, because no data was found in " + this->title + "!" << std::endl;
485 }
486 }
487 }
488
489 if (this->has(GIM_Flags::PowerSlider))
490 {
491 ImGui::SameLine();
492 ImGui::SetNextItemWidth(64);
493 if (ImGui::DragFloat("Power", &power, 0.05f, 0.05f, 2.0f, "%.2f"))
494 {
495 this->last_idr = 0;
496 this->last_y = 0;
497 render_image((fr_count == 0) ? nxy : fr_count);
498 b_trigger_update = true;
499 }
500 }
501
502 if (this->has(GIM_Flags::ColormapSelector))
503 {
504 ImGui::SameLine();
505 ImGui::SetNextItemWidth(100);
506 if (ImGui::Combo("Colormap", &data_cmap, cmaps, IM_ARRAYSIZE(cmaps)))
507 {
508 this->last_idr = 0;
509 this->last_y = 0;
510 render_image((fr_count == 0) ? nxy : fr_count);
511 b_trigger_update = true;
512 }
513 }
514
515 // Binning selector
516 ImGui::SameLine();
517 ImGui::SetNextItemWidth(60);
518 if (ImGui::Combo("Bin", &bin_factor_idx, bin_options, IM_ARRAYSIZE(bin_options)))
519 {
520 bin_factor = 1 << bin_factor_idx; // 0->1, 1->2, 2->4, 3->8
521 recreate_surface();
522 this->last_idr = 0;
523 this->last_y = 0;
524 render_image((fr_count == 0) ? nxy : fr_count);
525 b_trigger_update = true;
526 }
527
528 if (b_redraw && auto_render && fr_count > 0)
529 render_image(fr_count);
530
531 ImGui::SetNextWindowBgAlpha(0.0f);
532 ImGui::BeginChildFrame(ImGui::GetID("ImageFrame"), ImVec2(0.0f, 0.0f), ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
533 ImVec2 vAvail = ImGui::GetContentRegionAvail();
534 float scale = (std::min)(vAvail.x / sdl_srf->w, vAvail.y / sdl_srf->h);
535 float tex_h = sdl_srf->h * scale;
536 float tex_w = sdl_srf->w * scale;
537 float tex_h_z = tex_h * zoom;
538 float tex_w_z = tex_w * zoom;
539 if (b_redraw || b_trigger_update)
540 {
541 glBindTexture(GL_TEXTURE_2D, (*tex_id));
542 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sdl_srf->w, sdl_srf->h, 0,
543 GL_BGRA, GL_UNSIGNED_BYTE, sdl_srf->pixels);
544 }
545 ImVec2 pos = ImGui::GetCursorScreenPos();
546 ImGui::Image((ImTextureID)(*tex_id), ImVec2(tex_w_z, tex_h_z), uv_min, uv_max, tint_col, border_col);
547 if (ImGui::IsItemHovered())
548 {
549
550 // Get Mouse Inputs
551 float dz = (float)io.MouseWheel;
552 ImVec2 xy = ImGui::GetMousePos();
553
554 // Compute relative cursor positions
555 float rel_x = xy.x - pos.x - ImGui::GetScrollX();
556 float rel_y = xy.y - pos.y - ImGui::GetScrollY();
557
558 // Adjust Scroll positions
559 // Capture Start position of scroll or drag/pan
560 if ((std::abs(dz) > 0.0f) || ImGui::IsMouseClicked(ImGuiMouseButton_Left))
561 {
562 start_x = rel_x;
563 start_y = rel_y;
564 start_xs = ImGui::GetScrollX();
565 start_ys = ImGui::GetScrollY();
566 }
567
568 // Panning
569 if (ImGui::IsMouseDown(ImGuiMouseButton_Left))
570 {
571 ImGui::SetScrollX(start_xs - (rel_x - start_x));
572 ImGui::SetScrollY(start_ys - (rel_y - start_y));
573 }
574
575 // Zooming
576 if (std::abs(dz) > 0.0f)
577 {
578
579 float zoom2 = zoom + dz * 0.1;
580 zoom2 = (std::max)(1.0f, zoom2);
581
582 float dx = ((xy.x - pos.x) / tex_w_z) * tex_w * (zoom2 - zoom);
583 float dy = ((xy.y - pos.y) / tex_h_z) * tex_h * (zoom2 - zoom);
584
585 ImGui::SetScrollX(start_xs + dx);
586 ImGui::SetScrollY(start_ys + dy);
587
588 zoom = zoom2;
589 }
590
591 // Value Popup
592 if (ImGui::IsMouseDown(ImGuiMouseButton_Right))
593 {
594 float scale_fct = scale * zoom;
595 int x = (int)std::floor((xy.x - pos.x) / scale_fct);
596 int y = (int)std::floor((xy.y - pos.y) / scale_fct);
597 value_tooltip(x, y, zoom);
598 }
599 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
600 {
601 zoom = 1.0f;
602 ImGui::SetScrollX(0.0f);
603 ImGui::SetScrollY(0.0f);
604 }
605 }
606 ImGui::EndChildFrame();
607 }
608 ImGui::End();
609 b_trigger_update = false;
610}
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 341 of file ImGuiImageWindow.cpp.

342{
343 this->render_update_offset = render_update_offset;
344 render_window(b_redraw, last_y, b_trigger_update);
345}
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 348 of file ImGuiImageWindow.cpp.

349{
350 data_min = FLT_MAX;
351 data_max = -FLT_MAX;
352 data_range = FLT_MAX;
353}

◆ set_data()

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

Definition at line 286 of file ImGuiImageWindow.cpp.

287{
288 this->data = data;
289 set_nx_ny(width, height);
290 reset_limits();
291 if (!auto_render)
292 {
293 render_image();
294 };
295 b_data_set = true;
296}
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 310 of file ImGuiImageWindow.cpp.

311{
312 this->nx = width;
313 this->ny = height;
314 this->nxy = height * width;
315
316 data_fft.resize(nxy);
317 data_fft_f.resize(nxy);
318 data_val.resize(nxy);
319
320 reset_limits();
321 recreate_surface();
322}

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: