-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathcamera_view.cpp
More file actions
518 lines (400 loc) · 16.6 KB
/
camera_view.cpp
File metadata and controls
518 lines (400 loc) · 16.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/camera_view.h"
#include "polyscope/file_helpers.h"
#include "polyscope/pick.h"
#include "polyscope/polyscope.h"
#include "polyscope/render/engine.h"
#include "polyscope/point_cloud_color_quantity.h"
#include "polyscope/point_cloud_scalar_quantity.h"
#include "polyscope/point_cloud_vector_quantity.h"
#include "imgui.h"
#include "polyscope/view.h"
#include <fstream>
#include <iostream>
namespace polyscope {
// Initialize statics
const std::string CameraView::structureTypeName = "Camera View";
// Constructor
CameraView::CameraView(std::string name, const CameraParameters& params_)
: QuantityStructure<CameraView>(name, structureTypeName), params(params_),
widgetFocalLength(uniquePrefix() + "#widgetFocalLength", relativeValue(0.05)),
widgetThickness(uniquePrefix() + "#widgetThickness", 0.02),
widgetColor(uniquePrefix() + "#widgetColor", glm::vec3{0., 0., 0.}) {
if (options::warnForInvalidValues) {
if (!params.isfinite()) {
warning("Invalid +-inf or NaN values detected",
"in camera view parameters: " + name + "\n(set warnForInvalidValues=false to disable)");
}
}
updateObjectSpaceBounds();
}
void CameraView::draw() {
if (!isEnabled()) {
return;
}
// Ensure we have prepared buffers
if (nodeProgram == nullptr || edgeProgram == nullptr) {
prepare();
}
// The camera frame geometry attributes depend on the scene length scale. If the length scale has changed, regenerate
// those attributes. (It would be better if we could implement the frame geometry in uniforms only, so we don't have
// to do this)
if (preparedLengthScale != state::lengthScale) {
fillCameraWidgetGeometry(nodeProgram.get(), edgeProgram.get(), nullptr);
}
// Set program uniforms
setStructureUniforms(*nodeProgram);
setStructureUniforms(*edgeProgram);
glm::mat4 P = view::getCameraPerspectiveMatrix();
glm::mat4 Pinv = glm::inverse(P);
nodeProgram->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
nodeProgram->setUniform("u_viewport", render::engine->getCurrentViewport());
nodeProgram->setUniform("u_pointRadius", getWidgetFocalLength() * getWidgetThickness());
nodeProgram->setUniform("u_baseColor", widgetColor.get());
edgeProgram->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
edgeProgram->setUniform("u_viewport", render::engine->getCurrentViewport());
edgeProgram->setUniform("u_radius", getWidgetFocalLength() * getWidgetThickness());
edgeProgram->setUniform("u_baseColor", widgetColor.get());
render::engine->setMaterialUniforms(*nodeProgram, material);
render::engine->setMaterialUniforms(*edgeProgram, material);
// Draw the camera view wireframe
nodeProgram->draw();
edgeProgram->draw();
render::engine->applyTransparencySettings();
// Draw the quantities
for (auto& x : quantities) {
x.second->draw();
}
for (auto& x : floatingQuantities) {
x.second->draw();
}
}
void CameraView::drawDelayed() {
if (!isEnabled()) {
return;
}
for (auto& x : quantities) {
x.second->drawDelayed();
}
for (auto& x : floatingQuantities) {
x.second->drawDelayed();
}
}
void CameraView::drawPick() {
if (!isEnabled()) {
return;
}
// Ensure we have prepared buffers
if (pickFrameProgram == nullptr) {
preparePick();
}
// The camera frame geometry attributes depend on the scene length scale. If the length scale has changed, regenerate
// those attributes. (It would be better if we could implement the frame geometry in uniforms only, so we don't have
// to do this)
if (pickPreparedLengthScale != state::lengthScale) {
fillCameraWidgetGeometry(nullptr, nullptr, pickFrameProgram.get());
}
// Set uniforms
setStructureUniforms(*pickFrameProgram);
pickFrameProgram->setUniform("u_vertPickRadius", 0.);
pickFrameProgram->draw();
for (auto& x : quantities) {
x.second->drawPick();
}
for (auto& x : floatingQuantities) {
x.second->drawPick();
}
}
void CameraView::drawPickDelayed() {
if (!isEnabled()) {
return;
}
for (auto& x : quantities) {
x.second->drawPickDelayed();
}
for (auto& x : floatingQuantities) {
x.second->drawPickDelayed();
}
}
void CameraView::prepare() {
{
std::vector<std::string> rules = addStructureRules({"SHADE_BASECOLOR"});
if (wantsCullPosition()) rules.push_back("SPHERE_CULLPOS_FROM_CENTER");
rules = render::engine->addMaterialRules(material, rules);
nodeProgram = render::engine->requestShader("RAYCAST_SPHERE", rules);
}
{
std::vector<std::string> rules = addStructureRules({"SHADE_BASECOLOR"});
if (wantsCullPosition()) rules.push_back("CYLINDER_CULLPOS_FROM_MID");
rules = render::engine->addMaterialRules(material, rules);
edgeProgram = render::engine->requestShader("RAYCAST_CYLINDER", rules);
}
render::engine->setMaterial(*nodeProgram, material);
render::engine->setMaterial(*edgeProgram, material);
// Fill out the geometry data for the programs
fillCameraWidgetGeometry(nodeProgram.get(), edgeProgram.get(), nullptr);
}
void CameraView::preparePick() {
// Request pick indices if we don't already have them
if (pickStart == INVALID_IND) {
size_t pickCount = 1;
pickStart = pick::requestPickBufferRange(this, pickCount);
pickColor = pick::indToVec(pickStart);
}
// Create a new pick program
std::vector<std::string> rules = addStructureRules({"MESH_PROPAGATE_PICK_SIMPLE"});
if (wantsCullPosition()) rules.push_back("MESH_PROPAGATE_CULLPOS");
pickFrameProgram = render::engine->requestShader("MESH", rules, render::ShaderReplacementDefaults::Pick);
// Store data in buffers
fillCameraWidgetGeometry(nullptr, nullptr, pickFrameProgram.get());
}
void CameraView::fillCameraWidgetGeometry(render::ShaderProgram* nodeProgram, render::ShaderProgram* edgeProgram,
render::ShaderProgram* pickFrameProgram) {
// NOTE: this coullllld be done with uniforms, so we don't have to ever edit the geometry at all.
// FOV slightly tricky though.
// Camera frame geometry
// NOTE: some of this is duplicated in getFrameBillboardGeometry()
glm::vec3 root = params.getPosition();
glm::vec3 lookDir, upDir, rightDir;
std::tie(lookDir, upDir, rightDir) = params.getCameraFrame();
glm::vec3 frameCenter = root + lookDir * widgetFocalLength.get().asAbsolute();
float halfHeight = static_cast<float>(widgetFocalLength.get().asAbsolute() *
std::tan(glm::radians(params.getFoVVerticalDegrees()) / 2.));
glm::vec3 frameUp = upDir * halfHeight;
float halfWidth = params.getAspectRatioWidthOverHeight() * halfHeight;
glm::vec3 frameLeft = -glm::cross(lookDir, upDir) * halfWidth;
glm::vec3 frameUpperLeft = frameCenter + frameUp + frameLeft;
glm::vec3 frameUpperRight = frameCenter + frameUp - frameLeft;
glm::vec3 frameLowerLeft = frameCenter - frameUp + frameLeft;
glm::vec3 frameLowerRight = frameCenter - frameUp - frameLeft;
glm::vec3 triangleLeft = frameCenter + 1.2f * frameUp + 0.7f * frameLeft;
glm::vec3 triangleRight = frameCenter + 1.2f * frameUp - 0.7f * frameLeft;
glm::vec3 triangleTop = frameCenter + 2.f * frameUp;
if (nodeProgram) {
std::vector<glm::vec3> allPos{root, frameUpperLeft, frameUpperRight, frameLowerLeft, frameLowerRight,
triangleTop, triangleLeft, triangleRight};
nodeProgram->setAttribute("a_position", allPos);
preparedLengthScale = state::lengthScale;
}
if (edgeProgram) {
// Fill edges
std::vector<glm::vec3> posTail(11);
std::vector<glm::vec3> posTip(11);
auto addEdge = [&](glm::vec3 a, glm::vec3 b) {
posTail.push_back(a);
posTip.push_back(b);
};
addEdge(root, frameUpperLeft);
addEdge(root, frameUpperRight);
addEdge(root, frameLowerLeft);
addEdge(root, frameLowerRight);
addEdge(frameUpperLeft, frameUpperRight);
addEdge(frameUpperRight, frameLowerRight);
addEdge(frameLowerRight, frameLowerLeft);
addEdge(frameLowerLeft, frameUpperLeft);
addEdge(triangleLeft, triangleRight);
addEdge(triangleRight, triangleTop);
addEdge(triangleTop, triangleLeft);
edgeProgram->setAttribute("a_position_tail", posTail);
edgeProgram->setAttribute("a_position_tip", posTip);
}
if (pickFrameProgram) {
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
std::vector<glm::vec3> bcoord;
std::vector<glm::vec3> cullPos;
auto addPolygon = [&](std::vector<glm::vec3> vertices) {
size_t D = vertices.size();
// implicitly triangulate from root
glm::vec3 pRoot = vertices[0];
for (size_t j = 1; (j + 1) < D; j++) {
glm::vec3 pB = vertices[j];
glm::vec3 pC = vertices[(j + 1) % D];
glm::vec3 faceN = glm::cross(pB - pRoot, pC - pRoot);
// Vertex positions
positions.push_back(pRoot);
positions.push_back(pB);
positions.push_back(pC);
// Face normals
normals.push_back(faceN);
normals.push_back(faceN);
normals.push_back(faceN);
// Bary coords
bcoord.push_back(glm::vec3{1., 0., 0.});
bcoord.push_back(glm::vec3{0., 1., 0.});
bcoord.push_back(glm::vec3{0., 0., 1.});
// Cull position
cullPos.push_back(root);
cullPos.push_back(root);
cullPos.push_back(root);
}
pickPreparedLengthScale = state::lengthScale;
};
addPolygon({root, frameUpperRight, frameUpperLeft});
addPolygon({root, frameLowerRight, frameUpperRight});
addPolygon({root, frameLowerLeft, frameLowerRight});
addPolygon({root, frameUpperLeft, frameLowerLeft});
addPolygon({frameUpperLeft, frameUpperRight, frameLowerRight, frameLowerLeft});
addPolygon({triangleTop, triangleRight, triangleLeft});
pickFrameProgram->setAttribute("a_vertexPositions", positions);
if (pickFrameProgram->hasAttribute("a_vertexNormals")) {
// // this is not actually used, but it only gets optimized out on some platforms, not all
pickFrameProgram->setAttribute("a_vertexNormals", normals);
}
pickFrameProgram->setAttribute("a_barycoord", bcoord);
size_t nFaces = 7;
std::vector<glm::vec3> faceColor(3 * nFaces, pickColor);
std::vector<std::array<glm::vec3, 3>> tripleColors(3 * nFaces,
std::array<glm::vec3, 3>{pickColor, pickColor, pickColor});
std::shared_ptr<render::AttributeBuffer> tripleColorsBuff =
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
tripleColorsBuff->setData(tripleColors);
pickFrameProgram->setAttribute("a_vertexColors", tripleColorsBuff);
pickFrameProgram->setAttribute("a_faceColor", faceColor);
if (wantsCullPosition()) {
pickFrameProgram->setAttribute("a_cullPos", cullPos);
}
}
}
void CameraView::updateCameraParameters(const CameraParameters& newParams) {
params = newParams;
geometryChanged();
}
void CameraView::geometryChanged() {
// if the programs are populated, repopulate them
if (nodeProgram) {
fillCameraWidgetGeometry(nodeProgram.get(), edgeProgram.get(), nullptr);
}
if (pickFrameProgram) {
fillCameraWidgetGeometry(nullptr, nullptr, pickFrameProgram.get());
}
requestRedraw();
QuantityStructure<CameraView>::refresh();
}
CameraViewPickResult CameraView::interpretPickResult(const PickResult& rawResult) {
if (rawResult.structure != this) {
// caller must ensure that the PickResult belongs to this structure
// by checking the structure pointer or name
exception("called interpretPickResult(), but the pick result is not from this structure");
}
CameraViewPickResult result;
// currently nothing
return result;
}
void CameraView::buildPickUI(const PickResult& rawResult) {
CameraViewPickResult result = interpretPickResult(rawResult);
ImGui::Text("center: %s", to_string(params.getPosition()).c_str());
ImGui::Text("look dir: %s", to_string(params.getLookDir()).c_str());
ImGui::Text("up dir: %s", to_string(params.getUpDir()).c_str());
ImGui::Text("FoV (vert): %0.1f deg aspect ratio: %.2f", params.getFoVVerticalDegrees(),
params.getAspectRatioWidthOverHeight());
if (ImGui::Button("fly to")) {
setViewToThisCamera(true);
}
ImGui::Spacing();
ImGui::Indent(20.);
// Build GUI to show the quantities
// TODO this is inconsistently supported for other structures
ImGui::Columns(2);
ImGui::SetColumnWidth(0, ImGui::GetWindowWidth() / 3);
for (auto& x : quantities) {
x.second->buildPickUI(rawResult.localIndex);
}
ImGui::Indent(-20.);
}
void CameraView::buildCustomUI() {
ImGui::SameLine();
{ // colors
if (ImGui::ColorEdit3("Color", &widgetColor.get()[0], ImGuiColorEditFlags_NoInputs))
setWidgetColor(widgetColor.get());
}
if (ImGui::Button("fly to")) {
setViewToThisCamera(true);
}
ImGui::SameLine();
ImGui::Text("FoV: %0.1f deg aspect: %.2f", params.getFoVVerticalDegrees(), params.getAspectRatioWidthOverHeight());
}
void CameraView::buildCustomOptionsUI() {
ImGui::PushItemWidth(150 * options::uiScale);
if (widgetFocalLengthUpper == -777) widgetFocalLengthUpper = 2. * (*widgetFocalLength.get().getValuePtr());
if (ImGui::SliderFloat("widget focal length", widgetFocalLength.get().getValuePtr(), 0, widgetFocalLengthUpper,
"%.5f")) {
widgetFocalLength.manuallyChanged();
geometryChanged();
requestRedraw();
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
// the upper bound for the slider is dynamically adjust to be a bit bigger than the lower bound, but only does
// so on release of the widget (so it doesn't scaleo off to infinity)
widgetFocalLengthUpper = std::fmax(2. * (*widgetFocalLength.get().getValuePtr()), 0.0001);
}
if (ImGui::SliderFloat("widget thickness", &widgetThickness.get(), 0, 0.2, "%.5f")) {
widgetThickness.manuallyChanged();
requestRedraw();
}
ImGui::PopItemWidth();
}
void CameraView::updateObjectSpaceBounds() {
// bounding box is just the camera root location
glm::vec3 cameraPos = params.getPosition();
objectSpaceBoundingBox = std::make_tuple(cameraPos, cameraPos);
// no is there an obvious length scale...?
// we don't use it for much currently
objectSpaceLengthScale = 0.;
}
std::string CameraView::typeName() { return structureTypeName; }
void CameraView::refresh() {
nodeProgram.reset();
edgeProgram.reset();
pickFrameProgram.reset();
QuantityStructure<CameraView>::refresh(); // call base class version, which refreshes quantities
}
void CameraView::setViewToThisCamera(bool withFlight) {
// Adjust the params to push the view forward by eps so it doesn't clip into the frame
glm::vec3 look, up, right;
std::tie(look, up, right) = params.getCameraFrame();
glm::vec3 root = params.getPosition();
root += look * getWidgetFocalLength() * 0.01f;
CameraParameters adjParams(params.intrinsics, CameraExtrinsics::fromVectors(root, look, up));
if (withFlight) {
view::startFlightTo(adjParams);
} else {
view::setViewToCamera(adjParams);
}
}
// === Quantities
// === Setters and getters
CameraParameters CameraView::getCameraParameters() const { return params; }
CameraView* CameraView::setWidgetFocalLength(float newVal, bool isRelative) {
widgetFocalLength = ScaledValue<float>(newVal, isRelative);
geometryChanged();
polyscope::requestRedraw();
return this;
}
float CameraView::getWidgetFocalLength() { return widgetFocalLength.get().asAbsolute(); }
CameraView* CameraView::setWidgetThickness(float newVal) {
widgetThickness = newVal;
polyscope::requestRedraw();
return this;
}
float CameraView::getWidgetThickness() { return widgetThickness.get(); }
CameraView* CameraView::setWidgetColor(glm::vec3 val) {
widgetColor = val;
requestRedraw();
return this;
}
glm::vec3 CameraView::getWidgetColor() { return widgetColor.get(); }
std::tuple<glm::vec3, glm::vec3, glm::vec3> CameraView::getFrameBillboardGeometry() {
// NOTE: duplicated from fillCameraWidgetGeometry()
glm::vec3 root = params.getPosition();
glm::vec3 lookDir, upDir, rightDir;
std::tie(lookDir, upDir, rightDir) = params.getCameraFrame();
glm::vec3 frameCenter = root + lookDir * widgetFocalLength.get().asAbsolute();
float halfHeight = static_cast<float>(widgetFocalLength.get().asAbsolute() *
std::tan(glm::radians(params.getFoVVerticalDegrees()) / 2.));
glm::vec3 frameUp = upDir * halfHeight;
float halfWidth = params.getAspectRatioWidthOverHeight() * halfHeight;
glm::vec3 frameRight = glm::cross(lookDir, upDir) * halfWidth;
return std::tuple<glm::vec3, glm::vec3, glm::vec3>(frameCenter, frameUp, frameRight);
}
} // namespace polyscope