-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathhistogram.cpp
More file actions
280 lines (211 loc) · 9 KB
/
histogram.cpp
File metadata and controls
280 lines (211 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/histogram.h"
#include "polyscope/affine_remapper.h"
#include "polyscope/polyscope.h"
#include "imgui.h"
#include <algorithm>
#include <limits>
#include <stdexcept>
namespace polyscope {
Histogram::Histogram() {}
Histogram::Histogram(std::vector<float>& values, DataType dataType) { buildHistogram(values, dataType); }
Histogram::~Histogram() {}
void Histogram::buildHistogram(const std::vector<float>& values, DataType dataType_) {
dataType = dataType_;
// Build arrays of values
size_t N = values.size();
// == Build histogram
dataRange = robustMinMax(values);
colormapRange = dataRange;
// Helper to build the four histogram variants
auto buildCurve = [&](size_t binCount, std::vector<std::array<float, 2>>& curveX, std::vector<float>& curveY) {
// linspace coords
double range = dataRange.second - dataRange.first;
double inc = range / binCount;
std::vector<double> sumBin(binCount, 0.0);
// count values in buckets
for (size_t iData = 0; iData < N; iData++) {
double iBinf = binCount * (values[iData] - dataRange.first) / range;
size_t iBin = std::floor(glm::clamp(iBinf, 0.0, (double)binCount - 1));
// NaN values and finite values near the bottom of float range lead to craziness, so only increment bins if we got
// something reasonable
if (iBin < binCount) {
sumBin[iBin] += 1.0;
}
}
// build histogram coords
curveX = std::vector<std::array<float, 2>>(binCount);
curveY = std::vector<float>(binCount);
double prevSumU = 0.0;
double prevSumW = 0.0;
double prevXEnd = dataRange.first;
for (size_t iBin = 0; iBin < binCount; iBin++) {
// y value
curveY[iBin] = sumBin[iBin];
// x value
double xEnd = prevXEnd + inc;
curveX[iBin] = {{static_cast<float>(prevXEnd), static_cast<float>(xEnd)}};
prevXEnd = xEnd;
}
{ // Rescale curves to [0,1] in both dimensions
double maxHeight = *std::max_element(curveY.begin(), curveY.end());
for (size_t i = 0; i < binCount; i++) {
curveX[i][0] = (curveX[i][0] - dataRange.first) / range;
curveX[i][1] = (curveX[i][1] - dataRange.first) / range;
curveY[i] /= maxHeight;
}
}
};
buildCurve(rawHistBinCount, rawHistCurveX, rawHistCurveY);
}
void Histogram::updateColormap(const std::string& newColormap) {
colormap = newColormap;
if (program) {
program.reset();
}
}
void Histogram::fillBuffers() {
if (rawHistCurveY.size() == 0) {
exception("histogram fillBuffers() called before buildHistogram");
}
// Push to buffer
std::vector<glm::vec2> coords;
float histHeightStart = bottomBarHeight + bottomBarGap;
for (size_t i = 0; i < rawHistCurveX.size(); i++) {
float leftX = rawHistCurveX[i][0];
float rightX = rawHistCurveX[i][1];
float leftYTop = histHeightStart + (1. - histHeightStart) * rawHistCurveY[i];
float rightYTop = histHeightStart + (1. - histHeightStart) * rawHistCurveY[i];
float leftYBot = histHeightStart;
float rightYBot = histHeightStart;
// = Lower triangle (lower left, lower right, upper left)
coords.push_back(glm::vec2{leftX, leftYBot});
coords.push_back(glm::vec2{rightX, rightYBot});
coords.push_back(glm::vec2{leftX, leftYTop});
// = Upper triangle (lower right, upper right, upper left)
coords.push_back(glm::vec2{rightX, rightYBot});
coords.push_back(glm::vec2{rightX, rightYTop});
coords.push_back(glm::vec2{leftX, leftYTop});
}
// the long skinny bar along the bottom, which always shows regardless of histogram values
coords.push_back(glm::vec2{0., 0.});
coords.push_back(glm::vec2{1., 0.});
coords.push_back(glm::vec2{0., bottomBarHeight});
coords.push_back(glm::vec2{1., 0.});
coords.push_back(glm::vec2{1., bottomBarHeight});
coords.push_back(glm::vec2{0., bottomBarHeight});
program->setAttribute("a_coord", coords);
}
void Histogram::prepare() {
framebuffer = render::engine->generateFrameBuffer(texDim, texDim);
texture = render::engine->generateTextureBuffer(TextureFormat::RGBA8, texDim, texDim);
framebuffer->addColorBuffer(texture);
// Create the program
if (dataType == DataType::CATEGORICAL) {
// for categorical data only
program = render::engine->requestShader("HISTOGRAM_CATEGORICAL", {"SHADE_CATEGORICAL_COLORMAP"},
render::ShaderReplacementDefaults::Process);
} else {
// common case
program = render::engine->requestShader("HISTOGRAM", {"SHADE_COLORMAP_VALUE"},
render::ShaderReplacementDefaults::Process);
}
program->setTextureFromColormap("t_colormap", colormap, true);
fillBuffers();
}
void Histogram::renderToTexture() {
if (!program) {
prepare();
}
framebuffer->clearColor = {0.0, 0.0, 0.0};
framebuffer->clearAlpha = 0.2;
framebuffer->setViewport(0, 0, texDim, texDim);
framebuffer->bindForRendering();
framebuffer->clear();
// = Set uniforms
if (dataType == DataType::CATEGORICAL) {
// Used to restore [0,1] tvals to the orininal data range for the categorical int remapping
program->setUniform("u_dataRangeLow", dataRange.first);
program->setUniform("u_dataRangeHigh", dataRange.second);
} else {
// Colormap range (remapped to the 0-1 coords we use)
float rangeLow = (colormapRange.first - dataRange.first) / (dataRange.second - dataRange.first);
float rangeHigh = (colormapRange.second - dataRange.first) / (dataRange.second - dataRange.first);
program->setUniform("u_rangeLow", rangeLow);
program->setUniform("u_rangeHigh", rangeHigh);
}
// Draw
program->draw();
}
void Histogram::buildUI(float width) {
// NOTE: I'm surprised this works, since we're drawing in the middle of imgui's processing. Possible source of bugs?
renderToTexture();
// Compute size for image
float aspect = 4.0;
float w = width;
if (w == -1.0) {
w = .7 * ImGui::GetWindowWidth();
}
float h = w / aspect;
// Render image
ImGui::Image((ImTextureID)(intptr_t)texture->getNativeHandle(), ImVec2(w, h), ImVec2(0, 1), ImVec2(1, 0));
render::engine->preserveResourceUntilImguiFrameCompletes(texture);
// Helpful info for drawing annotations below
ImU32 annoColor = ImGui::ColorConvertFloat4ToU32(ImVec4(254 / 255., 221 / 255., 66 / 255., 1.0));
ImU32 annoColorDark = ImGui::ColorConvertFloat4ToU32(ImVec4(5. / 255., 5. / 255., 5. / 255., 1.0));
ImVec2 imageLowerLeft(ImGui::GetCursorScreenPos().x, ImGui::GetCursorScreenPos().y);
// Draw a cursor popup on mouseover
if (ImGui::IsItemHovered()) {
// Get mouse x coodinate within image
float mouseX = ImGui::GetMousePos().x - ImGui::GetCursorScreenPos().x - ImGui::GetScrollX();
double mouseT = mouseX / w;
double val = dataRange.first + mouseT * (dataRange.second - dataRange.first);
ImGui::SetTooltip("%g", val);
// Draw line
ImVec2 lineStart(imageLowerLeft.x + mouseX, imageLowerLeft.y - h - 3);
ImVec2 lineEnd(imageLowerLeft.x + mouseX, imageLowerLeft.y - 4);
ImGui::GetWindowDrawList()->AddLine(lineStart, lineEnd, annoColor);
}
/* This is pretty neat, but ultimately I decided I don't like the look of it. It has
* some value as a more obvious user widget than dragging imgui's sliders, however.
{ // Draw triangles that indicate where the colormap is
// clang-format off
float markerTriWidth = 0.05;
float imageLeft = imageLowerLeft.x;
float imageRight = imageLeft + w;
float imageTop = imageLowerLeft.y - 4;
float imageBot = imageTop + h;
float leftX = (colormapRange.first - dataRange.first) / (dataRange.second - dataRange.first);
float rightX = (colormapRange.second - dataRange.first) / (dataRange.second - dataRange.first);
float markerTriHeight = bottomBarHeight / 2;
// left triangle
ImGui::GetWindowDrawList()->AddTriangleFilled(
{imageLeft + w*(leftX - markerTriWidth) , imageTop},
{imageLeft + w*(leftX + markerTriWidth) , imageTop},
{imageLeft + w*leftX , imageTop - h*markerTriHeight},
annoColor
);
ImGui::GetWindowDrawList()->AddTriangle(
{imageLeft + w*(leftX - markerTriWidth) , imageTop},
{imageLeft + w*(leftX + markerTriWidth) , imageTop},
{imageLeft + w*leftX , imageTop - h*markerTriHeight},
annoColorDark
);
// right triangle
ImGui::GetWindowDrawList()->AddTriangleFilled(
{imageLeft + w*(rightX - markerTriWidth) , imageTop},
{imageLeft + w*(rightX + markerTriWidth) , imageTop},
{imageLeft + w*rightX , imageTop - h*markerTriHeight},
annoColor
);
ImGui::GetWindowDrawList()->AddTriangle(
{imageLeft + w*(rightX - markerTriWidth) , imageTop},
{imageLeft + w*(rightX + markerTriWidth) , imageTop},
{imageLeft + w*rightX , imageTop - h*markerTriHeight},
annoColorDark
);
// clang-format on
}
*/
}
} // namespace polyscope