ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
O3DVisualizerSelections.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <assert.h>
11 
12 #include "ecvMesh.h"
13 #include "ecvPointCloud.h"
18 
19 namespace cloudViewer {
20 namespace visualization {
21 namespace visualizer {
22 
23 namespace {
24 rendering::MaterialRecord MakeMaterial() {
25  rendering::MaterialRecord m;
26  m.shader = "defaultUnlit";
27  m.base_color = {1.0f, 0.0f, 1.0f, 1.0f};
28  return m;
29 }
30 } // namespace
31 
32 // ----------------------------------------------------------------------------
34  : widget3d_(widget3d) {}
35 
37 
39  auto name = std::string("__selection_") + std::to_string(next_id_++);
40  sets_.push_back({name, {}});
41  if (current_set_index_ < 0) {
42  current_set_index_ = int(sets_.size()) - 1;
43  }
44 }
45 
47  auto scene = widget3d_.GetScene();
48  if (scene->HasGeometry(sets_[index].name)) {
49  scene->RemoveGeometry(sets_[index].name);
50  }
51  sets_.erase(sets_.begin() + index);
52  current_set_index_ = std::min(int(sets_.size()) - 1, current_set_index_);
53 
54  if (!sets_.empty() && scene->HasGeometry(sets_[current_set_index_].name)) {
55  scene->ShowGeometry(sets_[current_set_index_].name, true);
56  }
57 }
58 
60  auto scene = widget3d_.GetScene();
61  if (scene->HasGeometry(sets_[current_set_index_].name)) {
62  scene->ShowGeometry(sets_[current_set_index_].name, false);
63  }
64 
65  current_set_index_ = index;
66 
67  if (!sets_.empty() && scene->HasGeometry(sets_[current_set_index_].name)) {
68  scene->ShowGeometry(sets_[current_set_index_].name, true);
69  }
70 }
71 
72 size_t O3DVisualizerSelections::GetNumberOfSets() const { return sets_.size(); }
73 
75  const std::map<std::string,
76  std::vector<std::pair<size_t, Eigen::Vector3d>>>
77  &indices) {
78  if (!sets_.empty()) {
79  auto &selection = sets_[current_set_index_];
80  for (auto &name_indices : indices) {
81  auto &name = name_indices.first;
82  for (auto idx_pt : name_indices.second) {
83  auto &idx = idx_pt.first;
84  auto &p = idx_pt.second;
85  selection.indices[name].insert({idx, pick_order_, p});
86  }
87  }
88  pick_order_ += 1;
89 
90  UpdateSelectionGeometry();
91  }
92 }
93 
95  const std::map<std::string,
96  std::vector<std::pair<size_t, Eigen::Vector3d>>>
97  &indices) {
98  if (!sets_.empty()) {
99  auto &selection = sets_[current_set_index_];
100  for (auto &name_indices : indices) {
101  auto &name = name_indices.first;
102  for (auto idx_pt : name_indices.second) {
103  auto &idx = idx_pt.first;
104  auto &p = idx_pt.second;
105  selection.indices[name].erase({idx, pick_order_, p});
106  }
107  }
108  pick_order_ += 1;
109 
110  UpdateSelectionGeometry();
111  }
112 }
113 
114 void O3DVisualizerSelections::UpdateSelectionGeometry() {
115  auto scene = widget3d_.GetScene();
116  auto &selection = sets_[current_set_index_];
117  if (scene->HasGeometry(selection.name)) {
118  scene->RemoveGeometry(selection.name);
119  }
120  std::vector<Eigen::Vector3d> points;
121  points.reserve(selection.indices.size());
122  for (auto &kv : selection.indices) {
123  for (auto &i : kv.second) {
124  points.push_back(i.point);
125  }
126  }
127 
128  if (!points.empty()) {
129  ccMesh spheres;
130  spheres.CreateInternalCloud();
131  for (auto &p : points) {
132  auto ps = ccMesh::CreateSphere(point_size_, 10);
133  ps->Translate(p);
134  spheres += *ps;
135  }
136  scene->AddGeometry(selection.name, &spheres, MakeMaterial());
137  scene->GetScene()->GeometryShadows(selection.name, false, false);
138  }
139 
140  widget3d_.ForceRedraw();
141 }
142 
143 std::vector<O3DVisualizerSelections::SelectionSet>
145  std::vector<SelectionSet> all;
146  all.reserve(sets_.size());
147  for (auto &s : sets_) {
148  all.push_back(s.indices);
149  }
150  return all;
151 }
152 
153 void O3DVisualizerSelections::SetPointSize(double radius_world) {
154  point_size_ = radius_world;
155  if (IsActive()) {
156  UpdatePointSize();
157  } else {
158  point_size_changed_ = true;
159  }
160 }
161 
163  if (is_active_) {
164  utility::LogError("Already active.");
165  }
166 
167  is_active_ = true;
168  widget3d_.SetViewControls(gui::SceneWidget::Controls::PICK_POINTS);
169  auto scene = widget3d_.GetScene();
170 
171  if (point_size_changed_) {
172  UpdatePointSize();
173  point_size_changed_ = false;
174  }
175 
176  auto &selection = sets_[current_set_index_];
177  if (scene->HasGeometry(selection.name)) {
178  scene->ShowGeometry(selection.name, true);
179  }
180 }
181 
182 bool O3DVisualizerSelections::IsActive() const { return is_active_; }
183 
185  auto scene = widget3d_.GetScene();
186 
187  auto &selection = sets_[current_set_index_];
188  if (scene->HasGeometry(selection.name)) {
189  scene->ShowGeometry(selection.name, false);
190  }
191 
192  is_active_ = false;
193 }
194 
196  const std::vector<gui::SceneWidget::PickableGeometry> &geometry) {
197  widget3d_.SetPickableGeometry(geometry);
198 }
199 
200 void O3DVisualizerSelections::UpdatePointSize() { UpdateSelectionGeometry(); }
201 
202 } // namespace visualizer
203 } // namespace visualization
204 } // namespace cloudViewer
std::string name
int points
Triangular mesh.
Definition: ecvMesh.h:35
static std::shared_ptr< ccMesh > CreateSphere(double radius=1.0, int resolution=20, bool create_uv_map=false)
std::shared_ptr< rendering::CloudViewerScene > GetScene() const
void SetPickableGeometry(const std::vector< PickableGeometry > &geometry)
void SelectIndices(const std::map< std::string, std::vector< std::pair< size_t, Eigen::Vector3d >>> &indices)
void SetSelectableGeometry(const std::vector< gui::SceneWidget::PickableGeometry > &geometry)
void UnselectIndices(const std::map< std::string, std::vector< std::pair< size_t, Eigen::Vector3d >>> &indices)
#define LogError(...)
Definition: Logging.h:60
int min(int a, int b)
Definition: cutil_math.h:53
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20