Ignition Rendering

API Reference

0.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 <string>
23 
25 
26 namespace ignition
27 {
28  namespace rendering
29  {
30  template <class T>
31  class IGNITION_RENDERING_VISIBLE BaseVisual :
32  public virtual Visual,
33  public virtual T
34  {
35  protected: BaseVisual();
36 
37  public: virtual ~BaseVisual();
38 
39  public: virtual unsigned int ChildCount() const;
40 
41  public: virtual math::Pose3d LocalPose() const;
42 
43  public: virtual void SetLocalPose(const math::Pose3d &_pose);
44 
45  public: virtual bool HasChild(ConstNodePtr _child) const;
46 
47  public: virtual bool HasChildId(unsigned int _id) const;
48 
49  public: virtual bool HasChildName(const std::string &_name) const;
50 
51  public: virtual NodePtr ChildById(unsigned int _id) const;
52 
53  public: virtual NodePtr ChildByName(const std::string &_name) const;
54 
55  public: virtual NodePtr ChildByIndex(unsigned int _index) const;
56 
57  public: virtual void AddChild(NodePtr _child);
58 
59  public: virtual NodePtr RemoveChild(NodePtr _child);
60 
61  public: virtual NodePtr RemoveChildById(unsigned int _id);
62 
63  public: virtual NodePtr RemoveChildByName(const std::string &_name);
64 
65  public: virtual NodePtr RemoveChildByIndex(unsigned int _index);
66 
67  public: virtual void RemoveChildren();
68 
69  public: virtual unsigned int GeometryCount() const;
70 
71  public: virtual bool HasGeometry(ConstGeometryPtr _geometry) const;
72 
73  public: virtual GeometryPtr GeometryByIndex(unsigned int _index) const;
74 
75  public: virtual void AddGeometry(GeometryPtr _geometry);
76 
77  public: virtual GeometryPtr RemoveGeometry(GeometryPtr _geometry);
78 
79  public: virtual GeometryPtr RemoveGeometryByIndex(unsigned int _index);
80 
81  public: virtual void RemoveGeometries();
82 
83  public: virtual void SetMaterial(const std::string &_name,
84  bool _unique = true);
85 
86  public: virtual void SetMaterial(MaterialPtr _material,
87  bool _unique = true);
88 
89  public: virtual void SetChildMaterial(MaterialPtr _material,
90  bool _unique = true);
91 
92  public: virtual void SetGeometryMaterial(MaterialPtr _material,
93  bool _unique = true);
94 
95  // Documentation inherited.
96  public: virtual MaterialPtr Material();
97 
98  public: virtual math::Vector3d LocalScale() const = 0;
99 
100  public: virtual void SetLocalScale(double _scale);
101 
102  public: virtual void SetLocalScale(double _x, double _y, double _z);
103 
104  public: virtual void SetLocalScale(const math::Vector3d &_scale);
105 
106  public: virtual math::Vector3d WorldScale() const;
107 
108  public: virtual void SetWorldScale(double _scale);
109 
110  public: virtual void SetWorldScale(double _x, double _y, double _z);
111 
112  public: virtual void SetWorldScale(const math::Vector3d &_scale);
113 
114  public: virtual void Scale(double _scale);
115 
116  public: virtual void Scale(double _x, double _y, double _z);
117 
118  public: virtual void Scale(const math::Vector3d &_scale);
119 
120  public: virtual bool InheritScale() const = 0;
121 
122  public: virtual void PreRender();
123 
124  public: virtual void Destroy();
125 
126  protected: virtual void PreRenderChildren();
127 
128  protected: virtual void PreRenderGeometries();
129 
130  protected: virtual NodeStorePtr Children() const = 0;
131 
132  protected: virtual GeometryStorePtr Geometries() const = 0;
133 
134  protected: virtual bool AttachChild(NodePtr _child) = 0;
135 
136  protected: virtual bool DetachChild(NodePtr _child) = 0;
137 
138  protected: virtual bool AttachGeometry(GeometryPtr _geometry) = 0;
139 
140  protected: virtual bool DetachGeometry(GeometryPtr _geometry) = 0;
141 
142  protected: virtual void SetLocalScaleImpl(
143  const math::Vector3d &_scale) = 0;
144 
146  protected: MaterialPtr material;
147  };
148 
150  template <class T>
152  {
153  }
154 
156  template <class T>
158  {
159  }
160 
162  template <class T>
163  math::Pose3d BaseVisual<T>::LocalPose() const
164  {
165  math::Pose3d rawPose = this->RawLocalPose();
166  math::Vector3d scale = this->LocalScale();
167  rawPose.Pos() += rawPose.Rot() * (scale * this->origin);
168  return rawPose;
169  }
170 
172  template <class T>
173  void BaseVisual<T>::SetLocalPose(const math::Pose3d &_pose)
174  {
175  math::Pose3d rawPose = _pose;
176  math::Vector3d scale = this->LocalScale();
177  rawPose.Pos() -= rawPose.Rot() * (scale * this->origin);
178  this->SetRawLocalPose(rawPose);
179  }
180 
182  template <class T>
183  unsigned int BaseVisual<T>::ChildCount() const
184  {
185  return this->Children()->Size();
186  }
187 
189  template <class T>
191  {
192  return this->Children()->Contains(_child);
193  }
194 
196  template <class T>
197  bool BaseVisual<T>::HasChildId(unsigned int _id) const
198  {
199  return this->Children()->ContainsId(_id);
200  }
201 
203  template <class T>
204  bool BaseVisual<T>::HasChildName(const std::string &_name) const
205  {
206  return this->Children()->ContainsName(_name);
207  }
208 
210  template <class T>
211  NodePtr BaseVisual<T>::ChildById(unsigned int _id) const
212  {
213  return this->Children()->GetById(_id);
214  }
215 
217  template <class T>
219  {
220  return this->Children()->GetByName(_name);
221  }
222 
224  template <class T>
225  NodePtr BaseVisual<T>::ChildByIndex(unsigned int _index) const
226  {
227  return this->Children()->GetByIndex(_index);
228  }
229 
231  template <class T>
233  {
234  if (this->AttachChild(_child))
235  {
236  this->Children()->Add(_child);
237  }
238  }
239 
241  template <class T>
243  {
244  NodePtr child = this->Children()->Remove(_child);
245  if (child) this->DetachChild(child);
246  return child;
247  }
248 
250  template <class T>
252  {
253  NodePtr child = this->Children()->RemoveById(_id);
254  if (child) this->DetachChild(child);
255  return child;
256  }
257 
259  template <class T>
261  {
262  NodePtr child = this->Children()->RemoveByName(_name);
263  if (child) this->DetachChild(child);
264  return child;
265  }
266 
268  template <class T>
270  {
271  NodePtr child = this->Children()->RemoveByIndex(_index);
272  if (child) this->DetachChild(child);
273  return child;
274  }
275 
277  template <class T>
279  {
280  unsigned int count = this->Children()->Size();
281 
282  for (unsigned int i = 0; i < count; ++i)
283  {
284  this->RemoveChildByIndex(i);
285  }
286  }
287 
289  template <class T>
290  unsigned int BaseVisual<T>::GeometryCount() const
291  {
292  return this->Geometries()->Size();
293  }
294 
296  template <class T>
298  {
299  return this->Geometries()->Contains(_geometry);
300  }
301 
303  template <class T>
304  GeometryPtr BaseVisual<T>::GeometryByIndex(unsigned int _index) const
305  {
306  return this->Geometries()->GetByIndex(_index);
307  }
308 
310  template <class T>
312  {
313  if (this->AttachGeometry(_geometry))
314  {
315  this->Geometries()->Add(_geometry);
316  }
317  }
318 
320  template <class T>
322  {
323  return this->Geometries()->Remove(_geometry);
324  }
325 
327  template <class T>
329  {
330  return this->Geometries()->RemoveByIndex(_index);
331  }
332 
334  template <class T>
336  {
337  this->Geometries()->RemoveAll();
338  }
339 
341  template <class T>
342  void BaseVisual<T>::SetMaterial(const std::string &_name, bool _unique)
343  {
344  MaterialPtr mat = this->Scene()->Material(_name);
345  if (mat) this->SetMaterial(mat, _unique);
346  }
347 
349  template <class T>
350  void BaseVisual<T>::SetMaterial(MaterialPtr _material, bool _unique)
351  {
352  _material = (_unique) ? _material->Clone() : _material;
353  this->SetChildMaterial(_material, false);
354  this->SetGeometryMaterial(_material, false);
355  this->material = _material;
356  }
357 
359  template <class T>
360  void BaseVisual<T>::SetChildMaterial(MaterialPtr _material, bool _unique)
361  {
362  unsigned int count = this->ChildCount();
363  _material = (_unique && count > 0) ? _material->Clone() : _material;
364 
365  for (unsigned int i = 0; i < count; ++i)
366  {
367  NodePtr child = this->ChildByIndex(i);
368  VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
369  if (visual) visual->SetMaterial(_material, false);
370  }
371  }
372 
374  template <class T>
375  void BaseVisual<T>::SetGeometryMaterial(MaterialPtr _material, bool _unique)
376  {
377  unsigned int count = this->GeometryCount();
378  _material = (_unique && count > 0) ? _material->Clone() : _material;
379 
380  for (unsigned int i = 0; i < count; ++i)
381  {
382  GeometryPtr geometry = this->GeometryByIndex(i);
383  geometry->SetMaterial(_material, false);
384  }
385  }
386 
388  template <class T>
390  {
391  return this->material;
392  }
393 
395  template <class T>
396  void BaseVisual<T>::SetLocalScale(double _scale)
397  {
398  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
399  }
400 
402  template <class T>
403  void BaseVisual<T>::SetLocalScale(double _x, double _y, double _z)
404  {
405  this->SetLocalScale(math::Vector3d(_x, _y, _z));
406  }
407 
409  template <class T>
410  void BaseVisual<T>::SetLocalScale(const math::Vector3d &_scale)
411  {
412  math::Pose3d rawPose = this->LocalPose();
413  this->SetLocalScaleImpl(_scale);
414  this->SetLocalPose(rawPose);
415  }
416 
418  template <class T>
419  math::Vector3d BaseVisual<T>::WorldScale() const
420  {
421  math::Vector3d scale = this->LocalScale();
422 
423  if (!this->InheritScale() || !this->HasParent())
424  {
425  return scale;
426  }
427 
428  return scale * this->Parent()->WorldScale();
429  }
430 
432  template <class T>
433  void BaseVisual<T>::SetWorldScale(double _scale)
434  {
435  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
436  }
437 
439  template <class T>
440  void BaseVisual<T>::SetWorldScale(double _x, double _y, double _z)
441  {
442  this->SetWorldScale(math::Vector3d(_x, _y, _z));
443  }
444 
446  template <class T>
447  void BaseVisual<T>::SetWorldScale(const math::Vector3d &_scale)
448  {
449  math::Vector3d currentScale = this->WorldScale();
450  this->SetLocalScale(_scale / currentScale);
451  }
452 
454  template <class T>
455  void BaseVisual<T>::Scale(double _scale)
456  {
457  this->Scale(math::Vector3d(_scale, _scale, _scale));
458  }
459 
461  template <class T>
462  void BaseVisual<T>::Scale(double _x, double _y, double _z)
463  {
464  this->Scale(math::Vector3d(_x, _y, _z));
465  }
466 
468  template <class T>
469  void BaseVisual<T>::Scale(const math::Vector3d &_scale)
470  {
471  this->SetLocalScale(_scale * this->LocalScale());
472  }
473 
475  template <class T>
477  {
478  T::PreRender();
479  this->PreRenderChildren();
480  this->PreRenderGeometries();
481  }
482 
484  template <class T>
486  {
487  T::Destroy();
488  this->Geometries()->DestroyAll();
489  this->Children()->RemoveAll();
490  }
491 
493  template <class T>
495  {
496  unsigned int count = this->ChildCount();
497 
498  for (unsigned int i = 0; i < count; ++i)
499  {
500  NodePtr child = this->ChildByIndex(i);
501  child->PreRender();
502  }
503  }
504 
506  template <class T>
508  {
509  unsigned int count = this->GeometryCount();
510 
511  for (unsigned int i = 0; i < count; ++i)
512  {
513  GeometryPtr geometry = this->GeometryByIndex(i);
514  geometry->PreRender();
515  }
516  }
517 
518  }
519 }
520 #endif
virtual math::Pose3d LocalPose() const
Get the local pose.
Definition: BaseVisual.hh:163
virtual unsigned int GeometryCount() const
Get the number of geometries attached to this visual.
Definition: BaseVisual.hh:290
virtual ~BaseVisual()
Definition: BaseVisual.hh:157
virtual math::Vector3d WorldScale() const
Get the world scale.
Definition: BaseVisual.hh:419
virtual void SetLocalScale(double _scale)
Set the local scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: BaseVisual.hh:396
virtual void PreRender()
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseVisual.hh:476
virtual void PreRenderChildren()
Definition: BaseVisual.hh:494
virtual NodePtr ChildByName(const std::string &_name) const
Get node with given name. If no child exists with given name, NULL will be returned.
Definition: BaseVisual.hh:218
virtual MaterialPtr Material()
Get the material assigned to attached visuals and geometries.
Definition: BaseVisual.hh:389
virtual void AddGeometry(GeometryPtr _geometry)
Add the given geometry to this visual. If the given node is already attached, no work will be done...
Definition: BaseVisual.hh:311
virtual NodePtr ChildByIndex(unsigned int _index) const
Get node at given index. If no child exists at given index, NULL will be returned.
Definition: BaseVisual.hh:225
BaseVisual()
Definition: BaseVisual.hh:151
virtual void SetGeometryMaterial(MaterialPtr _material, bool _unique=true)
Set the material for all attached geometries only.
Definition: BaseVisual.hh:375
virtual GeometryPtr RemoveGeometry(GeometryPtr _geometry)
Remove the given geometry from this visual. If the given node is not a child of this visual...
Definition: BaseVisual.hh:321
virtual void SetMaterial(const std::string &_name, bool _unique=true)
Set the material for all attached visuals and geometries. The specified material will be retrieved fr...
Definition: BaseVisual.hh:342
virtual void SetLocalPose(const math::Pose3d &_pose)
Set the local pose.
Definition: BaseVisual.hh:173
virtual GeometryPtr RemoveGeometryByIndex(unsigned int _index)
Remove the geometry at the given index from this visual. If the specified node is not attached this v...
Definition: BaseVisual.hh:328
STL class.
virtual void SetChildMaterial(MaterialPtr _material, bool _unique=true)
Set the material for all attached visuals only.
Definition: BaseVisual.hh:360
virtual void PreRenderGeometries()
Definition: BaseVisual.hh:507
virtual void Scale(double _scale)
Scale the current scale by the given scalar. The given scalar will be assigned to the x...
Definition: BaseVisual.hh:455
virtual NodePtr RemoveChildByIndex(unsigned int _index)
Remove the node at the given index from this visual. If the specified node is not a child of this vis...
Definition: BaseVisual.hh:269
virtual NodePtr RemoveChild(NodePtr _child)
Remove the given node from this visual. If the given node is not a child of this visual, no work will be done.
Definition: BaseVisual.hh:242
virtual void AddChild(NodePtr _child)
Add the given node to this visual. If the given node is already a child, no work will be done...
Definition: BaseVisual.hh:232
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node...
Definition: Scene.hh:44
virtual unsigned int ChildCount() const
Get number of child nodes.
Definition: BaseVisual.hh:183
T dynamic_pointer_cast(T... args)
virtual NodePtr RemoveChildByName(const std::string &_name)
Remove the node with the given name from this visual. If the specified node is not a child of this vi...
Definition: BaseVisual.hh:260
virtual bool HasChildId(unsigned int _id) const
Determine if node with given ID is an attached child.
Definition: BaseVisual.hh:197
Definition: BaseVisual.hh:31
virtual void RemoveChildren()
Remove all child nodes from this visual.
Definition: BaseVisual.hh:278
virtual NodePtr RemoveChildById(unsigned int _id)
Remove the node with the given ID from this visual. If the specified node is not a child of this visu...
Definition: BaseVisual.hh:251
Represents a visual node in a scene graph. A Visual is the only node that can have Geometry and other...
Definition: Visual.hh:30
virtual bool HasGeometry(ConstGeometryPtr _geometry) const
Determine if given geometry is attached to this visual.
Definition: BaseVisual.hh:297
virtual void SetWorldScale(double _scale)
Set the world scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: BaseVisual.hh:433
MaterialPtr material
Pointer to material assigned to this visual.
Definition: BaseVisual.hh:146
virtual void RemoveGeometries()
Remove all attached geometries from this visual.
Definition: BaseVisual.hh:335
virtual NodePtr ChildById(unsigned int _id) const
Get node with given ID. If no child exists with given ID, NULL will be returned.
Definition: BaseVisual.hh:211
Definition: ArrowVisual.hh:22
virtual void Destroy()
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseVisual.hh:485
Represents a surface material of a Geometry.
Definition: Material.hh:34
virtual GeometryPtr GeometryByIndex(unsigned int _index) const
Get geometry at given index. If no geometry exists at given index, NULL will be returned.
Definition: BaseVisual.hh:304
virtual bool HasChildName(const std::string &_name) const
Determine if node with given name is an attached child.
Definition: BaseVisual.hh:204
virtual bool HasChild(ConstNodePtr _child) const
Determine if given node is an attached child.
Definition: BaseVisual.hh:190
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...