1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-26 20:28:49 -05:00

Cuts mostly implemented. Finishing touches coming shortly

This commit is contained in:
Gordon McCann 2022-01-17 14:28:23 -05:00
parent 87b2041e8f
commit a2a01e568b
9 changed files with 161 additions and 12 deletions

View File

@ -3,6 +3,9 @@
#include "Renderer/RenderCommand.h" #include "Renderer/RenderCommand.h"
#include "Editor/EditorLayer.h" #include "Editor/EditorLayer.h"
//temp
#include "CutMap.h"
namespace Navigator { namespace Navigator {
Application* Application::s_instance = nullptr; Application* Application::s_instance = nullptr;
@ -19,6 +22,10 @@ namespace Navigator {
m_histMap.AddHistogram("myHisto", "joseph", 100, 0, 10); m_histMap.AddHistogram("myHisto", "joseph", 100, 0, 10);
CutMap::GetInstance().AddCut("joe_cut","joseph",0.0, 7.0);
m_histMap.AddCutToHistogramDraw("joe_cut", "myHisto");
m_imgui_layer = new ImGuiLayer(); m_imgui_layer = new ImGuiLayer();
PushOverlay(m_imgui_layer); PushOverlay(m_imgui_layer);
} }

View File

@ -1,5 +1,4 @@
#include "CutMap.h" #include "CutMap.h"
#include "imgui.h"
#include "implot.h" #include "implot.h"
namespace Navigator { namespace Navigator {
@ -33,10 +32,85 @@ namespace Navigator {
Cut2D::~Cut2D() {} Cut2D::~Cut2D() {}
/* /*
Even-odd point in polygon algorithm (see Wikipedia)
Walk around the sides of the polygon and check intersection with each of the sides.
Cast a ray from the point to infinity in any direction and check the number of intersections.
If odd number of intersections, point is inside. Even, point is outside.
Edge cases of point is a vertex or on a side considered.
*/ */
bool Cut2D::IsInside(double x, double y) const bool Cut2D::IsInside(double x, double y) const
{ {
return false; bool result = false;
size_t j = m_points.size() -1;
double slope;
for(size_t i=0; i<m_points.size(); i++)
{
if(x == m_points[i].x && y == m_points[i].y)
return true;
else if((m_points[i].y > y) != (m_points[j].y > y))
{
slope = (x - m_points[i].x)*(m_points[j].y - m_points[i].y) - (m_points[j].x - m_points[i].x)*(y - m_points[i].y);
if(slope == 0.0)
return true;
else if ((slope < 0.0) != (m_points[j].y < m_points[i].y))
result = !result;
}
j=i;
}
return result;
}
//Only in ImPlot/ImGui context!!!!
/*
Again, more complicated. Want to draw the polygon that makes up the 2D-cut. To perform this,
first need to convert plot coordinates into global window coords (pixels). Then tell ImGui to add
a polyline to the draw list, make it red and closed. For this to work, this must be called within the
BeignPlot()/EndPlot() combo for which it was created (why histos own the draw commands).
*/
void Cut2D::Draw() const
{
std::vector<ImVec2> draw_points;
draw_points.reserve(m_points.size());
auto draw_list = ImGui::GetWindowDrawList();
for(auto& point : m_points)
draw_points.push_back(ImPlot::PlotToPixels(point.x, point.y));
draw_list->AddPolyline(draw_points.data(), static_cast<int>(draw_points.size()), ImGui::ColorConvertFloat4ToU32(colorVec), ImDrawFlags_Closed, 0.1);
}
/* CutMap */
CutMap* CutMap::s_instance = new CutMap(); //Guarantee existance for duration of program.
CutMap::CutMap() {}
CutMap::~CutMap() {}
void CutMap::DrawCut(const std::string& name)
{
std::lock_guard<std::mutex> lock(m_cutMutex);
auto iter = m_map.find(name);
if(iter != m_map.end())
iter->second->Draw();
}
bool CutMap::IsInsideCut(const std::string& name, double xval, double yval)
{
std::lock_guard<std::mutex> lock(m_cutMutex);
bool result = false;
auto iter = m_map.find(name);
if(iter != m_map.end())
result = iter->second->IsInside(xval, yval);
return result;
}
std::vector<CutParams> CutMap::GetListOfCutParams()
{
std::lock_guard<std::mutex> lock(m_cutMutex);
std::vector<CutParams> list;
list.reserve(m_map.size());
for(auto& entry : m_map)
list.push_back(entry.second->GetCutParams());
return list;
} }
} }

View File

@ -2,6 +2,7 @@
#define CUT_MAP_H #define CUT_MAP_H
#include "NavCore.h" #include "NavCore.h"
#include "imgui.h"
#include <thread> #include <thread>
namespace Navigator { namespace Navigator {
@ -42,7 +43,7 @@ namespace Navigator {
inline const std::string& GetName() const { return m_params.name; } inline const std::string& GetName() const { return m_params.name; }
inline const std::string& GetXParameter() const { return m_params.x_par; } inline const std::string& GetXParameter() const { return m_params.x_par; }
inline const std::string& GetYParameter() const { return m_params.y_par; } inline const std::string& GetYParameter() const { return m_params.y_par; }
inline const CutParams& GetCutParams() const { return m_params }; inline const CutParams& GetCutParams() const { return m_params; }
protected: protected:
CutParams m_params; CutParams m_params;
}; };
@ -73,6 +74,7 @@ namespace Navigator {
private: private:
std::vector<Point> m_points; std::vector<Point> m_points;
const ImVec4 colorVec = {1.0, 0.0, 0.0, 0.5};
}; };
class NAV_API CutMap class NAV_API CutMap
@ -96,7 +98,7 @@ namespace Navigator {
void DrawCut(const std::string& name); void DrawCut(const std::string& name);
bool IsInsideCut(const std::string& name, double xval, double yval = 0); bool IsInsideCut(const std::string& name, double xval, double yval = 0);
std::vector<CutParams> GetListOfCutParams() const; std::vector<CutParams> GetListOfCutParams();
private: private:
std::unordered_map<std::string, std::unique_ptr<Cut>> m_map; std::unordered_map<std::string, std::unique_ptr<Cut>> m_map;

View File

@ -37,6 +37,11 @@ namespace Navigator {
m_spectrumPanel.UpdateActiveList(m_histoList); m_spectrumPanel.UpdateActiveList(m_histoList);
} }
void EditorLayer::UpdateCutLists()
{
m_cutList = CutMap::GetInstance().GetListOfCutParams();
}
void EditorLayer::OnImGuiRender() void EditorLayer::OnImGuiRender()
{ {
// We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into, // We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into,
@ -139,6 +144,7 @@ namespace Navigator {
UpdateHistogramLists(); UpdateHistogramLists();
UpdateCutLists();
m_spectrumPanel.OnImGuiRender(); m_spectrumPanel.OnImGuiRender();
if (ImGui::Begin("Spectra")) if (ImGui::Begin("Spectra"))
@ -154,7 +160,33 @@ namespace Navigator {
ImGui::BulletText("Y Parameter: %s", params.y_par.c_str()); ImGui::BulletText("Y Parameter: %s", params.y_par.c_str());
ImGui::BulletText("Y Bins: %d Y Min: %f Y Max: %f", params.nbins_y, params.min_y, params.max_y); ImGui::BulletText("Y Bins: %d Y Min: %f Y Max: %f", params.nbins_y, params.min_y, params.max_y);
} }
if(params.cutsDrawnUpon.size() != 0 && ImGui::TreeNode("Cuts Drawn"))
{
for(auto& cut : params.cutsDrawnUpon)
ImGui::BulletText("%s", cut.c_str());
ImGui::TreePop();
}
if(params.cutsAppliedTo.size() != 0 && ImGui::TreeNode("Cuts Applied"))
{
for(auto& cut : params.cutsAppliedTo)
ImGui::BulletText("%s", cut.c_str());
ImGui::TreePop();
}
ImGui::TreePop();
}
}
ImGui::End();
}
if(ImGui::Begin("Cuts"))
{
for(auto& params : m_cutList)
{
if(ImGui::TreeNode(params.name.c_str()))
{
ImGui::BulletText("X Parameter: %s", params.x_par.c_str());
if(params.y_par != "None")
ImGui::BulletText("Y Parameter: %s", params.y_par.c_str());
ImGui::TreePop(); ImGui::TreePop();
} }
} }

View File

@ -5,6 +5,7 @@
#include "Navigator/Events/Event.h" #include "Navigator/Events/Event.h"
#include "Navigator/Events/PhysicsEvent.h" #include "Navigator/Events/PhysicsEvent.h"
#include "Navigator/HistogramMap.h" #include "Navigator/HistogramMap.h"
#include "Navigator/CutMap.h"
#include "SpectrumPanel.h" #include "SpectrumPanel.h"
#include "FileDialog.h" #include "FileDialog.h"
@ -25,9 +26,11 @@ namespace Navigator {
private: private:
void UpdateHistogramLists(); void UpdateHistogramLists();
void UpdateCutLists();
bool OnPhysicsParamEvent(PhysicsParamEvent& event); bool OnPhysicsParamEvent(PhysicsParamEvent& event);
HistogramMap* m_histMap; //Not owned by the EditorLayer!! HistogramMap* m_histMap; //Not owned by the EditorLayer!!
std::vector<HistogramParameters> m_histoList; std::vector<HistogramParameters> m_histoList;
std::vector<CutParams> m_cutList;
std::vector<std::string> m_paramList; std::vector<std::string> m_paramList;
SpectrumPanel m_spectrumPanel; SpectrumPanel m_spectrumPanel;
FileDialog m_fileDialog; FileDialog m_fileDialog;

View File

@ -1,4 +1,5 @@
#include "Histogram.h" #include "Histogram.h"
#include "CutMap.h"
#include "implot.h" #include "implot.h"
/* /*
@ -53,6 +54,8 @@ namespace Navigator {
int bin = int((x - m_params.min_x)/(m_binWidth)); int bin = int((x - m_params.min_x)/(m_binWidth));
auto& cutmap = CutMap::GetInstance();
m_binCounts[bin] += 1.0; m_binCounts[bin] += 1.0;
} }
@ -61,6 +64,9 @@ namespace Navigator {
{ {
ImPlot::SetupAxes(m_params.x_par.c_str(), "Counts",0, ImPlotAxisFlags_LockMin); ImPlot::SetupAxes(m_params.x_par.c_str(), "Counts",0, ImPlotAxisFlags_LockMin);
ImPlot::PlotBars(m_params.name.c_str(), &m_binCenters.data()[0], &m_binCounts.data()[0], m_params.nbins_x, m_binWidth); ImPlot::PlotBars(m_params.name.c_str(), &m_binCenters.data()[0], &m_binCounts.data()[0], m_params.nbins_x, m_binWidth);
auto& cutmap = CutMap::GetInstance();
for(auto& cut : m_params.cutsDrawnUpon)
cutmap.DrawCut(cut);
} }
void Histogram1D::ClearData() void Histogram1D::ClearData()
@ -130,6 +136,9 @@ namespace Navigator {
ImPlot::SetupAxes(m_params.x_par.c_str(), m_params.y_par.c_str(), 0, 0); ImPlot::SetupAxes(m_params.x_par.c_str(), m_params.y_par.c_str(), 0, 0);
ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, 0, m_maxBinContent, NULL, ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, 0, m_maxBinContent, NULL,
ImPlotPoint(m_params.min_x, m_params.min_y), ImPlotPoint(m_params.max_x, m_params.max_y)); ImPlotPoint(m_params.min_x, m_params.min_y), ImPlotPoint(m_params.max_x, m_params.max_y));
auto& cutmap = CutMap::GetInstance();
for(auto& cut : m_params.cutsDrawnUpon)
cutmap.DrawCut(cut);
} }
void Histogram2D::ClearData() void Histogram2D::ClearData()

View File

@ -14,6 +14,8 @@ namespace Navigator {
std::string name; std::string name;
std::string x_par; std::string x_par;
std::string y_par; std::string y_par;
std::vector<std::string> cutsDrawnUpon;
std::vector<std::string> cutsAppliedTo;
int nbins_x = 0; int nbins_x = 0;
double min_x = 0; double min_x = 0;
double max_x = 0; double max_x = 0;
@ -44,6 +46,8 @@ namespace Navigator {
inline const std::string& GetXParam() const { return m_params.x_par; }; inline const std::string& GetXParam() const { return m_params.x_par; };
inline const std::string& GetYParam() const { return m_params.y_par; }; inline const std::string& GetYParam() const { return m_params.y_par; };
inline const std::string& GetName() const { return m_params.name; } inline const std::string& GetName() const { return m_params.name; }
inline void AddCutToBeDrawn(const std::string& name) { m_params.cutsDrawnUpon.push_back(name); }
inline void AddCutToBeApplied(const std::string& name) { m_params.cutsAppliedTo.push_back(name); }
protected: protected:
HistogramParameters m_params; HistogramParameters m_params;

View File

@ -10,6 +10,22 @@ namespace Navigator {
{ {
} }
void HistogramMap::AddCutToHistogramDraw(const std::string &cutname, const std::string &histoname)
{
std::lock_guard<std::mutex> lock(m_histMutex);
auto iter = m_map.find(histoname);
if(iter != m_map.end())
iter->second->AddCutToBeDrawn(cutname);
}
void HistogramMap::AddCutToHistogramApplied(const std::string &cutname, const std::string &histoname)
{
std::lock_guard<std::mutex> lock(m_histMutex);
auto iter = m_map.find(histoname);
if(iter != m_map.end())
iter->second->AddCutToBeApplied(cutname);
}
void HistogramMap::UpdateHistograms() void HistogramMap::UpdateHistograms()
{ {
std::lock_guard<std::mutex> guard(m_histMutex); std::lock_guard<std::mutex> guard(m_histMutex);

View File

@ -28,6 +28,8 @@ namespace Navigator {
m_map[name].reset(new Histogram2D(name, paramx, paramy, bins_x, min_x, max_x, bins_y, min_y, max_y)); m_map[name].reset(new Histogram2D(name, paramx, paramy, bins_x, min_x, max_x, bins_y, min_y, max_y));
} }
void AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname);
void AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname);
void UpdateHistograms(); void UpdateHistograms();
void DrawHistograms(); void DrawHistograms();