-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathOpen3DScene.cpp
More file actions
494 lines (431 loc) · 17.4 KB
/
Copy pathOpen3DScene.cpp
File metadata and controls
494 lines (431 loc) · 17.4 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
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include "open3d/visualization/rendering/Open3DScene.h"
#include <algorithm>
#include "open3d/geometry/Geometry.h"
#include "open3d/geometry/LineSet.h"
#include "open3d/geometry/TriangleMesh.h"
#include "open3d/visualization/gui/Application.h"
#include "open3d/visualization/rendering/MaterialRecord.h"
#include "open3d/visualization/rendering/Scene.h"
#include "open3d/visualization/rendering/View.h"
namespace open3d {
namespace visualization {
namespace rendering {
const std::string kAxisObjectName("__axis__");
const std::string kFastModelObjectSuffix("__fast__");
const std::string kLowQualityModelObjectSuffix("__low__");
namespace {
std::shared_ptr<geometry::TriangleMesh> CreateAxisGeometry(double axis_length) {
const double sphere_radius = 0.005 * axis_length;
const double cyl_radius = 0.0025 * axis_length;
const double cone_radius = 0.0075 * axis_length;
const double cyl_height = 0.975 * axis_length;
const double cone_height = 0.025 * axis_length;
auto mesh_frame = geometry::TriangleMesh::CreateSphere(sphere_radius);
mesh_frame->ComputeVertexNormals();
mesh_frame->PaintUniformColor(Eigen::Vector3d(0.5, 0.5, 0.5));
std::shared_ptr<geometry::TriangleMesh> mesh_arrow;
Eigen::Matrix4d transformation;
mesh_arrow = geometry::TriangleMesh::CreateArrow(cyl_radius, cone_radius,
cyl_height, cone_height);
mesh_arrow->ComputeVertexNormals();
mesh_arrow->PaintUniformColor(Eigen::Vector3d(1.0, 0.0, 0.0));
transformation << 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1;
mesh_arrow->Transform(transformation);
*mesh_frame += *mesh_arrow;
mesh_arrow = geometry::TriangleMesh::CreateArrow(cyl_radius, cone_radius,
cyl_height, cone_height);
mesh_arrow->ComputeVertexNormals();
mesh_arrow->PaintUniformColor(Eigen::Vector3d(0.0, 1.0, 0.0));
transformation << 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1;
mesh_arrow->Transform(transformation);
*mesh_frame += *mesh_arrow;
mesh_arrow = geometry::TriangleMesh::CreateArrow(cyl_radius, cone_radius,
cyl_height, cone_height);
mesh_arrow->ComputeVertexNormals();
mesh_arrow->PaintUniformColor(Eigen::Vector3d(0.0, 0.0, 1.0));
transformation << 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1;
mesh_arrow->Transform(transformation);
*mesh_frame += *mesh_arrow;
// Add UVs because material shader for axes expects them
mesh_frame->triangle_uvs_.resize(mesh_frame->triangles_.size() * 3,
{0.0, 0.0});
return mesh_frame;
}
void RecreateAxis(Scene* scene,
const geometry::AxisAlignedBoundingBox& bounds,
bool enabled) {
scene->RemoveGeometry(kAxisObjectName);
// Axes length should be the longer of the bounds extent or 25% of the
// distance from the origin. The latter is necessary so that the axis is
// big enough to be visible even if the object is far from the origin.
// See caterpillar.ply from Tanks & Temples.
auto axis_length = bounds.GetMaxExtent();
if (axis_length < 0.001) { // avoid div by zero errors in CreateAxes()
axis_length = 1.0;
}
axis_length = std::max(axis_length, 0.25 * bounds.GetCenter().norm());
auto mesh = CreateAxisGeometry(axis_length);
MaterialRecord mat;
mat.shader = "defaultUnlit";
scene->AddGeometry(kAxisObjectName, *mesh, mat);
// It looks awkward to have the axis cast a a shadow, and even stranger
// to receive a shadow.
scene->GeometryShadows(kAxisObjectName, false, false);
scene->ShowGeometry(kAxisObjectName, enabled);
}
} // namespace
Open3DScene::Open3DScene(Renderer& renderer) : renderer_(renderer) {
scene_ = renderer_.CreateScene();
auto scene = renderer_.GetScene(scene_);
view_ = scene->AddView(0, 0, 1, 1);
SetBackground({1.0f, 1.0f, 1.0f, 1.0f});
SetLighting(LightingProfile::MED_SHADOWS, {0.577f, -0.577f, -0.577f});
RecreateAxis(scene, bounds_, false);
}
Open3DScene::~Open3DScene() {
ClearGeometry();
auto scene = renderer_.GetScene(scene_);
scene->RemoveGeometry(kAxisObjectName);
scene->RemoveView(view_);
}
View* Open3DScene::GetView() const {
auto scene = renderer_.GetScene(scene_);
return scene->GetView(view_);
}
void Open3DScene::SetViewport(std::int32_t x,
std::int32_t y,
std::uint32_t width,
std::uint32_t height) {
// Setup the view in which we render to a texture. Since this is just a
// texture, we want our viewport to be the entire texture.
auto view = GetView();
// Since we are rendering into a texture (EnableViewCaching(true) below),
// we need to use the entire texture; the viewport passed in is the viewport
// with respect to the window, and we are setting the viewport with respect
// to the render target here.
view->SetViewport(0, 0, width, height);
view->EnableViewCaching(true);
}
void Open3DScene::ShowSkybox(bool enable) {
auto scene = renderer_.GetScene(scene_);
scene->ShowSkybox(enable);
}
void Open3DScene::ShowAxes(bool enable) {
auto scene = renderer_.GetScene(scene_);
if (enable && axis_dirty_) {
RecreateAxis(scene, bounds_, false);
axis_dirty_ = false;
}
scene->ShowGeometry(kAxisObjectName, enable);
}
void Open3DScene::SetBackground(const Eigen::Vector4f& color,
std::shared_ptr<geometry::Image> image /*=0*/) {
auto scene = renderer_.GetScene(scene_);
scene->SetBackground(color, image);
background_color = color;
}
const Eigen::Vector4f Open3DScene::GetBackgroundColor() const {
return background_color;
}
void Open3DScene::ShowGroundPlane(bool enable, Scene::GroundPlane plane) {
auto scene = renderer_.GetScene(scene_);
scene->EnableGroundPlane(enable, plane);
}
void Open3DScene::SetLighting(LightingProfile profile,
const Eigen::Vector3f& sun_dir) {
auto scene = renderer_.GetScene(scene_);
if (profile != LightingProfile::HARD_SHADOWS) {
if (scene->GetIndirectLight().empty()) {
auto path = gui::Application::GetInstance().GetResourcePath();
scene->SetIndirectLight(std::string(path) + "/default");
}
}
Eigen::Vector3f sun_color(1.0f, 1.0f, 1.0f);
// These intensities have been chosen so that a white object on a white
// background is clearly visible even when the highlight is next to the
// background. Increasing the intensities much more make the highlight's
// white too similar to the background's white.
switch (profile) {
case LightingProfile::HARD_SHADOWS:
scene->EnableIndirectLight(false);
scene->EnableSunLight(true);
scene->SetSunLight(sun_dir, sun_color, 100000);
break;
case LightingProfile::DARK_SHADOWS:
scene->EnableIndirectLight(true);
scene->EnableSunLight(true);
scene->SetIndirectLightIntensity(10000);
scene->SetSunLight(sun_dir, sun_color, 85000);
break;
default:
case LightingProfile::MED_SHADOWS:
scene->EnableIndirectLight(true);
scene->EnableSunLight(true);
scene->SetIndirectLightIntensity(20000);
scene->SetSunLight(sun_dir, sun_color, 80000);
break;
case LightingProfile::SOFT_SHADOWS:
scene->EnableIndirectLight(true);
scene->EnableSunLight(true);
scene->SetIndirectLightIntensity(37500);
scene->SetSunLight(sun_dir, sun_color, 75000);
break;
case LightingProfile::NO_SHADOWS:
scene->EnableIndirectLight(true);
scene->SetIndirectLightIntensity(37500);
scene->EnableSunLight(false);
break;
}
}
void Open3DScene::ClearGeometry() {
auto scene = renderer_.GetScene(scene_);
for (auto& g : geometries_) {
scene->RemoveGeometry(g.second.name);
if (!g.second.fast_name.empty()) {
scene->RemoveGeometry(g.second.fast_name);
}
if (!g.second.low_name.empty()) {
scene->RemoveGeometry(g.second.low_name);
}
}
geometries_.clear();
bounds_ = geometry::AxisAlignedBoundingBox();
axis_dirty_ = true;
}
void Open3DScene::AddGeometry(
const std::string& name,
const geometry::Geometry3D* geom,
const MaterialRecord& mat,
bool add_downsampled_copy_for_fast_rendering /*= true*/) {
size_t downsample_threshold = SIZE_MAX;
std::string fast_name;
if (add_downsampled_copy_for_fast_rendering) {
fast_name = name + "." + kFastModelObjectSuffix;
downsample_threshold = downsample_threshold_;
}
auto scene = renderer_.GetScene(scene_);
if (scene->AddGeometry(name, *geom, mat, fast_name, downsample_threshold)) {
bounds_ += scene->GetGeometryBoundingBox(name);
GeometryData info(name, "");
// If the downsampled object got created, add it. It may not have been
// created if downsampling wasn't enabled or if the object does not meet
// the threshold.
if (add_downsampled_copy_for_fast_rendering &&
scene->HasGeometry(fast_name)) {
info.fast_name = fast_name;
geometries_[fast_name] = info;
}
geometries_[name] = info;
SetGeometryToLOD(info, lod_);
}
axis_dirty_ = true;
}
void Open3DScene::AddGeometry(
const std::string& name,
const t::geometry::Geometry* geom,
const MaterialRecord& mat,
bool add_downsampled_copy_for_fast_rendering /*= true*/) {
size_t downsample_threshold = SIZE_MAX;
std::string fast_name;
if (add_downsampled_copy_for_fast_rendering) {
fast_name = name + "." + kFastModelObjectSuffix;
downsample_threshold = downsample_threshold_;
}
auto scene = renderer_.GetScene(scene_);
if (scene->AddGeometry(name, *geom, mat, fast_name, downsample_threshold)) {
auto bbox = scene->GetGeometryBoundingBox(name);
bounds_ += bbox;
GeometryData info(name, "");
// If the downsampled object got created, add it. It may not have been
// created if downsampling wasn't enabled or if the object does not meet
// the threshold.
if (add_downsampled_copy_for_fast_rendering &&
scene->HasGeometry(fast_name)) {
info.fast_name = fast_name;
auto lowq_name = name + kLowQualityModelObjectSuffix;
auto bbox_geom =
geometry::LineSet::CreateFromAxisAlignedBoundingBox(bbox);
MaterialRecord bbox_mat;
bbox_mat.base_color = {1.0f, 0.5f, 0.0f, 1.0f}; // orange
bbox_mat.shader = "unlitSolidColor";
scene->AddGeometry(lowq_name, *bbox_geom, bbox_mat);
info.low_name = lowq_name;
}
geometries_[name] = info;
SetGeometryToLOD(info, lod_);
}
// Axes may need to be recreated
axis_dirty_ = true;
}
bool Open3DScene::HasGeometry(const std::string& name) const {
auto scene = renderer_.GetScene(scene_);
return scene->HasGeometry(name);
}
void Open3DScene::RemoveGeometry(const std::string& name) {
auto scene = renderer_.GetScene(scene_);
auto g = geometries_.find(name);
if (g != geometries_.end()) {
scene->RemoveGeometry(name);
if (!g->second.fast_name.empty()) {
scene->RemoveGeometry(g->second.fast_name);
}
if (!g->second.low_name.empty()) {
scene->RemoveGeometry(g->second.low_name);
}
geometries_.erase(name);
}
}
bool Open3DScene::GeometryIsVisible(const std::string& name) {
auto scene = renderer_.GetScene(scene_);
return scene->GeometryIsVisible(name);
}
void Open3DScene::SetGeometryTransform(const std::string& name,
const Eigen::Matrix4d& transform) {
auto scene = renderer_.GetScene(scene_);
auto g = geometries_.find(name);
if (g != geometries_.end()) {
const Eigen::Transform<float, 3, Eigen::Affine> t(
transform.cast<float>());
scene->SetGeometryTransform(name, t);
if (!g->second.fast_name.empty()) {
scene->SetGeometryTransform(g->second.fast_name, t);
}
if (!g->second.low_name.empty()) {
scene->SetGeometryTransform(g->second.low_name, t);
}
}
}
Eigen::Matrix4d Open3DScene::GetGeometryTransform(const std::string& name) {
auto scene = renderer_.GetScene(scene_);
return scene->GetGeometryTransform(name).matrix().cast<double>();
}
void Open3DScene::ModifyGeometryMaterial(const std::string& name,
const MaterialRecord& mat) {
auto scene = renderer_.GetScene(scene_);
scene->OverrideMaterial(name, mat);
auto it = geometries_.find(name);
if (it != geometries_.end()) {
if (!it->second.fast_name.empty()) {
scene->OverrideMaterial(it->second.fast_name, mat);
}
// Don't want to override low_name, as that is a bounding box.
}
}
void Open3DScene::SetGeometryBackfaceCulling(const std::string& name,
bool enable) {
auto scene = renderer_.GetScene(scene_);
auto g = geometries_.find(name);
if (g != geometries_.end()) {
scene->SetGeometryBackfaceCulling(name, enable);
if (!g->second.fast_name.empty()) {
scene->SetGeometryBackfaceCulling(g->second.fast_name, enable);
}
}
}
void Open3DScene::ShowGeometry(const std::string& name, bool show) {
auto it = geometries_.find(name);
if (it != geometries_.end()) {
it->second.visible = show;
int n_lowq_visible = 0;
for (auto& g : geometries_) {
if (g.second.visible && !g.second.low_name.empty()) {
n_lowq_visible += 1;
}
}
use_low_quality_if_available_ = (n_lowq_visible > 1);
SetGeometryToLOD(it->second, lod_);
}
}
void Open3DScene::AddModel(const std::string& name,
const TriangleMeshModel& model) {
auto scene = renderer_.GetScene(scene_);
if (scene->AddGeometry(name, model)) {
GeometryData info(name, "");
bounds_ += scene->GetGeometryBoundingBox(name);
geometries_[name] = info;
scene->ShowGeometry(name, true);
}
axis_dirty_ = true;
}
void Open3DScene::UpdateMaterial(const MaterialRecord& mat) {
auto scene = renderer_.GetScene(scene_);
for (auto& g : geometries_) {
scene->OverrideMaterial(g.second.name, mat);
if (!g.second.fast_name.empty()) {
scene->OverrideMaterial(g.second.fast_name, mat);
}
// Low-quality model is a bounding box right now, and we want it to
// be a solid color, so we do not want to override.
// if (!g.second.low_name.empty()) {
// scene->OverrideMaterial(g.second.low_name, mat);
// }
}
}
void Open3DScene::UpdateModelMaterial(const std::string& name,
const TriangleMeshModel& model) {
// Re-apply per-submesh materials in place. This avoids removing and
// re-adding the geometry, which would reset the model's transform and
// visibility state.
auto scene = renderer_.GetScene(scene_);
scene->OverrideMaterial(name, model);
}
std::vector<std::string> Open3DScene::GetGeometries() {
std::vector<std::string> names;
names.reserve(geometries_.size());
for (auto& it : geometries_) {
names.push_back(it.first);
}
return names;
}
void Open3DScene::SetLOD(LOD lod) {
if (lod != lod_) {
lod_ = lod;
for (auto& g : geometries_) {
SetGeometryToLOD(g.second, lod);
}
}
}
void Open3DScene::SetGeometryToLOD(const GeometryData& data, LOD lod) {
auto scene = renderer_.GetScene(scene_);
scene->ShowGeometry(data.name, false);
if (!data.fast_name.empty()) {
scene->ShowGeometry(data.fast_name, false);
}
if (!data.low_name.empty()) {
scene->ShowGeometry(data.low_name, false);
}
if (data.visible) {
if (lod == LOD::HIGH_DETAIL) {
scene->ShowGeometry(data.name, true);
} else {
std::string id;
if (use_low_quality_if_available_) {
id = data.low_name;
}
if (id.empty()) {
id = data.fast_name;
}
if (id.empty()) {
id = data.name;
}
scene->ShowGeometry(id, true);
}
}
}
Open3DScene::LOD Open3DScene::GetLOD() const { return lod_; }
Scene* Open3DScene::GetScene() const { return renderer_.GetScene(scene_); }
Camera* Open3DScene::GetCamera() const {
auto scene = renderer_.GetScene(scene_);
auto view = scene->GetView(view_);
return view->GetCamera();
}
Renderer& Open3DScene::GetRenderer() const { return renderer_; }
} // namespace rendering
} // namespace visualization
} // namespace open3d