1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-22 18:28:52 -05:00

Added SpectrumSerializer class as well as minor bug fixes to the open/save dialog

This commit is contained in:
Gordon McCann 2022-01-22 20:05:08 -05:00
parent 1f7e1a49ae
commit 5177908c02
6 changed files with 340 additions and 5 deletions

View File

@ -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()

View File

@ -34,6 +34,8 @@ namespace Navigator {
virtual void Draw() const = 0;
virtual bool Is1D() const = 0;
virtual bool Is2D() const = 0;
virtual std::vector<double> GetXValues() const = 0;
virtual std::vector<double> 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<double> GetXValues() const override { return std::vector<double>({ m_minVal, m_maxVal }); }
virtual std::vector<double> GetYValues() const override { return std::vector<double>(); }
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<double> GetXValues() const override { return m_xpoints; }
virtual std::vector<double> GetYValues() const override { return m_ypoints; }
private:
std::vector<double> m_xpoints;

View File

@ -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();

View File

@ -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();

View File

@ -0,0 +1,294 @@
#include "SpectrumSerializer.h"
#include "HistogramMap.h"
#include "CutMap.h"
#include <fstream>
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<double> cut_xdata;
std::vector<double> 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();
}
}

View File

@ -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