Gazebo Rendering

API Reference

6.6.3
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 ignition
31 {
32  namespace rendering
33  {
34  inline namespace IGNITION_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.
111  const override;
112 
113  // Documentation inherited.
114  public: virtual ignition::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 = IGN_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  ignerr << "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  ignerr << "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  this->PreRenderChildren();
310  this->PreRenderGeometries();
311  }
312 
314  template <class T>
316  {
317  this->Geometries()->DestroyAll();
318  this->Children()->RemoveAll();
319  this->material.reset();
320  T::Destroy();
321  }
322 
324  template <class T>
326  {
327  auto children_ =
328  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
329  this->Children());
330  if (!children_)
331  {
332  ignerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
333  return;
334  }
335  for (auto it = children_->Begin(); it != children_->End(); ++it)
336  {
337  it->second->PreRender();
338  }
339  }
340 
342  template <class T>
344  {
345  unsigned int count = this->GeometryCount();
346 
347  for (unsigned int i = 0; i < count; ++i)
348  {
349  GeometryPtr geometry = this->GeometryByIndex(i);
350  geometry->PreRender();
351  }
352  }
353 
355  template <class T>
357  {
358  return this->wireframe;
359  }
360 
362  template <class T>
364  {
365  ignerr << "SetWireframe(" << _show << ") not supported for "
366  << "render engine: " << this->Scene()->Engine()->Name()
367  << std::endl;
368  }
369 
371  template <class T>
372  void BaseVisual<T>::SetVisible(bool _visible)
373  {
374  ignerr << "SetVisible(" << _visible << ") not supported for "
375  << "render engine: " << this->Scene()->Engine()->Name()
376  << std::endl;
377  }
378 
380  template <class T>
382  {
384 
385  // Recursively loop through child visuals
386  auto childNodes =
387  std::dynamic_pointer_cast<BaseStore<ignition::rendering::Node, T>>(
388  this->Children());
389  if (!childNodes)
390  {
391  ignerr << "Cast failed in BaseVisual::LocalBoundingBox" << std::endl;
392  return box;
393  }
394  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
395  {
396  NodePtr child = it->second;
397  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
398  if (visual)
399  {
400  ignition::math::AxisAlignedBox aabb = visual->LocalBoundingBox();
401  if (aabb.Min().IsFinite() && aabb.Max().IsFinite())
402  box.Merge(aabb);
403  }
404  }
405  return box;
406  }
407 
409  template <class T>
411  {
413 
414  // Recursively loop through child visuals
415  auto childNodes =
416  std::dynamic_pointer_cast<BaseStore<ignition::rendering::Node, T>>(
417  this->Children());
418  if (!childNodes)
419  {
420  ignerr << "Cast failed in BaseVisual::BoundingBox" << std::endl;
421  return box;
422  }
423  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
424  {
425  NodePtr child = it->second;
426  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
427  if (visual)
428  box.Merge(visual->BoundingBox());
429  }
430  return box;
431  }
432 
434  template <class T>
435  void BaseVisual<T>::AddVisibilityFlags(uint32_t _flags)
436  {
437  this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
438  }
439 
441  template <class T>
443  {
444  this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
445  }
446 
448  template <class T>
449  void BaseVisual<T>::SetVisibilityFlags(uint32_t _flags)
450  {
451  this->visibilityFlags = _flags;
452 
453  // recursively set child visuals' visibility flags
454  auto childNodes =
455  std::dynamic_pointer_cast<BaseStore<ignition::rendering::Node, T>>(
456  this->Children());
457  if (!childNodes)
458  {
459  ignerr << "Cast failed in BaseVisual::SetVisibiltyFlags" << std::endl;
460  return;
461  }
462  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
463  {
464  NodePtr child = it->second;
465  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
466  if (visual)
467  visual->SetVisibilityFlags(_flags);
468  }
469  }
470 
472  template <class T>
474  {
475  return this->visibilityFlags;
476  }
477 
479  template <class T>
481  NodePtr _newParent) const
482  {
483  ScenePtr scene_ = this->Scene();
484  if (nullptr == scene_)
485  {
486  ignerr << "Cloning a visual failed because the visual to be cloned is "
487  << "not attached to a scene.\n";
488  return nullptr;
489  }
490  VisualPtr result;
491  if (_name.empty())
492  result = scene_->CreateVisual();
493  else
494  result = scene_->CreateVisual(_name);
495 
496  if (nullptr != _newParent)
497  {
498  auto parentScene = _newParent->Scene();
499  if (nullptr != parentScene && parentScene->Id() != scene_->Id())
500  {
501  ignerr << "Cloning a visual failed because the desired parent of the "
502  << "cloned visual belongs to a different scene.\n";
503  scene_->DestroyVisual(result);
504  return nullptr;
505  }
506  _newParent->AddChild(result);
507  }
508 
509  result->SetOrigin(this->Origin());
510  result->SetInheritScale(this->InheritScale());
511  result->SetLocalScale(this->LocalScale());
512  result->SetLocalPose(this->LocalPose());
513  result->SetVisibilityFlags(this->VisibilityFlags());
514  result->SetWireframe(this->Wireframe());
515 
516  // if the visual that was cloned has child visuals, clone those as well
517  auto children_ =
518  std::dynamic_pointer_cast<BaseStore<ignition::rendering::Node, T>>(
519  this->Children());
520  if (!children_)
521  {
522  ignerr << "Cast failed in BaseVisual::Clone\n";
523  scene_->DestroyVisual(result);
524  return nullptr;
525  }
526  for (auto it = children_->Begin(); it != children_->End(); ++it)
527  {
528  NodePtr child = it->second;
529  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
530  // recursively delete all cloned visuals if the child cannot be
531  // retrieved, or if cloning the child visual failed
532  if (!visual || !visual->Clone("", result))
533  {
534  ignerr << "Cloning a child visual failed.\n";
535  scene_->DestroyVisual(result, true);
536  return nullptr;
537  }
538  }
539 
540  for (unsigned int i = 0; i < this->GeometryCount(); ++i)
541  result->AddGeometry(this->GeometryByIndex(i)->Clone());
542 
543  if (this->Material())
544  result->SetMaterial(this->Material());
545 
546  for (const auto &[key, val] : this->userData)
547  result->SetUserData(key, val);
548 
549  return result;
550  }
551  }
552  }
553 }
554 #endif
virtual GeometryPtr GeometryByIndex(unsigned int _index) const override
Get geometry at given index. If no geometry exists at given index, NULL will be returned.
Definition: gz/rendering/base/BaseVisual.hh:200
virtual GeometryStorePtr Geometries() const =0
virtual bool DetachGeometry(GeometryPtr _geometry)=0
STL class.
STL class.
virtual void AddVisibilityFlags(uint32_t _flags) override
Add visibility flags.
Definition: gz/rendering/base/BaseVisual.hh:435
Definition: gz/rendering/base/BaseVisual.hh:37
virtual void SetWireframe(bool _show) override
Enable or disable wireframe.
Definition: gz/rendering/base/BaseVisual.hh:363
2D or 3D Bounding box. It stores the position / orientation / size info of the box and its label
Definition: gz/rendering/BoundingBox.hh:41
virtual void RemoveGeometries() override
Remove all attached geometries from this visual.
Definition: gz/rendering/base/BaseVisual.hh:235
Vector3< T > & Pos()
virtual std::string Name() const =0
Get name of the render-engine.
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
virtual void SetVisible(bool _visible) override
Specify if this visual is visible.
Definition: gz/rendering/base/BaseVisual.hh:372
virtual unsigned int GeometryCount() const override
Get the number of geometries attached to this visual.
Definition: gz/rendering/base/BaseVisual.hh:186
virtual void PreRenderChildren() override
Definition: gz/rendering/base/BaseVisual.hh:325
virtual void SetGeometryMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached geometries only.
Definition: gz/rendering/base/BaseVisual.hh:286
#define ignerr
MaterialPtr material
Pointer to material assigned to this visual.
Definition: gz/rendering/base/BaseVisual.hh:132
virtual ignition::math::AxisAlignedBox LocalBoundingBox() const override
Get the local bounding box of the visual.
Definition: gz/rendering/base/BaseVisual.hh:381
virtual ignition::math::AxisAlignedBox BoundingBox() const override
Get the bounding box in world frame coordinates.
Definition: gz/rendering/base/BaseVisual.hh:410
virtual void AddGeometry(GeometryPtr _geometry) override
Add the given geometry to this visual. If the given node is already attached, no work will be done.
Definition: gz/rendering/base/BaseVisual.hh:207
virtual void SetVisibilityFlags(uint32_t _flags) override
Set visibility flags.
Definition: gz/rendering/base/BaseVisual.hh:449
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node....
Definition: gz/rendering/Scene.hh:49
uint32_t visibilityFlags
Visual's visibility flags.
Definition: gz/rendering/base/BaseVisual.hh:135
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: gz/rendering/base/BaseVisual.hh:307
virtual void PreRenderGeometries()
Definition: gz/rendering/base/BaseVisual.hh:343
Represents a visual node in a scene graph. A Visual is the only node that can have Geometry and other...
Definition: gz/rendering/Visual.hh:33
#define IGN_VISIBILITY_ALL
Render everything visibility mask.
Definition: gz/rendering/RenderTypes.hh:26
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: gz/rendering/base/BaseVisual.hh:158
virtual MaterialPtr Material(const std::string &_name) const =0
Get material registered under the given name. If no material is registered under the given name,...
virtual bool AttachGeometry(GeometryPtr _geometry)=0
virtual GeometryPtr RemoveGeometryByIndex(unsigned int _index) override
Remove the geometry at the given index from this visual. If the specified node is not attached this v...
Definition: gz/rendering/base/BaseVisual.hh:228
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: gz/rendering/base/BaseVisual.hh:168
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: gz/rendering/base/BaseVisual.hh:315
virtual uint32_t VisibilityFlags() const override
Get visibility flags.
Definition: gz/rendering/base/BaseVisual.hh:473
virtual void SetMaterial(const std::string &_name, bool _unique=true) override
Set the material for all attached visuals and geometries. The specified material will be retrieved fr...
Definition: gz/rendering/base/BaseVisual.hh:245
T endl(T... args)
virtual VisualPtr Clone(const std::string &_name, NodePtr _newParent) const override
Clone the visual (and its children) with a new name.
Definition: gz/rendering/base/BaseVisual.hh:480
bool wireframe
True if wireframe mode is enabled else false.
Definition: gz/rendering/base/BaseVisual.hh:141
BaseVisual()
Definition: gz/rendering/base/BaseVisual.hh:146
T empty(T... args)
virtual MaterialPtr Material() const override
Get the material assigned to attached visuals and geometries.
Definition: gz/rendering/base/BaseVisual.hh:300
virtual bool HasGeometry(ConstGeometryPtr _geometry) const override
Determine if given geometry is attached to this visual.
Definition: gz/rendering/base/BaseVisual.hh:193
virtual GeometryPtr RemoveGeometry(GeometryPtr _geometry) override
Remove the given geometry from this visual. If the given node is not a child of this visual,...
Definition: gz/rendering/base/BaseVisual.hh:217
Represents a surface material of a Geometry.
Definition: gz/rendering/Material.hh:47
bool IsFinite() const
void Merge(const AxisAlignedBox &_box)
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached visuals only.
Definition: gz/rendering/base/BaseVisual.hh:263
virtual void RemoveVisibilityFlags(uint32_t _flags) override
Remove visibility flags.
Definition: gz/rendering/base/BaseVisual.hh:442
ignition::math::AxisAlignedBox boundingBox
The bounding box of the visual.
Definition: gz/rendering/base/BaseVisual.hh:138
virtual ~BaseVisual()
Definition: gz/rendering/base/BaseVisual.hh:152
Quaternion< T > & Rot()
virtual bool Wireframe() const override
Get whether wireframe is enabled for this visual.
Definition: gz/rendering/base/BaseVisual.hh:356