File: radiation_handler.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (216 lines) | stat: -rw-r--r-- 8,486 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "radiation_handler.h"
#include "imgui/pfd/pfd_utils.h"
#include "resources.h"
#include "core/style.h"
#include "common/widgets/themed_widgets.h"
#include "main_ui.h"

#include "common/image/meta.h"

namespace satdump
{
    void RadiationViewerHandler::init()
    {
        products = (RadiationProducts *)ViewerHandler::products;

        for (int c = 0; c < (int)products->channel_counts.size(); c++)
            select_channel_image_str += products->get_channel_name(c) + '\0';

        update();
    }

    RadiationViewerHandler::~RadiationViewerHandler()
    {
        handler_thread_pool.stop();
        for (int i = 0; i < handler_thread_pool.size(); i++)
        {
            if (handler_thread_pool.get_thread(i).joinable())
                handler_thread_pool.get_thread(i).join();
        }
    }

    void RadiationViewerHandler::update()
    {
        if (selected_visualization_id == 0)
        {
            handler_thread_pool.clear_queue();
            handler_thread_pool.push([this](int)
                                     {   async_image_mutex.lock();
                                    is_updating = true;
                                    logger->info("Update map...");

                                    RadiationMapCfg cfg;
                                    cfg.channel = select_channel_image_id + 1;
                                    cfg.radius = 5;
                                    cfg.min = map_min;
                                    cfg.max = map_max;
                                    map_img = make_radiation_map(*products, cfg);
                                    image_view.update(map_img);

                                    logger->info("Done");
                                    is_updating = false;
                                    async_image_mutex.unlock(); });
        }
        else if (selected_visualization_id == 1)
        {
            graph_values.clear();
            graph_values.resize(products->channel_counts.size());
            for (int c = 0; c < (int)products->channel_counts.size(); c++)
            {
                for (int i = 0; i < (int)products->channel_counts[c].size(); i++)
                    graph_values[c].push_back(products->channel_counts[c][i]);
            }
        }
    }

    void RadiationViewerHandler::drawMenu()
    {
        if (ImGui::CollapsingHeader("View"))
        {
            if (ImGui::RadioButton(u8"\uf84c   Map", &selected_visualization_id, 0))
                update();
            if (ImGui::RadioButton(u8"\uf437   Graph", &selected_visualization_id, 1))
                update();

            if (selected_visualization_id == 0)
            {
                if (ImGui::Combo("###mapchannelcomboid", &select_channel_image_id, select_channel_image_str.c_str()))
                    update();
                ImGui::SetNextItemWidth(ImGui::GetWindowWidth() / 2);
                if (ImGui::DragInt("##Min", &map_min, 1.0f, 0, 255, "Min: %d", ImGuiSliderFlags_AlwaysClamp))
                    update();
                ImGui::SameLine();
                ImGui::SetNextItemWidth(ImGui::GetWindowWidth() / 2);
                if (ImGui::DragInt("##Max", &map_max, 1.0f, 0, 255, "Max: %d", ImGuiSliderFlags_AlwaysClamp))
                    update();
            }

            bool save_disabled = selected_visualization_id != 0 || is_updating;
            if (save_disabled)
                style::beginDisabled();

            if (ImGui::Button("Save"))
            {
                handler_thread_pool.push([this](int)
                                         {   async_image_mutex.lock();
                        is_updating = true;
                        logger->info("Saving Image...");
                        std::string default_path = config::main_cfg["satdump_directories"]["default_image_output_directory"]["value"].get<std::string>();
                        std::string saved_at = save_image_dialog(products->instrument_name + "_map", default_path, "Save Map", &map_img, &viewer_app->save_type); 

                        if (saved_at == "")
                            logger->info("Save cancelled");
                        else
                            logger->info("Saved current map at %s", saved_at.c_str());
                        is_updating = false;
                        async_image_mutex.unlock(); });
            }
            if (save_disabled)
                style::endDisabled();

            if (!canBeProjected())
                style::beginDisabled();
            if (ImGui::Button("Add to Projections"))
                addCurrentToProjections();
            ImGui::SameLine();
            proj_notif.draw();
            if (!canBeProjected())
                style::endDisabled();
        }
    }

    void RadiationViewerHandler::drawContents(ImVec2 win_size)
    {
        if (selected_visualization_id == 0)
        {
            image_view.draw(win_size);
        }
        else if (selected_visualization_id == 1)
        {
            ImGui::BeginChild("RadiationPlot");
            for (int i = 0; i < (int)products->channel_counts.size(); i++)
            {
                ImGui::BeginChild(("RadiationPlotChild##" + std::to_string(i)).c_str(), ImVec2(ImGui::GetWindowWidth(), 50 * ui_scale));
                widgets::ThemedPlotLines(style::theme.plot_bg.Value, 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));
                ImGui::Spacing();
                ImGui::Separator();
                ImGui::Spacing();
                ImGui::EndChild();
            }
            ImGui::EndChild();
        }
    }

    float RadiationViewerHandler::drawTreeMenu()
    {
        return 0;
    }

    bool RadiationViewerHandler::canBeProjected()
    {
        return selected_visualization_id == 0;
    }

    void RadiationViewerHandler::addCurrentToProjections()
    {
        if (canBeProjected())
        {
            try
            {
                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);

                double tl_lon = -180;
                double tl_lat = 90;
                double br_lon = 180;
                double br_lat = -90;

                nlohmann::json proj_cfg;
                proj_cfg["type"] = "equirec";
                proj_cfg["offset_x"] = tl_lon;
                proj_cfg["offset_y"] = tl_lat;
                proj_cfg["scalar_x"] = (br_lon - tl_lon) / double(map_img.width());
                proj_cfg["scalar_y"] = (br_lat - tl_lat) / double(map_img.height());

                image::set_metadata_proj_cfg(map_img, proj_cfg);

                // Create projection title
                std::string timestring, object_name, instrument_name, composite_name;
                composite_name = "Channel " + std::to_string(select_channel_image_id);
                if (products->has_timestamps(select_channel_image_id))
                    timestring = "[" + timestamp_to_string(get_median(products->get_timestamps(select_channel_image_id))) + "] ";
                else
                    timestring = "";
                if (products->has_tle())
                    object_name = products->get_tle().name;
                else
                    object_name = "";
                if (timestring != "" || object_name != "")
                    object_name += "\n";
                if (instrument_cfg.contains("name"))
                    instrument_name = instrument_cfg["name"];
                else
                    instrument_name = products->instrument_name;

                viewer_app->projection_layers.push_front({timestring + object_name + instrument_name + " - " + composite_name, map_img});

                //                if (rotate_image)
                //                    viewer_app->projection_layers.front().img.mirror(true, true);
                proj_notif.set_message(style::theme.green, "Added!");
            }
            catch (std::exception &e)
            {
                logger->error("Could not project image! %s", e.what());
            }
        }
        else
        {
            logger->error("Current image can't be projected!");
        }
    }
}