Gazebo Rendering

API Reference

7.4.2
gz/rendering/base/BaseVisual.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifndef GZ_RENDERING_BASE_BASEVISUAL_HH_
18 #define GZ_RENDERING_BASE_BASEVISUAL_HH_
19 
20 #include <map>
21 #include <string>
22 
23 #include <gz/math/AxisAlignedBox.hh>
24 
25 #include "gz/rendering/Visual.hh"
26 #include "gz/rendering/Storage.hh"
29 
30 namespace gz
31 {
32  namespace rendering
33  {
34  inline namespace GZ_RENDERING_VERSION_NAMESPACE {
35  //
36  template <class T>
37  class BaseVisual :
38  public virtual Visual,
39  public virtual T
40  {
41  protected: BaseVisual();
42 
43  public: virtual ~BaseVisual();
44 
45  public: virtual math::Pose3d LocalPose() const override;
46 
47  public: virtual void SetLocalPose(const math::Pose3d &_pose) override;
48 
49  public: virtual unsigned int GeometryCount() const override;
50 
51  public: virtual bool HasGeometry(ConstGeometryPtr _geometry) const
52  override;
53 
54  public: virtual GeometryPtr GeometryByIndex(unsigned int _index) const
55  override;
56 
57  public: virtual void AddGeometry(GeometryPtr _geometry) override;
58 
59  public: virtual GeometryPtr RemoveGeometry(GeometryPtr _geometry)
60  override;
61 
62  public: virtual GeometryPtr RemoveGeometryByIndex(unsigned int _index)
63  override;
64 
65  public: virtual void RemoveGeometries() override;
66 
67  public: virtual void SetMaterial(const std::string &_name,
68  bool _unique = true) override;
69 
70  public: virtual void SetMaterial(MaterialPtr _material,
71  bool _unique = true) override;
72 
73  public: virtual void SetChildMaterial(MaterialPtr _material,
74  bool _unique = true) override;
75 
76  public: virtual void SetGeometryMaterial(MaterialPtr _material,
77  bool _unique = true) override;
78 
79  // Documentation inherited.
80  public: virtual MaterialPtr Material() const override;
81 
82  // Documentation inherited.
83  public: virtual void SetWireframe(bool _show) override;
84 
85  // Documentation inherited.
86  public: virtual bool Wireframe() const override;
87 
88  // Documentation inherited.
89  public: virtual void SetVisible(bool _visible) override;
90 
91  // Documentation inherited.
92  public: virtual void SetVisibilityFlags(uint32_t _flags) override;
93 
94  // Documentation inherited.
95  public: virtual uint32_t VisibilityFlags() const override;
96 
97  // Documentation inherited.
98  public: virtual void AddVisibilityFlags(uint32_t _flags) override;
99 
100  // Documentation inherited.
101  public: virtual void RemoveVisibilityFlags(uint32_t _flags) override;
102 
103  // Documentation inherited.
104  public: virtual void PreRender() override;
105 
106  // Documentation inherited
107  public: virtual void Destroy() override;
108 
109  // Documentation inherited.
110  public: virtual gz::math::AxisAlignedBox BoundingBox()
111  const override;
112 
113  // Documentation inherited.
114  public: virtual gz::math::AxisAlignedBox LocalBoundingBox()
115  const override;
116 
117  // Documentation inherited.
118  public: virtual VisualPtr Clone(const std::string &_name,
119  NodePtr _newParent) const override;
120 
121  protected: virtual void PreRenderChildren() override;
122 
123  protected: virtual void PreRenderGeometries();
124 
125  protected: virtual GeometryStorePtr Geometries() const = 0;
126 
127  protected: virtual bool AttachGeometry(GeometryPtr _geometry) = 0;
128 
129  protected: virtual bool DetachGeometry(GeometryPtr _geometry) = 0;
130 
132  protected: MaterialPtr material;
133 
135  protected: uint32_t visibilityFlags = GZ_VISIBILITY_ALL;
136 
139 
141  protected: bool wireframe = false;
142  };
143 
145  template <class T>
147  {
148  }
149 
151  template <class T>
153  {
154  }
155 
157  template <class T>
159  {
160  math::Pose3d rawPose = this->RawLocalPose();
161  math::Vector3d scale = this->LocalScale();
162  rawPose.Pos() += rawPose.Rot() * (scale * this->origin);
163  return rawPose;
164  }
165 
167  template <class T>
169  {
170  math::Pose3d rawPose = _pose;
171  math::Vector3d scale = this->LocalScale();
172  rawPose.Pos() -= rawPose.Rot() * (scale * this->origin);
173 
174  if (!rawPose.IsFinite())
175  {
176  gzerr << "Unable to set pose of a node: "
177  << "non-finite (nan, inf) values detected." << std::endl;
178  return;
179  }
180 
181  this->SetRawLocalPose(rawPose);
182  }
183 
185  template <class T>
186  unsigned int BaseVisual<T>::GeometryCount() const
187  {
188  return this->Geometries()->Size();
189  }
190 
192  template <class T>
194  {
195  return this->Geometries()->Contains(_geometry);
196  }
197 
199  template <class T>
200  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
201  {
202  return this->Geometries()->GetByIndex(_index);
203  }
204 
206  template <class T>
208  {
209  if (this->AttachGeometry(_geometry))
210  {
211  this->Geometries()->Add(_geometry);
212  }
213  }
214 
216  template <class T>
218  {
219  if (this->DetachGeometry(_geometry))
220  {
221  this->Geometries()->Remove(_geometry);
222  }
223  return _geometry;
224  }
225 
227  template <class T>
229  {
230  return this->RemoveGeometry(this->GeometryByIndex(_index));
231  }
232 
234  template <class T>
236  {
237  for (unsigned int i = this->GeometryCount(); i > 0; --i)
238  {
239  this->RemoveGeometryByIndex(i-1);
240  }
241  }
242 
244  template <class T>
245  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
246  {
247  MaterialPtr mat = this->Scene()->Material(_name);
248  if (mat) this->SetMaterial(mat, _unique);
249  }
250 
252  template <class T>
253  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
254  {
255  _material = (_unique) ? _material->Clone() : _material;
256  this->SetChildMaterial(_material, false);
257  this->SetGeometryMaterial(_material, false);
258  this->material = _material;
259  }
260 
262  template <class T>
263  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
264  {
265  unsigned int count = this->ChildCount();
266  _material = (_unique && count > 0) ? _material->Clone() : _material;
267 
268  auto children_ =
269  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
270  this->Children());
271  if (!children_)
272  {
273  gzerr << "Cast failed in BaseVisual::SetChildMaterial" << std::endl;
274  return;
275  }
276  for (auto it = children_->Begin(); it != children_->End(); ++it)
277  {
278  NodePtr child = it->second;
279  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
280  if (visual) visual->SetMaterial(_material, false);
281  }
282  }
283 
285  template <class T>
286  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
287  {
288  unsigned int count = this->GeometryCount();
289  _material = (_unique && count > 0) ? _material->Clone() : _material;
290 
291  for (unsigned int i = 0; i < count; ++i)
292  {
293  GeometryPtr geometry = this->GeometryByIndex(i);
294  geometry->SetMaterial(_material, false);
295  }
296  }
297 
299  template <class T>
301  {
302  return this->material;
303  }
304 
306  template <class T>
308  {
309  T::PreRender();
310  this->PreRenderChildren();
311  this->PreRenderGeometries();
312  }
313 
315  template <class T>
317  {
318  this->Geometries()->DestroyAll();
319  this->Children()->RemoveAll();
320  this->material.reset();
321  T::Destroy();
322  }
323 
325  template <class T>
327  {
328  auto children_ =
329  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
330  this->Children());
331  if (!children_)
332  {
333  gzerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
334  return;
335  }
336  for (auto it = children_->Begin(); it != children_->End(); ++it)
337  {
338  it->second->PreRender();
339  }
340  }
341 
343  template <class T>
345  {
346  unsigned int count = this->GeometryCount();
347 
348  for (unsigned int i = 0; i < count; ++i)
349  {
350  GeometryPtr geometry = this->GeometryByIndex(i);
351  geometry->PreRender();
352  }
353  }
354 
356  template <class T>
358  {
359  return this->wireframe;
360  }
361 
363  template <class T>
365  {
366  gzerr << "SetWireframe(" << _show << ") not supported for "
367  << "render engine: " << this->Scene()->Engine()->Name()
368  << std::endl;
369  }
370 
372  template <class T>
373  void BaseVisual<T>::SetVisible(bool _visible)
374  {
375  gzerr << "SetVisible(" << _visible << ") not supported for "
376  << "render engine: " << this->Scene()->Engine()->Name()
377  << std::endl;
378  }
379 
381  template <class T>
383  {
385 
386  // Recursively loop through child visuals
387  auto childNodes =
388  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
389  this->Children());
390  if (!childNodes)
391  {
392  gzerr << "Cast failed in BaseVisual::LocalBoundingBox" << std::endl;
393  return box;
394  }
395  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
396  {
397  NodePtr child = it->second;
398  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
399  if (visual)
400  {
401  gz::math::AxisAlignedBox aabb = visual->LocalBoundingBox();
402  if (aabb.Min().IsFinite() && aabb.Max().IsFinite())
403  box.Merge(aabb);
404  }
405  }
406  return box;
407  }
408 
410  template <class T>
412  {
414 
415  // Recursively loop through child visuals
416  auto childNodes =
417  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
418  this->Children());
419  if (!childNodes)
420  {
421  gzerr << "Cast failed in BaseVisual::BoundingBox" << std::endl;
422  return box;
423  }
424  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
425  {
426  NodePtr child = it->second;
427  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
428  if (visual)
429  box.Merge(visual->BoundingBox());
430  }
431  return box;
432  }
433 
435  template <class T>
436  void BaseVisual<T>::AddVisibilityFlags(uint32_t _flags)
437  {
438  this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
439  }
440 
442  template <class T>
444  {
445  this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
446  }
447 
449  template <class T>
450  void BaseVisual<T>::SetVisibilityFlags(uint32_t _flags)
451  {
452  this->visibilityFlags = _flags;
453 
454  // recursively set child visuals' visibility flags
455  auto childNodes =
456  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
457  this->Children());
458  if (!childNodes)
459  {
460  gzerr << "Cast failed in BaseVisual::SetVisibiltyFlags" << std::endl;
461  return;
462  }
463  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
464  {
465  NodePtr child = it->second;
466  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
467  if (visual)
468  visual->SetVisibilityFlags(_flags);
469  }
470  }
471 
473  template <class T>
475  {
476  return this->visibilityFlags;
477  }
478 
480  template <class T>
482  NodePtr _newParent) const
483  {
484  ScenePtr scene_ = this->Scene();
485  if (nullptr == scene_)
486  {
487  gzerr << "Cloning a visual failed because the visual to be cloned is "
488  << "not attached to a scene.\n";
489  return nullptr;
490  }
491  VisualPtr result;
492  if (_name.empty())
493  result = scene_->CreateVisual();
494  else
495  result = scene_->CreateVisual(_name);
496 
497  if (nullptr != _newParent)
498  {
499  auto parentScene = _newParent->Scene();
500  if (nullptr != parentScene && parentScene->Id() != scene_->Id())
501  {
502  gzerr << "Cloning a visual failed because the desired parent of the "
503  << "cloned visual belongs to a different scene.\n";
504  scene_->DestroyVisual(result);
505  return nullptr;
506  }
507  _newParent->AddChild(result);
508  }
509 
510  result->SetOrigin(this->Origin());
511  result->SetInheritScale(this->InheritScale());
512  result->SetLocalScale(this->LocalScale());
513  result->SetLocalPose(this->LocalPose());
514  result->SetVisibilityFlags(this->VisibilityFlags());
515  result->SetWireframe(this->Wireframe());
516 
517  // if the visual that was cloned has child visuals, clone those as well
518  auto children_ =
519  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
520  this->Children());
521  if (!children_)
522  {
523  gzerr << "Cast failed in BaseVisual::Clone\n";
524  scene_->DestroyVisual(result);
525  return nullptr;
526  }
527  for (auto it = children_->Begin(); it != children_->End(); ++it)
528  {
529  NodePtr child = it->second;
530  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
531  // recursively delete all cloned visuals if the child cannot be
532  // retrieved, or if cloning the child visual failed
533  if (!visual || !visual->Clone("", result))
534  {
535  gzerr << "Cloning a child visual failed.\n";
536  scene_->DestroyVisual(result, true);
537  return nullptr;
538  }
539  }
540 
541  for (unsigned int i = 0; i < this->GeometryCount(); ++i)
542  result->AddGeometry(this->GeometryByIndex(i)->Clone());
543 
544  if (this->Material())
545  result->SetMaterial(this->Material());
546 
547  for (const auto &[key, val] : this->userData)
548  result->SetUserData(key, val);
549 
550  return result;
551  }
552  }
553  }
554 }
555 #endif