Skip to content

Commit 994acb0

Browse files
authored
add optional onscreen colorbar (#366)
* WIP on color bars * fix missed rename * WIP more structure * remove unused code, fix bug * add colormap texture getter * more window pane placement logic * missed header from pane placement * working version of onscreen colorbar * expose setters for onscreen colorbar * style tweaks * unrelated spacing fixes * more style tuning * add export function * minor fix to colorbar options menu * add vec2 persistent value * add set location option * add tests * add onscreen to demo
1 parent cc4d603 commit 994acb0

27 files changed

+712
-417
lines changed

examples/demo-app/demo_app.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void processFileOBJ(std::string filename) {
143143
randColor[iV] = {{polyscope::randomUnit(), polyscope::randomUnit(), polyscope::randomUnit()}};
144144
}
145145
polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("cX_really_really_stupid_long_name_how_dumb", valX);
146-
polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("cY", valY);
146+
auto q = polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("cY", valY);
147+
q->setOnscreenColorbarEnabled(true); // set the onscreen colormap for this one
147148
polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("cZ", valZ);
148149
polyscope::getSurfaceMesh(niceName)->addVertexColorQuantity("vColor", randColor);
149150
polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("cY_sym", valY, polyscope::DataType::SYMMETRIC);

include/polyscope/color_bar.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

include/polyscope/curve_network_scalar_quantity.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "polyscope/affine_remapper.h"
66
#include "polyscope/curve_network.h"
7-
#include "polyscope/histogram.h"
87
#include "polyscope/render/color_maps.h"
98
#include "polyscope/scalar_quantity.h"
109

include/polyscope/histogram.h

Lines changed: 0 additions & 59 deletions
This file was deleted.

include/polyscope/internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ extern bool& pointCloudEfficiencyWarningReported;
2626
// global members
2727
extern FloatingQuantityStructure*& globalFloatingQuantityStructure;
2828

29+
30+
// == UI and layout related
31+
extern float imguiStackMargin;
32+
extern float lastWindowHeightPolyscope;
33+
extern float lastWindowHeightUser;
34+
extern float lastRightSideFreeX;
35+
extern float lastRightSideFreeY;
36+
extern float leftWindowsWidth;
37+
extern float rightWindowsWidth;
38+
2939
} // namespace internal
3040
} // namespace polyscope

include/polyscope/persistent_value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern PersistentCache<double> persistentCache_double;
134134
extern PersistentCache<float> persistentCache_float;
135135
extern PersistentCache<bool> persistentCache_bool;
136136
extern PersistentCache<std::string> persistentCache_string;
137+
extern PersistentCache<glm::vec2> persistentCache_glmvec2;
137138
extern PersistentCache<glm::vec3> persistentCache_glmvec3;
138139
extern PersistentCache<glm::mat4> persistentCache_glmmat4;
139140
extern PersistentCache<ScaledValue<double>> persistentCache_scaleddouble;
@@ -150,6 +151,7 @@ template<> inline PersistentCache<double>& getPersistentCacheR
150151
template<> inline PersistentCache<float>& getPersistentCacheRef<float>() { return persistentCache_float; }
151152
template<> inline PersistentCache<bool>& getPersistentCacheRef<bool>() { return persistentCache_bool; }
152153
template<> inline PersistentCache<std::string>& getPersistentCacheRef<std::string>() { return persistentCache_string; }
154+
template<> inline PersistentCache<glm::vec2>& getPersistentCacheRef<glm::vec2>() { return persistentCache_glmvec2; }
153155
template<> inline PersistentCache<glm::vec3>& getPersistentCacheRef<glm::vec3>() { return persistentCache_glmvec3; }
154156
template<> inline PersistentCache<glm::mat4>& getPersistentCacheRef<glm::mat4>() { return persistentCache_glmmat4; }
155157
template<> inline PersistentCache<ScaledValue<double>>& getPersistentCacheRef<ScaledValue<double>>() { return persistentCache_scaleddouble; }

include/polyscope/point_cloud_color_quantity.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "polyscope/affine_remapper.h"
66
#include "polyscope/color_quantity.h"
7-
#include "polyscope/histogram.h"
87
#include "polyscope/point_cloud.h"
98
#include "polyscope/point_cloud_quantity.h"
109

include/polyscope/point_cloud_scalar_quantity.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#pragma once
44

55
#include "polyscope/affine_remapper.h"
6-
#include "polyscope/histogram.h"
76
#include "polyscope/point_cloud.h"
87
#include "polyscope/render/color_maps.h"
98
#include "polyscope/scalar_quantity.h"

include/polyscope/render/color_maps.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ struct ValueColorMap {
8181
double scaledVal = val * (values.size() - 1);
8282
double lowerVal = std::floor(scaledVal);
8383
double upperBlendVal = scaledVal - lowerVal;
84-
unsigned int lowerInd = static_cast<unsigned int>(lowerVal);
85-
unsigned int upperInd = lowerInd + 1;
84+
int lowerInd = static_cast<unsigned int>(lowerVal);
85+
int upperInd = lowerInd + 1;
86+
87+
lowerInd = std::min(std::max(0, lowerInd), (int)values.size() - 1);
88+
upperInd = std::min(std::max(0, upperInd), (int)values.size() - 1);
8689

8790
return (float)(1.0 - upperBlendVal) * values[lowerInd] + (float)upperBlendVal * values[upperInd];
8891
}

include/polyscope/render/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ class Engine {
614614
std::vector<std::unique_ptr<ValueColorMap>> colorMaps;
615615
const ValueColorMap& getColorMap(const std::string& name);
616616
void loadColorMap(std::string cmapName, std::string filename);
617+
std::shared_ptr<TextureBuffer> getColorMapTexture2d(const std::string& cmapName);
617618

618619
// Helpers
619620
std::vector<glm::vec3> screenTrianglesCoords(); // two triangles which cover the screen

0 commit comments

Comments
 (0)