Ignition Rendering

API Reference

6.3.1
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 IGNITION_RENDERING_BASE_BASEVISUAL_HH_
18 #define IGNITION_RENDERING_BASE_BASEVISUAL_HH_
19 
20 #include <map>
21 #include <string>
22 
24 
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_ =
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  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_ =
330  this->Children());
331  if (!children_)
332  {
333  ignerr << "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  ignerr << "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  ignerr << "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 =
389  this->Children());
390  if (!childNodes)
391  {
392  ignerr << "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  ignition::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 =
418  this->Children());
419  if (!childNodes)
420  {
421  ignerr << "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 =
457  this->Children());
458  if (!childNodes)
459  {
460  ignerr << "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  ignerr << "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  ignerr << "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_ =
520  this->Children());
521  if (!children_)
522  {
523  ignerr << "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  ignerr << "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
virtual void RemoveVisibilityFlags(uint32_t _flags) override
Remove visibility flags.
Definition: BaseVisual.hh:443
virtual MaterialPtr Material() const override
Get the material assigned to attached visuals and geometries.
Definition: BaseVisual.hh:300
virtual ~BaseVisual()
Definition: BaseVisual.hh:152
#define IGN_VISIBILITY_ALL
Render everything visibility mask.
Definition: RenderTypes.hh:26
T empty(T... args)
virtual uint32_t VisibilityFlags() const override
Get visibility flags.
Definition: BaseVisual.hh:474
virtual void SetWireframe(bool _show) override
Enable or disable wireframe.
Definition: BaseVisual.hh:364
virtual bool Wireframe() const override
Get whether wireframe is enabled for this visual.
Definition: BaseVisual.hh:357
bool IsFinite() const
BaseVisual()
Definition: BaseVisual.hh:146
const Vector3d & Max() const
virtual ignition::math::AxisAlignedBox BoundingBox() const override
Get the bounding box in world frame coordinates.
Definition: BaseVisual.hh:411
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: BaseVisual.hh:217
virtual void AddVisibilityFlags(uint32_t _flags) override
Add visibility flags.
Definition: BaseVisual.hh:436
virtual unsigned int GeometryCount() const override
Get the number of geometries attached to this visual.
Definition: BaseVisual.hh:186
T endl(T... args)
2D or 3D Bounding box. It stores the position / orientation / size info of the box and its label ...
Definition: BoundingBox.hh:36
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: BaseVisual.hh:228
Definition: BaseStorage.hh:88
ignition::math::AxisAlignedBox boundingBox
The bounding box of the visual.
Definition: BaseVisual.hh:138
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: BaseVisual.hh:158
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: BaseVisual.hh:168
STL class.
virtual void PreRenderGeometries()
Definition: BaseVisual.hh:344
virtual void SetVisibilityFlags(uint32_t _flags) override
Set visibility flags.
Definition: BaseVisual.hh:450
virtual bool HasGeometry(ConstGeometryPtr _geometry) const override
Determine if given geometry is attached to this visual.
Definition: BaseVisual.hh:193
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node...
Definition: Scene.hh:49
virtual void RemoveGeometries() override
Remove all attached geometries from this visual.
Definition: BaseVisual.hh:235
void Merge(const AxisAlignedBox &_box)
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseVisual.hh:307
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: BaseVisual.hh:207
T dynamic_pointer_cast(T... args)
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: BaseVisual.hh:245
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
const Quaternion< T > & Rot() const
virtual VisualPtr Clone(const std::string &_name, NodePtr _newParent) const override
Clone the visual (and its children) with a new name.
Definition: BaseVisual.hh:481
virtual void SetVisible(bool _visible) override
Specify if this visual is visible.
Definition: BaseVisual.hh:373
Definition: BaseVisual.hh:37
Represents a visual node in a scene graph. A Visual is the only node that can have Geometry and other...
Definition: Visual.hh:33
virtual ignition::math::AxisAlignedBox LocalBoundingBox() const override
Get the local bounding box of the visual.
Definition: BaseVisual.hh:382
virtual std::string Name() const =0
Get name of the render-engine.
MaterialPtr material
Pointer to material assigned to this visual.
Definition: BaseVisual.hh:132
#define ignerr
const Vector3< T > & Pos() const
virtual void PreRenderChildren() override
Definition: BaseVisual.hh:326
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: BaseVisual.hh:200
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseVisual.hh:316
const Vector3d & Min() const
Represents a surface material of a Geometry.
Definition: Material.hh:47
virtual void SetGeometryMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached geometries only.
Definition: BaseVisual.hh:286
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached visuals only.
Definition: BaseVisual.hh:263
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...