From b314c79fdde4425a43bca31d0e6a146bc22959b2 Mon Sep 17 00:00:00 2001 From: Aang23 Date: Sun, 6 Apr 2025 13:17:30 +0200 Subject: [PATCH] Some jason-3 work --- Doxyfile.in | 1 + plugins/bitview_app/bitview.h | 2 +- .../jason3/instruments/amr2/amr2_reader.cpp | 2 +- .../jason3/instruments/lpt/lpt_reader.cpp | 4 +- .../jason3/module_jason3_instruments.cpp | 126 +++++++- .../airspy_sdr_support/airspy_dev.h | 13 +- src-core/dsp/device/options_displayer.h | 53 +--- .../handler/dataset/TextEditor.cpp | 0 .../handler/dataset/TextEditor.h | 0 .../handler/dataset/dataset_handler.cpp | 0 .../handler/dataset/dataset_handler.h | 0 .../dataset/dataset_product_handler.cpp | 0 .../handler/dataset/dataset_product_handler.h | 0 .../dataset/flowgraph/datasetproc_node.h | 0 .../handler/dataset/flowgraph/flowgraph.cpp | 0 .../handler/dataset/flowgraph/flowgraph.h | 0 .../handler/dataset/flowgraph/image_nodes.h | 0 .../dataset/flowgraph/imageproduct_node.h | 0 .../handler/dataset/flowgraph_processor.cpp | 0 .../handler/dataset/flowgraph_processor.h | 0 .../handler/dataset/lua/geodetic.cpp | 0 .../handler/dataset/lua/handler_image.cpp | 0 .../handler/dataset/lua/image.cpp | 0 .../handler/dataset/lua/logger.cpp | 0 .../handler/dataset/lua/lua_bind.h | 0 .../handler/dataset/lua/product.cpp | 0 .../handler/dataset/lua/product_image.cpp | 0 .../handler/dataset/lua/projection.cpp | 0 .../handler/dataset/lua_processor.cpp | 0 .../handler/dataset/lua_processor.h | 0 .../handler/dataset/processor.h | 0 .../{products2 => }/handler/dummy_handler.h | 0 src-core/{products2 => }/handler/handler.cpp | 0 src-core/{products2 => }/handler/handler.h | 0 .../handler/image/image_handler.cpp | 2 +- .../handler/image/image_handler.h | 0 .../handler/processing_handler.h | 0 .../handler/product/image_product_handler.cpp | 0 .../handler/product/image_product_handler.h | 0 .../handler/product/product_handler.cpp | 0 .../handler/product/product_handler.h | 0 .../product/punctiform_product_handler.cpp | 168 +++++++++++ .../product/punctiform_product_handler.h | 33 +++ .../handler/projection/proj_ui.h | 0 .../handler/projection/projection_handler.cpp | 0 .../handler/projection/projection_handler.h | 0 .../handler/trash/trash_handler.cpp | 0 .../handler/trash/trash_handler.h | 0 src-core/{products2 => }/handler/tree.h | 0 .../handler/vector/dbf_file/dbf_file.cpp | 0 .../handler/vector/dbf_file/dbf_file.h | 0 .../handler/vector/shapefile_handler.cpp | 0 .../handler/vector/shapefile_handler.h | 0 .../product/punctiform_product_handler.cpp | 95 ------ src-core/products2/product.cpp | 5 +- src-core/products2/product_process.cpp | 5 + .../products2/punctiform/product_dotmap.cpp | 91 ++++++ .../products2/punctiform/product_dotmap.h | 22 ++ src-core/products2/punctiform_product.cpp | 58 ++++ src-core/products2/punctiform_product.h | 89 ++++++ .../viewer2/dsp/dsp_flowgraph_handler.h | 2 +- src-interface/viewer2/dsp/newrec.h | 2 +- src-interface/viewer2/dsp/waterfall_test.h | 2 +- .../tools_todoreworkmove/lut_generator.h | 2 +- src-interface/viewer2/viewer.cpp | 12 +- src-interface/viewer2/viewer.h | 2 +- src-testing/CMakeLists.txt | 4 +- src-testing/main.cpp | 275 ++++++------------ src-testing/main_SDR.cpp | 221 ++++++++++++++ src-testing/main_as.cpp | 259 +++++++++++++++++ 70 files changed, 1200 insertions(+), 350 deletions(-) rename src-core/{products2 => }/handler/dataset/TextEditor.cpp (100%) rename src-core/{products2 => }/handler/dataset/TextEditor.h (100%) rename src-core/{products2 => }/handler/dataset/dataset_handler.cpp (100%) rename src-core/{products2 => }/handler/dataset/dataset_handler.h (100%) rename src-core/{products2 => }/handler/dataset/dataset_product_handler.cpp (100%) rename src-core/{products2 => }/handler/dataset/dataset_product_handler.h (100%) rename src-core/{products2 => }/handler/dataset/flowgraph/datasetproc_node.h (100%) rename src-core/{products2 => }/handler/dataset/flowgraph/flowgraph.cpp (100%) rename src-core/{products2 => }/handler/dataset/flowgraph/flowgraph.h (100%) rename src-core/{products2 => }/handler/dataset/flowgraph/image_nodes.h (100%) rename src-core/{products2 => }/handler/dataset/flowgraph/imageproduct_node.h (100%) rename src-core/{products2 => }/handler/dataset/flowgraph_processor.cpp (100%) rename src-core/{products2 => }/handler/dataset/flowgraph_processor.h (100%) rename src-core/{products2 => }/handler/dataset/lua/geodetic.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/handler_image.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/image.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/logger.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/lua_bind.h (100%) rename src-core/{products2 => }/handler/dataset/lua/product.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/product_image.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua/projection.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua_processor.cpp (100%) rename src-core/{products2 => }/handler/dataset/lua_processor.h (100%) rename src-core/{products2 => }/handler/dataset/processor.h (100%) rename src-core/{products2 => }/handler/dummy_handler.h (100%) rename src-core/{products2 => }/handler/handler.cpp (100%) rename src-core/{products2 => }/handler/handler.h (100%) rename src-core/{products2 => }/handler/image/image_handler.cpp (99%) rename src-core/{products2 => }/handler/image/image_handler.h (100%) rename src-core/{products2 => }/handler/processing_handler.h (100%) rename src-core/{products2 => }/handler/product/image_product_handler.cpp (100%) rename src-core/{products2 => }/handler/product/image_product_handler.h (100%) rename src-core/{products2 => }/handler/product/product_handler.cpp (100%) rename src-core/{products2 => }/handler/product/product_handler.h (100%) create mode 100644 src-core/handler/product/punctiform_product_handler.cpp rename src-core/{products2 => }/handler/product/punctiform_product_handler.h (51%) rename src-core/{products2 => }/handler/projection/proj_ui.h (100%) rename src-core/{products2 => }/handler/projection/projection_handler.cpp (100%) rename src-core/{products2 => }/handler/projection/projection_handler.h (100%) rename src-core/{products2 => }/handler/trash/trash_handler.cpp (100%) rename src-core/{products2 => }/handler/trash/trash_handler.h (100%) rename src-core/{products2 => }/handler/tree.h (100%) rename src-core/{products2 => }/handler/vector/dbf_file/dbf_file.cpp (100%) rename src-core/{products2 => }/handler/vector/dbf_file/dbf_file.h (100%) rename src-core/{products2 => }/handler/vector/shapefile_handler.cpp (100%) rename src-core/{products2 => }/handler/vector/shapefile_handler.h (100%) delete mode 100644 src-core/products2/handler/product/punctiform_product_handler.cpp create mode 100644 src-core/products2/punctiform/product_dotmap.cpp create mode 100644 src-core/products2/punctiform/product_dotmap.h create mode 100644 src-core/products2/punctiform_product.cpp create mode 100644 src-core/products2/punctiform_product.h create mode 100644 src-testing/main_SDR.cpp create mode 100644 src-testing/main_as.cpp diff --git a/Doxyfile.in b/Doxyfile.in index 62873baa9..85560cdb6 100644 --- a/Doxyfile.in +++ b/Doxyfile.in @@ -2,6 +2,7 @@ PROJECT_NAME = SatDump OUTPUT_DIRECTORY = @CMAKE_CURRENT_BINARY_DIR@/documentation/ INPUT = src-core/common/ccsds \ src-core/products2 \ + src-core/handler \ src-interface/viewer2 \ src-core/projection \ src-core/dsp \ diff --git a/plugins/bitview_app/bitview.h b/plugins/bitview_app/bitview.h index 5141aa392..f6d9006a1 100644 --- a/plugins/bitview_app/bitview.h +++ b/plugins/bitview_app/bitview.h @@ -1,6 +1,6 @@ #pragma once -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "imgui/imgui.h" #include "imgui/imgui_internal.h" #include diff --git a/plugins/jason3_support/jason3/instruments/amr2/amr2_reader.cpp b/plugins/jason3_support/jason3/instruments/amr2/amr2_reader.cpp index e178bf149..49f004daa 100644 --- a/plugins/jason3_support/jason3/instruments/amr2/amr2_reader.cpp +++ b/plugins/jason3_support/jason3/instruments/amr2/amr2_reader.cpp @@ -24,7 +24,7 @@ namespace jason3 return; // We need to know where the satellite was when that packet was created - time_t currentTime = ccsds::parseCCSDSTime(packet, 16743, 1); + time_t currentTime = ccsds::parseCCSDSTime(packet, 16743 + 1056, 1); // TODOREWORK!!!! // Also write them as a raw images for (int i = 0, y = 0; i < 12; i++) diff --git a/plugins/jason3_support/jason3/instruments/lpt/lpt_reader.cpp b/plugins/jason3_support/jason3/instruments/lpt/lpt_reader.cpp index 90c5df993..97b4d83b0 100644 --- a/plugins/jason3_support/jason3/instruments/lpt/lpt_reader.cpp +++ b/plugins/jason3_support/jason3/instruments/lpt/lpt_reader.cpp @@ -1,7 +1,7 @@ #define cimg_use_jpeg #include "lpt_reader.h" #include "resources.h" -//#include "tle.h" +// #include "tle.h" #include "common/ccsds/ccsds_time.h" #ifndef M_PI @@ -32,7 +32,7 @@ namespace jason3 frames++; // We need to know where the satellite was when that packet was created - double currentTime = ccsds::parseCCSDSTimeFull(packet, 16743, 1); + double currentTime = ccsds::parseCCSDSTimeFull(packet, 16743 + 1056, 1); timestamps.push_back(currentTime); for (int ch = 0; ch < channel_count; ch++) diff --git a/plugins/jason3_support/jason3/module_jason3_instruments.cpp b/plugins/jason3_support/jason3/module_jason3_instruments.cpp index 8d5823915..ada74239b 100644 --- a/plugins/jason3_support/jason3/module_jason3_instruments.cpp +++ b/plugins/jason3_support/jason3/module_jason3_instruments.cpp @@ -10,6 +10,13 @@ #include +#include "nlohmann/json_utils.h" +#include "products2/image_product.h" +#include "products2/punctiform_product.h" +#include "products2/dataset.h" +#include "common/tracking/tle.h" +#include "resources.h" + namespace jason3 { namespace instruments @@ -80,10 +87,41 @@ namespace jason3 data_in.close(); + // Products dataset + satdump::products::DataSet dataset; + dataset.satellite_name = "Jason-3"; + dataset.timestamp = get_median(amr2_reader.timestamps); + + std::optional satellite_tle = satdump::general_tle_registry->get_from_norad_time(41240, dataset.timestamp); + + /* // AMR-2 + { + amr2_status = SAVING; + std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/AMR-2"; + + if (!std::filesystem::exists(directory)) + std::filesystem::create_directory(directory); + + logger->info("----------- AMR-2"); + logger->info("Lines : " + std::to_string(amr2_reader.lines)); + + satdump::products::ImageProduct amr2_products; + amr2_products.instrument_name = "amr_2"; + amr2_products.set_proj_cfg_tle_timestamps(loadJsonFile(resources::getResourcePath("projections_settings/jason3_amr2.json")), satellite_tle, amr2_reader.timestamps); + + for (int i = 0; i < 3; i++) + amr2_products.images.push_back({i, "AMR2-" + std::to_string(i + 1), std::to_string(i + 1), amr2_reader.getChannel(i), 16}); + + amr2_products.save(directory); + dataset.products_list.push_back("AMR-2"); + + amr2_status = DONE; + }*/ + // AMR-2 { amr2_status = SAVING; - std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/AMR-2"; + std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/AMR-2_RAD"; if (!std::filesystem::exists(directory)) std::filesystem::create_directory(directory); @@ -91,17 +129,97 @@ namespace jason3 logger->info("----------- AMR-2"); logger->info("Lines : " + std::to_string(amr2_reader.lines)); + satdump::products::PunctiformProduct amr2_products; + amr2_products.instrument_name = "amr_2"; + amr2_products.set_tle(satellite_tle); + for (int i = 0; i < 3; i++) { + satdump::products::PunctiformProduct::DataHolder h; + // TODOREWORK h.abs_index = i; + h.channel_name = std::to_string(i + 1); + h.timestamps = amr2_reader.timestamps; auto img = amr2_reader.getChannel(i); - image::save_img(img, directory + "/AMR2-" + std::to_string(i + 1)); + for (int i2 = 0; i2 < amr2_reader.timestamps.size(); i2++) + h.data.push_back((double)img.get(0, 0, i2)); + amr2_products.data.push_back(h); } + + amr2_products.save(directory); + dataset.products_list.push_back("AMR-2_RAD"); + amr2_status = DONE; } - // TODO SEM & POSEIDON + // LPT + { + lpt_aps_b_status = lpt_aps_a_status = lpt_els_b_status = lpt_els_a_status = SAVING; + std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/LPT"; - std::this_thread::sleep_for(std::chrono::seconds(10)); + if (!std::filesystem::exists(directory)) + std::filesystem::create_directory(directory); + + logger->info("----------- LPT"); + logger->info("ELS-A : " + std::to_string(lpt_els_a_reader.timestamps.size())); + logger->info("ELS-B : " + std::to_string(lpt_els_b_reader.timestamps.size())); + logger->info("APS-A : " + std::to_string(lpt_aps_a_reader.timestamps.size())); + logger->info("APS-B : " + std::to_string(lpt_aps_b_reader.timestamps.size())); + + satdump::products::PunctiformProduct lpt_products; + lpt_products.instrument_name = "jason_lpt"; + lpt_products.set_tle(satellite_tle); + + for (int i = 0; i < lpt_els_a_reader.channel_counts.size(); i++) + { + satdump::products::PunctiformProduct::DataHolder h; + h.channel_name = "ELS-A " + std::to_string(i + 1); + h.timestamps = lpt_els_a_reader.timestamps; + for (int x = 0; x < lpt_els_a_reader.channel_counts[i].size(); x++) + h.data.push_back(lpt_els_a_reader.channel_counts[i][x]); + lpt_products.data.push_back(h); + } + + for (int i = 0; i < lpt_els_b_reader.channel_counts.size(); i++) + { + satdump::products::PunctiformProduct::DataHolder h; + h.channel_name = "ELS-B " + std::to_string(i + 1); + h.timestamps = lpt_els_b_reader.timestamps; + for (int x = 0; x < lpt_els_b_reader.channel_counts[i].size(); x++) + h.data.push_back(lpt_els_b_reader.channel_counts[i][x]); + lpt_products.data.push_back(h); + } + + for (int i = 0; i < lpt_aps_a_reader.channel_counts.size(); i++) + { + satdump::products::PunctiformProduct::DataHolder h; + h.channel_name = "APS-A " + std::to_string(i + 1); + h.timestamps = lpt_aps_a_reader.timestamps; + for (int x = 0; x < lpt_aps_a_reader.channel_counts[i].size(); x++) + h.data.push_back(lpt_aps_a_reader.channel_counts[i][x]); + lpt_products.data.push_back(h); + } + + for (int i = 0; i < lpt_aps_b_reader.channel_counts.size(); i++) + { + satdump::products::PunctiformProduct::DataHolder h; + h.channel_name = "APS-B " + std::to_string(i + 1); + h.timestamps = lpt_aps_b_reader.timestamps; + for (int x = 0; x < lpt_aps_b_reader.channel_counts[i].size(); x++) + h.data.push_back(lpt_aps_b_reader.channel_counts[i][x]); + lpt_products.data.push_back(h); + } + + lpt_products.save(directory); + dataset.products_list.push_back("LPT"); + + lpt_aps_b_status = lpt_aps_a_status = lpt_els_b_status = lpt_els_a_status = DONE; + } + + // TODOREWORK POSEIDON + logger->critical("TODOREWORK POSEIDON!!!"); + // std::this_thread::sleep_for(std::chrono::seconds(10)); + + dataset.save(d_output_file_hint.substr(0, d_output_file_hint.rfind('/'))); } void Jason3InstrumentsDecoderModule::drawUI(bool window) diff --git a/plugins/sdr_sources/airspy_sdr_support/airspy_dev.h b/plugins/sdr_sources/airspy_sdr_support/airspy_dev.h index 534f689aa..6f6bd9888 100644 --- a/plugins/sdr_sources/airspy_sdr_support/airspy_dev.h +++ b/plugins/sdr_sources/airspy_sdr_support/airspy_dev.h @@ -40,14 +40,15 @@ namespace satdump nlohmann::json get_cfg_list() { - nlohmann::json p = devInfo.params; + nlohmann::json p; p["serial"]["type"] = "string"; - p["rx"]["type"] = "sub"; - p["rx"]["max"] = 1; - auto &rxch0 = p["rx"]["sub"]; + p["rx0"]["type"] = "sub"; + auto &rxch0 = p["rx0"]["sub"]; { + rxch0 = devInfo.params; + rxch0["frequency"]["type"] = "float"; if (!rxch0.contains("samplerate")) @@ -85,7 +86,7 @@ namespace satdump { nlohmann::json v; v["serial"] = p_serial; - auto &r = v["rx"][0]; + auto &r = v["rx0"]; r["samplerate"] = p_samplerate; r["frequency"] = p_frequency; r["gain_type"] = p_gain_type; @@ -102,7 +103,7 @@ namespace satdump void set_cfg(nlohmann::json v) { setValFromJSONIfExists(p_serial, v["serial"]); - auto &r = v["rx"][0]; + auto &r = v["rx0"]; setValFromJSONIfExists(p_samplerate, r["samplerate"]); setValFromJSONIfExists(p_frequency, r["frequency"]); setValFromJSONIfExists(p_gain_type, r["gain_type"]); diff --git a/src-core/dsp/device/options_displayer.h b/src-core/dsp/device/options_displayer.h index 6c08d6238..f65e1cbc1 100644 --- a/src-core/dsp/device/options_displayer.h +++ b/src-core/dsp/device/options_displayer.h @@ -26,7 +26,7 @@ namespace satdump bool is_uint = false; bool is_int = false; bool is_float = false; - bool is_sub_array = false; + bool is_sub = false; bool is_list = false; bool is_range = false; @@ -35,8 +35,6 @@ namespace satdump std::vector list; std::vector list_string; std::array range; - int sub_array_max = 0; - nlohmann::json sub_array_cfg; // Holding values bool _bool = false; @@ -44,7 +42,7 @@ namespace satdump uint64_t _uint = 0; int64_t _int = 0; double _float = 0; - std::vector> _sub_array; + std::shared_ptr _sub; }; std::mutex opts_mtx; @@ -74,12 +72,12 @@ namespace satdump h.is_uint = vv["type"] == "uint"; h.is_int = vv["type"] == "int"; h.is_float = vv["type"] == "float"; - h.is_sub_array = vv["type"] == "sub"; + h.is_sub = vv["type"] == "sub"; - if (h.is_sub_array) + if (h.is_sub) { - h.sub_array_cfg = vv["sub"]; - h.sub_array_max = vv["max"]; + h._sub = std::make_shared(); + h._sub->add_options(vv["sub"]); } h.is_list = vv.contains("list"); @@ -114,7 +112,6 @@ namespace satdump { if (vals.contains(v.id)) { - printf("%d\n", v.is_sub_array); auto &j = vals[v.id]; if (v.is_bool && j.is_boolean()) v._bool = j; @@ -126,18 +123,8 @@ namespace satdump v._int = j; else if (v.is_float && j.is_number()) v._float = j; - else if (v.is_sub_array && j.is_array()) - { - int nsize = j.size(); - v._sub_array.clear(); - for (int i = 0; i < nsize; i++) - { - auto nsub = std::make_shared(); - nsub->add_options(v.sub_array_cfg); - nsub->set_values(j[i]); - v._sub_array.push_back(nsub); - } - } + else if (v.is_sub && j.is_object()) + v._sub->set_values(j); else throw satdump_exception("Invalid options or JSON value! (SET " + v.id + ")"); } @@ -162,11 +149,8 @@ namespace satdump j = v._int; else if (v.is_float) j = v._float; - else if (v.is_sub_array) - { - for (int i = 0; i < v._sub_array.size(); i++) - j[i] = v._sub_array[i]->get_values(); - } + else if (v.is_sub) + j = v._sub->get_values(); else throw satdump_exception("Invalid options or JSON value! (GET)"); } @@ -273,26 +257,15 @@ namespace satdump // TODOREWORK USE DOUBLE INSTEAD u |= ImGui::InputDouble(id.c_str(), &v._float); } - else if (v.is_sub_array) + else if (v.is_sub) { // TODOREWORK? Check ImGui::Separator(); ImGui::Text("%s", id.c_str()); ImGui::Separator(); - for (int i = 0; i < v._sub_array.size(); i++) - { - u |= v._sub_array[i]->draw(); - ImGui::Separator(); - } - - if (v._sub_array.size() < v.sub_array_max) - { - if (ImGui::Button("Add")) - { - // TODOREWORk v._sub_array.push_back() - } - } + u |= v._sub->draw(); + ImGui::Separator(); } else { diff --git a/src-core/products2/handler/dataset/TextEditor.cpp b/src-core/handler/dataset/TextEditor.cpp similarity index 100% rename from src-core/products2/handler/dataset/TextEditor.cpp rename to src-core/handler/dataset/TextEditor.cpp diff --git a/src-core/products2/handler/dataset/TextEditor.h b/src-core/handler/dataset/TextEditor.h similarity index 100% rename from src-core/products2/handler/dataset/TextEditor.h rename to src-core/handler/dataset/TextEditor.h diff --git a/src-core/products2/handler/dataset/dataset_handler.cpp b/src-core/handler/dataset/dataset_handler.cpp similarity index 100% rename from src-core/products2/handler/dataset/dataset_handler.cpp rename to src-core/handler/dataset/dataset_handler.cpp diff --git a/src-core/products2/handler/dataset/dataset_handler.h b/src-core/handler/dataset/dataset_handler.h similarity index 100% rename from src-core/products2/handler/dataset/dataset_handler.h rename to src-core/handler/dataset/dataset_handler.h diff --git a/src-core/products2/handler/dataset/dataset_product_handler.cpp b/src-core/handler/dataset/dataset_product_handler.cpp similarity index 100% rename from src-core/products2/handler/dataset/dataset_product_handler.cpp rename to src-core/handler/dataset/dataset_product_handler.cpp diff --git a/src-core/products2/handler/dataset/dataset_product_handler.h b/src-core/handler/dataset/dataset_product_handler.h similarity index 100% rename from src-core/products2/handler/dataset/dataset_product_handler.h rename to src-core/handler/dataset/dataset_product_handler.h diff --git a/src-core/products2/handler/dataset/flowgraph/datasetproc_node.h b/src-core/handler/dataset/flowgraph/datasetproc_node.h similarity index 100% rename from src-core/products2/handler/dataset/flowgraph/datasetproc_node.h rename to src-core/handler/dataset/flowgraph/datasetproc_node.h diff --git a/src-core/products2/handler/dataset/flowgraph/flowgraph.cpp b/src-core/handler/dataset/flowgraph/flowgraph.cpp similarity index 100% rename from src-core/products2/handler/dataset/flowgraph/flowgraph.cpp rename to src-core/handler/dataset/flowgraph/flowgraph.cpp diff --git a/src-core/products2/handler/dataset/flowgraph/flowgraph.h b/src-core/handler/dataset/flowgraph/flowgraph.h similarity index 100% rename from src-core/products2/handler/dataset/flowgraph/flowgraph.h rename to src-core/handler/dataset/flowgraph/flowgraph.h diff --git a/src-core/products2/handler/dataset/flowgraph/image_nodes.h b/src-core/handler/dataset/flowgraph/image_nodes.h similarity index 100% rename from src-core/products2/handler/dataset/flowgraph/image_nodes.h rename to src-core/handler/dataset/flowgraph/image_nodes.h diff --git a/src-core/products2/handler/dataset/flowgraph/imageproduct_node.h b/src-core/handler/dataset/flowgraph/imageproduct_node.h similarity index 100% rename from src-core/products2/handler/dataset/flowgraph/imageproduct_node.h rename to src-core/handler/dataset/flowgraph/imageproduct_node.h diff --git a/src-core/products2/handler/dataset/flowgraph_processor.cpp b/src-core/handler/dataset/flowgraph_processor.cpp similarity index 100% rename from src-core/products2/handler/dataset/flowgraph_processor.cpp rename to src-core/handler/dataset/flowgraph_processor.cpp diff --git a/src-core/products2/handler/dataset/flowgraph_processor.h b/src-core/handler/dataset/flowgraph_processor.h similarity index 100% rename from src-core/products2/handler/dataset/flowgraph_processor.h rename to src-core/handler/dataset/flowgraph_processor.h diff --git a/src-core/products2/handler/dataset/lua/geodetic.cpp b/src-core/handler/dataset/lua/geodetic.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/geodetic.cpp rename to src-core/handler/dataset/lua/geodetic.cpp diff --git a/src-core/products2/handler/dataset/lua/handler_image.cpp b/src-core/handler/dataset/lua/handler_image.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/handler_image.cpp rename to src-core/handler/dataset/lua/handler_image.cpp diff --git a/src-core/products2/handler/dataset/lua/image.cpp b/src-core/handler/dataset/lua/image.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/image.cpp rename to src-core/handler/dataset/lua/image.cpp diff --git a/src-core/products2/handler/dataset/lua/logger.cpp b/src-core/handler/dataset/lua/logger.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/logger.cpp rename to src-core/handler/dataset/lua/logger.cpp diff --git a/src-core/products2/handler/dataset/lua/lua_bind.h b/src-core/handler/dataset/lua/lua_bind.h similarity index 100% rename from src-core/products2/handler/dataset/lua/lua_bind.h rename to src-core/handler/dataset/lua/lua_bind.h diff --git a/src-core/products2/handler/dataset/lua/product.cpp b/src-core/handler/dataset/lua/product.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/product.cpp rename to src-core/handler/dataset/lua/product.cpp diff --git a/src-core/products2/handler/dataset/lua/product_image.cpp b/src-core/handler/dataset/lua/product_image.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/product_image.cpp rename to src-core/handler/dataset/lua/product_image.cpp diff --git a/src-core/products2/handler/dataset/lua/projection.cpp b/src-core/handler/dataset/lua/projection.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua/projection.cpp rename to src-core/handler/dataset/lua/projection.cpp diff --git a/src-core/products2/handler/dataset/lua_processor.cpp b/src-core/handler/dataset/lua_processor.cpp similarity index 100% rename from src-core/products2/handler/dataset/lua_processor.cpp rename to src-core/handler/dataset/lua_processor.cpp diff --git a/src-core/products2/handler/dataset/lua_processor.h b/src-core/handler/dataset/lua_processor.h similarity index 100% rename from src-core/products2/handler/dataset/lua_processor.h rename to src-core/handler/dataset/lua_processor.h diff --git a/src-core/products2/handler/dataset/processor.h b/src-core/handler/dataset/processor.h similarity index 100% rename from src-core/products2/handler/dataset/processor.h rename to src-core/handler/dataset/processor.h diff --git a/src-core/products2/handler/dummy_handler.h b/src-core/handler/dummy_handler.h similarity index 100% rename from src-core/products2/handler/dummy_handler.h rename to src-core/handler/dummy_handler.h diff --git a/src-core/products2/handler/handler.cpp b/src-core/handler/handler.cpp similarity index 100% rename from src-core/products2/handler/handler.cpp rename to src-core/handler/handler.cpp diff --git a/src-core/products2/handler/handler.h b/src-core/handler/handler.h similarity index 100% rename from src-core/products2/handler/handler.h rename to src-core/handler/handler.h diff --git a/src-core/products2/handler/image/image_handler.cpp b/src-core/handler/image/image_handler.cpp similarity index 99% rename from src-core/products2/handler/image/image_handler.cpp rename to src-core/handler/image/image_handler.cpp index 0033e4cd7..97b61bcfa 100644 --- a/src-core/products2/handler/image/image_handler.cpp +++ b/src-core/handler/image/image_handler.cpp @@ -16,7 +16,7 @@ #include "core/config.h" // TODOREWORK! -#include "products2/handler/vector/shapefile_handler.h" +#include "handler/vector/shapefile_handler.h" #include "resources.h" namespace satdump diff --git a/src-core/products2/handler/image/image_handler.h b/src-core/handler/image/image_handler.h similarity index 100% rename from src-core/products2/handler/image/image_handler.h rename to src-core/handler/image/image_handler.h diff --git a/src-core/products2/handler/processing_handler.h b/src-core/handler/processing_handler.h similarity index 100% rename from src-core/products2/handler/processing_handler.h rename to src-core/handler/processing_handler.h diff --git a/src-core/products2/handler/product/image_product_handler.cpp b/src-core/handler/product/image_product_handler.cpp similarity index 100% rename from src-core/products2/handler/product/image_product_handler.cpp rename to src-core/handler/product/image_product_handler.cpp diff --git a/src-core/products2/handler/product/image_product_handler.h b/src-core/handler/product/image_product_handler.h similarity index 100% rename from src-core/products2/handler/product/image_product_handler.h rename to src-core/handler/product/image_product_handler.h diff --git a/src-core/products2/handler/product/product_handler.cpp b/src-core/handler/product/product_handler.cpp similarity index 100% rename from src-core/products2/handler/product/product_handler.cpp rename to src-core/handler/product/product_handler.cpp diff --git a/src-core/products2/handler/product/product_handler.h b/src-core/handler/product/product_handler.h similarity index 100% rename from src-core/products2/handler/product/product_handler.h rename to src-core/handler/product/product_handler.h diff --git a/src-core/handler/product/punctiform_product_handler.cpp b/src-core/handler/product/punctiform_product_handler.cpp new file mode 100644 index 000000000..40b77632b --- /dev/null +++ b/src-core/handler/product/punctiform_product_handler.cpp @@ -0,0 +1,168 @@ +#include "punctiform_product_handler.h" +#include "nlohmann/json_utils.h" + +#include "imgui/imgui_stdlib.h" +#include "imgui/implot/implot.h" // TODOREWORK + +#include "products2/punctiform/product_dotmap.h" + +namespace satdump +{ + namespace viewer + { + PunctiformProductHandler::PunctiformProductHandler(std::shared_ptr p) + : ProductHandler(p) + { + handler_tree_icon = "\uf71e"; + + product = (products::PunctiformProduct *)ProductHandler::product.get(); + if (product->data.size()) + selected_channel = product->data[0].channel_name; + + tryApplyDefaultPreset(); + } + + PunctiformProductHandler::~PunctiformProductHandler() + { + } + + void PunctiformProductHandler::drawMenu() + { + bool needs_to_be_disabled = is_processing; + bool needs_to_update = false; + + if (ImGui::CollapsingHeader("Channels", ImGuiTreeNodeFlags_DefaultOpen)) + { + if (needs_to_be_disabled) + style::beginDisabled(); + + if (ImGui::RadioButton("Graph", current_mode == MODE_GRAPH)) + { + current_mode = MODE_GRAPH; + needs_to_update = true; + } + ImGui::SameLine(); + if (ImGui::RadioButton("Dot Map", current_mode == MODE_DOTMAP)) + { + current_mode = MODE_DOTMAP; + needs_to_update = true; + } + + if (ImGui::BeginCombo("Channel##selectechannel", selected_channel.c_str())) + { + for (auto &p : product->data) + { + if (ImGui::Selectable(p.channel_name.c_str(), selected_channel == p.channel_name)) + { + selected_channel = p.channel_name; + needs_to_update = true; + } + } + ImGui::EndCombo(); + } + + if (current_mode == MODE_DOTMAP) + { + ImGui::InputDouble("Range Min", &range_min); + ImGui::InputDouble("Range Max", &range_max); + } + + needs_to_update |= ImGui::Button("Update"); + + if (needs_to_be_disabled) + style::endDisabled(); + } + + needs_to_update |= renderPresetMenu(); // TODOREWORK move in top drawMenu? + + if (current_mode == MODE_DOTMAP) + img_handler.drawMenu(); + + if (needs_to_update) + asyncProcess(); + } + + void PunctiformProductHandler::setConfig(nlohmann::json p) + { + if (p.contains("mode")) + { + if (p["mode"] == "graph") + current_mode = MODE_GRAPH; + else if (p["mode"] == "dotmap") + current_mode = MODE_DOTMAP; + } + + selected_channel = getValueOrDefault(p["selected_channel"], selected_channel); + range_min = getValueOrDefault(p["range_min"], range_min); + range_max = getValueOrDefault(p["range_max"], range_max); + } + + nlohmann::json PunctiformProductHandler::getConfig() + { + nlohmann::json p; + + if (current_mode == MODE_GRAPH) + p["mode"] = "graph"; + else if (current_mode == MODE_DOTMAP) + p["mode"] = "dotmap"; + + p["channel"] = selected_channel; + p["range_min"] = range_min = 0; + p["range_max"] = range_max = 10; + + return p; + } + + void PunctiformProductHandler::do_process() + { + try + { + if (current_mode == MODE_DOTMAP) + { + auto img = products::generate_dotmap_product_image(product, selected_channel, -1, -1, 5, true, true, range_min, range_max); + img_handler.updateImage(img); + } + } + catch (std::exception &e) + { + logger->error("Could not process punctiform data! %s", e.what()); + } + } + + void PunctiformProductHandler::saveResult(std::string directory) + { + // TODOREWORK + } + + void PunctiformProductHandler::drawMenuBar() + { + if (current_mode == MODE_DOTMAP) + img_handler.drawMenuBar(); + } + + void PunctiformProductHandler::drawContents(ImVec2 win_size) + { + if (current_mode == MODE_GRAPH) + { + ImPlot::BeginPlot("PlotHandler", {win_size.x, win_size.y}); + + for (auto &ch : product->data) + { + if (ch.channel_name == selected_channel) + { + std::vector t; + for (int i = 0; i < ch.timestamps.size(); i++) + t.push_back(i); + ImPlot::PlotLine(std::string("Channel " + ch.channel_name).c_str(), t.data(), ch.data.data(), ch.timestamps.size()); + } + } + + ImPlot::EndPlot(); + } + else if (current_mode == MODE_DOTMAP) + { + img_handler.drawContents(win_size); + } + } + } +} diff --git a/src-core/products2/handler/product/punctiform_product_handler.h b/src-core/handler/product/punctiform_product_handler.h similarity index 51% rename from src-core/products2/handler/product/punctiform_product_handler.h rename to src-core/handler/product/punctiform_product_handler.h index b56c084f6..27c79abf8 100644 --- a/src-core/products2/handler/product/punctiform_product_handler.h +++ b/src-core/handler/product/punctiform_product_handler.h @@ -7,6 +7,8 @@ #include "imgui/imgui_stdlib.h" #include "core/style.h" +#include "handler/image/image_handler.h" + namespace satdump { namespace viewer @@ -20,6 +22,21 @@ namespace satdump // Products products::PunctiformProduct *product; + // Needed for local stuff + enum current_mode_t + { + MODE_GRAPH, + MODE_DOTMAP, + }; + + current_mode_t current_mode = MODE_GRAPH; + + ImageHandler img_handler; + + std::string selected_channel; + double range_min = 0; + double range_max = 10; + // Proc function void do_process(); @@ -28,6 +45,22 @@ namespace satdump void drawContents(ImVec2 win_size); void drawMenuBar(); + // + void addSubHandler(std::shared_ptr handler) + { + img_handler.addSubHandler(handler); + } + + void delSubHandler(std::shared_ptr handler, bool now = false) + { + img_handler.delSubHandler(handler, true); + } + + void drawTreeMenu(std::shared_ptr &h) + { + img_handler.drawTreeMenu(h); + } + void setConfig(nlohmann::json p); nlohmann::json getConfig(); diff --git a/src-core/products2/handler/projection/proj_ui.h b/src-core/handler/projection/proj_ui.h similarity index 100% rename from src-core/products2/handler/projection/proj_ui.h rename to src-core/handler/projection/proj_ui.h diff --git a/src-core/products2/handler/projection/projection_handler.cpp b/src-core/handler/projection/projection_handler.cpp similarity index 100% rename from src-core/products2/handler/projection/projection_handler.cpp rename to src-core/handler/projection/projection_handler.cpp diff --git a/src-core/products2/handler/projection/projection_handler.h b/src-core/handler/projection/projection_handler.h similarity index 100% rename from src-core/products2/handler/projection/projection_handler.h rename to src-core/handler/projection/projection_handler.h diff --git a/src-core/products2/handler/trash/trash_handler.cpp b/src-core/handler/trash/trash_handler.cpp similarity index 100% rename from src-core/products2/handler/trash/trash_handler.cpp rename to src-core/handler/trash/trash_handler.cpp diff --git a/src-core/products2/handler/trash/trash_handler.h b/src-core/handler/trash/trash_handler.h similarity index 100% rename from src-core/products2/handler/trash/trash_handler.h rename to src-core/handler/trash/trash_handler.h diff --git a/src-core/products2/handler/tree.h b/src-core/handler/tree.h similarity index 100% rename from src-core/products2/handler/tree.h rename to src-core/handler/tree.h diff --git a/src-core/products2/handler/vector/dbf_file/dbf_file.cpp b/src-core/handler/vector/dbf_file/dbf_file.cpp similarity index 100% rename from src-core/products2/handler/vector/dbf_file/dbf_file.cpp rename to src-core/handler/vector/dbf_file/dbf_file.cpp diff --git a/src-core/products2/handler/vector/dbf_file/dbf_file.h b/src-core/handler/vector/dbf_file/dbf_file.h similarity index 100% rename from src-core/products2/handler/vector/dbf_file/dbf_file.h rename to src-core/handler/vector/dbf_file/dbf_file.h diff --git a/src-core/products2/handler/vector/shapefile_handler.cpp b/src-core/handler/vector/shapefile_handler.cpp similarity index 100% rename from src-core/products2/handler/vector/shapefile_handler.cpp rename to src-core/handler/vector/shapefile_handler.cpp diff --git a/src-core/products2/handler/vector/shapefile_handler.h b/src-core/handler/vector/shapefile_handler.h similarity index 100% rename from src-core/products2/handler/vector/shapefile_handler.h rename to src-core/handler/vector/shapefile_handler.h diff --git a/src-core/products2/handler/product/punctiform_product_handler.cpp b/src-core/products2/handler/product/punctiform_product_handler.cpp deleted file mode 100644 index 9a1e2d556..000000000 --- a/src-core/products2/handler/product/punctiform_product_handler.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "punctiform_product_handler.h" -#include "nlohmann/json_utils.h" - -#include "imgui/imgui_stdlib.h" -#include "imgui/implot/implot.h" // TODOREWORK - -namespace satdump -{ - namespace viewer - { - PunctiformProductHandler::PunctiformProductHandler(std::shared_ptr p) - : ProductHandler(p) - { - handler_tree_icon = "\uf71e"; - - product = (products::PunctiformProduct *)ProductHandler::product.get(); - - tryApplyDefaultPreset(); - } - - PunctiformProductHandler::~PunctiformProductHandler() - { - } - - void PunctiformProductHandler::drawMenu() - { - bool needs_to_be_disabled = is_processing; - - if (ImGui::CollapsingHeader("Channels", ImGuiTreeNodeFlags_DefaultOpen)) - { - if (needs_to_be_disabled) - style::beginDisabled(); - - if (needs_to_be_disabled) - style::endDisabled(); - } - - // needs_to_update |= renderPresetMenu(); // TODOREWORK move in top drawMenu? - } - - void PunctiformProductHandler::setConfig(nlohmann::json p) - { - // if (p.contains("expression")) - // { - // expression = p["expression"]; - // channel_selection_curr_id = -1; - // } - } - - nlohmann::json PunctiformProductHandler::getConfig() - { - nlohmann::json p; - - // p["channel_calibrated"] = channel_calibrated; - - return p; - } - - void PunctiformProductHandler::do_process() - { - try - { - } - catch (std::exception &e) - { - logger->error("Could not process image! %s", e.what()); - } - } - - void PunctiformProductHandler::saveResult(std::string directory) - { - // TODOREWORK - } - - void PunctiformProductHandler::drawMenuBar() - { - } - - void PunctiformProductHandler::drawContents(ImVec2 win_size) - { - ImPlot::BeginPlot("PlotHandler"); - - if (product->data.size() >= 3) - { - logger->info(product->data[0].timestamps.size()); - std::vector t; - for (int i = 0; i < product->data[0].timestamps.size(); i++) - t.push_back(i); - ImPlot::PlotLine("Data 1", t.data(), product->data[0].data.data(), product->data[0].timestamps.size()); - } - - ImPlot::EndPlot(); - } - } -} diff --git a/src-core/products2/product.cpp b/src-core/products2/product.cpp index d09f55f10..92dbd4245 100644 --- a/src-core/products2/product.cpp +++ b/src-core/products2/product.cpp @@ -7,6 +7,7 @@ #include "core/exception.h" #include "image_product.h" +#include "punctiform_product.h" namespace satdump { @@ -79,9 +80,11 @@ namespace satdump void registerProducts() { - product_loaders.clear(); + product_loaders.clear(); /*TODOREWORK*/ product_loaders.emplace("image", RegisteredProduct{PRODUCT_LOADER_FUN(ImageProduct), /*process_image_products*/ [](satdump::products::Product *, std::string) { logger->error("NO PROCESSOR!"); }}); + product_loaders.emplace("punctiform", RegisteredProduct{PRODUCT_LOADER_FUN(PunctiformProduct), /*process_image_products*/ [](satdump::products::Product *, std::string) + { logger->error("NO PROCESSOR!"); }}); // products_loaders.emplace("radiation", RegisteredProducts{PRODUCTS_LOADER_FUN(RadiationProducts), process_radiation_products}); // products_loaders.emplace("scatterometer", RegisteredProducts{PRODUCTS_LOADER_FUN(ScatterometerProducts), process_scatterometer_products}); diff --git a/src-core/products2/product_process.cpp b/src-core/products2/product_process.cpp index 47ea4698e..d314c9755 100644 --- a/src-core/products2/product_process.cpp +++ b/src-core/products2/product_process.cpp @@ -2,6 +2,7 @@ #include "handler/product/product_handler.h" #include "handler/product/image_product_handler.h" +#include "handler/product/punctiform_product_handler.h" namespace satdump { @@ -12,6 +13,10 @@ namespace satdump std::shared_ptr handler; if (p->type == "image") handler = std::make_shared(p); + else if (p->type == "punctiform") + handler = std::make_shared(p); + else + logger->critical("TODOREWORK"); auto instr_cfg = handler->getInstrumentCfg(); if (instr_cfg.contains("presets")) diff --git a/src-core/products2/punctiform/product_dotmap.cpp b/src-core/products2/punctiform/product_dotmap.cpp new file mode 100644 index 000000000..526c3a09e --- /dev/null +++ b/src-core/products2/punctiform/product_dotmap.cpp @@ -0,0 +1,91 @@ +#include "product_dotmap.h" +#include "common/image/io.h" +#include "common/image/meta.h" + +namespace satdump +{ + namespace products + { + image::Image generate_dotmap_product_image(PunctiformProduct *product, std::string channel, int width, int height, + int dotsize, bool background, bool use_lut, + double min, double max, float *progress) + { + image::Image map; + + if (background) + { + image::load_jpeg(map, "resources/maps/nasa.jpg"); + if (width != -1 && height != -1) + map.resize_bilinear(width, height); + } + else + { + map.init(8, width, height, 3); + } + + int chid = product->getChannelIndexByName(channel); + auto &ch = product->getChannelByName(channel); + int img_x = map.width(); + int img_y = map.height(); + + for (int i = 0; i < ch.data.size(); i++) + { + auto satpos = product->get_sample_position(chid, i); // tracker.get_sat_position_at(v.timestamps[i]); + + int image_y = img_y - ((90.0f + satpos.lat) / 180.0f) * img_y; + int image_x = (satpos.lon / 360) * img_x + (img_x / 2); + + image_y = image_y % img_y; + image_x = image_x % img_x; + + // printf("%d %d %f\n", image_x, image_y, ch.data[i]); + + // std::vector color = {(p.data[2].data[i].value - 12500) / 1000.0, + // (p.data[1].data[i].value - 12600) / 1000.0, + // (p.data[0].data[i].value - 12500) / 1000.0, + // 1}; + + // std::vector color = {(p.data[1].data[i].value - 12200) / 1000.0, + // (p.data[1].data[i].value - 12200) / 1000.0, + // (p.data[1].data[i].value - 12200) / 1000.0, + // 1}; + + // std::vector color = {(p.data[1].data[i].value - 13400) / 1000.0, + // (p.data[1].data[i].value - 13400) / 1000.0, + // (p.data[1].data[i].value - 13400) / 1000.0, + // 1}; + + std::vector color = {(ch.data[i] - min) / (max - min), + (ch.data[i] - min) / (max - min), + (ch.data[i] - min) / (max - min), + 1}; + + for (auto &c : color) + { + if (c > 1.0) + c = 1; + if (c < 0) + c = 0; + } + + map.draw_circle(image_x % img_x, image_y, 2, color, true); + } + + nlohmann::json proj_cfg; + double tl_lon = -180; + double tl_lat = 90; + double br_lon = 180; + double br_lat = -90; + 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.width()); + proj_cfg["scalar_y"] = (br_lat - tl_lat) / double(map.height()); + proj_cfg["width"] = map.width(); + proj_cfg["height"] = map.height(); + image::set_metadata_proj_cfg(map, proj_cfg); + + return map; + } + } +} \ No newline at end of file diff --git a/src-core/products2/punctiform/product_dotmap.h b/src-core/products2/punctiform/product_dotmap.h new file mode 100644 index 000000000..92443cb3e --- /dev/null +++ b/src-core/products2/punctiform/product_dotmap.h @@ -0,0 +1,22 @@ +#pragma once + +/** + * @file product_dotmap.h + */ + +#include "common/image/image.h" +#include "products2/punctiform_product.h" + +namespace satdump +{ + namespace products + { + /** + TODOREWORK Make this generic, and extract points & projection from product + */ + image::Image generate_dotmap_product_image(PunctiformProduct *product, std::string channel, + int width, int height, int dotsize, bool background, bool use_lut, + double min, double max, + float *progress = nullptr); + } +} \ No newline at end of file diff --git a/src-core/products2/punctiform_product.cpp b/src-core/products2/punctiform_product.cpp new file mode 100644 index 000000000..470b24fea --- /dev/null +++ b/src-core/products2/punctiform_product.cpp @@ -0,0 +1,58 @@ +#include "punctiform_product.h" +#include "logger.h" +#include "common/tracking/tracking.h" +#include "core/exception.h" + +namespace satdump +{ + namespace products + { + void PunctiformProduct::save(std::string directory) + { + type = "punctiform"; // TODOREWORK rename all this... + + contents["data"] = data; + + Product::save(directory); + } + + void PunctiformProduct::load(std::string file) + { + Product::load(file); + std::string directory = std::filesystem::path(file).parent_path().string(); + + data = contents["data"]; + } + + geodetic::geodetic_coords_t PunctiformProduct::get_sample_position(int ch_index, int sample_index) + { + if (ch_index >= data.size()) + throw satdump_exception("Invalid channel " + std::to_string(ch_index) + "!"); + + auto &ch = data[ch_index]; + + if (sample_index > ch.data.size()) + throw satdump_exception("Invalid sample index " + std::to_string(sample_index) + " our of " + std::to_string(ch.data.size()) + "!"); + + if (ch.positions.size() > 0) + { + return ch.positions[sample_index]; + } + else if (has_tle()) /*TODOREWORK EPHEM vs TLE must be generic */ + { + if (satellite_tracker == nullptr) + satellite_tracker = new satdump::SatelliteTracker((satdump::TLE)get_tle()); + satdump::SatelliteTracker *t = (satdump::SatelliteTracker *)satellite_tracker; + return t->get_sat_position_at(ch.timestamps[sample_index]); + } + + throw satdump_exception("No positions or TLE/Ephem to provide sample position!"); + } + + PunctiformProduct::~PunctiformProduct() + { + if (satellite_tracker != nullptr) + delete (satdump::SatelliteTracker *)satellite_tracker; + } + } +} \ No newline at end of file diff --git a/src-core/products2/punctiform_product.h b/src-core/products2/punctiform_product.h new file mode 100644 index 000000000..5f4ba9bc5 --- /dev/null +++ b/src-core/products2/punctiform_product.h @@ -0,0 +1,89 @@ +#pragma once + +/** + * @file radiation_product.h + */ + +#include "product.h" +#include "common/geodetic/json.h" +#include "core/exception.h" + +namespace satdump +{ + namespace products + { + /** + * TODOREWORK + */ + class PunctiformProduct : public Product + { + public: + /** + * @brief TODOREWORK + */ + struct DataHolder + { + // TODOREWORK int abs_index = -1; + std::string channel_name; + std::vector timestamps; + std::vector positions; + std::vector data; + + // std::string calibration_type = ""; /* TODOREWORK */ + NLOHMANN_DEFINE_TYPE_INTRUSIVE(DataHolder, /*abs_index,*/ channel_name, timestamps, positions, data) + }; + + std::vector data; + + public: + // TODOREWORK + void set_tle(nlohmann::json tle) + { + contents["tle"] = tle; + } + + bool has_tle() + { + return contents.contains("tle"); + } + + nlohmann::json get_tle() + { + return contents["tle"]; + } + + private: + void *satellite_tracker = nullptr; + + public: + // TODOREWORK + geodetic::geodetic_coords_t get_sample_position(int ch_index, int sample_index); + + int getChannelIndexByName(std::string name) + { + for (int i = 0; i < data.size(); i++) + if (data[i].channel_name == name) + return i; + throw satdump_exception("Invalid channel for punctiform product : " + name); + } + + DataHolder &getChannelByName(std::string name) + { + for (auto &d : data) + if (d.channel_name == name) + return d; + throw satdump_exception("Invalid channel for punctiform product : " + name); + } + + public: + // TODOREWORK + + public: + virtual void + save(std::string directory); + virtual void load(std::string file); + + virtual ~PunctiformProduct(); + }; + } +} \ No newline at end of file diff --git a/src-interface/viewer2/dsp/dsp_flowgraph_handler.h b/src-interface/viewer2/dsp/dsp_flowgraph_handler.h index 29a8025ef..caa948ea7 100644 --- a/src-interface/viewer2/dsp/dsp_flowgraph_handler.h +++ b/src-interface/viewer2/dsp/dsp_flowgraph_handler.h @@ -1,6 +1,6 @@ #pragma once -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "flowgraph/flowgraph.h" // TODOREWORK, move into plugin? Or Core? diff --git a/src-interface/viewer2/dsp/newrec.h b/src-interface/viewer2/dsp/newrec.h index b64393068..f1353c579 100644 --- a/src-interface/viewer2/dsp/newrec.h +++ b/src-interface/viewer2/dsp/newrec.h @@ -1,6 +1,6 @@ #pragma once -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "dsp/device/dev.h" #include "dsp/device/options_displayer.h" #include "dsp/fft/fft_pan.h" diff --git a/src-interface/viewer2/dsp/waterfall_test.h b/src-interface/viewer2/dsp/waterfall_test.h index c84e9bc69..5b659607b 100644 --- a/src-interface/viewer2/dsp/waterfall_test.h +++ b/src-interface/viewer2/dsp/waterfall_test.h @@ -1,6 +1,6 @@ #pragma once -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "common/widgets/fft_plot.h" #include "common/widgets/waterfall_plot.h" diff --git a/src-interface/viewer2/tools_todoreworkmove/lut_generator.h b/src-interface/viewer2/tools_todoreworkmove/lut_generator.h index 1bf8572de..cb573c890 100644 --- a/src-interface/viewer2/tools_todoreworkmove/lut_generator.h +++ b/src-interface/viewer2/tools_todoreworkmove/lut_generator.h @@ -1,6 +1,6 @@ #pragma once -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "common/widgets/fft_plot.h" #include "common/widgets/waterfall_plot.h" diff --git a/src-interface/viewer2/viewer.cpp b/src-interface/viewer2/viewer.cpp index 7b5ae783b..4378f1d23 100644 --- a/src-interface/viewer2/viewer.cpp +++ b/src-interface/viewer2/viewer.cpp @@ -8,12 +8,12 @@ // #include "resources.h" #include "main_ui.h" -#include "products2/handler/dummy_handler.h" -#include "products2/handler/product/image_product_handler.h" // TODOREWORK CLEAN -#include "products2/handler/dataset/dataset_handler.h" -#include "products2/handler/vector/shapefile_handler.h" -#include "products2/handler/projection/projection_handler.h" -#include "products2/handler/trash/trash_handler.h" +#include "handler/dummy_handler.h" +#include "handler/product/image_product_handler.h" // TODOREWORK CLEAN +#include "handler/dataset/dataset_handler.h" +#include "handler/vector/shapefile_handler.h" +#include "handler/projection/projection_handler.h" +#include "handler/trash/trash_handler.h" #include "dsp/dsp_flowgraph_handler.h" // TODOREWORK #include "resources.h" diff --git a/src-interface/viewer2/viewer.h b/src-interface/viewer2/viewer.h index 3b88412d8..ff84e4340 100644 --- a/src-interface/viewer2/viewer.h +++ b/src-interface/viewer2/viewer.h @@ -4,7 +4,7 @@ #include "imgui/imgui.h" #include -#include "products2/handler/handler.h" +#include "handler/handler.h" #include "imgui/pfd/portable-file-dialogs.h" diff --git a/src-testing/CMakeLists.txt b/src-testing/CMakeLists.txt index cf015de20..8b907c7a8 100755 --- a/src-testing/CMakeLists.txt +++ b/src-testing/CMakeLists.txt @@ -1,9 +1,9 @@ cmake_minimum_required(VERSION 3.12) -file(GLOB_RECURSE SatDump_testing_CPPS main.cpp) +file(GLOB_RECURSE SatDump_testing_CPPS main.cpp angelscript/*.cpp aatc/*.cpp) add_executable(satdump-testing ${SatDump_testing_CPPS}) target_include_directories(satdump-testing PUBLIC src-testing src-core) # Link against the core -target_link_libraries(satdump-testing PUBLIC satdump_core vulkan) # mfhdf) \ No newline at end of file +target_link_libraries(satdump-testing PUBLIC satdump_core vulkan) # mfhdf) diff --git a/src-testing/main.cpp b/src-testing/main.cpp index 7d282717a..6f99c0033 100644 --- a/src-testing/main.cpp +++ b/src-testing/main.cpp @@ -11,211 +11,114 @@ **********************************************************************/ #include "logger.h" +#include "products2/punctiform_product.h" -#if 1 - -#include "dsp/device/dev.h" -#include "init.h" +#include "common/image/io.h" int main(int argc, char *argv[]) { initLogger(); - /* logger->set_level(slog::LOG_ERROR); - satdump::initSatdump(); - completeLoggerInit(); - logger->set_level(slog::LOG_TRACE); - - auto devs = satdump::ndsp::getDeviceList(); - - for (auto &d : devs) - { - auto i = satdump::ndsp::getDeviceInstanceFromInfo(d); - d.params = i->get_cfg_list(); - logger->debug("\n" + nlohmann::json(d).dump(4)); - }*/ -} +#if 0 #else -#include -#include -#include -#include -#include + satdump::products::PunctiformProduct p; + p.load("/tmp/jason3_test/AMR-2_RAD/product.cbor"); -#include "init.h" -#include "common/dsp_source_sink/dsp_sample_source.h" -#include "common/dsp/fft/fft_pan.h" + image::Image map; + image::load_jpeg(map, "resources/maps/nasa.jpg"); -#include + int channel = 1; + int img_x = 200; // map.width(); + int img_y = 100; // map.height(); -// HTTP Handler for stats -void http_handle(nng_aio *aio) -{ - std::string jsonstr = "{\"api\": true}"; - - nng_http_res *res; - nng_http_res_alloc(&res); - nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size()); - nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8"); - nng_aio_set_output(aio, 0, res); - nng_aio_finish(aio, 0); -} - -// HTTP Handler for commands -void http_handleP(nng_aio *aio) -{ - std::string jsonstr = "{\"api\": true}"; - - nng_http_req *msg = (nng_http_req *)nng_aio_get_input(aio, 0); - - void *ptr; - size_t ptrl; - nng_http_req_get_data(msg, &ptr, &ptrl); - logger->info("Got : %s", (char *)ptr); - - nng_http_res *res; - nng_http_res_alloc(&res); - nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size()); - nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8"); - nng_aio_set_output(aio, 0, res); - nng_aio_finish(aio, 0); -} - -int main(int argc, char *argv[]) -{ - initLogger(); - - logger->set_level(slog::LOG_OFF); - satdump::initSatdump(); - completeLoggerInit(); - logger->set_level(slog::LOG_TRACE); - - dsp::registerAllSources(); - - /////////////////////// - - std::shared_ptr dsp_source; + struct Pixel { - auto all_sources = dsp::getAllAvailableSources(); - for (auto &s : all_sources) - { - logger->trace(s.name); - if (s.source_type == "rtlsdr") - dsp_source = dsp::getSourceFromDescriptor(s); - } - } - - dsp_source->open(); - dsp_source->set_samplerate(2.4e6); - dsp_source->set_frequency(431.8e6); - dsp_source->start(); - - std::shared_ptr fftPan = std::make_shared(dsp_source->output_stream); - fftPan->set_fft_settings(65536, dsp_source->get_samplerate(), 30); - fftPan->avg_num = 1; - - //////////////////////// - - std::string http_server_url = "http://0.0.0.0:8080"; - - nng_http_server *http_server; - nng_url *url; - { - - nng_url_parse(&url, http_server_url.c_str()); - nng_http_server_hold(&http_server, url); - } - - nng_http_handler *handler_api; - { - nng_http_handler_alloc(&handler_api, "/api", http_handle); - nng_http_handler_set_method(handler_api, "GET"); - nng_http_server_add_handler(http_server, handler_api); - } - - nng_http_handler *handler_apiP; - { - nng_http_handler_alloc(&handler_api, "/apip", http_handleP); - nng_http_handler_set_method(handler_api, "POST"); - nng_http_server_add_handler(http_server, handler_api); - } - - nng_socket socket; - - { - - int rv = 0; - if (rv = nng_bus0_open(&socket); rv != 0) - { - printf("pair open error\n"); - } - - if (rv = nng_listen(socket, "ws://0.0.0.0:8080/ws", nullptr, 0); rv != 0) - { - printf("server listen error\n"); - } - } - - nng_url_free(url); - - nng_http_server_start(http_server); - - fftPan->on_fft = [&](float *b) - { - std::vector send_buf(8 + 65536); - float *min = (float *)&send_buf[0]; - float *max = (float *)&send_buf[4]; - uint8_t *dat = &send_buf[8]; - - *min = 1e6; - *max = -1e6; - - for (int i = 0; i < 65536; i++) - { - if (*min > b[i]) - *min = b[i]; - if (*max < b[i]) - *max = b[i]; - } - - for (int i = 0; i < 65536; i++) - { - float val = (b[i] - *min) / (*max - *min); - dat[i] = val * 255; - } - - nng_send(socket, send_buf.data(), send_buf.size(), NNG_FLAG_NONBLOCK); + int x; + int y; + std::vector color; }; - fftPan->start(); - while (1) + std::vector all_pixels; + + for (int i = 0; i < p.data[channel].data.size(); i++) { - /*char *buf = nullptr; - size_t size; + auto &v = p.data[0]; - int rv = 0; - printf("WAIT\n"); - if (rv = nng_recv(socket, &buf, &size, NNG_FLAG_ALLOC); rv != 0) + auto satpos = p.get_sample_position(0, i); // tracker.get_sat_position_at(v.timestamps[i]); + + int image_y = img_y - ((90.0f + satpos.lat) / 180.0f) * img_y; + int image_x = (satpos.lon / 360) * img_x + (img_x / 2); + + image_y = image_y % img_y; + image_x = image_x % img_x; + + printf("%d %d %f\n", image_x, image_y, v.data[i]); + + // std::vector color = {(p.data[2].data[i].value - 12500) / 1000.0, + // (p.data[1].data[i].value - 12600) / 1000.0, + // (p.data[0].data[i].value - 12500) / 1000.0, + // 1}; + + // std::vector color = {(p.data[1].data[i].value - 12200) / 1000.0, + // (p.data[1].data[i].value - 12200) / 1000.0, + // (p.data[1].data[i].value - 12200) / 1000.0, + // 1}; + + // std::vector color = {(p.data[1].data[i].value - 13400) / 1000.0, + // (p.data[1].data[i].value - 13400) / 1000.0, + // (p.data[1].data[i].value - 13400) / 1000.0, + // 1}; + + std::vector color = {(v.data[i] - 12500) / 1000.0, + (v.data[i] - 12500) / 1000.0, + (v.data[i] - 12500) / 1000.0, + 1}; + + for (auto &c : color) { - printf("recv error: %s\n", nng_strerror(rv)); + if (c > 1.0) + c = 1; + if (c < 0) + c = 0; } - printf("server get with client: %s\n", buf); + all_pixels.push_back({image_x, image_y, color}); - std::string testStr = "Server Reply!\n\r"; - // int rv = 0; - if (rv = nng_send(socket, (void *)testStr.c_str(), testStr.size(), NULL); rv != 0) - { - printf("send error: %s\n", nng_strerror(rv)); - } - - printf("SEND\n"); -*/ - sleep(1); + map.draw_circle(image_x % img_x, image_y, 2, color, true); } - nng_http_server_stop(http_server); - nng_http_server_release(http_server); -} -#endif \ No newline at end of file + image::save_jpeg(map, "/home/alan/Downloads/test_jason_new.jpg"); + map.fill(0); + + for (int x = 0; x < img_x; x++) + { + logger->info(x); + for (int y = 0; y < img_y; y++) + { + int best_id = 0; + float best_dist = 1e6; + for (int p = 0; p < all_pixels.size(); p++) + { + float dx = all_pixels[p].x - x; + float dy = all_pixels[p].y - y; + float dist = sqrtf(dx * dx + dy * dy); + if (dist < best_dist) + { + best_dist = dist; + best_id = p; + } + } + + if (best_dist < 5) + map.draw_pixel(x, y, all_pixels[best_id].color); + } + } + + map.crop(0, 0, 200, 100); + // map.resize_bilinear(2048, 1024); + map.resize(2048, 1024); + + image::save_jpeg(map, "/home/alan/Downloads/test_jason2_new.jpg"); +#endif +} \ No newline at end of file diff --git a/src-testing/main_SDR.cpp b/src-testing/main_SDR.cpp new file mode 100644 index 000000000..60e0807fa --- /dev/null +++ b/src-testing/main_SDR.cpp @@ -0,0 +1,221 @@ +/********************************************************************** + * This file is used for testing random stuff without running the + * whole of SatDump, which comes in handy for debugging individual + * elements before putting them all together in modules... + * + * If you are an user, ignore this file which will not be built by + * default, and if you're a developper in need of doing stuff here... + * Go ahead! + * + * Don't judge the code you might see in there! :) + **********************************************************************/ + +#include "logger.h" + +#if 1 + +#include "dsp/device/dev.h" +#include "init.h" + +int main(int argc, char *argv[]) +{ + initLogger(); + + logger->set_level(slog::LOG_ERROR); + satdump::initSatdump(); + completeLoggerInit(); + logger->set_level(slog::LOG_TRACE); + + auto devs = satdump::ndsp::getDeviceList(); + + for (auto &d : devs) + { + auto i = satdump::ndsp::getDeviceInstanceFromInfo(d); + d.params = i->get_cfg_list(); + logger->debug("\n" + nlohmann::json(d).dump(4)); + } +} + +#else +#include +#include +#include +#include +#include + +#include "init.h" +#include "common/dsp_source_sink/dsp_sample_source.h" +#include "common/dsp/fft/fft_pan.h" + +#include + +// HTTP Handler for stats +void http_handle(nng_aio *aio) +{ + std::string jsonstr = "{\"api\": true}"; + + nng_http_res *res; + nng_http_res_alloc(&res); + nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size()); + nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8"); + nng_aio_set_output(aio, 0, res); + nng_aio_finish(aio, 0); +} + +// HTTP Handler for commands +void http_handleP(nng_aio *aio) +{ + std::string jsonstr = "{\"api\": true}"; + + nng_http_req *msg = (nng_http_req *)nng_aio_get_input(aio, 0); + + void *ptr; + size_t ptrl; + nng_http_req_get_data(msg, &ptr, &ptrl); + logger->info("Got : %s", (char *)ptr); + + nng_http_res *res; + nng_http_res_alloc(&res); + nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size()); + nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8"); + nng_aio_set_output(aio, 0, res); + nng_aio_finish(aio, 0); +} + +int main(int argc, char *argv[]) +{ + initLogger(); + + logger->set_level(slog::LOG_OFF); + satdump::initSatdump(); + completeLoggerInit(); + logger->set_level(slog::LOG_TRACE); + + dsp::registerAllSources(); + + /////////////////////// + + std::shared_ptr dsp_source; + { + auto all_sources = dsp::getAllAvailableSources(); + for (auto &s : all_sources) + { + logger->trace(s.name); + if (s.source_type == "rtlsdr") + dsp_source = dsp::getSourceFromDescriptor(s); + } + } + + dsp_source->open(); + dsp_source->set_samplerate(2.4e6); + dsp_source->set_frequency(431.8e6); + dsp_source->start(); + + std::shared_ptr fftPan = std::make_shared(dsp_source->output_stream); + fftPan->set_fft_settings(65536, dsp_source->get_samplerate(), 30); + fftPan->avg_num = 1; + + //////////////////////// + + std::string http_server_url = "http://0.0.0.0:8080"; + + nng_http_server *http_server; + nng_url *url; + { + + nng_url_parse(&url, http_server_url.c_str()); + nng_http_server_hold(&http_server, url); + } + + nng_http_handler *handler_api; + { + nng_http_handler_alloc(&handler_api, "/api", http_handle); + nng_http_handler_set_method(handler_api, "GET"); + nng_http_server_add_handler(http_server, handler_api); + } + + nng_http_handler *handler_apiP; + { + nng_http_handler_alloc(&handler_api, "/apip", http_handleP); + nng_http_handler_set_method(handler_api, "POST"); + nng_http_server_add_handler(http_server, handler_api); + } + + nng_socket socket; + + { + + int rv = 0; + if (rv = nng_bus0_open(&socket); rv != 0) + { + printf("pair open error\n"); + } + + if (rv = nng_listen(socket, "ws://0.0.0.0:8080/ws", nullptr, 0); rv != 0) + { + printf("server listen error\n"); + } + } + + nng_url_free(url); + + nng_http_server_start(http_server); + + fftPan->on_fft = [&](float *b) + { + std::vector send_buf(8 + 65536); + float *min = (float *)&send_buf[0]; + float *max = (float *)&send_buf[4]; + uint8_t *dat = &send_buf[8]; + + *min = 1e6; + *max = -1e6; + + for (int i = 0; i < 65536; i++) + { + if (*min > b[i]) + *min = b[i]; + if (*max < b[i]) + *max = b[i]; + } + + for (int i = 0; i < 65536; i++) + { + float val = (b[i] - *min) / (*max - *min); + dat[i] = val * 255; + } + + nng_send(socket, send_buf.data(), send_buf.size(), NNG_FLAG_NONBLOCK); + }; + fftPan->start(); + + while (1) + { + /*char *buf = nullptr; + size_t size; + + int rv = 0; + printf("WAIT\n"); + if (rv = nng_recv(socket, &buf, &size, NNG_FLAG_ALLOC); rv != 0) + { + printf("recv error: %s\n", nng_strerror(rv)); + } + + printf("server get with client: %s\n", buf); + + std::string testStr = "Server Reply!\n\r"; + // int rv = 0; + if (rv = nng_send(socket, (void *)testStr.c_str(), testStr.size(), NULL); rv != 0) + { + printf("send error: %s\n", nng_strerror(rv)); + } + + printf("SEND\n"); +*/ + sleep(1); + } + + nng_http_server_stop(http_server); + nng_http_server_release(http_server); +} +#endif \ No newline at end of file diff --git a/src-testing/main_as.cpp b/src-testing/main_as.cpp new file mode 100644 index 000000000..5d587780b --- /dev/null +++ b/src-testing/main_as.cpp @@ -0,0 +1,259 @@ +/********************************************************************** + * This file is used for testing random stuff without running the + * whole of SatDump, which comes in handy for debugging individual + * elements before putting them all together in modules... + * + * If you are an user, ignore this file which will not be built by + * default, and if you're a developper in need of doing stuff here... + * Go ahead! + * + * Don't judge the code you might see in there! :) + **********************************************************************/ + +#include "logger.h" + +#if 0 +#include "angelscript/angelscript.h" +#include "angelscript/scriptstdstring/scriptstdstring.h" +#include "angelscript/scriptbuilder/scriptbuilder.h" + // #include "aatc/aatc.hpp" + // #include "aatc/aatc_container_vector.hpp" +#include "angelscript/scriptarray/scriptarray.h" +#include "angelscript/scriptjson/scriptjson.h" + + // Implement a simple message callback function + void MessageCallback(const asSMessageInfo *msg, void *param) + { + const char *type = "ERR "; + if (msg->type == asMSGTYPE_WARNING) + type = "WARN"; + else if (msg->type == asMSGTYPE_INFORMATION) + type = "INFO"; + // printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message); + + if (msg->type == asMSGTYPE_INFORMATION) + logger->info("[AngelScript] %s (%d, %d) : %s : %s", msg->section, msg->row, msg->col, type, msg->message); + else if (msg->type == asMSGTYPE_WARNING) + logger->warn("[AngelScript] %s (%d, %d) : %s : %s", msg->section, msg->row, msg->col, type, msg->message); + else if (msg->type == asMSGTYPE_ERROR) + logger->error("[AngelScript] %s (%d, %d) : %s : %s", msg->section, msg->row, msg->col, type, msg->message); + } + + // Print the script string to the standard output stream + void print(std::string &msg) + { + printf("%s", msg.c_str()); + } + +#define IMAGELIB_NOINLINE_SETGET 1 +#include "common/image/image.h" +#include "common/image/io.h" + + namespace img + { + void ImageConstructor(void *memory) + { + // Initialize the pre-allocated memory by calling the + // object constructor with the placement-new operator + new (memory) image::Image(); + } + + void ImageDestructor(void *memory) + { + logger->error("Destruct %d", ((image::Image *)memory)->size()); + // Uninitialize the memory by calling the object destructor + ((image::Image *)memory)->~Image(); + } + + /*void ImageSet(void *memory, int p, int v) + { + ((image::Image *)memory)->set(p, v); + } + + int ImageGet(void *memory, int p) + { + return ((image::Image *)memory)->get(p); + }*/ + + void ImageDrawCircle(void *memory, int x0, int y0, int radius, CScriptArray *color, bool fill) + { + ((image::Image *)memory)->draw_circle(x0, y0, radius, std::vector((double *)color->At(0), (double *)color->At(0) + color->GetSize()), fill); + } + } + + namespace namelog + { + void trace(std::string &f) { logger->trace("[AngelScript] " + f); } + void debug(std::string &f) { logger->debug("[AngelScript] " + f); } + void info(std::string &f) { logger->info("[AngelScript] " + f); } + void warn(std::string &f) { logger->warn("[AngelScript] " + f); } + void error(std::string &f) { logger->error("[AngelScript] " + f); } + void critical(std::string &f) { logger->critical("[AngelScript] " + f); } + } + +#include "nlohmann/json.hpp" + + namespace j + { + void JConstructor(void *memory) + { + // Initialize the pre-allocated memory by calling the + // object constructor with the placement-new operator + new (memory) nlohmann::basic_json<>(); + } + + void JDestructor(void *memory) + { + // Uninitialize the memory by calling the object destructor + ((nlohmann::basic_json<> *)memory)->~basic_json(); + } + } +#endif + +int main(int argc, char *argv[]) +{ +#if 0 + initLogger(); + + // Create the script engine + asIScriptEngine *engine = asCreateScriptEngine(); + + // Set the message callback to receive information on errors in human readable form. + int r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); + // assert(r >= 0); + + // AngelScript doesn't have a built-in string type, as there is no definite standard + // string type for C++ applications. Every developer is free to register its own string type. + // The SDK do however provide a standard add-on for registering a string type, so it's not + // necessary to implement the registration yourself if you don't want to. + RegisterStdString(engine); + RegisterScriptArray(engine, true); + RegisterScriptJson(engine); + // aatc::RegisterAllContainers(engine); + + // Register the function that we want the scripts to call + r = engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(print), asCALL_CDECL); + // assert(r >= 0); + + { + engine->SetDefaultNamespace("logger"); + r = engine->RegisterGlobalFunction("void trace(const string &in)", asFUNCTION(namelog::trace), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void debug(const string &in)", asFUNCTION(namelog::debug), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void info(const string &in)", asFUNCTION(namelog::info), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void warn(const string &in)", asFUNCTION(namelog::error), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void error(const string &in)", asFUNCTION(namelog::error), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void critical(const string &in)", asFUNCTION(namelog::critical), asCALL_CDECL); + } + + { + engine->SetDefaultNamespace("image"); + + r = engine->RegisterObjectType("Image", sizeof(image::Image), asOBJ_VALUE | asGetTypeTraits()); + // assert(r >= 0); + // Register the behaviours + r = engine->RegisterObjectBehaviour("Image", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(img::ImageConstructor), asCALL_CDECL_OBJLAST); + // assert(r >= 0); + r = engine->RegisterObjectBehaviour("Image", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(img::ImageDestructor), asCALL_CDECL_OBJLAST); + // assert(r >= 0); + + r = engine->RegisterObjectMethod("Image", "void init(int, uint64, uint64, int)", asMETHOD(image::Image, init), asCALL_THISCALL); // assert( r >= 0 ); + r = engine->RegisterObjectMethod("Image", "void fill(int)", asMETHOD(image::Image, fill), asCALL_THISCALL); // assert( r >= 0 ); + r = engine->RegisterObjectMethod("Image", "uint64 size()", asMETHOD(image::Image, size), asCALL_THISCALL); + + r = engine->RegisterObjectMethod("Image", "void set(uint64, int)", asMETHODPR(image::Image, set, (size_t, int), void), asCALL_THISCALL); + r = engine->RegisterObjectMethod("Image", "void set(uint64, uint64, int)", asMETHODPR(image::Image, set, (size_t, size_t, int), void), asCALL_THISCALL); + r = engine->RegisterObjectMethod("Image", "void set(uint64, uint64, uint64, int)", asMETHODPR(image::Image, set, (size_t, size_t, size_t, int), void), asCALL_THISCALL); + r = engine->RegisterObjectMethod("Image", "int get(uint64)", asMETHODPR(image::Image, get, (size_t), int), asCALL_THISCALL); + r = engine->RegisterObjectMethod("Image", "int get(uint64, uint64)", asMETHODPR(image::Image, get, (size_t, size_t), int), asCALL_THISCALL); + r = engine->RegisterObjectMethod("Image", "int get(uint64, uint64, uint64)", asMETHODPR(image::Image, get, (size_t, size_t, size_t), int), asCALL_THISCALL); + + r = engine->RegisterObjectMethod("Image", "void draw_circle(int, int, int, array@, bool)", asFUNCTION(img::ImageDrawCircle), asCALL_CDECL_OBJFIRST); + + engine->RegisterObjectMethod("Image", "Image &opAssign(const Image &in)", asMETHODPR(image::Image, operator=, (const image::Image &), image::Image &), asCALL_THISCALL); + + // r = engine->RegisterGlobalFunction("Image load_img(string)", asFUNCTIONPR(image::load_img, (std::string), image::Image), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void load_img(Image &out, string)", asFUNCTIONPR(image::load_img, (image::Image &, std::string), void), asCALL_CDECL); + r = engine->RegisterGlobalFunction("void save_img(const Image &in, string, bool)", asFUNCTIONPR(image::save_img, (image::Image &, std::string, bool), void), asCALL_CDECL); + } + + nlohmann::json test2j; + test2j["hello"] = true; + + { + + engine->RegisterGlobalProperty("json testj", &test2j); + // engine->SetDefaultNamespace("nlohmann"); + // r = engine->RegisterObjectType("json", sizeof(nlohmann::json), asOBJ_VALUE | asGetTypeTraits()); + + // r = engine->RegisterObjectBehaviour("json", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(j::JConstructor), asCALL_CDECL_OBJLAST); + // r = engine->RegisterObjectBehaviour("json", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(j::JDestructor), asCALL_CDECL_OBJLAST); + + // engine->RegisterObjectMethod("json", "json &opAssign(const json &in)", asMETHODPR(nlohmann::json, operator=, (const nlohmann::json &), nlohmann::json &), asCALL_THISCALL); + } + + //////////////////////////////////////// + + { + // The CScriptBuilder helper is an add-on that loads the file, + // performs a pre-processing pass if necessary, and then tells + // the engine to build a script module. + CScriptBuilder builder; + int r = builder.StartNewModule(engine, "MyModule"); + if (r < 0) + { + // If the code fails here it is usually because there + // is no more memory to allocate the module + printf("Unrecoverable error while starting a new module.\n"); + return 1; + } + r = builder.AddSectionFromFile("test.as"); + if (r < 0) + { + // The builder wasn't able to load the file. Maybe the file + // has been removed, or the wrong name was given, or some + // preprocessing commands are incorrectly written. + printf("Please correct the errors in the script and try again.\n"); + return 1; + } + r = builder.BuildModule(); + if (r < 0) + { + // An error occurred. Instruct the script writer to fix the + // compilation errors that were listed in the output stream. + printf("Please correct the errors in the script and try again.\n"); + return 1; + } + + ////////////// + + // Find the function that is to be called. + asIScriptModule *mod = engine->GetModule("MyModule"); + asIScriptFunction *func = mod->GetFunctionByDecl("void main()"); + if (func == 0) + { + // The function couldn't be found. Instruct the script writer + // to include the expected function in the script. + printf("The script must have the function 'void main()'. Please add it and try again.\n"); + return 1; + } + + // Create our context, prepare it, and then execute + asIScriptContext *ctx = engine->CreateContext(); + ctx->Prepare(func); + r = ctx->Execute(); + if (r != asEXECUTION_FINISHED) + { + // The execution didn't complete as expected. Determine what happened. + if (r == asEXECUTION_EXCEPTION) + { + // An exception occurred, let the script writer know what happened so it can be corrected. + printf("An exception '%s' occurred. Please correct the code and try again.\n", ctx->GetExceptionString()); + } + } + + // Clean up + ctx->Release(); + engine->ShutDownAndRelease(); + } +#endif +}