satdump/src-interface/viewer/radiation_handler.cpp

195 lines
6.6 KiB
C++
Raw Normal View History

2022-04-16 08:10:25 +02:00
#include "radiation_handler.h"
#include "core/config.h"
#include "imgui/pfd/pfd_utils.h"
2022-04-16 08:10:25 +02:00
#include "resources.h"
2022-10-17 00:51:02 +02:00
#include "core/module.h"
2022-12-05 00:22:59 +01:00
#include "core/style.h"
2022-12-05 20:24:19 +01:00
#include "common/projection/reprojector.h"
2022-04-16 08:10:25 +02:00
2023-07-31 22:06:35 -04:00
#ifdef _MSC_VER
#include <direct.h>
#endif
2022-04-16 08:10:25 +02:00
namespace satdump
{
void RadiationViewerHandler::init()
{
products = (RadiationProducts *)ViewerHandler::products;
2022-05-12 18:19:01 +02:00
for (int c = 0; c < (int)products->channel_counts.size(); c++)
2022-10-17 00:44:03 +02:00
select_channel_image_str += products->get_channel_name(c) + '\0';
2022-04-16 08:10:25 +02:00
update();
}
void RadiationViewerHandler::update()
{
if (selected_visualization_id == 0)
{
2022-05-21 13:41:21 +02:00
RadiationMapCfg cfg;
cfg.channel = select_channel_image_id + 1;
cfg.radius = 5;
cfg.min = map_min;
cfg.max = map_max;
2022-12-05 00:22:59 +01:00
map_img = make_radiation_map(*products, cfg);
image_view.update(map_img);
2022-04-16 08:10:25 +02:00
}
else if (selected_visualization_id == 1)
{
2022-09-09 22:12:42 +02:00
graph_values.clear();
2022-04-16 08:10:25 +02:00
graph_values.resize(products->channel_counts.size());
2022-05-12 18:19:01 +02:00
for (int c = 0; c < (int)products->channel_counts.size(); c++)
2022-04-16 08:10:25 +02:00
{
2022-05-12 18:19:01 +02:00
for (int i = 0; i < (int)products->channel_counts[c].size(); i++)
2022-04-16 08:10:25 +02:00
graph_values[c].push_back(products->channel_counts[c][i]);
}
}
}
void RadiationViewerHandler::drawMenu()
{
2022-12-10 12:54:33 +01:00
if (ImGui::CollapsingHeader("View"))
2022-04-16 08:10:25 +02:00
{
2022-09-13 15:06:09 -07:00
if (ImGui::RadioButton(u8"\uf84c Map", &selected_visualization_id, 0))
2022-04-16 08:10:25 +02:00
update();
2022-09-13 15:06:09 -07:00
if (ImGui::RadioButton(u8"\uf437 Graph", &selected_visualization_id, 1))
2022-04-16 08:10:25 +02:00
update();
if (selected_visualization_id == 0)
{
if (ImGui::Combo("###mapchannelcomboid", &select_channel_image_id, select_channel_image_str.c_str()))
update();
2022-10-12 22:29:58 +02:00
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() / 2);
2022-09-09 22:28:43 +02:00
if (ImGui::DragInt("##Min", &map_min, 1.0f, 0, 255, "Min: %d", ImGuiSliderFlags_AlwaysClamp))
2022-09-09 22:12:42 +02:00
update();
2022-09-09 22:28:43 +02:00
ImGui::SameLine();
2022-10-12 22:29:58 +02:00
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() / 2);
if (ImGui::DragInt("##Max", &map_max, 1.0f, 0, 255, "Max: %d", ImGuiSliderFlags_AlwaysClamp))
2022-09-09 22:12:42 +02:00
update();
2022-04-16 08:10:25 +02:00
}
2022-12-05 00:22:59 +01:00
if (selected_visualization_id != 0)
style::beginDisabled();
if (ImGui::Button("Save"))
{
std::string default_ext = satdump::config::main_cfg["satdump_general"]["image_format"]["value"].get<std::string>();
2023-07-31 22:06:35 -04:00
std::string default_path = config::main_cfg["satdump_directories"]["default_image_output_directory"]["value"].get<std::string>();
#ifdef _MSC_VER
if (default_path == ".")
{
char* cwd;
cwd = _getcwd(NULL, 0);
if (cwd != 0)
default_path = cwd;
}
default_path += "\\";
#else
default_path += "/";
#endif
std::string default_name = default_path + products->instrument_name + "_map." + default_ext;
2022-12-05 00:22:59 +01:00
#ifndef __ANDROID__
auto result = pfd::save_file("Save Image", default_name, get_file_formats(default_ext));
2022-12-05 00:22:59 +01:00
while (!result.ready(1000))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (result.result().size() > 0)
{
std::string path = result.result();
2023-05-08 23:08:34 +02:00
logger->info("Saving current image at %s", path.c_str());
2022-12-05 00:22:59 +01:00
map_img.save_img(path);
}
#else
std::string path = "/storage/emulated/0/" + default_name;
2023-05-08 23:08:34 +02:00
logger->info("Saving current image at %s", path.c_str());
2022-12-05 00:22:59 +01:00
map_img.save_img("" + path);
#endif
}
if (selected_visualization_id != 0)
style::endDisabled();
2022-12-05 20:24:19 +01:00
if (!canBeProjected())
style::beginDisabled();
ImGui::Checkbox("Project", &should_project);
if (!canBeProjected())
style::endDisabled();
2022-04-16 08:10:25 +02:00
}
}
void RadiationViewerHandler::drawContents(ImVec2 win_size)
{
if (selected_visualization_id == 0)
{
image_view.draw(win_size);
}
else if (selected_visualization_id == 1)
{
2022-10-17 00:44:03 +02:00
ImGui::BeginChild("RadiationPlot");
2022-10-12 22:29:58 +02:00
for (int i = 0; i < (int)products->channel_counts.size(); i++)
{
2022-12-05 00:22:59 +01:00
ImGui::BeginChild(("RadiationPlotChild##" + std::to_string(i)).c_str(), ImVec2(ImGui::GetWindowWidth(), 50 * ui_scale));
ImGui::PlotLines(products->get_channel_name(i).c_str(), graph_values[i].data(), graph_values[i].size(), 0, NULL, 0, 255, ImVec2(ImGui::GetWindowWidth() - 100 * ui_scale, 30 * ui_scale));
2022-09-09 22:12:42 +02:00
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
2022-10-17 00:44:03 +02:00
ImGui::EndChild();
2022-09-09 22:12:42 +02:00
}
2022-04-16 08:10:25 +02:00
ImGui::EndChild();
}
}
float RadiationViewerHandler::drawTreeMenu()
{
return 0;
}
2022-12-05 20:24:19 +01:00
bool RadiationViewerHandler::canBeProjected()
{
return selected_visualization_id == 0;
}
bool RadiationViewerHandler::hasProjection()
{
return projection_ready && should_project;
}
bool RadiationViewerHandler::shouldProject()
{
return should_project;
}
void RadiationViewerHandler::updateProjection(int width, int height, nlohmann::json settings, float *progess)
{
if (canBeProjected())
{
RadiationMapCfg cfg;
cfg.channel = select_channel_image_id + 1;
cfg.radius = 5;
cfg.min = map_min;
cfg.max = map_max;
auto map_img = make_radiation_map(*products, cfg, true);
reprojection::ReprojectionOperation op;
op.source_prj_info = nlohmann::json::parse("{\"type\":\"equirectangular\",\"tl_lon\":-180,\"tl_lat\":90,\"br_lon\":180,\"br_lat\":-90}");
op.target_prj_info = settings;
op.img = map_img;
op.output_width = width;
op.output_height = height;
reprojection::ProjectionResult res = reprojection::reproject(op, progess);
projected_img = res.img;
projection_ready = true;
}
else
{
logger->error("Current image can't be projected!");
}
}
image::Image<uint16_t> &RadiationViewerHandler::getProjection()
{
return projected_img;
}
}