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
GuiUtils.cpp
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#include "GuiUtils.h"
15
16template <typename T>
17void save_numpy(std::string *path, int nx, int ny, std::vector<T> *data)
18{
19 std::string ext = std::filesystem::path(*path).extension().string();
20 if (ext != ".npy")
21 {
22 *path += ".npy";
23 }
24 const std::vector<long unsigned> shape{static_cast<long unsigned>(ny), static_cast<long unsigned>(nx)};
25 npy::SaveArrayAsNumpy(path->c_str(), false, shape.size(), shape.data(), *data);
26}
27template void save_numpy<float>(std::string *path, int nx, int ny, std::vector<float> *data);
28template void save_numpy<std::complex<float>>(std::string *path, int nx, int ny, std::vector<std::complex<float>> *data);
29
30void save_image(std::string *path, SDL_Surface *sdl_srf)
31{
32 std::string ext = std::filesystem::path(*path).extension().string();
33 if ((ext != ".png") && (ext != ".PNG"))
34 {
35 *path += ".png";
36 }
37 IMG_SavePNG(sdl_srf, path->c_str());
38}
39
40// Vertical Splitter Container
41void v_splitter(float thickness, float &size0, const float &min_h, const float &max_h, const float &offset)
42{
43
44 ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyle().Colors[ImGuiCol_ScrollbarGrab]);
45 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyle().Colors[ImGuiCol_ScrollbarGrabHovered]);
46 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyle().Colors[ImGuiCol_ScrollbarGrabActive]);
47 ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, ImGui::GetStyle().ScrollbarRounding);
48 ImGui::Button("v", ImVec2(-1, thickness));
49 ImGui::PopStyleColor(3);
50 ImGui::PopStyleVar(1);
51 if (ImGui::IsItemHovered() || ImGui::IsItemActive())
52 {
53 ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
54 }
55
56 if (ImGui::IsItemActive())
57 {
58 float d = ImGui::GetMousePos().y - offset;
59 if (d < min_h)
60 size0 = min_h;
61 else if (d > max_h)
62 size0 = max_h;
63 else
64 size0 = d;
65 }
66};
67
68Main_Dock::Main_Dock(ImGuiID dock_id)
69{
70 this->dock_id = dock_id;
71}
72
73void Main_Dock::render(ImVec2 pos, ImVec2 size)
74{
75 ImGui::SetNextWindowPos(pos);
76 ImGui::SetNextWindowSize(size);
77 ImGui::Begin("DockWindow", nullptr, window_flags);
78 ImGui::DockSpace(dock_id);
79
80 static auto first_time = true;
81 if (first_time)
82 {
83 first_time = false;
84 ImGui::DockBuilderRemoveNode(dock_id); // clear any previous layout
85 ImGui::DockBuilderAddNode(dock_id, dockspace_flags);
86 ImGui::DockBuilderSetNodePos(dock_id, pos);
87 ImGui::DockBuilderSetNodeSize(dock_id, size);
88 ImGui::DockBuilderDockWindow("RICOM", dock_id);
89 ImGui::DockBuilderFinish(dock_id);
90 }
91 ImGui::End();
92}
93
94namespace SDL_Utils
95{
96 // Draw a pixel on the surface at (x, y) for a given colormap
97 void draw_pixel(SDL_Surface *surface, int x, int y, float val, int col_map)
98 {
99 tinycolormap::Color c = tinycolormap::GetColor(val, tinycolormap::ColormapType(col_map));
100 Uint32 px = SDL_MapRGB(surface->format, (Uint8)(c.ri()), (Uint8)(c.gi()), (Uint8)(c.bi()));
101 Uint32 *const target_pixel = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel);
102 *target_pixel = px;
103 }
104
105 void draw_pixel(SDL_Surface *surface, int x, int y, float ang, float mag, int col_map)
106 {
107 tinycolormap::Color c = mag * tinycolormap::GetColor(ang, tinycolormap::ColormapType(col_map));
108 Uint32 px = SDL_MapRGB(surface->format, (Uint8)(c.ri()), (Uint8)(c.gi()), (Uint8)(c.bi()));
109 Uint32 *const target_pixel = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel);
110 *target_pixel = px;
111 }
112}
template void save_numpy< float >(std::string *path, int nx, int ny, std::vector< float > *data)
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
void v_splitter(float thickness, float &size0, const float &min_h, const float &max_h, const float &offset)
Definition GuiUtils.cpp:41
const ImGuiWindowFlags window_flags
Definition GuiUtils.h:34
void render(ImVec2 pos, ImVec2 size)
Definition GuiUtils.cpp:73
const ImGuiDockNodeFlags dockspace_flags
Definition GuiUtils.h:33
ImGuiID dock_id
Definition GuiUtils.h:32
Main_Dock(ImGuiID dock_id)
Definition GuiUtils.cpp:68
void draw_pixel(SDL_Surface *surface, int x, int y, float val, int col_map)
Definition GuiUtils.cpp:97