ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
GuiSettingsModel.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 <Logging.h>
11 
12 namespace cloudViewer {
13 namespace visualization {
14 
15 namespace {
16 // When we move to C++20 it would be nice to use designated initializers
17 // for the lighting profiles and materials.
18 class LightingProfileBuilder {
19 public:
20  LightingProfileBuilder& name(const std::string& name) {
21  profile_.name = name;
22  return *this;
23  }
24  LightingProfileBuilder& ibl_intensity(float x) {
25  profile_.ibl_intensity = x;
26  return *this;
27  }
28  LightingProfileBuilder& sun_intensity(float x) {
29  profile_.sun_intensity = x;
30  return *this;
31  }
32  LightingProfileBuilder& sun_dir(const Eigen::Vector3f& dir) {
33  profile_.sun_dir = dir;
34  return *this;
35  }
36  LightingProfileBuilder& sun_color(const Eigen::Vector3f& c) {
37  profile_.sun_color = c;
38  return *this;
39  }
40  LightingProfileBuilder& ibl_rotation(const rendering::Scene::Transform& t) {
41  profile_.ibl_rotation = t;
42  return *this;
43  }
44  LightingProfileBuilder& ibl_enabled(bool enabled) {
45  profile_.ibl_enabled = enabled;
46  return *this;
47  }
48  LightingProfileBuilder& use_default_ibl(bool use) {
49  profile_.use_default_ibl = use;
50  return *this;
51  }
52  LightingProfileBuilder& sun_enabled(bool enabled) {
53  profile_.sun_enabled = enabled;
54  return *this;
55  }
56 
57  GuiSettingsModel::LightingProfile build() { return profile_; }
58 
59 private:
60  GuiSettingsModel::LightingProfile profile_;
61 };
62 
63 } // namespace
64 
65 // static member definition
66 constexpr const char* GuiSettingsModel::DEFAULT_IBL;
67 constexpr const char* GuiSettingsModel::CUSTOM_IBL;
68 constexpr const char* GuiSettingsModel::DEFAULT_MATERIAL_NAME;
69 constexpr const char* GuiSettingsModel::MATERIAL_FROM_FILE_NAME;
70 constexpr const char* GuiSettingsModel::POINT_CLOUD_PROFILE_NAME;
71 
72 using namespace cloudViewer;
73 
74 const std::vector<GuiSettingsModel::LightingProfile>
76  LightingProfileBuilder()
77  .name("Bright day with sun at +Y [default]")
78  .ibl_intensity(45000)
79  .sun_intensity(45000)
80  .sun_dir({0.577f, -0.577f, -0.577f})
81  .build(),
82  LightingProfileBuilder()
83  .name("Bright day with sun at -Y")
84  .ibl_intensity(45000)
85  .sun_intensity(45000)
86  .sun_dir({0.577f, 0.577f, 0.577f})
87  .sun_color({1.0f, 1.0f, 1.0f})
88  .ibl_rotation(
89  rendering::Scene::Transform(Eigen::AngleAxisf(
90  float(M_PI), Eigen::Vector3f::UnitX())))
91  .build(),
92  LightingProfileBuilder()
93  .name("Bright day with sun at +Z")
94  .ibl_intensity(45000)
95  .sun_intensity(45000)
96  .sun_dir({0.577f, 0.577f, -0.577f})
97  .build(),
98  LightingProfileBuilder()
99  .name("Less bright day with sun at +Y")
100  .ibl_intensity(35000)
101  .sun_intensity(50000)
102  .sun_dir({0.577f, -0.577f, -0.577f})
103  .build(),
104  LightingProfileBuilder()
105  .name("Less bright day with sun at -Y")
106  .ibl_intensity(35000)
107  .sun_intensity(50000)
108  .sun_dir({0.577f, 0.577f, 0.577f})
109  .sun_color({1.0f, 1.0f, 1.0f})
110  .ibl_rotation(
111  rendering::Scene::Transform(Eigen::AngleAxisf(
112  float(M_PI), Eigen::Vector3f::UnitX())))
113  .build(),
114  LightingProfileBuilder()
115  .name("Less bright day with sun at +Z")
116  .ibl_intensity(35000)
117  .sun_intensity(50000)
118  .sun_dir({0.577f, 0.577f, -0.577f})
119  .build(),
120  LightingProfileBuilder()
121  .name(POINT_CLOUD_PROFILE_NAME)
122  .ibl_intensity(60000)
123  .sun_intensity(50000)
124  .sun_dir({0.577f, -0.577f, -0.577f})
125  .sun_color({1.0f, 1.0f, 1.0f})
126  .ibl_rotation(rendering::Scene::Transform::Identity())
127  .ibl_enabled(true)
128  .use_default_ibl(true)
129  .sun_enabled(false)
130  .build()};
131 
132 const std::map<std::string, const GuiSettingsModel::LitMaterial>
134  {DEFAULT_MATERIAL_NAME, {}},
135  {"Metal (rougher)",
136  {{1.0f, 1.0f, 1.0f}, 1.0f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f}},
137  {"Metal (smoother)",
138  {{1.0f, 1.0f, 1.0f}, 1.0f, 0.2f, 0.5f, 0.0f, 0.0f, 0.0f}},
139  {"Plastic",
140  {{1.0f, 1.0f, 1.0f}, 0.0f, 0.5f, 0.5f, 0.5f, 0.8f, 0.0f}},
141  {"Glazed ceramic",
142  {{1.0f, 1.0f, 1.0f}, 0.0f, 0.5f, 0.5f, 1.0f, 0.2f, 0.0f}},
143  {"Clay",
144  {{0.7725f, 0.7725f, 0.7725f},
145  0.0f,
146  1.0f,
147  0.35f,
148  0.0f,
149  0.0f,
150  0.0f}},
151 };
152 
156 }
157 
160  // Lighting profile 0 will always be default even for Point Clouds
162 }
163 
166  if (m != GuiSettingsModel::prefab_materials_.end()) {
167  return m->second;
168  }
169  utility::LogError("Internal Error: could not find default lit material");
170  // Makes compiler happy, but won't get here: LogError() throws exception
171  // (Can throw here because this is a development error, and will happen
172  // early in execution)
173  static LitMaterial errorMaterial;
174  return errorMaterial;
175 }
176 
178  lighting_ = GetDefaultLightingProfile();
179  current_materials_.lit = GetDefaultLitMaterial();
180  current_materials_.lit_name = DEFAULT_MATERIAL_NAME;
181 }
182 
183 bool GuiSettingsModel::GetShowSkybox() const { return show_skybox_; }
185  show_skybox_ = show;
186  NotifyChanged();
187 }
188 
189 bool GuiSettingsModel::GetShowAxes() const { return show_axes_; }
191  show_axes_ = show;
192  NotifyChanged();
193 }
194 
195 bool GuiSettingsModel::GetShowGround() const { return show_ground_; }
197  show_ground_ = show;
198  NotifyChanged();
199 }
200 
201 bool GuiSettingsModel::GetSunFollowsCamera() const { return sun_follows_cam_; }
203  sun_follows_cam_ = follow;
204  NotifyChanged();
205 }
206 
207 bool GuiSettingsModel::GetBasicMode() const { return basic_mode_enabled_; }
209  basic_mode_enabled_ = enable;
210  NotifyChanged(true);
211 }
212 
214  return wireframe_mode_enabled_;
215 }
217  wireframe_mode_enabled_ = enable;
218  NotifyChanged();
219 }
220 
221 const Eigen::Vector3f& GuiSettingsModel::GetBackgroundColor() const {
222  return bg_color_;
223 }
224 void GuiSettingsModel::SetBackgroundColor(const Eigen::Vector3f& color) {
225  bg_color_ = color;
226  NotifyChanged();
227 }
228 
230  return lighting_;
231 }
232 
234  lighting_ = profile;
235  user_has_changed_lighting_profile_ = true;
236  user_has_customized_lighting_ = false;
237  NotifyChanged();
238 }
240  lighting_ = profile;
241  user_has_customized_lighting_ = true;
242  NotifyChanged();
243 }
244 
246  return current_type_;
247 }
249  current_type_ = type;
250  NotifyChanged(true);
251 }
252 
254  const {
255  return current_materials_;
256 }
257 
259  return current_materials_;
260 }
261 
263  const std::string& name) {
264  auto color = current_materials_.lit.base_color;
265  current_materials_.lit = material;
266  current_materials_.lit_name = name;
267  if (user_has_changed_color_) {
268  current_materials_.lit.base_color = color;
269  }
270  NotifyChanged(true);
271 }
272 
274  const std::string& name) {
275  current_materials_ = materials;
276  current_materials_.lit_name = name;
277  NotifyChanged(true);
278 }
279 
280 void GuiSettingsModel::SetCurrentMaterials(const std::string& name) {
281  current_materials_.lit_name = name;
282  NotifyChanged(true);
283 }
284 
286  auto unlit_color = current_materials_.unlit.base_color;
287  auto lit_color = current_materials_.lit.base_color;
288  current_materials_ = Materials();
289  current_materials_.lit_name = DEFAULT_MATERIAL_NAME;
290  if (user_has_changed_color_) {
291  current_materials_.unlit.base_color = unlit_color;
292  current_materials_.lit.base_color = lit_color;
293  }
294  NotifyChanged(true);
295 }
296 
297 const Eigen::Vector3f& GuiSettingsModel::GetCurrentMaterialColor() const {
298  if (user_has_changed_color_) {
299  return current_materials_.lit.base_color;
300  } else {
301  if (current_type_ == LIT) {
302  return current_materials_.lit.base_color;
303  } else if (current_type_ == UNLIT) {
304  return current_materials_.unlit.base_color;
305  } else {
306  static Eigen::Vector3f white = {1.0f, 1.0f, 1.0f};
307  return white;
308  }
309  }
310 }
311 
312 void GuiSettingsModel::SetCurrentMaterialColor(const Eigen::Vector3f& color) {
313  if (current_type_ == LIT) {
314  current_materials_.lit.base_color = color;
315  user_has_changed_color_ = true;
316  NotifyChanged(true);
317  } else if (current_type_ == UNLIT) {
318  current_materials_.unlit.base_color = color;
319  user_has_changed_color_ = true;
320  NotifyChanged(true);
321  } else {
322  // Doesn't make sense to set material color for depth / normal
323  }
324 }
325 
327  if (custom_default_color.x() >= 0.0f) {
328  current_materials_.unlit.base_color = custom_default_color;
329  current_materials_.lit.base_color = custom_default_color;
330  } else {
331  Materials defaults;
332  current_materials_.unlit.base_color = defaults.unlit.base_color;
333  current_materials_.lit.base_color = defaults.lit.base_color;
334  }
335  user_has_changed_color_ = false;
336  NotifyChanged(true);
337 }
338 
339 void GuiSettingsModel::SetCustomDefaultColor(const Eigen::Vector3f color) {
340  custom_default_color = color;
341 }
342 
344  custom_default_color = {-1.0f, -1.0f, -1.0f};
345 }
346 
348  return int(current_materials_.point_size);
349 }
350 
352  current_materials_.point_size = float(size);
353  NotifyChanged(true);
354 }
355 
357  if (user_wants_estimate_normals_) {
358  user_wants_estimate_normals_ = false;
359  return true;
360  } else {
361  return false;
362  }
363 }
364 
366  user_wants_estimate_normals_ = true;
367  NotifyChanged();
368 }
369 
371  return displaying_point_clouds_;
372 }
374  displaying_point_clouds_ = displaying;
375  NotifyChanged();
376 }
377 
379  return user_has_changed_lighting_profile_;
380 }
381 
383  return user_has_customized_lighting_;
384 }
385 
387  return user_has_changed_color_;
388 }
389 
390 void GuiSettingsModel::SetOnChanged(std::function<void(bool)> on_changed) {
391  on_changed_ = on_changed;
392 }
393 
394 void GuiSettingsModel::NotifyChanged(bool material_changed /*= false*/) {
395  if (on_changed_) {
396  on_changed_(material_changed);
397  }
398 }
399 
400 } // namespace visualization
401 } // namespace cloudViewer
constexpr double M_PI
Pi.
Definition: CVConst.h:19
int size
std::string name
char type
math::float4 color
CloudViewerScene::LightingProfile profile
const Eigen::Vector3f & GetBackgroundColor() const
static constexpr const char * DEFAULT_IBL
static constexpr const char * MATERIAL_FROM_FILE_NAME
void SetCustomLighting(const LightingProfile &profile)
void SetCurrentMaterials(const Materials &materials, const std::string &name)
void SetLitMaterial(const LitMaterial &material, const std::string &name)
static const LitMaterial & GetDefaultLitMaterial()
void SetBackgroundColor(const Eigen::Vector3f &color)
void SetCustomDefaultColor(const Eigen::Vector3f color)
void SetCurrentMaterialColor(const Eigen::Vector3f &color)
static constexpr const char * CUSTOM_IBL
void SetDisplayingPointClouds(bool displaying)
If true, enables point size.
static const LightingProfile & GetDefaultLightingProfile()
static constexpr const char * DEFAULT_MATERIAL_NAME
void SetLightingProfile(const LightingProfile &profile)
const Eigen::Vector3f & GetCurrentMaterialColor() const
static const std::map< std::string, const LitMaterial > prefab_materials_
static const LightingProfile & GetDefaultPointCloudLightingProfile()
void SetOnChanged(std::function< void(bool)> on_changed)
static const std::vector< LightingProfile > lighting_profiles_
static constexpr const char * POINT_CLOUD_PROFILE_NAME
const LightingProfile & GetLighting() const
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Scene.h:55
#define LogError(...)
Definition: Logging.h:60
Generic file read and write utility for python interface.
constexpr Rgb white(MAX, MAX, MAX)