Skip to content

Commit

Permalink
small update to the graphics interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tjira committed Nov 8, 2024
1 parent 860bc2d commit cc79493
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
4 changes: 3 additions & 1 deletion include/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <ImGuiFileDialog.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <Eigen/Core>
#include <implot.h>

class Viewer {
public:
Viewer(const std::vector<std::string>& inputs);
void gui() const;
void gui_plot(const std::vector<Eigen::MatrixXd>& matrices, std::vector<std::tuple<int, std::vector<int>, int>>& indices) const;
Eigen::MatrixXd read(const std::string& path) const;

private:
GLFWPointer pointer; std::vector<std::string> inputs;
Expand Down
71 changes: 65 additions & 6 deletions src/viewer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "viewer.h"

#include <iostream>

Viewer::Viewer(const std::vector<std::string>& inputs) : inputs(inputs) {
// initialize GLFW and throw error if failed
if(!glfwInit()) throw std::runtime_error("ERROR DURING GLFW INITIALIZATION");
Expand All @@ -17,7 +19,7 @@ Viewer::Viewer(const std::vector<std::string>& 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);
Expand All @@ -26,14 +28,20 @@ Viewer::Viewer(const std::vector<std::string>& 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<Eigen::MatrixXd> matrices; for (const std::string& input : inputs) matrices.push_back(read(input));

// set the default indices
std::vector<std::tuple<int, std::vector<int>, int>> indices; for (int i = 0; i < matrices.size(); i++) indices.push_back({1, {1}, 0});

// enter the render loop
while (!glfwWindowShouldClose(pointer.window)) {

// clear the color and depth buffer
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();
Expand All @@ -44,13 +52,64 @@ Viewer::Viewer(const std::vector<std::string>& 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<Eigen::MatrixXd>& matrices, std::vector<std::tuple<int, std::vector<int>, 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;
}

0 comments on commit cc79493

Please sign in to comment.