From cc794935f4f3c0dee81c63a8d8195056802f94da Mon Sep 17 00:00:00 2001 From: Tomas Jira Date: Fri, 8 Nov 2024 16:01:50 +0100 Subject: [PATCH] small update to the graphics interface --- include/viewer.h | 4 ++- src/viewer.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/include/viewer.h b/include/viewer.h index dcdd9ce..55f0415 100644 --- a/include/viewer.h +++ b/include/viewer.h @@ -6,12 +6,14 @@ #include #include #include +#include #include class Viewer { public: Viewer(const std::vector& inputs); - void gui() const; + void gui_plot(const std::vector& matrices, std::vector, int>>& indices) const; + Eigen::MatrixXd read(const std::string& path) const; private: GLFWPointer pointer; std::vector inputs; diff --git a/src/viewer.cpp b/src/viewer.cpp index 4edf3ba..dac1271 100644 --- a/src/viewer.cpp +++ b/src/viewer.cpp @@ -1,5 +1,7 @@ #include "viewer.h" +#include + Viewer::Viewer(const std::vector& inputs) : inputs(inputs) { // initialize GLFW and throw error if failed if(!glfwInit()) throw std::runtime_error("ERROR DURING GLFW INITIALIZATION"); @@ -17,7 +19,7 @@ Viewer::Viewer(const std::vector& inputs) : inputs(inputs) { if (glfwMakeContextCurrent(pointer.window); !gladLoadGL()) throw std::runtime_error("ERROR DURING GLAD INITIALIZATION"); // set some GLAD options - glEnable(GL_DEPTH_TEST), glEnable(GL_CULL_FACE), glEnable(GL_STENCIL_TEST); + glEnable(GL_DEPTH_TEST), glEnable(GL_CULL_FACE); // set some GLFW options glfwSetWindowUserPointer(pointer.window, &pointer); glfwSwapInterval(1); @@ -26,6 +28,12 @@ Viewer::Viewer(const std::vector& inputs) : inputs(inputs) { ImGui::CreateContext(); ImPlot::CreateContext(); ImGui_ImplOpenGL3_Init("#version 420"); ImGui_ImplGlfw_InitForOpenGL(pointer.window, true); ImGui::GetIO().IniFilename = nullptr; { + // read the inputs + std::vector matrices; for (const std::string& input : inputs) matrices.push_back(read(input)); + + // set the default indices + std::vector, int>> indices; for (int i = 0; i < matrices.size(); i++) indices.push_back({1, {1}, 0}); + // enter the render loop while (!glfwWindowShouldClose(pointer.window)) { @@ -33,7 +41,7 @@ Viewer::Viewer(const std::vector& inputs) : inputs(inputs) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // render stuff - gui(); + gui_plot(matrices, indices); // swap buffers and poll events glfwSwapBuffers(pointer.window); glfwPollEvents(); @@ -44,13 +52,64 @@ Viewer::Viewer(const std::vector& inputs) : inputs(inputs) { ImGui_ImplGlfw_Shutdown(); ImGui_ImplOpenGL3_Shutdown(); ImPlot::DestroyContext(); ImGui::DestroyContext(); } -void Viewer::gui() const { - // begin the frame - ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); +void Viewer::gui_plot(const std::vector& matrices, std::vector, int>>& indices) const { + // begin the frame and extract window size + ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); int width, height; glfwGetWindowSize(pointer.window, &width, &height); // show demo - ImGui::ShowDemoWindow(); ImPlot::ShowDemoWindow(); + // ImGui::ShowDemoWindow(); ImPlot::ShowDemoWindow(); + + // begin the plotter window + ImGui::SetNextWindowPos({0, 0}); ImGui:: SetNextWindowSize({(float)width, (float)height}); ImGui::Begin("Plotter", nullptr); + + // begin the plot + if (ImPlot::BeginPlot("##plot")) { + + + // plot the data + for (int i = 0; i < matrices.size(); i++) for (int j = 0; j < std::get<1>(indices.at(i)).size(); j++) { + Eigen::VectorXd x = matrices.at(i).col(0), y = matrices.at(i).col(std::get<1>(indices.at(i)).at(j)); ImPlot::PlotLine("##", x.data(), y.data(), x.rows()); + } + + // end the plot + ImPlot::EndPlot(); + } + + // show the matrix plot options + for (size_t i = 0, j = 0; i < indices.size(); i++) { + + // add the column sliders + for (size_t k = 0; k < std::get<1>(indices.at(i)).size(); k++, j++) { + ImGui::SetNextItemWidth(100); ImGui::PushID(j); if (k) {ImGui::SameLine();} ImGui::SliderInt("##", &std::get<1>(indices.at(i)).at(k), 0, matrices.at(i).cols() - 1); ImGui::PopID(); + } + + // add the button to add a column to the plot + ImGui::SameLine(); if (ImGui::Button("Add Column")) std::get<1>(indices.at(i)).push_back(std::get<1>(indices.at(i)).at(std::get<1>(indices.at(i)).size() - 1) + 1); + + // add the button to remove a column from the plot + ImGui::SameLine(); if (ImGui::Button(("Remove Column"))) if (std::get<1>(indices.at(i)).size() > 1) std::get<1>(indices.at(i)).pop_back(); + + // add the animation step slider + ImGui::SameLine(); ImGui::SetNextItemWidth(100); ImGui::PushID(j); ImGui::SliderInt(("##" + std::to_string(i)).c_str(), &std::get<2>(indices.at(i)), 0, matrices.at(i).cols() - 1); ImGui::PopID(); + + // increase the indices + for (int& index : std::get<1>(indices.at(i))) index = (index + std::get<2>(indices.at(i))) % matrices.at(i).cols(); + } + + // end the plotter window + ImGui::End(); // end the frame ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } + +Eigen::MatrixXd Viewer::read(const std::string& path) const { + // open the file and extract shape + std::ifstream fstream(path); int rows, cols; fstream >> rows >> cols; + + // create the matrix and fill it + Eigen::MatrixXd matrix(rows, cols); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) fstream >> matrix(i, j); + + // return the matrix + return matrix; +}