From 3ca782d43532f73ce24b4e2c8d233afff82cd6d3 Mon Sep 17 00:00:00 2001 From: Aang23 Date: Thu, 23 Jan 2025 15:57:53 +0100 Subject: [PATCH] WIP --- src-core/common/image/equation.cpp | 2 +- .../dataset/dataset_product_handler.cpp | 2 + .../handler/dataset/flowgraph/flowgraph.h | 29 +++++-- .../handler/dataset/flowgraph/image_nodes.h | 80 +++++++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) diff --git a/src-core/common/image/equation.cpp b/src-core/common/image/equation.cpp index 59e8ca4d7..34e32d6f3 100644 --- a/src-core/common/image/equation.cpp +++ b/src-core/common/image/equation.cpp @@ -32,7 +32,7 @@ namespace image { auto tkts = splitString(p.tkt, ','); for (int i = 0; i < p.img->channels(); i++) - equParser.DefineVar(tkts.size() == p.img->channels() ? tkts[i] : (p.tkt + "_" + std::to_string(i + 1)), &p.val[i]); + equParser.DefineVar(tkts.size() > i ? tkts[i] : (p.tkt + "_" + std::to_string(i + 1)), &p.val[i]); } if (p.img->depth() > depth) diff --git a/src-interface/viewer2/handler/dataset/dataset_product_handler.cpp b/src-interface/viewer2/handler/dataset/dataset_product_handler.cpp index f70ffddc7..ee1203530 100644 --- a/src-interface/viewer2/handler/dataset/dataset_product_handler.cpp +++ b/src-interface/viewer2/handler/dataset/dataset_product_handler.cpp @@ -31,6 +31,8 @@ namespace satdump { return std::make_shared(); }); flowgraph.node_internal_registry.emplace("image_reproj", []() { return std::make_shared(); }); + flowgraph.node_internal_registry.emplace("image_equation", []() + { return std::make_shared(); }); } DatasetProductHandler::~DatasetProductHandler() diff --git a/src-interface/viewer2/handler/dataset/flowgraph/flowgraph.h b/src-interface/viewer2/handler/dataset/flowgraph/flowgraph.h index 283ed4f6e..d0dd39f30 100644 --- a/src-interface/viewer2/handler/dataset/flowgraph/flowgraph.h +++ b/src-interface/viewer2/handler/dataset/flowgraph/flowgraph.h @@ -29,6 +29,15 @@ namespace satdump std::vector outputs; bool has_run; + public: + std::function add_io_callback; + + void addInputDynamic(InOutConfig cfg) + { + inputs.push_back(cfg); + add_io_callback(cfg, false); + } + public: void reset() { @@ -75,8 +84,8 @@ namespace satdump const std::shared_ptr internal; bool pos_was_set = false; - double pos_x = 0; - double pos_y = 0; + float pos_x = 0; + float pos_y = 0; struct InOut { @@ -97,15 +106,23 @@ namespace satdump node_io.push_back({f->getNewNodeIOID(&node_io), io.name, false}); for (auto &io : internal->outputs) node_io.push_back({f->getNewNodeIOID(&node_io), io.name, true}); + internal->add_io_callback = [this, f](NodeInternal::InOutConfig io, bool out) + { + node_io.push_back({f->getNewNodeIOID(&node_io), io.name, out}); + }; } - Node(nlohmann::json j, std::shared_ptr i) + Node(Flowgraph *f, nlohmann::json j, std::shared_ptr i) : id(j["id"]), internal_id(j["int_id"]), title(i->title), node_io(j["io"]), internal(i) { internal->from_json(j["int_cfg"]); + internal->add_io_callback = [this, f](NodeInternal::InOutConfig io, bool out) + { + node_io.push_back({f->getNewNodeIOID(&node_io), io.name, out}); + }; - pos_x = j.contains("pos_x") ? j["pos_x"].get() : 0; - pos_y = j.contains("pos_y") ? j["pos_y"].get() : 0; + pos_x = j.contains("pos_x") ? j["pos_x"].get() : 0; + pos_y = j.contains("pos_y") ? j["pos_y"].get() : 0; } nlohmann::json getJSON() @@ -168,7 +185,7 @@ namespace satdump for (auto &n : j["nodes"].items()) { auto i = node_internal_registry[n.value()["int_id"]](); - nodes.push_back(std::make_shared(n.value(), i)); + nodes.push_back(std::make_shared(this, n.value(), i)); } links = j["links"]; } diff --git a/src-interface/viewer2/handler/dataset/flowgraph/image_nodes.h b/src-interface/viewer2/handler/dataset/flowgraph/image_nodes.h index 5ffd3b85a..6cfc7d0a0 100644 --- a/src-interface/viewer2/handler/dataset/flowgraph/image_nodes.h +++ b/src-interface/viewer2/handler/dataset/flowgraph/image_nodes.h @@ -5,6 +5,7 @@ #include "common/image/meta.h" #include "projection/projection.h" #include "projection/reprojector.h" +#include "common/image/equation.h" namespace satdump { @@ -122,4 +123,83 @@ namespace satdump nlohmann::json to_json() { return {}; } void from_json(nlohmann::json j) {} }; + + class ImageEquation_Node : public NodeInternal + { + private: + struct ChConfig + { + std::string input_name; + std::string tokens; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(ChConfig, input_name, tokens); + }; + + std::vector channels; + std::string equation; + + public: + ImageEquation_Node() + : NodeInternal("Image Equation") + { + outputs.push_back({"Image"}); + } + + void process() + { + std::vector ch; + for (int i = 0; i < channels.size(); i++) + { + std::shared_ptr img = std::static_pointer_cast(inputs[i].ptr); + ch.push_back({channels[i].tokens, img.get()}); + logger->warn(channels[i].tokens); + } + + std::shared_ptr img_out = std::make_shared(); + *img_out = image::generate_image_equation(ch, equation); + outputs[0].ptr = img_out; + + has_run = true; + } + + void render() + { + ImGui::SetNextItemWidth(200 * ui_scale); + ImGui::InputTextMultiline("Equation", &equation); + + for (auto &c : channels) + { + ImGui::Separator(); + ImGui::Text(c.input_name.c_str()); + ImGui::SameLine(); + ImGui::SetNextItemWidth(200 * ui_scale); + ImGui::InputText(std::string("##input" + c.input_name).c_str(), &c.tokens); + } + ImGui::Separator(); + + if (ImGui::Button("Add")) + { + std::string name = "Img" + std::to_string(channels.size() + 1); + addInputDynamic({name}); + channels.push_back({name, "chimg" + std::to_string(channels.size() + 1)}); + } + } + + nlohmann::json to_json() + { + nlohmann::json j; + j["equation"] = equation; + j["channels"] = channels; + return j; + } + + void from_json(nlohmann::json j) + { + equation = j["equation"]; + channels = j["channels"]; + inputs.clear(); + for (auto &c : channels) + inputs.push_back({c.input_name}); + } + }; } \ No newline at end of file