4 changed files with 230 additions and 41 deletions
@ -0,0 +1,135 @@ |
|||
//
|
|||
// Created by cflin on 4/8/23.
|
|||
//
|
|||
|
|||
#ifndef RIGIDIPC_COLORBARPLUGIN_H |
|||
#define RIGIDIPC_COLORBARPLUGIN_H |
|||
|
|||
#include <Eigen/Dense> |
|||
#include <igl/opengl/glfw/Viewer.h> |
|||
#include <igl/opengl/glfw/imgui/ImGuiMenu.h> |
|||
|
|||
class ColorbarPlugin { |
|||
public: |
|||
ColorbarPlugin() { |
|||
// initialize
|
|||
Eigen::MatrixXd rgb; |
|||
Eigen::MatrixXd f=Eigen::VectorXd::LinSpaced(100,0.0,1.0); |
|||
for (size_t i = 0; i < igl::NUM_COLOR_MAP_TYPES; ++i) { |
|||
GLuint id = 0; |
|||
igl::colormap(static_cast<igl::ColorMapType>(i),f,0.0,1.0,rgb); |
|||
// std::cout<<std::endl;
|
|||
// std::cout<<rgb<<std::endl;
|
|||
texture_from_colormap(rgb, id); |
|||
colormaps_[i] = id; |
|||
} |
|||
} |
|||
|
|||
// Draws a combo box for selecting the colormap
|
|||
int draw_colormap_combo() const { |
|||
const char *items[]{ |
|||
"Inferno", |
|||
"Jet", |
|||
"Magma", |
|||
"Parula", |
|||
"Plasma", |
|||
"Viridis", |
|||
}; |
|||
static int selected_index = 5; |
|||
static const char *current_item = items[selected_index]; |
|||
ImVec2 combo_pos = ImGui::GetCursorScreenPos(); |
|||
if (ImGui::BeginCombo("Colormap##combo", "")) { |
|||
for (int n = 0; n < IM_ARRAYSIZE(items); ++n) { |
|||
// You can store your selection however you want, outside or inside your objects
|
|||
bool is_selected = (current_item == items[n]); |
|||
ImGui::PushID(n); |
|||
if (ImGui::Selectable("", is_selected)) { |
|||
current_item = items[n]; |
|||
selected_index = n; |
|||
} |
|||
ImGui::SameLine(0, 0); |
|||
ImGui::Image( |
|||
reinterpret_cast<ImTextureID>(colormaps_[n]), |
|||
ImVec2(ImGui::GetTextLineHeight(), ImGui::GetTextLineHeight()), |
|||
ImVec2(0, 0), ImVec2(1, 1)); |
|||
ImGui::SameLine(); |
|||
ImGui::Text("%s", items[n]); |
|||
if (is_selected) { |
|||
ImGui::SetItemDefaultFocus(); |
|||
} |
|||
ImGui::PopID(); |
|||
} |
|||
ImGui::EndCombo(); |
|||
} |
|||
|
|||
ImVec2 backup_pos = ImGui::GetCursorScreenPos(); |
|||
ImGuiStyle &style = ImGui::GetStyle(); |
|||
ImGui::SetCursorScreenPos(ImVec2(combo_pos.x + style.FramePadding.x, combo_pos.y + style.FramePadding.y)); |
|||
float h = ImGui::GetTextLineHeight(); |
|||
ImGui::Image( |
|||
reinterpret_cast<ImTextureID>(colormaps_[selected_index]), |
|||
ImVec2(h, h)); |
|||
ImGui::SameLine(); |
|||
ImGui::Text("%s", current_item); |
|||
ImGui::SetCursorScreenPos(backup_pos); |
|||
|
|||
return selected_index; |
|||
} |
|||
|
|||
void draw_colorbar(igl::ColorMapType cm, float xmin, float xmax, |
|||
const Eigen::Vector4f &background_color) const { |
|||
ImVec4 color(0, 0, 0, 1); |
|||
auto rgb = background_color; |
|||
// http://stackoverflow.com/a/3943023/112731
|
|||
if (rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114 > 186) { |
|||
color = ImVec4(1, 1, 1, 1); |
|||
} |
|||
float w = 20; |
|||
float h = 100; |
|||
ImGui::BeginGroup(); |
|||
ImGui::BeginGroup(); |
|||
ImGui::TextColored(color, "%.3g", xmin); |
|||
ImGui::Dummy(ImVec2(0, h - 2 * ImGui::GetTextLineHeightWithSpacing())); |
|||
ImGui::TextColored(color, "%.3g", xmax); |
|||
ImGui::EndGroup(); |
|||
ImGui::SameLine(); |
|||
ImGui::Image(reinterpret_cast<ImTextureID>(colormaps_[cm]), ImVec2(w, h)); |
|||
ImGui::EndGroup(); |
|||
} |
|||
|
|||
static void texture_from_colormap(const Eigen::MatrixXd &rgb, GLuint &id) { |
|||
int width = 1; |
|||
int height = (int) rgb.rows(); |
|||
|
|||
Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> cmap; |
|||
if (rgb.maxCoeff() > 1.0) { |
|||
cmap = rgb.cast<unsigned char>(); |
|||
} else { |
|||
cmap = (rgb.array() * 255.f).cast<unsigned char>(); |
|||
} |
|||
assert(cmap.cols() == 3); |
|||
cmap.conservativeResize(cmap.rows(), 4); |
|||
cmap.col(3).setConstant(255); |
|||
|
|||
if (id == 0) { |
|||
glGenTextures(1, &id); |
|||
} |
|||
glBindTexture(GL_TEXTURE_2D, id); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|||
glTexImage2D( |
|||
GL_TEXTURE_2D, 0, GL_RGBA, |
|||
width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, cmap.data()); |
|||
glGenerateMipmap(GL_TEXTURE_2D); |
|||
glBindTexture(GL_TEXTURE_2D, 0); |
|||
} |
|||
|
|||
protected: |
|||
std::array<GLuint, igl::NUM_COLOR_MAP_TYPES> colormaps_; |
|||
igl::ColorMapType colormap_type_ = igl::COLOR_MAP_TYPE_VIRIDIS; |
|||
}; |
|||
|
|||
|
|||
#endif //RIGIDIPC_COLORBARPLUGIN_H
|
Loading…
Reference in new issue