Gazebo Rendering

API Reference

8.2.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 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 gz
31 {
32  namespace rendering
33  {
34  inline namespace GZ_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 bool Static() const override;
93 
94  // Documentation inherited.
95  public: virtual void SetStatic(bool _static) override;
96 
97  // Documentation inherited.
98  public: virtual void SetVisibilityFlags(uint32_t _flags) override;
99 
100  // Documentation inherited.
101  public: virtual uint32_t VisibilityFlags() const override;
102 
103  // Documentation inherited.
104  public: virtual void AddVisibilityFlags(uint32_t _flags) override;
105 
106  // Documentation inherited.
107  public: virtual void RemoveVisibilityFlags(uint32_t _flags) override;
108 
109  // Documentation inherited.
110  public: virtual void PreRender() override;
111 
112  // Documentation inherited
113  public: virtual void Destroy() override;
114 
115  // Documentation inherited.
117  const override;
118 
119  // Documentation inherited.
121  const override;
122 
123  // Documentation inherited.
124  public: virtual VisualPtr Clone(const std::string &_name,
125  NodePtr _newParent) const override;
126 
127  protected: virtual void PreRenderChildren() override;
128 
129  protected: virtual void PreRenderGeometries();
130 
131  protected: virtual GeometryStorePtr Geometries() const = 0;
132 
133  protected: virtual bool AttachGeometry(GeometryPtr _geometry) = 0;
134 
135  protected: virtual bool DetachGeometry(GeometryPtr _geometry) = 0;
136 
138  protected: MaterialPtr material;
139 
141  protected: uint32_t visibilityFlags = GZ_VISIBILITY_ALL;
142 
145 
147  protected: bool wireframe = false;
148  };
149 
151  template <class T>
153  {
154  }
155 
157  template <class T>
159  {
160  }
161 
163  template <class T>
165  {
166  math::Pose3d rawPose = this->RawLocalPose();
167  math::Vector3d scale = this->LocalScale();
168  rawPose.Pos() += rawPose.Rot() * (scale * this->origin);
169  return rawPose;
170  }
171 
173  template <class T>
175  {
176  math::Pose3d rawPose = _pose;
177  math::Vector3d scale = this->LocalScale();
178  rawPose.Pos() -= rawPose.Rot() * (scale * this->origin);
179 
180  if (!rawPose.IsFinite())
181  {
182  gzerr << "Unable to set pose of a node: "
183  << "non-finite (nan, inf) values detected." << std::endl;
184  return;
185  }
186 
187  this->SetRawLocalPose(rawPose);
188  }
189 
191  template <class T>
192  unsigned int BaseVisual<T>::GeometryCount() const
193  {
194  return this->Geometries()->Size();
195  }
196 
198  template <class T>
200  {
201  return this->Geometries()->Contains(_geometry);
202  }
203 
205  template <class T>
206  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
207  {
208  return this->Geometries()->GetByIndex(_index);
209  }
210 
212  template <class T>
214  {
215  if (this->AttachGeometry(_geometry))
216  {
217  this->Geometries()->Add(_geometry);
218  }
219  }
220 
222  template <class T>
224  {
225  if (this->DetachGeometry(_geometry))
226  {
227  this->Geometries()->Remove(_geometry);
228  }
229  return _geometry;
230  }
231 
233  template <class T>
235  {
236  return this->RemoveGeometry(this->GeometryByIndex(_index));
237  }
238 
240  template <class T>
242  {
243  for (unsigned int i = this->GeometryCount(); i > 0; --i)
244  {
245  this->RemoveGeometryByIndex(i-1);
246  }
247  }
248 
250  template <class T>
251  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
252  {
253  MaterialPtr mat = this->Scene()->Material(_name);
254  if (mat) this->SetMaterial(mat, _unique);
255  }
256 
258  template <class T>
259  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
260  {
261  _material = (_unique) ? _material->Clone() : _material;
262  this->SetChildMaterial(_material, false);
263  this->SetGeometryMaterial(_material, false);
264  this->material = _material;
265  }
266 
268  template <class T>
269  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
270  {
271  unsigned int count = this->ChildCount();
272  _material = (_unique && count > 0) ? _material->Clone() : _material;
273 
274  auto children_ =
275  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
276  this->Children());
277  if (!children_)
278  {
279  gzerr << "Cast failed in BaseVisual::SetChildMaterial" << std::endl;
280  return;
281  }
282  for (auto it = children_->Begin(); it != children_->End(); ++it)
283  {
284  VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
285  if (visual) visual->SetMaterial(_material, false);
286  }
287  }
288 
290  template <class T>
291  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
292  {
293  unsigned int count = this->GeometryCount();
294  _material = (_unique && count > 0) ? _material->Clone() : _material;
295 
296  for (unsigned int i = 0; i < count; ++i)
297  {
298  GeometryPtr geometry = this->GeometryByIndex(i);
299  geometry->SetMaterial(_material, false);
300  }
301  }
302 
304  template <class T>
306  {
307  return this->material;
308  }
309 
311  template <class T>
313  {
314  T::PreRender();
315  this->PreRenderChildren();
316  this->PreRenderGeometries();
317  }
318 
320  template <class T>
322  {
323  this->Geometries()->DestroyAll();
324  this->Children()->RemoveAll();
325  this->material.reset();
326  T::Destroy();
327  }
328 
330  template <class T>
332  {
333  auto children_ =
334  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
335  this->Children());
336  if (!children_)
337  {
338  gzerr << "Cast failed in BaseVisual::PreRenderChildren" << std::endl;
339  return;
340  }
341  for (auto it = children_->Begin(); it != children_->End(); ++it)
342  {
343  (*it)->PreRender();
344  }
345  }
346 
348  template <class T>
350  {
351  unsigned int count = this->GeometryCount();
352 
353  for (unsigned int i = 0; i < count; ++i)
354  {
355  GeometryPtr geometry = this->GeometryByIndex(i);
356  geometry->PreRender();
357  }
358  }
359 
361  template <class T>
363  {
364  return this->wireframe;
365  }
366 
368  template <class T>
370  {
371  gzerr << "SetWireframe(" << _show << ") not supported for "
372  << "render engine: " << this->Scene()->Engine()->Name()
373  << std::endl;
374  }
375 
377  template <class T>
378  void BaseVisual<T>::SetVisible(bool _visible)
379  {
380  gzerr << "SetVisible(" << _visible << ") not supported for "
381  << "render engine: " << this->Scene()->Engine()->Name()
382  << std::endl;
383  }
384 
386  template <class T>
388  {
389  return false;
390  }
391 
393  template <class T>
394  void BaseVisual<T>::SetStatic(bool _static)
395  {
396  gzerr << "SetStatic(" << _static << ") not supported for "
397  << "render engine: " << this->Scene()->Engine()->Name()
398  << std::endl;
399  }
400 
402  template <class T>
404  {
406 
407  // Recursively loop through child visuals
408  auto childNodes =
409  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
410  this->Children());
411  if (!childNodes)
412  {
413  gzerr << "Cast failed in BaseVisual::LocalBoundingBox" << std::endl;
414  return box;
415  }
416  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
417  {
418  VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
419  if (visual)
420  {
421  gz::math::AxisAlignedBox aabb = visual->LocalBoundingBox();
422  if (aabb.Min().IsFinite() && aabb.Max().IsFinite())
423  box.Merge(aabb);
424  }
425  }
426  return box;
427  }
428 
430  template <class T>
432  {
434 
435  // Recursively loop through child visuals
436  auto childNodes =
437  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
438  this->Children());
439  if (!childNodes)
440  {
441  gzerr << "Cast failed in BaseVisual::BoundingBox" << std::endl;
442  return box;
443  }
444  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
445  {
446  VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
447  if (visual)
448  box.Merge(visual->BoundingBox());
449  }
450  return box;
451  }
452 
454  template <class T>
455  void BaseVisual<T>::AddVisibilityFlags(uint32_t _flags)
456  {
457  this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
458  }
459 
461  template <class T>
463  {
464  this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
465  }
466 
468  template <class T>
469  void BaseVisual<T>::SetVisibilityFlags(uint32_t _flags)
470  {
471  this->visibilityFlags = _flags;
472 
473  // recursively set child visuals' visibility flags
474  auto childNodes =
475  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
476  this->Children());
477  if (!childNodes)
478  {
479  gzerr << "Cast failed in BaseVisual::SetVisibiltyFlags" << std::endl;
480  return;
481  }
482  for (auto it = childNodes->Begin(); it != childNodes->End(); ++it)
483  {
484  VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
485  if (visual)
486  visual->SetVisibilityFlags(_flags);
487  }
488  }
489 
491  template <class T>
493  {
494  return this->visibilityFlags;
495  }
496 
498  template <class T>
500  NodePtr _newParent) const
501  {
502  ScenePtr scene_ = this->Scene();
503  if (nullptr == scene_)
504  {
505  gzerr << "Cloning a visual failed because the visual to be cloned is "
506  << "not attached to a scene.\n";
507  return nullptr;
508  }
509  VisualPtr result;
510  if (_name.empty())
511  result = scene_->CreateVisual();
512  else
513  result = scene_->CreateVisual(_name);
514 
515  if (nullptr != _newParent)
516  {
517  auto parentScene = _newParent->Scene();
518  if (nullptr != parentScene && parentScene->Id() != scene_->Id())
519  {
520  gzerr << "Cloning a visual failed because the desired parent of the "
521  << "cloned visual belongs to a different scene.\n";
522  scene_->DestroyVisual(result);
523  return nullptr;
524  }
525  _newParent->AddChild(result);
526  }
527 
528  result->SetOrigin(this->Origin());
529  result->SetInheritScale(this->InheritScale());
530  result->SetLocalScale(this->LocalScale());
531  result->SetLocalPose(this->LocalPose());
532  result->SetVisibilityFlags(this->VisibilityFlags());
533  result->SetWireframe(this->Wireframe());
534 
535  // if the visual that was cloned has child visuals, clone those as well
536  auto children_ =
537  std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
538  this->Children());
539  if (!children_)
540  {
541  gzerr << "Cast failed in BaseVisual::Clone\n";
542  scene_->DestroyVisual(result);
543  return nullptr;
544  }
545  for (auto it = children_->Begin(); it != children_->End(); ++it)
546  {
547  VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
548  // recursively delete all cloned visuals if the child cannot be
549  // retrieved, or if cloning the child visual failed
550  if (!visual || !visual->Clone("", result))
551  {
552  gzerr << "Cloning a child visual failed.\n";
553  scene_->DestroyVisual(result, true);
554  return nullptr;
555  }
556  }
557 
558  for (unsigned int i = 0; i < this->GeometryCount(); ++i)
559  result->AddGeometry(this->GeometryByIndex(i)->Clone());
560 
561  if (this->Material())
562  result->SetMaterial(this->Material());
563 
564  for (const auto &[key, val] : this->userData)
565  result->SetUserData(key, val);
566 
567  // set static property after the geometry is added
568  result->SetStatic(this->Static());
569  return result;
570  }
571  }
572  }
573 }
574 #endif