Gazebo Rendering

API Reference

6.6.3
gz/rendering/base/BaseNode.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_BASENODE_HH_
18 #define GZ_RENDERING_BASE_BASENODE_HH_
19 
20 #include <map>
21 #include <string>
22 
23 #include "gz/rendering/Node.hh"
24 #include "gz/rendering/Storage.hh"
26 
27 namespace ignition
28 {
29  namespace rendering
30  {
31  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
32  //
33  template <class T>
34  class BaseNode :
35  public virtual Node,
36  public virtual T
37  {
38  protected: BaseNode();
39 
40  public: virtual ~BaseNode();
41 
42  public: virtual NodePtr Parent() const override = 0;
43 
44  // Documentation inherited
45  public: virtual void RemoveParent() override;
46 
47  public: virtual math::Vector3d LocalPosition() const override;
48 
49  public: virtual math::Pose3d LocalPose() const override;
50 
51  // Documentation inherited
52  public: virtual math::Pose3d InitialLocalPose() const override;
53 
54  public: virtual void SetLocalPose(const math::Pose3d &_pose) override;
55 
56  public: virtual void SetLocalPosition(double _x, double _y, double _z)
57  override;
58 
59  public: virtual void SetLocalPosition(const math::Vector3d &_position)
60  override;
61 
62  public: virtual math::Quaterniond LocalRotation() const override;
63 
64  public: virtual void SetLocalRotation(double _r, double _p, double _y)
65  override;
66 
67  public: virtual void SetLocalRotation(double _w, double _x, double _y,
68  double _z) override;
69 
70  public: virtual void SetLocalRotation(const math::Quaterniond &_rotation)
71  override;
72 
73  public: virtual math::Pose3d WorldPose() const override;
74 
75  public: virtual void SetWorldPose(const math::Pose3d &_pose) override;
76 
77  public: virtual math::Vector3d WorldPosition() const override;
78 
79  public: virtual void SetWorldPosition(double _x, double _y, double _z)
80  override;
81 
82  public: virtual void SetWorldPosition(const math::Vector3d &_position)
83  override;
84 
85  public: virtual math::Quaterniond WorldRotation() const override;
86 
87  public: virtual void SetWorldRotation(double _r, double _p, double _y)
88  override;
89 
90  public: virtual void SetWorldRotation(double _w, double _x, double _y,
91  double _z) override;
92 
93  public: virtual void SetWorldRotation(const math::Quaterniond &_rotation)
94  override;
95 
96  public: virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose)
97  const override;
98 
99  public: virtual math::Vector3d Origin() const override;
100 
101  public: virtual void SetOrigin(double _x, double _y, double _z) override;
102 
103  public: virtual void SetOrigin(const math::Vector3d &_origin) override;
104 
105  // Documentation inherited
106  public: virtual math::Vector3d LocalScale() const override = 0;
107 
108  // Documentation inherited
109  public: virtual void SetLocalScale(double _scale) override;
110 
111  // Documentation inherited
112  public: virtual void SetLocalScale(double _x, double _y, double _z)
113  override;
114 
115  // Documentation inherited
116  public: virtual void SetLocalScale(const math::Vector3d &_scale) override;
117 
118  // Documentation inherited
119  public: virtual math::Vector3d WorldScale() const override;
120 
121  // Documentation inherited
122  public: virtual void SetWorldScale(double _scale) override;
123 
124  // Documentation inherited
125  public: virtual void SetWorldScale(double _x, double _y, double _z)
126  override;
127 
128  // Documentation inherited
129  public: virtual void SetWorldScale(const math::Vector3d &_scale) override;
130 
131  // Documentation inherited
132  public: virtual void Scale(double _scale) override;
133 
134  // Documentation inherited
135  public: virtual void Scale(double _x, double _y, double _z) override;
136 
137  // Documentation inherited
138  public: virtual void Scale(const math::Vector3d &_scale) override;
139 
140  // Documentation inherited
141  public: virtual bool InheritScale() const override = 0;
142 
143  public: virtual void Destroy() override;
144 
145  public: virtual unsigned int ChildCount() const override;
146 
147  public: virtual bool HasChild(ConstNodePtr _child) const override;
148 
149  public: virtual bool HasChildId(unsigned int _id) const override;
150 
151  public: virtual bool HasChildName(const std::string &_name) const
152  override;
153 
154  public: virtual NodePtr ChildById(unsigned int _id) const override;
155 
156  public: virtual NodePtr ChildByName(const std::string &_name) const
157  override;
158 
159  public: virtual NodePtr ChildByIndex(unsigned int _index) const override;
160 
161  public: virtual void AddChild(NodePtr _child) override;
162 
163  public: virtual NodePtr RemoveChild(NodePtr _child) override;
164 
165  public: virtual NodePtr RemoveChildById(unsigned int _id) override;
166 
167  public: virtual NodePtr RemoveChildByName(const std::string &_name)
168  override;
169 
170  public: virtual NodePtr RemoveChildByIndex(unsigned int _index) override;
171 
172  public: virtual void RemoveChildren() override;
173 
174  public: virtual void PreRender() override;
175 
176  // Documentation inherited
177  public: virtual void SetUserData(const std::string &_key, Variant _value)
178  override;
179 
180  // Documentation inherited
181  public: virtual Variant UserData(const std::string &_key) const override;
182 
183  // Documentation inherited
184  public: virtual bool HasUserData(const std::string &_key) const override;
185 
186  protected: virtual void PreRenderChildren();
187 
188  protected: virtual math::Pose3d RawLocalPose() const = 0;
189 
190  protected: virtual void SetRawLocalPose(const math::Pose3d &_pose) = 0;
191 
192  protected: virtual NodeStorePtr Children() const = 0;
193 
194  protected: virtual bool AttachChild(NodePtr _child) = 0;
195 
196  protected: virtual bool DetachChild(NodePtr _child) = 0;
197 
200  protected: virtual void SetLocalScaleImpl(
201  const math::Vector3d &_scale) = 0;
202 
203  protected: math::Vector3d origin;
204 
207  protected: bool initialLocalPoseSet = false;
208 
210  protected: ignition::math::Pose3d initialLocalPose =
212 
215  };
216 
218  template <class T>
220  {
221  }
222 
224  template <class T>
226  {
227  }
228 
230  template <class T>
232  {
233  NodePtr parent = this->Parent();
234 
235  if (parent)
236  {
237  auto baseShared = this->shared_from_this();
238  auto thisShared = std::dynamic_pointer_cast<BaseNode<T>>(baseShared);
239  parent->RemoveChild(thisShared);
240  }
241  }
242 
243 
245  template <class T>
247  {
248  if (_child->Id() == this->Id())
249  {
250  ignerr << "Cannot add self as a child node" << std::endl;
251  return;
252  }
253 
254  if (this->AttachChild(_child))
255  {
256  this->Children()->Add(_child);
257  }
258  }
259 
261  template <class T>
263  {
264  NodePtr child = this->Children()->Remove(_child);
265  if (child) this->DetachChild(child);
266  return child;
267  }
268 
270  template <class T>
272  {
273  NodePtr child = this->Children()->RemoveById(_id);
274  if (child) this->DetachChild(child);
275  return child;
276  }
277 
279  template <class T>
281  {
282  NodePtr child = this->Children()->RemoveByName(_name);
283  if (child) this->DetachChild(child);
284  return child;
285  }
286 
288  template <class T>
290  {
291  NodePtr child = this->Children()->RemoveByIndex(_index);
292  if (child) this->DetachChild(child);
293  return child;
294  }
295 
297  template <class T>
299  {
300  for (unsigned int i = this->ChildCount(); i > 0; --i)
301  {
302  this->RemoveChildByIndex(i - 1);
303  }
304  }
305 
307  template <class T>
309  {
310  this->PreRenderChildren();
311  }
312 
314  template <class T>
316  {
317  unsigned int count = this->ChildCount();
318 
319  for (unsigned int i = 0; i < count; ++i)
320  {
321  this->ChildByIndex(i)->PreRender();
322  }
323  }
324 
326  template <class T>
328  {
329  math::Pose3d pose = this->RawLocalPose();
330  pose.Pos() += pose.Rot() * this->origin;
331  return pose;
332  }
333 
335  template <class T>
337  {
338  if (!_pose.IsFinite())
339  {
340  ignerr << "Unable to set non-finite pose [" << _pose
341  << "] to node [" << this->Name() << "]" << std::endl;
342  return;
343  }
344 
345  math::Pose3d pose = _pose;
346  pose.Pos() = pose.Pos() - pose.Rot() * this->origin;
347 
348  if (!initialLocalPoseSet)
349  {
350  this->initialLocalPose = pose;
351  this->initialLocalPoseSet = true;
352  }
353 
354  this->SetRawLocalPose(pose);
355  }
356 
358  template <class T>
360  {
361  return this->initialLocalPose;
362  }
363 
365  template <class T>
367  {
368  return this->LocalPose().Pos();
369  }
370 
372  template <class T>
373  void BaseNode<T>::SetLocalPosition(double _x, double _y, double _z)
374  {
375  this->SetLocalPosition(math::Vector3d(_x, _y, _z));
376  }
377 
379  template <class T>
381  {
382  if (!_position.IsFinite())
383  {
384  ignerr << "Unable to set non-finite position [" << _position
385  << "] to node [" << this->Name() << "]" << std::endl;
386  return;
387  }
388 
389  math::Pose3d pose = this->LocalPose();
390  pose.Pos() = _position;
391  this->SetLocalPose(pose);
392  }
393 
395  template <class T>
397  {
398  return this->LocalPose().Rot();
399  }
400 
402  template <class T>
403  void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
404  {
405  this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
406  }
407 
409  template <class T>
410  void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
411  double _z)
412  {
413  this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
414  }
415 
417  template <class T>
419  {
420  if (!_rotation.IsFinite())
421  {
422  ignerr << "Unable to set non-finite rotation [" << _rotation
423  << "] to node [" << this->Name() << "]" << std::endl;
424  return;
425  }
426 
427  math::Pose3d pose = this->LocalPose();
428  pose.Rot() = _rotation;
429  this->SetLocalPose(pose);
430  }
431 
433  template <class T>
435  {
436  NodePtr parent = this->Parent();
437  math::Pose3d pose = this->LocalPose();
438 
439  if (!parent)
440  {
441  return pose;
442  }
443 
444  return parent->WorldPose() * pose;
445  }
446 
448  template <class T>
450  {
451  math::Pose3d pose = this->WorldToLocal(_pose);
452  this->SetLocalPose(pose);
453  }
454 
456  template <class T>
457  void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
458  {
459  this->SetWorldPosition(math::Vector3d(_x, _y, _z));
460  }
461 
463  template <class T>
465  {
466  return this->WorldPose().Pos();
467  }
468 
470  template <class T>
472  {
473  math::Pose3d pose = this->WorldPose();
474  pose.Pos() = _position;
475  this->SetWorldPose(pose);
476  }
477 
479  template <class T>
481  {
482  return this->WorldPose().Rot();
483  }
484 
486  template <class T>
487  void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
488  {
489  this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
490  }
491 
493  template <class T>
494  void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
495  double _z)
496  {
497  this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
498  }
499 
501  template <class T>
503  {
504  math::Pose3d pose = this->WorldPose();
505  pose.Rot() = _rotation;
506  this->SetWorldPose(pose);
507  }
508 
510  template <class T>
512  {
513  NodePtr parent = this->Parent();
514 
515  if (!parent)
516  {
517  return _pose;
518  }
519 
520  return _pose - parent->WorldPose();
521  }
522 
524  template <class T>
526  {
527  return this->origin;
528  }
529 
531  template <class T>
532  void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
533  {
534  this->SetOrigin(math::Vector3d(_x, _y, _z));
535  }
536 
538  template <class T>
540  {
541  if (!_origin.IsFinite())
542  {
543  ignerr << "Unable to set non-finite origin [" << _origin
544  << "] to node [" << this->Name() << "]" << std::endl;
545  return;
546  }
547  this->origin = _origin;
548  }
549 
551  template <class T>
552  void BaseNode<T>::SetLocalScale(double _scale)
553  {
554  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
555  }
556 
558  template <class T>
559  void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
560  {
561  this->SetLocalScale(math::Vector3d(_x, _y, _z));
562  }
563 
565  template <class T>
567  {
568  math::Pose3d rawPose = this->LocalPose();
569  this->SetLocalScaleImpl(_scale);
570  this->SetLocalPose(rawPose);
571  }
572 
574  template <class T>
576  {
577  math::Vector3d scale = this->LocalScale();
578 
579  if (!this->InheritScale() || !this->HasParent())
580  {
581  return scale;
582  }
583 
584  return scale * this->Parent()->WorldScale();
585  }
586 
588  template <class T>
589  void BaseNode<T>::SetWorldScale(double _scale)
590  {
591  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
592  }
593 
595  template <class T>
596  void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
597  {
598  this->SetWorldScale(math::Vector3d(_x, _y, _z));
599  }
600 
602  template <class T>
604  {
606  if (this->InheritScale() && this->HasParent())
607  toScale = this->Parent()->WorldScale();
608 
609  this->SetLocalScale(_scale / toScale);
610  }
611 
613  template <class T>
614  void BaseNode<T>::Scale(double _scale)
615  {
616  this->Scale(math::Vector3d(_scale, _scale, _scale));
617  }
618 
620  template <class T>
621  void BaseNode<T>::Scale(double _x, double _y, double _z)
622  {
623  this->Scale(math::Vector3d(_x, _y, _z));
624  }
625 
627  template <class T>
628  void BaseNode<T>::Scale(const math::Vector3d &_scale)
629  {
630  this->SetLocalScale(_scale * this->LocalScale());
631  }
632 
634  template <class T>
636  {
637  T::Destroy();
638  this->RemoveParent();
639  }
640 
642  template <class T>
643  unsigned int BaseNode<T>::ChildCount() const
644  {
645  return this->Children()->Size();
646  }
647 
649  template <class T>
651  {
652  return this->Children()->Contains(_child);
653  }
654 
656  template <class T>
657  bool BaseNode<T>::HasChildId(unsigned int _id) const
658  {
659  return this->Children()->ContainsId(_id);
660  }
661 
663  template <class T>
664  bool BaseNode<T>::HasChildName(const std::string &_name) const
665  {
666  return this->Children()->ContainsName(_name);
667  }
668 
670  template <class T>
671  NodePtr BaseNode<T>::ChildById(unsigned int _id) const
672  {
673  return this->Children()->GetById(_id);
674  }
675 
677  template <class T>
679  {
680  return this->Children()->GetByName(_name);
681  }
682 
684  template <class T>
685  NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
686  {
687  return this->Children()->GetByIndex(_index);
688  }
689  }
690 
692  template <class T>
693  void BaseNode<T>::SetUserData(const std::string &_key, Variant _value)
694  {
695  this->userData[_key] = _value;
696  }
697 
699  template <class T>
701  {
702  Variant value;
703  auto it = this->userData.find(_key);
704  if (it != this->userData.end())
705  value = it->second;
706  return value;
707  }
708 
710  template <class T>
711  bool BaseNode<T>::HasUserData(const std::string &_key) const
712  {
713  return this->userData.find(_key) != this->userData.end();
714  }
715  }
716 }
717 #endif
virtual void PreRenderChildren()
Definition: gz/rendering/base/BaseNode.hh:315
virtual void SetWorldRotation(double _r, double _p, double _y) override
Set the world rotation.
Definition: gz/rendering/base/BaseNode.hh:487
std::variant< std::monostate, int, float, double, std::string, bool, unsigned int > Variant
Alias for a variant that can hold various types of data. The first type of the variant is std::monost...
Definition: gz/rendering/Node.hh:47
virtual bool InheritScale() const override=0
Determine if this visual inherits scale from this parent.
Represents a single posable node in the scene graph.
Definition: gz/rendering/Node.hh:51
virtual ~BaseNode()
Definition: gz/rendering/base/BaseNode.hh:225
STL class.
STL class.
virtual bool HasChild(ConstNodePtr _child) const override
Determine if given node is an attached child.
Definition: gz/rendering/base/BaseNode.hh:650
virtual math::Vector3d LocalPosition() const override
Get the local position.
Definition: gz/rendering/base/BaseNode.hh:366
virtual bool HasChildName(const std::string &_name) const override
Determine if node with given name is an attached child.
Definition: gz/rendering/base/BaseNode.hh:664
virtual void SetWorldScale(double _scale) override
Set the world scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: gz/rendering/base/BaseNode.hh:589
virtual bool AttachChild(NodePtr _child)=0
virtual void RemoveChildren() override
Remove all child nodes from this node This detaches all the child nodes but does not destroy them.
Definition: gz/rendering/base/BaseNode.hh:298
virtual void SetRawLocalPose(const math::Pose3d &_pose)=0
virtual void Scale(double _scale) override
Scale the current scale by the given scalar. The given scalar will be assigned to the x,...
Definition: gz/rendering/base/BaseNode.hh:614
virtual void SetLocalPosition(double _x, double _y, double _z) override
Set the local position.
Definition: gz/rendering/base/BaseNode.hh:373
Vector3< T > & Pos()
virtual math::Pose3d RawLocalPose() const =0
static const Vector3 One
virtual void AddChild(NodePtr _child) override
Add the given node to this node. If the given node is already a child, no work will be done.
Definition: gz/rendering/base/BaseNode.hh:246
virtual Variant UserData(const std::string &_key) const override
Get custom data stored in this node.
Definition: gz/rendering/base/BaseNode.hh:700
virtual math::Vector3d WorldPosition() const override
Get the world position.
Definition: gz/rendering/base/BaseNode.hh:464
virtual void SetLocalScaleImpl(const math::Vector3d &_scale)=0
Implementation of the SetLocalScale function.
virtual math::Pose3d InitialLocalPose() const override
Get the initial local pose.
Definition: gz/rendering/base/BaseNode.hh:359
virtual void SetUserData(const std::string &_key, Variant _value) override
Store any custom data associated with this node.
Definition: gz/rendering/base/BaseNode.hh:693
virtual bool DetachChild(NodePtr _child)=0
#define ignerr
BaseNode()
Definition: gz/rendering/base/BaseNode.hh:219
virtual NodePtr ChildByName(const std::string &_name) const override
Get node with given name. If no child exists with given name, NULL will be returned.
Definition: gz/rendering/base/BaseNode.hh:678
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: gz/rendering/base/BaseNode.hh:336
virtual NodePtr Parent() const override=0
Get the parent Node.
virtual math::Quaterniond WorldRotation() const override
Get the world rotation.
Definition: gz/rendering/base/BaseNode.hh:480
virtual void SetLocalScale(double _scale) override
Set the local scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: gz/rendering/base/BaseNode.hh:552
virtual NodePtr RemoveChild(NodePtr _child) override
Remove (detach) the given node from this node. If the given node is not a child of this node,...
Definition: gz/rendering/base/BaseNode.hh:262
virtual void SetWorldPose(const math::Pose3d &_pose) override
Set the world pose.
Definition: gz/rendering/base/BaseNode.hh:449
std::map< std::string, Variant > userData
A map of custom key value data.
Definition: gz/rendering/base/BaseNode.hh:214
virtual bool HasUserData(const std::string &_key) const override
Check if node has custom data.
Definition: gz/rendering/base/BaseNode.hh:711
virtual math::Pose3d WorldPose() const override
Get the world pose.
Definition: gz/rendering/base/BaseNode.hh:434
virtual NodePtr RemoveChildByIndex(unsigned int _index) override
Remove (detach) the node at the given index from this node. If the specified node is not a child of t...
Definition: gz/rendering/base/BaseNode.hh:289
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/BaseNode.hh:308
bool initialLocalPoseSet
Flag to indicate whether initial local pose is set for this node.
Definition: gz/rendering/base/BaseNode.hh:207
math::Vector3d origin
Definition: gz/rendering/base/BaseNode.hh:203
virtual unsigned int ChildCount() const override
Get number of child nodes.
Definition: gz/rendering/base/BaseNode.hh:643
virtual void SetOrigin(double _x, double _y, double _z) override
Set position of origin. The position should be relative to the original origin of the geometry.
Definition: gz/rendering/base/BaseNode.hh:532
virtual NodeStorePtr Children() const =0
virtual math::Vector3d Origin() const override
Get position of origin.
Definition: gz/rendering/base/BaseNode.hh:525
virtual void SetWorldPosition(double _x, double _y, double _z) override
Set the world position.
Definition: gz/rendering/base/BaseNode.hh:457
virtual NodePtr ChildById(unsigned int _id) const override
Get node with given ID. If no child exists with given ID, NULL will be returned.
Definition: gz/rendering/base/BaseNode.hh:671
T endl(T... args)
Definition: gz/rendering/base/BaseNode.hh:34
virtual bool HasChildId(unsigned int _id) const override
Determine if node with given ID is an attached child.
Definition: gz/rendering/base/BaseNode.hh:657
virtual math::Quaterniond LocalRotation() const override
Get the local rotation.
Definition: gz/rendering/base/BaseNode.hh:396
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: gz/rendering/base/BaseNode.hh:327
virtual math::Vector3d LocalScale() const override=0
Get the local scale.
ignition::math::Pose3d initialLocalPose
Initial local pose for this node.
Definition: gz/rendering/base/BaseNode.hh:210
virtual NodePtr RemoveChildById(unsigned int _id) override
Remove (detach) the node with the given ID from this node. If the specified node is not a child of th...
Definition: gz/rendering/base/BaseNode.hh:271
virtual void RemoveParent() override
Detach this Node from its parent. If this Node does not have a parent, no work will be done.
Definition: gz/rendering/base/BaseNode.hh:231
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: gz/rendering/base/BaseNode.hh:635
virtual math::Vector3d WorldScale() const override
Get the world scale.
Definition: gz/rendering/base/BaseNode.hh:575
bool IsFinite() const
static const Pose3< T > Zero
virtual NodePtr ChildByIndex(unsigned int _index) const override
Get node at given index. If no child exists at given index, NULL will be returned.
Definition: gz/rendering/base/BaseNode.hh:685
virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose) const override
Convert given world pose to local pose.
Definition: gz/rendering/base/BaseNode.hh:511
Quaternion< T > & Rot()
virtual void SetLocalRotation(double _r, double _p, double _y) override
Set the local rotation.
Definition: gz/rendering/base/BaseNode.hh:403
virtual NodePtr RemoveChildByName(const std::string &_name) override
Remove (detach) the node with the given name from this node. If the specified node is not a child of ...
Definition: gz/rendering/base/BaseNode.hh:280