Ignition Rendering

API Reference

4.0.0
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() override;
81 
82  // Documentation inherited.
83  public: virtual void SetVisible(bool _visible) override;
84 
85  // Documentation inherited.
86  public: virtual void SetVisibilityFlags(uint32_t _flags) override;
87 
88  // Documentation inherited.
89  public: virtual uint32_t VisibilityFlags() const override;
90 
91  // Documentation inherited.
92  public: virtual void AddVisibilityFlags(uint32_t _flags) override;
93 
94  // Documentation inherited.
95  public: virtual void RemoveVisibilityFlags(uint32_t _flags) override;
96 
97  // Documentation inherited.
98  public: virtual void PreRender() override;
99 
100  // Documentation inherited
101  public: virtual void Destroy() override;
102 
103  // Documentation inherited.
104  public: virtual void SetUserData(const std::string &_key,
105  Variant _value) override;
106 
107  // Documentation inherited.
108  public: virtual Variant UserData(const std::string &_key) const override;
109 
110  // Documentation inherited.
111  public: virtual ignition::math::AxisAlignedBox BoundingBox()
112  const override;
113 
114  // Documentation inherited.
115  public: virtual ignition::math::AxisAlignedBox LocalBoundingBox()
116  const override;
117 
118  protected: virtual void PreRenderChildren() override;
119 
120  protected: virtual void PreRenderGeometries();
121 
122  protected: virtual GeometryStorePtr Geometries() const = 0;
123 
124  protected: virtual bool AttachGeometry(GeometryPtr _geometry) = 0;
125 
126  protected: virtual bool DetachGeometry(GeometryPtr _geometry) = 0;
127 
129  protected: MaterialPtr material;
130 
133 
135  protected: uint32_t visibilityFlags = IGN_VISIBILITY_ALL;
136 
139  };
140 
142  template <class T>
144  {
145  }
146 
148  template <class T>
150  {
151  }
152 
154  template <class T>
156  {
157  math::Pose3d rawPose = this->RawLocalPose();
158  math::Vector3d scale = this->LocalScale();
159  rawPose.Pos() += rawPose.Rot() * (scale * this->origin);
160  return rawPose;
161  }
162 
164  template <class T>
166  {
167  math::Pose3d rawPose = _pose;
168  math::Vector3d scale = this->LocalScale();
169  rawPose.Pos() -= rawPose.Rot() * (scale * this->origin);
170  this->SetRawLocalPose(rawPose);
171  }
172 
174  template <class T>
175  unsigned int BaseVisual<T>::GeometryCount() const
176  {
177  return this->Geometries()->Size();
178  }
179 
181  template <class T>
183  {
184  return this->Geometries()->Contains(_geometry);
185  }
186 
188  template <class T>
189  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
190  {
191  return this->Geometries()->GetByIndex(_index);
192  }
193 
195  template <class T>
197  {
198  if (this->AttachGeometry(_geometry))
199  {
200  this->Geometries()->Add(_geometry);
201  }
202  }
203 
205  template <class T>
207  {
208  if (this->DetachGeometry(_geometry))
209  {
210  this->Geometries()->Remove(_geometry);
211  }
212  return _geometry;
213  }
214 
216  template <class T>
218  {
219  return this->RemoveGeometry(this->GeometryByIndex(_index));
220  }
221 
223  template <class T>
225  {
226  for (unsigned int i = this->GeometryCount(); i > 0; --i)
227  {
228  this->RemoveGeometryByIndex(i-1);
229  }
230  }
231 
233  template <class T>
234  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
235  {
236  MaterialPtr mat = this->Scene()->Material(_name);
237  if (mat) this->SetMaterial(mat, _unique);
238  }
239 
241  template <class T>
242  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
243  {
244  _material = (_unique) ? _material->Clone() : _material;
245  this->SetChildMaterial(_material, false);
246  this->SetGeometryMaterial(_material, false);
247  this->material = _material;
248  }
249 
251  template <class T>
252  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
253  {
254  unsigned int count = this->ChildCount();
255  _material = (_unique && count > 0) ? _material->Clone() : _material;
256 
257  auto children_ =
259  this->Children());
260  if (!children_)
261  {
262  ignerr << "Cast failed in BaseVisual::SetChildMaterial" << std::endl;
263  return;
264  }
265  for (auto it = children_->Begin(); it != children_->End(); ++it)
266  {
267  NodePtr child = it->second;
268  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
269  if (visual) visual->SetMaterial(_material, false);
270  }
271  }
272 
274  template <class T>
275  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
276  {
277  unsigned int count = this->GeometryCount();
278  _material = (_unique && count > 0) ? _material->Clone() : _material;
279 
280  for (unsigned int i = 0; i < count; ++i)
281  {
282  GeometryPtr geometry = this->GeometryByIndex(i);
283  geometry->SetMaterial(_material, false);
284  }
285  }
286 
288  template <class T>
290  {
291  return this->material;
292  }
293 
295  template <class T>
297  {
298  T::PreRender();
299  this->PreRenderChildren();
300  this->PreRenderGeometries();
301  }
302 
304  template <class T>
306  {
307  this->Geometries()->DestroyAll();
308  this->Children()->RemoveAll();
309  this->material.reset();
310  T::Destroy();
311  }
312 
314  template <class T>
316  {
317  auto children_ =
319  this->Children());
320  if (!children_)
321  {
322  ignerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
323  return;
324  }
325  for (auto it = children_->Begin(); it != children_->End(); ++it)
326  {
327  it->second->PreRender();
328  }
329  }
330 
332  template <class T>
334  {
335  unsigned int count = this->GeometryCount();
336 
337  for (unsigned int i = 0; i < count; ++i)
338  {
339  GeometryPtr geometry = this->GeometryByIndex(i);
340  geometry->PreRender();
341  }
342  }
343 
345  template <class T>
346  void BaseVisual<T>::SetVisible(bool _visible)
347  {
348  ignerr << "SetVisible(" << _visible << ") not supported for "
349  << "render engine: " << this->Scene()->Engine()->Name()
350  << std::endl;
351  }
352 
354  template <class T>
356  {
358 
359  // Recursively loop through child visuals
360  auto childNodes =
362  this->Children());
363  if (!childNodes)
364  {
365  ignerr << "Cast failed in BaseVisual::LocalBoundingBox" << std::endl;
366  return box;
367  }
368  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
369  {
370  NodePtr child = it->second;
371  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
372  if (visual)
373  {
374  ignition::math::AxisAlignedBox aabb = visual->LocalBoundingBox();
375  if (aabb.Min().IsFinite() && aabb.Max().IsFinite())
376  box.Merge(aabb);
377  }
378  }
379  return box;
380  }
381 
383  template <class T>
385  {
387 
388  // Recursively loop through child visuals
389  auto childNodes =
391  this->Children());
392  if (!childNodes)
393  {
394  ignerr << "Cast failed in BaseVisual::BoundingBox" << std::endl;
395  return box;
396  }
397  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
398  {
399  NodePtr child = it->second;
400  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
401  if (visual)
402  box.Merge(visual->BoundingBox());
403  }
404  return box;
405  }
406 
408  template <class T>
409  void BaseVisual<T>::AddVisibilityFlags(uint32_t _flags)
410  {
411  this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
412  }
413 
415  template <class T>
417  {
418  this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
419  }
420 
422  template <class T>
423  void BaseVisual<T>::SetVisibilityFlags(uint32_t _flags)
424  {
425  this->visibilityFlags = _flags;
426 
427  // recursively set child visuals' visibility flags
428  auto childNodes =
430  this->Children());
431  if (!childNodes)
432  {
433  ignerr << "Cast failed in BaseVisual::SetVisibiltyFlags" << std::endl;
434  return;
435  }
436  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
437  {
438  NodePtr child = it->second;
439  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
440  if (visual)
441  visual->SetVisibilityFlags(_flags);
442  }
443  }
444 
446  template <class T>
448  {
449  return this->visibilityFlags;
450  }
451 
453  template <class T>
455  {
456  this->userData[_key] = _value;
457  }
458 
460  template <class T>
462  {
463  Variant value;
464  auto it = this->userData.find(_key);
465  if (it != this->userData.end())
466  value = it->second;
467  return value;
468  }
469  }
470  }
471 }
472 #endif
virtual void RemoveVisibilityFlags(uint32_t _flags) override
Remove visibility flags.
Definition: BaseVisual.hh:416
virtual ~BaseVisual()
Definition: BaseVisual.hh:149
std::map< std::string, Variant > userData
A map of custom key value data.
Definition: BaseVisual.hh:132
virtual uint32_t VisibilityFlags() const override
Get visibility flags.
Definition: BaseVisual.hh:447
BaseVisual()
Definition: BaseVisual.hh:143
const Vector3d & Max() const
virtual ignition::math::AxisAlignedBox BoundingBox() const override
Get the bounding box in world frame coordinates.
Definition: BaseVisual.hh:384
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:206
virtual void AddVisibilityFlags(uint32_t _flags) override
Add visibility flags.
Definition: BaseVisual.hh:409
#define IGN_VISIBILITY_ALL
Render everything visibility mask.
Definition: RenderTypes.hh:26
virtual unsigned int GeometryCount() const override
Get the number of geometries attached to this visual.
Definition: BaseVisual.hh:175
T endl(T... args)
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:217
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:155
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: BaseVisual.hh:165
STL class.
virtual void PreRenderGeometries()
Definition: BaseVisual.hh:333
virtual void SetVisibilityFlags(uint32_t _flags) override
Set visibility flags.
Definition: BaseVisual.hh:423
virtual bool HasGeometry(ConstGeometryPtr _geometry) const override
Determine if given geometry is attached to this visual.
Definition: BaseVisual.hh:182
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node...
Definition: Scene.hh:48
virtual void RemoveGeometries() override
Remove all attached geometries from this visual.
Definition: BaseVisual.hh:224
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:296
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:196
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:234
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
const Quaternion< T > & Rot() const
virtual void SetVisible(bool _visible) override
Specify if this visual is visible.
Definition: BaseVisual.hh:346
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:37
virtual ignition::math::AxisAlignedBox LocalBoundingBox() const override
Get the local bounding box of the visual.
Definition: BaseVisual.hh:355
virtual Variant UserData(const std::string &_key) const override
Get custom data stored in this visual.
Definition: BaseVisual.hh:461
virtual std::string Name() const =0
Get name of the render-engine.
MaterialPtr material
Pointer to material assigned to this visual.
Definition: BaseVisual.hh:129
#define ignerr
const Vector3< T > & Pos() const
virtual void PreRenderChildren() override
Definition: BaseVisual.hh:315
virtual void SetUserData(const std::string &_key, Variant _value) override
Store any custom data associated with this visual.
Definition: BaseVisual.hh:454
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:189
std::variant< int, float, double, std::string > Variant
Definition: Visual.hh:32
virtual MaterialPtr Material() override
Get the material assigned to attached visuals and geometries.
Definition: BaseVisual.hh:289
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseVisual.hh:305
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:275
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached visuals only.
Definition: BaseVisual.hh:252
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...