Gazebo Rendering

API Reference

3.7.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 #include "gz/rendering/Visual.hh"
23 #include "gz/rendering/Storage.hh"
24 
27 
28 namespace ignition
29 {
30  namespace rendering
31  {
32  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
33  //
34  template <class T>
35  class BaseVisual :
36  public virtual Visual,
37  public virtual T
38  {
39  protected: BaseVisual();
40 
41  public: virtual ~BaseVisual();
42 
43  public: virtual math::Pose3d LocalPose() const override;
44 
45  public: virtual void SetLocalPose(const math::Pose3d &_pose) override;
46 
47  public: virtual unsigned int GeometryCount() const override;
48 
49  public: virtual bool HasGeometry(ConstGeometryPtr _geometry) const
50  override;
51 
52  public: virtual GeometryPtr GeometryByIndex(unsigned int _index) const
53  override;
54 
55  public: virtual void AddGeometry(GeometryPtr _geometry) override;
56 
57  public: virtual GeometryPtr RemoveGeometry(GeometryPtr _geometry)
58  override;
59 
60  public: virtual GeometryPtr RemoveGeometryByIndex(unsigned int _index)
61  override;
62 
63  public: virtual void RemoveGeometries() override;
64 
65  public: virtual void SetMaterial(const std::string &_name,
66  bool _unique = true) override;
67 
68  public: virtual void SetMaterial(MaterialPtr _material,
69  bool _unique = true) override;
70 
71  public: virtual void SetChildMaterial(MaterialPtr _material,
72  bool _unique = true) override;
73 
74  public: virtual void SetGeometryMaterial(MaterialPtr _material,
75  bool _unique = true) override;
76 
77  // Documentation inherited.
78  public: virtual MaterialPtr Material() override;
79 
80  // Documentation inherited.
81  public: virtual void SetVisible(bool _visible) override;
82 
83  public: virtual void PreRender() override;
84 
85  // Documentation inherited
86  public: virtual void Destroy() override;
87 
88  // Documentation inherited.
89  public: virtual void SetUserData(const std::string &_key,
90  Variant _value) override;
91 
92  // Documentation inherited.
93  public: virtual Variant UserData(const std::string &_key) const override;
94 
95  protected: virtual void PreRenderChildren() override;
96 
97  protected: virtual void PreRenderGeometries();
98 
99  protected: virtual GeometryStorePtr Geometries() const = 0;
100 
101  protected: virtual bool AttachGeometry(GeometryPtr _geometry) = 0;
102 
103  protected: virtual bool DetachGeometry(GeometryPtr _geometry) = 0;
104 
106  protected: MaterialPtr material;
107 
110  };
111 
113  template <class T>
115  {
116  }
117 
119  template <class T>
121  {
122  }
123 
125  template <class T>
127  {
128  math::Pose3d rawPose = this->RawLocalPose();
129  math::Vector3d scale = this->LocalScale();
130  rawPose.Pos() += rawPose.Rot() * (scale * this->origin);
131  return rawPose;
132  }
133 
135  template <class T>
137  {
138  math::Pose3d rawPose = _pose;
139  math::Vector3d scale = this->LocalScale();
140  rawPose.Pos() -= rawPose.Rot() * (scale * this->origin);
141 
142  if (!rawPose.IsFinite())
143  {
144  ignerr << "Unable to set pose of a node: "
145  << "non-finite (nan, inf) values detected." << std::endl;
146  return;
147  }
148 
149  this->SetRawLocalPose(rawPose);
150  }
151 
153  template <class T>
154  unsigned int BaseVisual<T>::GeometryCount() const
155  {
156  return this->Geometries()->Size();
157  }
158 
160  template <class T>
162  {
163  return this->Geometries()->Contains(_geometry);
164  }
165 
167  template <class T>
168  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
169  {
170  return this->Geometries()->GetByIndex(_index);
171  }
172 
174  template <class T>
176  {
177  if (this->AttachGeometry(_geometry))
178  {
179  this->Geometries()->Add(_geometry);
180  }
181  }
182 
184  template <class T>
186  {
187  if (this->DetachGeometry(_geometry))
188  {
189  this->Geometries()->Remove(_geometry);
190  }
191  return _geometry;
192  }
193 
195  template <class T>
197  {
198  return this->RemoveGeometry(this->GeometryByIndex(_index));
199  }
200 
202  template <class T>
204  {
205  for (unsigned int i = this->GeometryCount(); i > 0; --i)
206  {
207  this->RemoveGeometryByIndex(i-1);
208  }
209  }
210 
212  template <class T>
213  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
214  {
215  MaterialPtr mat = this->Scene()->Material(_name);
216  if (mat) this->SetMaterial(mat, _unique);
217  }
218 
220  template <class T>
221  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
222  {
223  _material = (_unique) ? _material->Clone() : _material;
224  this->SetChildMaterial(_material, false);
225  this->SetGeometryMaterial(_material, false);
226  this->material = _material;
227  }
228 
230  template <class T>
231  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
232  {
233  unsigned int count = this->ChildCount();
234  _material = (_unique && count > 0) ? _material->Clone() : _material;
235 
236  auto children_ =
237  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
238  this->Children());
239  if (!children_)
240  {
241  ignerr << "Cast failed in BaseVisual::SetChildMaterial" << std::endl;
242  return;
243  }
244  for (auto it = children_->Begin(); it != children_->End(); ++it)
245  {
246  NodePtr child = it->second;
247  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
248  if (visual) visual->SetMaterial(_material, false);
249  }
250  }
251 
253  template <class T>
254  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
255  {
256  unsigned int count = this->GeometryCount();
257  _material = (_unique && count > 0) ? _material->Clone() : _material;
258 
259  for (unsigned int i = 0; i < count; ++i)
260  {
261  GeometryPtr geometry = this->GeometryByIndex(i);
262  geometry->SetMaterial(_material, false);
263  }
264  }
265 
267  template <class T>
269  {
270  return this->material;
271  }
272 
274  template <class T>
276  {
277  T::PreRender();
278  this->PreRenderChildren();
279  this->PreRenderGeometries();
280  }
281 
283  template <class T>
285  {
286  this->Geometries()->DestroyAll();
287  this->Children()->RemoveAll();
288  this->material.reset();
289  T::Destroy();
290  }
291 
293  template <class T>
295  {
296  auto children_ =
297  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
298  this->Children());
299  if (!children_)
300  {
301  ignerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
302  return;
303  }
304  for (auto it = children_->Begin(); it != children_->End(); ++it)
305  {
306  it->second->PreRender();
307  }
308  }
309 
311  template <class T>
313  {
314  unsigned int count = this->GeometryCount();
315 
316  for (unsigned int i = 0; i < count; ++i)
317  {
318  GeometryPtr geometry = this->GeometryByIndex(i);
319  geometry->PreRender();
320  }
321  }
322 
324  template <class T>
325  void BaseVisual<T>::SetVisible(bool _visible)
326  {
327  ignerr << "SetVisible(" << _visible << ") not supported for "
328  << "render engine: " << this->Scene()->Engine()->Name()
329  << std::endl;
330  }
331 
333  template <class T>
335  {
336  this->userData[_key] = _value;
337  }
338 
340  template <class T>
342  {
343  Variant value;
344  auto it = this->userData.find(_key);
345  if (it != this->userData.end())
346  value = it->second;
347  return value;
348  }
349  }
350  }
351 }
352 #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:168
virtual GeometryStorePtr Geometries() const =0
virtual bool DetachGeometry(GeometryPtr _geometry)=0
STL class.
STL class.
Definition: gz/rendering/base/BaseVisual.hh:35
virtual void RemoveGeometries() override
Remove all attached geometries from this visual.
Definition: gz/rendering/base/BaseVisual.hh:203
virtual void SetUserData(const std::string &_key, Variant _value) override
Store any custom data associated with this visual.
Definition: gz/rendering/base/BaseVisual.hh:334
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:325
virtual unsigned int GeometryCount() const override
Get the number of geometries attached to this visual.
Definition: gz/rendering/base/BaseVisual.hh:154
virtual void PreRenderChildren() override
Definition: gz/rendering/base/BaseVisual.hh:294
virtual void SetGeometryMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached geometries only.
Definition: gz/rendering/base/BaseVisual.hh:254
#define ignerr
MaterialPtr material
Pointer to material assigned to this visual.
Definition: gz/rendering/base/BaseVisual.hh:106
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:175
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node....
Definition: gz/rendering/Scene.hh:48
std::map< std::string, Variant > userData
A map of custom key value data.
Definition: gz/rendering/base/BaseVisual.hh:109
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:275
virtual void PreRenderGeometries()
Definition: gz/rendering/base/BaseVisual.hh:312
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:36
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: gz/rendering/base/BaseVisual.hh:126
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:196
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: gz/rendering/base/BaseVisual.hh:136
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:284
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:213
T endl(T... args)
std::variant< int, float, double, std::string > Variant
Definition: gz/rendering/Visual.hh:31
BaseVisual()
Definition: gz/rendering/base/BaseVisual.hh:114
virtual Variant UserData(const std::string &_key) const override
Get custom data stored in this visual.
Definition: gz/rendering/base/BaseVisual.hh:341
virtual bool HasGeometry(ConstGeometryPtr _geometry) const override
Determine if given geometry is attached to this visual.
Definition: gz/rendering/base/BaseVisual.hh:161
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:185
Represents a surface material of a Geometry.
Definition: gz/rendering/Material.hh:47
virtual MaterialPtr Material() override
Get the material assigned to attached visuals and geometries.
Definition: gz/rendering/base/BaseVisual.hh:268
bool IsFinite() const
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true) override
Set the material for all attached visuals only.
Definition: gz/rendering/base/BaseVisual.hh:231
virtual ~BaseVisual()
Definition: gz/rendering/base/BaseVisual.hh:120
Quaternion< T > & Rot()