-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathpick.cpp
More file actions
265 lines (202 loc) · 8.84 KB
/
pick.cpp
File metadata and controls
265 lines (202 loc) · 8.84 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
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/pick.h"
#include "polyscope/polyscope.h"
#include <limits>
#include <tuple>
#include <unordered_map>
namespace polyscope {
PickResult pickAtScreenCoords(glm::vec2 screenCoords) {
int xInd, yInd;
glm::ivec2 bufferInds = view::screenCoordsToBufferIndsVec(screenCoords);
return pickAtBufferInds(bufferInds);
}
PickResult pickAtBufferInds(glm::ivec2 bufferInds) {
PickResult result;
// Query the pick buffer
// (this necessarily renders to pickFrameBuffer)
std::tuple<Structure*, Quantity*, uint64_t> rawPickResult = pick::evaluatePickQueryFull(bufferInds.x, bufferInds.y);
// Query the depth buffer populated above
render::FrameBuffer* pickFramebuffer = render::engine->pickFramebuffer.get();
float clipDepth = pickFramebuffer->readDepth(bufferInds.x, view::bufferHeight - bufferInds.y);
// Transcribe result into return tuple
result.structure = std::get<0>(rawPickResult);
result.quantity = std::get<1>(rawPickResult);
result.bufferInds = bufferInds;
result.screenCoords = view::bufferIndsToScreenCoords(bufferInds);
result.position = view::screenCoordsAndDepthToWorldPosition(result.screenCoords, clipDepth);
result.depth = glm::length(result.position - view::getCameraWorldPosition());
// null-initialize, might be overwritten below
result.isHit = false;
result.structureType = "";
result.structureName = "";
result.quantityName = "";
result.localIndex = INVALID_IND_64;
if (result.structure != nullptr) {
result.structureHandle = result.structure->getWeakHandle<Structure>();
result.isHit = true;
result.structureType = result.structure->subtypeName;
result.structureName = result.structure->name;
result.localIndex = std::get<2>(rawPickResult);
}
if (result.quantity != nullptr) {
result.isHit = true;
result.quantityName = result.quantity->name;
// ideally we would have typenames and WeakHandles for quantities too, but the classes do not offer either of those
// for now. ONEDAY: add them
result.localIndex = std::get<2>(rawPickResult);
}
return result;
}
// == Manage stateful picking
void resetSelection() {
state::globalContext.haveSelectionVal = false;
state::globalContext.currSelectionPickResult = PickResult();
}
bool haveSelection() { return state::globalContext.haveSelectionVal; }
void resetSelectionIfStructure(Structure* s) {
if (state::globalContext.haveSelectionVal && state::globalContext.currSelectionPickResult.structure == s) {
resetSelection();
}
}
PickResult getSelection() { return state::globalContext.currSelectionPickResult; }
void setSelection(PickResult newPick) {
if (!newPick.isHit) {
resetSelection();
} else {
state::globalContext.haveSelectionVal = true;
state::globalContext.currSelectionPickResult = newPick;
}
}
namespace pick {
// == Set up picking
namespace {
// helper sharing logic for both cases below
uint64_t requestPickBufferRange(Structure* requestingStructure, Quantity* requestingQuantity, uint64_t count) {
if (requestingStructure != nullptr && requestingQuantity != nullptr) {
// this is an either-or function sharing logic, it shouldn't be called with both non-null
throw std::logic_error("only one of the requestPickBufferRange() pointers should be null, not both");
}
// Check if we can satisfy the request
uint64_t maxPickInd = std::numeric_limits<uint64_t>::max();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
if (bitsForPickPacking < 22) {
uint64_t bitMax = 1ULL << (bitsForPickPacking * 3);
if (bitMax < maxPickInd) {
maxPickInd = bitMax;
}
}
#pragma GCC diagnostic pop
if (count > maxPickInd || maxPickInd - count < state::globalContext.nextPickBufferInd) {
exception("Wow, you sure do have a lot of stuff, Polyscope can't even count it all. (Ran out of indices while "
"enumerating structure elements for pick buffer.)");
}
uint64_t ret = state::globalContext.nextPickBufferInd;
state::globalContext.nextPickBufferInd += count;
if (requestingStructure != nullptr) {
state::globalContext.structureRanges[requestingStructure] =
std::make_tuple(ret, state::globalContext.nextPickBufferInd);
}
if (requestingQuantity != nullptr) {
state::globalContext.quantityRanges[requestingQuantity] =
std::make_tuple(ret, state::globalContext.nextPickBufferInd);
}
return ret;
}
} // namespace
uint64_t requestPickBufferRange(Structure* requestingStructure, uint64_t count) {
return requestPickBufferRange(requestingStructure, nullptr, count);
}
uint64_t requestPickBufferRange(Quantity* requestingQuantity, uint64_t count) {
return requestPickBufferRange(nullptr, requestingQuantity, count);
}
// == Helpers
std::tuple<Structure*, Quantity*, uint64_t> globalIndexToLocal(uint64_t globalInd) {
// ONEDAY: this could be asymptotically better if we cared
// Loop through the ranges that we have allocated to find the one corresponding to this structure.
for (const auto& x : state::globalContext.structureRanges) {
Structure* structure = x.first;
uint64_t rangeStart = std::get<0>(x.second);
uint64_t rangeEnd = std::get<1>(x.second);
if (globalInd >= rangeStart && globalInd < rangeEnd) {
return {structure, nullptr, globalInd - rangeStart};
}
}
// Look through quantity ranges
for (const auto& x : state::globalContext.quantityRanges) {
Quantity* quantity = x.first;
uint64_t rangeStart = std::get<0>(x.second);
uint64_t rangeEnd = std::get<1>(x.second);
if (globalInd >= rangeStart && globalInd < rangeEnd) {
// look up the structure that goes with this quantity
Structure* structure = &quantity->parent;
return {structure, quantity, globalInd - rangeStart};
}
}
return {nullptr, nullptr, 0};
}
uint64_t localIndexToGlobal(std::tuple<Structure*, Quantity*, uint64_t> localPick) {
Structure* structurePtr = std::get<0>(localPick);
Quantity* quantityPtr = std::get<1>(localPick);
uint64_t localInd = std::get<2>(localPick);
if (structurePtr == nullptr && quantityPtr == nullptr) return 0;
if (state::globalContext.structureRanges.find(structurePtr) != state::globalContext.structureRanges.end()) {
std::tuple<uint64_t, uint64_t> range = state::globalContext.structureRanges[structurePtr];
uint64_t rangeStart = std::get<0>(range);
uint64_t rangeEnd = std::get<1>(range);
return rangeStart + localInd;
}
if (state::globalContext.quantityRanges.find(quantityPtr) != state::globalContext.quantityRanges.end()) {
std::tuple<uint64_t, uint64_t> range = state::globalContext.quantityRanges[quantityPtr];
uint64_t rangeStart = std::get<0>(range);
uint64_t rangeEnd = std::get<1>(range);
return rangeStart + localInd;
}
exception("structure/quantity does not match any allocated pick range");
return 0;
}
std::pair<Structure*, uint64_t> pickAtScreenCoords(glm::vec2 screenCoords) {
int xInd, yInd;
std::tie(xInd, yInd) = view::screenCoordsToBufferInds(screenCoords);
return pickAtBufferCoords(xInd, yInd);
}
std::pair<Structure*, uint64_t> pickAtBufferCoords(int xPos, int yPos) { return evaluatePickQuery(xPos, yPos); }
std::tuple<Structure*, Quantity*, uint64_t> evaluatePickQueryFull(int xPos, int yPos) {
// NOTE: hack used for debugging: if xPos == yPos == -1 we do a pick render but do not query the value.
// Be sure not to pick outside of buffer
if (xPos < -1 || xPos >= view::bufferWidth || yPos < -1 || yPos >= view::bufferHeight) {
return {nullptr, nullptr, 0};
}
render::FrameBuffer* pickFramebuffer = render::engine->pickFramebuffer.get();
render::engine->setDepthMode(DepthMode::Less);
render::engine->setBlendMode(BlendMode::Disable);
pickFramebuffer->resize(view::bufferWidth, view::bufferHeight);
pickFramebuffer->setViewport(0, 0, view::bufferWidth, view::bufferHeight);
pickFramebuffer->clearColor = glm::vec3{0., 0., 0.};
if (!pickFramebuffer->bindForRendering()) return {nullptr, nullptr, 0};
pickFramebuffer->clear();
// Render pick buffer
for (auto& cat : state::structures) {
for (auto& x : cat.second) {
x.second->drawPick();
}
}
for (auto& catMap : state::structures) {
for (auto& s : catMap.second) {
s.second->drawPickDelayed();
}
}
if (xPos == -1 || yPos == -1) {
return {nullptr, nullptr, 0};
}
// Read from the pick buffer
std::array<float, 4> result = pickFramebuffer->readFloat4(xPos, view::bufferHeight - yPos);
uint64_t globalInd = pick::vecToInd(glm::vec3{result[0], result[1], result[2]});
return pick::globalIndexToLocal(globalInd);
}
std::pair<Structure*, uint64_t> evaluatePickQuery(int xPos, int yPos) {
std::tuple<Structure*, Quantity*, uint64_t> t = evaluatePickQueryFull(xPos, yPos);
return std::pair<Structure*, uint64_t>(std::get<0>(t), std::get<2>(t));
}
} // namespace pick
} // namespace polyscope