From b805cf929289f3d051f3cce7718f5fd28481ae26 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sat, 26 Feb 2022 23:38:54 -0500 Subject: [PATCH] Fixed a small bug in the RemoveSpectrum line. Added exporting for histograms to .csv files. --- .../src/Navigator/Editor/EditorLayer.cpp | 80 ++++++++++++++++++- Navigator/src/Navigator/Editor/EditorLayer.h | 3 + Navigator/src/Navigator/Histogram.h | 3 + Navigator/src/Navigator/SpectrumManager.cpp | 14 +++- Navigator/src/Navigator/SpectrumManager.h | 3 +- 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index e71f8ac..6c4d6ae 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -1,5 +1,6 @@ #include "EditorLayer.h" #include "imgui.h" +#include "misc/cpp/imgui_stdlib.h" #include "implot.h" #include "FileDialog.h" #include "Navigator/Application.h" @@ -11,7 +12,7 @@ namespace Navigator { EditorLayer::EditorLayer() : - Layer("EditorLayer"), m_removeHistogram(false), m_removeCut(false) + Layer("EditorLayer"), m_removeHistogram(false), m_removeCut(false), m_exportHistogram(false) { } @@ -152,6 +153,14 @@ namespace Navigator { } ImGui::EndMenu(); } + if (ImGui::BeginMenu("Export")) + { + if (ImGui::MenuItem(ICON_FA_SAVE "\tAs .csv")) + { + m_exportHistogram = true; + } + ImGui::EndMenu(); + } ImGui::EndMenuBar(); } @@ -180,6 +189,8 @@ namespace Navigator { RemoveHistogramDialog(); RemoveCutDialog(); + + ExportHistogramDialog(); if(m_spectrumPanel.OnImGuiRender(m_histoList, m_cutList, m_paramList)) { @@ -254,7 +265,7 @@ namespace Navigator { if (ImGui::Selectable(gram.name.c_str(), gram.name == selectedGram, ImGuiSelectableFlags_DontClosePopups)) selectedGram = gram.name; } - ImGui::EndPopup(); + ImGui::EndCombo(); } if (ImGui::Button("Ok")) { @@ -306,4 +317,69 @@ namespace Navigator { ImGui::EndPopup(); } } + + void EditorLayer::ExportHistogramDialog() + { + static std::string filename = ""; + static HistogramParameters selectedGram = HistogramParameters(); + if(m_exportHistogram) + { + filename = ""; + selectedGram = HistogramParameters(); + m_exportHistogram = false; + ImGui::OpenPopup("Export Histogram"); + } + if(ImGui::BeginPopupModal("Export Histogram")) + { + if(ImGui::BeginCombo("Histogram", selectedGram.name.c_str())) + { + for (auto& gram : m_histoList) + { + if (ImGui::Selectable(gram.name.c_str(), gram.name == selectedGram.name, ImGuiSelectableFlags_DontClosePopups)) + selectedGram = gram; + } + ImGui::EndCombo(); + } + ImGui::InputText("File", &filename); + ImGui::SameLine(); + if(ImGui::Button("Open")) + { + m_fileDialog.SetSaveFileDialog(true); + } + std::string result = m_fileDialog.ImGuiRenderSaveFile(".csv"); + if(!result.empty()) + filename = result; + if(ImGui::Button("Ok")) + { + ExportHistogram(selectedGram, filename); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if(ImGui::Button("Cancel")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + } + + void EditorLayer::ExportHistogram(HistogramParameters selectedGram, const std::string& filename) + { + std::ofstream output(filename); + if(!output.is_open()) + { + NAV_ERROR("Unable to create export file {0}. Check pathing.", filename); + return; + } + + std::vector data = SpectrumManager::GetInstance().GetBinData(selectedGram.name); + + output<<"Histogram Name,"< GetBinData() { return std::vector(); } inline HistogramParameters& GetParameters() { return m_params; } inline const std::string& GetXParam() const { return m_params.x_par; }; inline const std::string& GetYParam() const { return m_params.y_par; }; @@ -84,6 +85,7 @@ namespace Navigator { virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; inline virtual bool Is1D() const override { return true; } inline virtual bool Is2D() const override { return false; } + inline virtual std::vector GetBinData() override { return m_binCounts; } private: void InitBins(); @@ -105,6 +107,7 @@ namespace Navigator { virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; inline virtual bool Is1D() const override { return false; } inline virtual bool Is2D() const override { return true; } + inline virtual std::vector GetBinData() override { return m_binCounts; } inline virtual float* GetColorScaleRange() override { return m_colorScaleRange; } diff --git a/Navigator/src/Navigator/SpectrumManager.cpp b/Navigator/src/Navigator/SpectrumManager.cpp index a2795e1..6e67bbc 100644 --- a/Navigator/src/Navigator/SpectrumManager.cpp +++ b/Navigator/src/Navigator/SpectrumManager.cpp @@ -128,6 +128,18 @@ namespace Navigator { return nullptr; } + std::vector SpectrumManager::GetBinData(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(name); + if (iter != m_histoMap.end()) + { + return iter->second->GetBinData(); + } + + return std::vector(); + } + StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region) { std::lock_guard guard(m_managerMutex); @@ -282,4 +294,4 @@ namespace Navigator { } return result; } -} \ No newline at end of file +} diff --git a/Navigator/src/Navigator/SpectrumManager.h b/Navigator/src/Navigator/SpectrumManager.h index 3899dbf..f2525b8 100644 --- a/Navigator/src/Navigator/SpectrumManager.h +++ b/Navigator/src/Navigator/SpectrumManager.h @@ -36,6 +36,7 @@ namespace Navigator { void DrawHistogram(const std::string& name); const HistogramParameters& GetHistogramParams(const std::string& name); float* GetColorScaleRange(const std::string& name); + std::vector GetBinData(const std::string& name); StatResults AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region); std::vector GetListOfHistograms(); @@ -76,4 +77,4 @@ namespace Navigator { } -#endif \ No newline at end of file +#endif