Ignition Rendering

API Reference

5.1.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 
171  if (!rawPose.IsFinite())
172  {
173  ignerr << "Unable to set pose of a node: "
174  << "non-finite (nan, inf) values detected." << std::endl;
175  return;
176  }
177 
178  this->SetRawLocalPose(rawPose);
179  }
180 
182  template <class T>
183  unsigned int BaseVisual<T>::GeometryCount() const
184  {
185  return this->Geometries()->Size();
186  }
187 
189  template <class T>
191  {
192  return this->Geometries()->Contains(_geometry);
193  }
194 
196  template <class T>
197  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
198  {
199  return this->Geometries()->GetByIndex(_index);
200  }
201 
203  template <class T>
205  {
206  if (this->AttachGeometry(_geometry))
207  {
208  this->Geometries()->Add(_geometry);
209  }
210  }
211 
213  template <class T>
215  {
216  if (this->DetachGeometry(_geometry))
217  {
218  this->Geometries()->Remove(_geometry);
219  }
220  return _geometry;
221  }
222 
224  template <class T>
226  {
227  return this->RemoveGeometry(this->GeometryByIndex(_index));
228  }
229 
231  template <class T>
233  {
234  for (unsigned int i = this->GeometryCount(); i > 0; --i)
235  {
236  this->RemoveGeometryByIndex(i-1);
237  }
238  }
239 
241  template <class T>
242  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
243  {
244  MaterialPtr mat = this->Scene()->Material(_name);
245  if (mat) this->SetMaterial(mat, _unique);
246  }
247 
249  template <class T>
250  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
251  {
252  _material = (_unique) ? _material->Clone() : _material;
253  this->SetChildMaterial(_material, false);
254  this->SetGeometryMaterial(_material, false);
255  this->material = _material;
256  }
257 
259  template <class T>
260  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
261  {
262  unsigned int count = this->ChildCount();
263  _material = (_unique && count > 0) ? _material->Clone() : _material;
264 
265  auto children_ =
267  this->Children());
268  if (!children_)
269  {
270  ignerr << "Cast failed in BaseVisual::SetChildMaterial" << std::endl;
271  return;
272  }
273  for (auto it = children_->Begin(); it != children_->End(); ++it)
274  {
275  NodePtr child = it->second;
276  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
277  if (visual) visual->SetMaterial(_material, false);
278  }
279  }
280 
282  template <class T>
283  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
284  {
285  unsigned int count = this->GeometryCount();
286  _material = (_unique && count > 0) ? _material->Clone() : _material;
287 
288  for (unsigned int i = 0; i < count; ++i)
289  {
290  GeometryPtr geometry = this->GeometryByIndex(i);
291  geometry->SetMaterial(_material, false);
292  }
293  }
294 
296  template <class T>
298  {
299  return this->material;
300  }
301 
303  template <class T>
305  {
306  T::PreRender();
307  this->PreRenderChildren();
308  this->PreRenderGeometries();
309  }
310 
312  template <class T>
314  {
315  this->Geometries()->DestroyAll();
316  this->Children()->RemoveAll();
317  this->material.reset();
318  T::Destroy();
319  }
320 
322  template <class T>
324  {
325  auto children_ =
327  this->Children());
328  if (!children_)
329  {
330  ignerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
331  return;
332  }
333  for (auto it = children_->Begin(); it != children_->End(); ++it)
334  {
335  it->second->PreRender();
336  }
337  }
338 
340  template <class T>
342  {
343  unsigned int count = this->GeometryCount();
344 
345  for (unsigned int i = 0; i < count; ++i)
346  {
347  GeometryPtr geometry = this->GeometryByIndex(i);
348  geometry->PreRender();
349  }
350  }
351 
353  template <class T>
354  void BaseVisual<T>::SetVisible(bool _visible)
355  {
356  ignerr << "SetVisible(" << _visible << ") not supported for "
357  << "render engine: " << this->Scene()->Engine()->Name()
358  << std::endl;
359  }
360 
362  template <class T>
364  {
366 
367  // Recursively loop through child visuals
368  auto childNodes =
370  this->Children());
371  if (!childNodes)
372  {
373  ignerr << "Cast failed in BaseVisual::LocalBoundingBox" << std::endl;
374  return box;
375  }
376  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
377  {
378  NodePtr child = it->second;
379  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
380  if (visual)
381  {
382  ignition::math::AxisAlignedBox aabb = visual->LocalBoundingBox();
383  if (aabb.Min().IsFinite() && aabb.Max().IsFinite())
384  box.Merge(aabb);
385  }
386  }
387  return box;
388  }
389 
391  template <class T>
393  {
395 
396  // Recursively loop through child visuals
397  auto childNodes =
399  this->Children());
400  if (!childNodes)
401  {
402  ignerr << "Cast failed in BaseVisual::BoundingBox" << std::endl;
403  return box;
404  }
405  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
406  {
407  NodePtr child = it->second;
408  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
409  if (visual)
410  box.Merge(visual->BoundingBox());
411  }
412  return box;
413  }
414 
416  template <class T>
417  void BaseVisual<T>::AddVisibilityFlags(uint32_t _flags)
418  {
419  this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
420  }
421 
423  template <class T>
425  {
426  this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
427  }
428 
430  template <class T>
431  void BaseVisual<T>::SetVisibilityFlags(uint32_t _flags)
432  {
433  this->visibilityFlags = _flags;
434 
435  // recursively set child visuals' visibility flags
436  auto childNodes =
438  this->Children());
439  if (!childNodes)
440  {
441  ignerr << "Cast failed in BaseVisual::SetVisibiltyFlags" << std::endl;
442  return;
443  }
444  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
445  {
446  NodePtr child = it->second;
447  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
448  if (visual)
449  visual->SetVisibilityFlags(_flags);
450  }
451  }
452 
454  template <class T>
456  {
457  return this->visibilityFlags;
458  }
459 
461  template <class T>
463  {
464  this->userData[_key] = _value;
465  }
466 
468  template <class T>
470  {
471  Variant value;
472  auto it = this->userData.find(_key);
473  if (it != this->userData.end())
474  value = it->second;
475  return value;
476  }
477  }
478  }
479 }
480 #endif
virtual void RemoveVisibilityFlags(uint32_t _flags) override
Remove visibility flags.
Definition: BaseVisual.hh:424
virtual ~BaseVisual()
Definition: BaseVisual.hh:149
#define IGN_VISIBILITY_ALL
Render everything visibility mask.
Definition: RenderTypes.hh:26
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:455
bool IsFinite() const
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:392
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:214
virtual void AddVisibilityFlags(uint32_t _flags) override
Add visibility flags.
Definition: BaseVisual.hh:417
virtual unsigned int GeometryCount() const override
Get the number of geometries attached to this visual.
Definition: BaseVisual.hh:183
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:225
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:341
virtual void SetVisibilityFlags(uint32_t _flags) override
Set visibility flags.
Definition: BaseVisual.hh:431
virtual bool HasGeometry(ConstGeometryPtr _geometry) const override
Determine if given geometry is attached to this visual.
Definition: BaseVisual.hh:190
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:232
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:304
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:204
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:242
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:354
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:363
virtual Variant UserData(const std::string &_key) const override
Get custom data stored in this visual.
Definition: BaseVisual.hh:469
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:323
virtual void SetUserData(const std::string &_key, Variant _value) override
Store any custom data associated with this visual.
Definition: BaseVisual.hh:462
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:197
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:297
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseVisual.hh:313
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:283
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached visuals only.
Definition: BaseVisual.hh:260
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...