diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index c4d6a3e..7ba761f 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -23,6 +23,8 @@ namespace Navigator { PushLayer(new EditorLayer()); m_imgui_layer = new ImGuiLayer(); PushOverlay(m_imgui_layer); + + /* HistogramMap& histMap = HistogramMap::GetInstance(); histMap.AddHistogram(HistogramParameters("myHisto", "joseph", 100, 0, 10)); histMap.AddHistogram(HistogramParameters("myHisto2D", "joseph", "joseph", 100, 0, 10, 100, 0, 10)); @@ -32,8 +34,8 @@ namespace Navigator { histMap.AddCutToHistogramDraw("joe_cut", "myHisto"); histMap.AddCutToHistogramDraw("joe2D_cut", "myHisto2D"); - + */ } Application::~Application() diff --git a/Navigator/src/Navigator/CutMap.h b/Navigator/src/Navigator/CutMap.h index 62d2fee..6734739 100644 --- a/Navigator/src/Navigator/CutMap.h +++ b/Navigator/src/Navigator/CutMap.h @@ -34,6 +34,8 @@ namespace Navigator { virtual void Draw() const = 0; virtual bool Is1D() const = 0; virtual bool Is2D() const = 0; + virtual std::vector GetXValues() const = 0; + virtual std::vector GetYValues() const = 0; inline const std::string& GetName() const { return m_params.name; } inline const std::string& GetXParameter() const { return m_params.x_par; } @@ -52,6 +54,8 @@ namespace Navigator { virtual void Draw() const override; virtual bool Is1D() const override { return true; } virtual bool Is2D() const override { return false; } + virtual std::vector GetXValues() const override { return std::vector({ m_minVal, m_maxVal }); } + virtual std::vector GetYValues() const override { return std::vector(); } private: double m_minVal, m_maxVal; @@ -66,6 +70,8 @@ namespace Navigator { virtual void Draw() const override; virtual bool Is1D() const override { return false; } virtual bool Is2D() const override { return true; } + virtual std::vector GetXValues() const override { return m_xpoints; } + virtual std::vector GetYValues() const override { return m_ypoints; } private: std::vector m_xpoints; diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index 99579cf..c729373 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -3,6 +3,7 @@ #include "implot.h" #include "FileDialog.h" #include "Navigator/Application.h" +#include "Navigator/SpectrumSerializer.h" namespace Navigator { @@ -128,9 +129,16 @@ namespace Navigator { std::string open_file_result = m_fileDialog.ImGuiRenderOpenFile(".nav"); std::string save_file_result = m_fileDialog.ImGuiRenderSaveFile(".nav"); if (!open_file_result.empty()) - NAV_INFO("Found a Open File!"); + { + SpectrumSerializer serializer(open_file_result); + serializer.DeserializeData(); + } else if (!save_file_result.empty()) - NAV_INFO("Found a Save File!"); + { + NAV_INFO("Found a Save File! {0}", save_file_result); + SpectrumSerializer serializer(save_file_result); + serializer.SerializeData(); + } m_spectrumDialog.ImGuiRenderSpectrumDialog(); diff --git a/Navigator/src/Navigator/Editor/FileDialog.cpp b/Navigator/src/Navigator/Editor/FileDialog.cpp index c787e0c..d680f95 100644 --- a/Navigator/src/Navigator/Editor/FileDialog.cpp +++ b/Navigator/src/Navigator/Editor/FileDialog.cpp @@ -42,7 +42,8 @@ namespace Navigator { ImGui::InputText("Selected", &m_selectedItem); if (ImGui::Button("Ok")) { - result = m_currentPath.string() + m_selectedItem; + std::filesystem::path filepath = m_currentPath / m_selectedItem; + result = filepath.string(); ImGui::CloseCurrentPopup(); } ImGui::SameLine(); @@ -117,7 +118,8 @@ namespace Navigator { ImGui::InputText("Selected", &m_selectedItem); if (ImGui::Button("Ok")) { - result = m_currentPath.string() + m_selectedItem; + std::filesystem::path filepath = m_currentPath / m_selectedItem; + result = filepath.string(); ImGui::CloseCurrentPopup(); } ImGui::SameLine(); diff --git a/Navigator/src/Navigator/SpectrumSerializer.cpp b/Navigator/src/Navigator/SpectrumSerializer.cpp new file mode 100644 index 0000000..f511133 --- /dev/null +++ b/Navigator/src/Navigator/SpectrumSerializer.cpp @@ -0,0 +1,294 @@ +#include "SpectrumSerializer.h" +#include "HistogramMap.h" +#include "CutMap.h" + +#include + +namespace Navigator { + + SpectrumSerializer::SpectrumSerializer(const std::string& filepath) : + m_filename(filepath) + { + } + + SpectrumSerializer::~SpectrumSerializer() {} + + void SpectrumSerializer::SerializeData() + { + HistogramMap& histMap = HistogramMap::GetInstance(); + CutMap& cutMap = CutMap::GetInstance(); + + std::ofstream output(m_filename); + if (!output.is_open()) + { + NAV_ERROR("Unable to open {0} to write data.", m_filename); + return; + } + + output << "begin_cuts" << std::endl; + for (auto& iter : cutMap) + { + if (iter.second->Is1D()) + { + output << "\tbegin_cut1D" << std::endl; + output << "\t\tname: " << iter.second->GetName() << std::endl; + output << "\t\txparam: " << iter.second->GetXParameter() << std::endl; + output << "\t\tminValue: " << iter.second->GetXValues()[0] << std::endl; + output << "\t\tmaxValue: " << iter.second->GetXValues()[1] << std::endl; + output << "\tend_cut1D" << std::endl; + } + else if (iter.second->Is2D()) + { + output << "\tbegin_cut2D" << std::endl; + output << "\t\tname: " << iter.second->GetName() << std::endl; + output << "\t\txparam: " << iter.second->GetXParameter() << std::endl; + output << "\t\typaram: " << iter.second->GetYParameter() << std::endl; + output << "\t\tbegin_xvalues" << std::endl; + for (const auto& value : iter.second->GetXValues()) + { + output << "\t\t\t" << value << std::endl; + } + output << "\t\tend_xvalues" << std::endl; + output << "\t\tbegin_yvalues" << std::endl; + for (const auto& value : iter.second->GetYValues()) + { + output << "\t\t\t" << value << std::endl; + } + output << "\t\tend_yvalues" << std::endl; + output << "\tend_cut2D" << std::endl; + } + } + output << "end_cuts" << std::endl; + + output << "begin_histograms" << std::endl; + for (auto& iter : histMap) + { + if (iter.second->Is1D()) + { + const auto& params = iter.second->GetParameters(); + output << "\tbegin_histogram1D" << std::endl; + output << "\t\tname: " << params.name << std::endl; + output << "\t\txparam: " << params.x_par << std::endl; + output << "\t\tNxbins: " << params.nbins_x << std::endl; + output << "\t\tXMin: " << params.min_x << std::endl; + output << "\t\tXMax: " << params.max_x << std::endl; + output << "\t\tbegin_cutsdrawn" << std::endl; + for (const auto& name : params.cutsDrawnUpon) + { + output << "\t\t\t" << name << std::endl; + } + output << "\t\tend_cutsdrawn" << std::endl; + output << "\t\tbegin_cutsapplied" << std::endl; + for (const auto& name : params.cutsAppliedTo) + { + output << "\t\t\t" << name << std::endl; + } + output << "\t\tend_cutsapplied" << std::endl; + output << "\tend_histogram1D" << std::endl; + } + else if (iter.second->Is2D()) + { + const auto& params = iter.second->GetParameters(); + output << "\tbegin_histogram2D" << std::endl; + output << "\t\tname: " << params.name << std::endl; + output << "\t\txparam: " << params.x_par << std::endl; + output << "\t\typaram: " << params.y_par << std::endl; + output << "\t\tNxbins: " << params.nbins_x << std::endl; + output << "\t\tXMin: " << params.min_x << std::endl; + output << "\t\tXMax: " << params.max_x << std::endl; + output << "\t\tNybins: " << params.nbins_y << std::endl; + output << "\t\tYMin: " << params.min_y << std::endl; + output << "\t\tYMax: " << params.max_y << std::endl; + output << "\t\tbegin_cutsdrawn" << std::endl; + for (const auto& name : params.cutsDrawnUpon) + { + output << "\t\t\t" << name << std::endl; + } + output << "\t\tend_cutsdrawn" << std::endl; + output << "\t\tbegin_cutsapplied" << std::endl; + for (const auto& name : params.cutsAppliedTo) + { + output << "\t\t\t" << name << std::endl; + } + output << "\t\tend_cutsapplied" << std::endl; + output << "\tend_histogram2D" << std::endl; + } + } + output << "end_histograms" << std::endl; + + NAV_INFO("Successfully saved data to {0}", m_filename); + output.close(); + } + + void SpectrumSerializer::DeserializeData() + { + HistogramMap& histMap = HistogramMap::GetInstance(); + CutMap& cutMap = CutMap::GetInstance(); + + std::ifstream input(m_filename); + if (!input.is_open()) + { + NAV_ERROR("Unable to open {0} to read data!", m_filename); + return; + } + + std::string check; + double value_doub; + int value_int; + CutParams cut_data, reset_cut; + std::vector cut_xdata; + std::vector cut_ydata; + HistogramParameters hist_data, reset_hist; + + while (input >> check) + { + if (check == "begin_cuts") + { + while (input >> check) + { + cut_data = reset_cut; + cut_xdata.clear(); + cut_ydata.clear(); + if (check == "begin_cut1D") + { + input >> check >> cut_data.name; + input >> check >> cut_data.x_par; + input >> check >> value_doub; + cut_xdata.push_back(value_doub); + input >> check >> value_doub; + cut_xdata.push_back(value_doub); + input >> check; + cutMap.AddCut(cut_data, cut_xdata[0], cut_xdata[1]); + } + else if (check == "begin_cut2D") + { + input >> check >> cut_data.name; + input >> check >> cut_data.x_par; + input >> check >> cut_data.y_par; + while (input >> check) + { + if (check == "begin_xvalues") + continue; + else if (check == "end_xvalues") + break; + else + cut_xdata.push_back(std::stod(check)); + } + while (input >> check) + { + if (check == "begin_yvalues") + continue; + else if (check == "end_yvalues") + break; + else + cut_ydata.push_back(std::stod(check)); + } + input >> check; + cutMap.AddCut(cut_data, cut_xdata, cut_ydata); + } + else if (check == "end_cuts") + break; + else + { + NAV_ERROR("Deserialization error; unexpected check condition while parsing cut data! Current value: {0}", check); + input.close(); + return; + } + } + } + else if (check == "begin_histograms") + { + while (input >> check) + { + hist_data = reset_hist; + if (check == "begin_histogram1D") + { + input >> check >> hist_data.name; + input >> check >> hist_data.x_par; + input >> check >> hist_data.nbins_x; + input >> check >> hist_data.min_x; + input >> check >> hist_data.max_x; + while (input >> check) + { + if (check == "begin_cutsdrawn") + continue; + else if (check == "end_cutsdrawn") + break; + else + { + hist_data.cutsDrawnUpon.push_back(check); + } + } + while (input >> check) + { + if (check == "begin_cutsapplied") + continue; + else if (check == "end_cutsapplied") + break; + else + { + hist_data.cutsAppliedTo.push_back(check); + } + } + input >> check; + histMap.AddHistogram(hist_data); + } + else if (check == "begin_histogram2D") + { + input >> check >> hist_data.name; + input >> check >> hist_data.x_par; + input >> check >> hist_data.y_par; + input >> check >> hist_data.nbins_x; + input >> check >> hist_data.min_x; + input >> check >> hist_data.max_x; + input >> check >> hist_data.nbins_y; + input >> check >> hist_data.min_y; + input >> check >> hist_data.max_y; + while (input >> check) + { + if (check == "begin_cutsdrawn") + continue; + else if (check == "end_cutsdrawn") + break; + else + { + hist_data.cutsDrawnUpon.push_back(check); + } + } + while (input >> check) + { + if (check == "begin_cutsapplied") + continue; + else if (check == "end_cutsapplied") + break; + else + { + hist_data.cutsAppliedTo.push_back(check); + } + } + input >> check; + histMap.AddHistogram(hist_data); + } + else if (check == "end_histograms") + break; + else + { + NAV_ERROR("Deserialization error; unexpected check condition while parsing histogram data! Current value: {0}", check); + input.close(); + return; + } + } + } + else + { + NAV_ERROR("Deserialization error; unexpected check condition at top level! Current value: {0}", check); + input.close(); + return; + } + } + + NAV_INFO("Successfully loaded data from {0}", m_filename); + input.close(); + } + +} \ No newline at end of file diff --git a/Navigator/src/Navigator/SpectrumSerializer.h b/Navigator/src/Navigator/SpectrumSerializer.h new file mode 100644 index 0000000..95a805f --- /dev/null +++ b/Navigator/src/Navigator/SpectrumSerializer.h @@ -0,0 +1,23 @@ +#ifndef SPECTRUM_SERIALIZER_H +#define SPECTRUM_SERIALIZER_H + +namespace Navigator { + + class NAV_API SpectrumSerializer + { + public: + SpectrumSerializer(const std::string& filepath); + ~SpectrumSerializer(); + + void SerializeData(); + void DeserializeData(); + + inline const std::string& GetFilename() { return m_filename; } + + private: + std::string m_filename; + }; + +} + +#endif \ No newline at end of file