|
| 1 | +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "polyscope/quantity.h" |
| 6 | +#include "polyscope/render/color_maps.h" |
| 7 | +#include "polyscope/render/engine.h" |
| 8 | +#include "polyscope/widget.h" |
| 9 | + |
| 10 | +#include <vector> |
| 11 | + |
| 12 | + |
| 13 | +namespace polyscope { |
| 14 | + |
| 15 | +// A histogram that shows up in ImGUI window |
| 16 | +// ONEDAY: we could definitely make a better histogram widget for categorical data... |
| 17 | + |
| 18 | +class OncreenColorBarWidget; |
| 19 | + |
| 20 | +class ColorBar { |
| 21 | +public: |
| 22 | + friend class OnscreenColorBarWidget; |
| 23 | + ColorBar(Quantity& parent_); // must call buildHistogram() with data after |
| 24 | + ~ColorBar(); |
| 25 | + |
| 26 | + void buildHistogram(const std::vector<float>& values, DataType datatype); |
| 27 | + void updateColormap(const std::string& newColormap); |
| 28 | + |
| 29 | + // Width = -1 means set automatically |
| 30 | + void buildUI(float width = -1.0); |
| 31 | + |
| 32 | + Quantity& parent; |
| 33 | + std::pair<double, double> colormapRange; // in DATA values, not [0,1] |
| 34 | + |
| 35 | + void exportColorbarToSVG(const std::string& filename); |
| 36 | + |
| 37 | + // Getters and setters |
| 38 | + |
| 39 | + void setOnscreenColorbarEnabled(bool newEnabled); |
| 40 | + bool getOnscreenColorbarEnabled(); |
| 41 | + |
| 42 | + // Location in screen coords. (-1,-1), means "place automatically" (default) |
| 43 | + void setOnscreenColorbarLocation(glm::vec2 newScreenCoords); |
| 44 | + glm::vec2 getOnscreenColorbarLocation(); |
| 45 | + |
| 46 | +private: |
| 47 | + // Basic data defining the color map |
| 48 | + DataType dataType = DataType::STANDARD; |
| 49 | + std::pair<double, double> dataRange; |
| 50 | + |
| 51 | + // == The inline horizontal histogram visualization in the structures bar |
| 52 | + |
| 53 | + // Manage histogram counts |
| 54 | + void fillHistogramBuffers(); |
| 55 | + size_t rawHistBinCount = 51; |
| 56 | + std::vector<float> rawHistCurveY; |
| 57 | + std::vector<std::array<float, 2>> rawHistCurveX; |
| 58 | + |
| 59 | + // Render to a texture for the inline histogram visualization in the structures bar |
| 60 | + void renderInlineHistogramToTexture(); |
| 61 | + void prepareInlineHistogram(); |
| 62 | + unsigned int texDim = 600; |
| 63 | + std::shared_ptr<render::TextureBuffer> inlineHistogramTexture = nullptr; |
| 64 | + std::shared_ptr<render::FrameBuffer> inlineHistogramFramebuffer = nullptr; |
| 65 | + std::shared_ptr<render::ShaderProgram> inlineHistogramProgram = nullptr; |
| 66 | + std::string colormap = "viridis"; |
| 67 | + |
| 68 | + // A few parameters which control appearance |
| 69 | + float bottomBarHeight = 0.35; |
| 70 | + float bottomBarGap = 0.1; |
| 71 | + |
| 72 | + // == The optional vertical colorbar which floats ont he main display |
| 73 | + PersistentValue<bool> onscreenColorbarEnabled; |
| 74 | + PersistentValue<glm::vec2> onscreenColorbarLocation; |
| 75 | + std::shared_ptr<render::TextureBuffer> cmapTexture; // this is just the plain colormap rgb |
| 76 | + void prepareOnscreenColorBar(); |
| 77 | + std::unique_ptr<Widget> onscreenColorBarWidget = nullptr; |
| 78 | +}; |
| 79 | + |
| 80 | +class OnscreenColorBarWidget : public Widget { |
| 81 | +public: |
| 82 | + OnscreenColorBarWidget(ColorBar& parent_); |
| 83 | + virtual void draw() override; |
| 84 | + |
| 85 | +private: |
| 86 | + ColorBar& parent; |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +} // namespace polyscope |
0 commit comments