Ignition Rendering

API Reference

6.3.1
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 IGNITION_RENDERING_BASE_BASENODE_HH_
18 #define IGNITION_RENDERING_BASE_BASENODE_HH_
19 
20 #include <map>
21 #include <string>
22 
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  T::PreRender();
311  this->PreRenderChildren();
312  }
313 
315  template <class T>
317  {
318  unsigned int count = this->ChildCount();
319 
320  for (unsigned int i = 0; i < count; ++i)
321  {
322  this->ChildByIndex(i)->PreRender();
323  }
324  }
325 
327  template <class T>
329  {
330  math::Pose3d pose = this->RawLocalPose();
331  pose.Pos() += pose.Rot() * this->origin;
332  return pose;
333  }
334 
336  template <class T>
338  {
339  math::Pose3d pose = _pose;
340  pose.Pos() = pose.Pos() - pose.Rot() * this->origin;
341 
342  if (!pose.IsFinite())
343  {
344  ignerr << "Unable to set pose of a node: "
345  << "non-finite (nan, inf) values detected." << std::endl;
346  return;
347  }
348 
349  if (!initialLocalPoseSet)
350  {
351  this->initialLocalPose = pose;
352  this->initialLocalPoseSet = true;
353  }
354 
355  this->SetRawLocalPose(pose);
356  }
357 
359  template <class T>
361  {
362  return this->initialLocalPose;
363  }
364 
366  template <class T>
368  {
369  return this->LocalPose().Pos();
370  }
371 
373  template <class T>
374  void BaseNode<T>::SetLocalPosition(double _x, double _y, double _z)
375  {
376  this->SetLocalPosition(math::Vector3d(_x, _y, _z));
377  }
378 
380  template <class T>
382  {
383  math::Pose3d pose = this->LocalPose();
384  pose.Pos() = _position;
385  this->SetLocalPose(pose);
386  }
387 
389  template <class T>
391  {
392  return this->LocalPose().Rot();
393  }
394 
396  template <class T>
397  void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
398  {
399  this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
400  }
401 
403  template <class T>
404  void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
405  double _z)
406  {
407  this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
408  }
409 
411  template <class T>
413  {
414  math::Pose3d pose = this->LocalPose();
415  pose.Rot() = _rotation;
416  this->SetLocalPose(pose);
417  }
418 
420  template <class T>
422  {
423  NodePtr parent = this->Parent();
424  math::Pose3d pose = this->LocalPose();
425 
426  if (!parent)
427  {
428  return pose;
429  }
430 
431  return parent->WorldPose() * pose;
432  }
433 
435  template <class T>
437  {
438  math::Pose3d pose = this->WorldToLocal(_pose);
439  this->SetLocalPose(pose);
440  }
441 
443  template <class T>
444  void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
445  {
446  this->SetWorldPosition(math::Vector3d(_x, _y, _z));
447  }
448 
450  template <class T>
452  {
453  return this->WorldPose().Pos();
454  }
455 
457  template <class T>
459  {
460  math::Pose3d pose = this->WorldPose();
461  pose.Pos() = _position;
462  this->SetWorldPose(pose);
463  }
464 
466  template <class T>
468  {
469  return this->WorldPose().Rot();
470  }
471 
473  template <class T>
474  void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
475  {
476  this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
477  }
478 
480  template <class T>
481  void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
482  double _z)
483  {
484  this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
485  }
486 
488  template <class T>
490  {
491  math::Pose3d pose = this->WorldPose();
492  pose.Rot() = _rotation;
493  this->SetWorldPose(pose);
494  }
495 
497  template <class T>
499  {
500  NodePtr parent = this->Parent();
501 
502  if (!parent)
503  {
504  return _pose;
505  }
506 
507  return _pose - parent->WorldPose();
508  }
509 
511  template <class T>
513  {
514  return this->origin;
515  }
516 
518  template <class T>
519  void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
520  {
521  this->SetOrigin(math::Vector3d(_x, _y, _z));
522  }
523 
525  template <class T>
527  {
528  this->origin = _origin;
529  }
530 
532  template <class T>
533  void BaseNode<T>::SetLocalScale(double _scale)
534  {
535  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
536  }
537 
539  template <class T>
540  void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
541  {
542  this->SetLocalScale(math::Vector3d(_x, _y, _z));
543  }
544 
546  template <class T>
548  {
549  math::Pose3d rawPose = this->LocalPose();
550  this->SetLocalScaleImpl(_scale);
551  this->SetLocalPose(rawPose);
552  }
553 
555  template <class T>
557  {
558  math::Vector3d scale = this->LocalScale();
559 
560  if (!this->InheritScale() || !this->HasParent())
561  {
562  return scale;
563  }
564 
565  return scale * this->Parent()->WorldScale();
566  }
567 
569  template <class T>
570  void BaseNode<T>::SetWorldScale(double _scale)
571  {
572  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
573  }
574 
576  template <class T>
577  void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
578  {
579  this->SetWorldScale(math::Vector3d(_x, _y, _z));
580  }
581 
583  template <class T>
585  {
587  if (this->InheritScale() && this->HasParent())
588  toScale = this->Parent()->WorldScale();
589 
590  this->SetLocalScale(_scale / toScale);
591  }
592 
594  template <class T>
595  void BaseNode<T>::Scale(double _scale)
596  {
597  this->Scale(math::Vector3d(_scale, _scale, _scale));
598  }
599 
601  template <class T>
602  void BaseNode<T>::Scale(double _x, double _y, double _z)
603  {
604  this->Scale(math::Vector3d(_x, _y, _z));
605  }
606 
608  template <class T>
609  void BaseNode<T>::Scale(const math::Vector3d &_scale)
610  {
611  this->SetLocalScale(_scale * this->LocalScale());
612  }
613 
615  template <class T>
617  {
618  T::Destroy();
619  this->RemoveParent();
620  }
621 
623  template <class T>
624  unsigned int BaseNode<T>::ChildCount() const
625  {
626  return this->Children()->Size();
627  }
628 
630  template <class T>
632  {
633  return this->Children()->Contains(_child);
634  }
635 
637  template <class T>
638  bool BaseNode<T>::HasChildId(unsigned int _id) const
639  {
640  return this->Children()->ContainsId(_id);
641  }
642 
644  template <class T>
645  bool BaseNode<T>::HasChildName(const std::string &_name) const
646  {
647  return this->Children()->ContainsName(_name);
648  }
649 
651  template <class T>
652  NodePtr BaseNode<T>::ChildById(unsigned int _id) const
653  {
654  return this->Children()->GetById(_id);
655  }
656 
658  template <class T>
660  {
661  return this->Children()->GetByName(_name);
662  }
663 
665  template <class T>
666  NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
667  {
668  return this->Children()->GetByIndex(_index);
669  }
670  }
671 
673  template <class T>
674  void BaseNode<T>::SetUserData(const std::string &_key, Variant _value)
675  {
676  this->userData[_key] = _value;
677  }
678 
680  template <class T>
682  {
683  Variant value;
684  auto it = this->userData.find(_key);
685  if (it != this->userData.end())
686  value = it->second;
687  return value;
688  }
689 
691  template <class T>
692  bool BaseNode<T>::HasUserData(const std::string &_key) const
693  {
694  return this->userData.find(_key) != this->userData.end();
695  }
696  }
697 }
698 #endif
virtual math::Pose3d WorldPose() const override
Get the world pose.
Definition: BaseNode.hh:421
virtual bool HasUserData(const std::string &_key) const override
Check if node has custom data.
Definition: BaseNode.hh:692
virtual math::Quaterniond LocalRotation() const override
Get the local rotation.
Definition: BaseNode.hh:390
bool IsFinite() const
virtual void SetLocalScale(double _scale) override
Set the local scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: BaseNode.hh:533
virtual math::Quaterniond WorldRotation() const override
Get the world rotation.
Definition: BaseNode.hh:467
virtual unsigned int ChildCount() const override
Get number of child nodes.
Definition: BaseNode.hh:624
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseNode.hh:616
virtual math::Vector3d WorldScale() const override
Get the world scale.
Definition: BaseNode.hh:556
virtual Variant UserData(const std::string &_key) const override
Get custom data stored in this node.
Definition: BaseNode.hh:681
virtual void SetWorldPosition(double _x, double _y, double _z) override
Set the world position.
Definition: BaseNode.hh:444
T endl(T... args)
virtual bool HasChildId(unsigned int _id) const override
Determine if node with given ID is an attached child.
Definition: BaseNode.hh:638
virtual void SetWorldRotation(double _r, double _p, double _y) override
Set the world rotation.
Definition: BaseNode.hh:474
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: BaseNode.hh:659
static const Pose3< T > Zero
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: BaseNode.hh:271
Represents a single posable node in the scene graph.
Definition: Node.hh:51
virtual ~BaseNode()
Definition: BaseNode.hh:225
virtual math::Vector3d Origin() const override
Get position of origin.
Definition: BaseNode.hh:512
STL class.
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: BaseNode.hh:519
virtual math::Pose3d InitialLocalPose() const override
Get the initial local pose.
Definition: BaseNode.hh:360
virtual math::Vector3d LocalPosition() const override
Get the local position.
Definition: BaseNode.hh:367
virtual bool HasChildName(const std::string &_name) const override
Determine if node with given name is an attached child.
Definition: BaseNode.hh:645
virtual void SetWorldScale(double _scale) override
Set the world scale. The given scale will be assigned to the x, y, and z coordinates.
Definition: BaseNode.hh:570
virtual void RemoveChildren() override
Remove all child nodes from this node This detaches all the child nodes but does not destroy them...
Definition: BaseNode.hh:298
virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose) const override
Convert given world pose to local pose.
Definition: BaseNode.hh:498
virtual void SetWorldPose(const math::Pose3d &_pose) override
Set the world pose.
Definition: BaseNode.hh:436
virtual void SetLocalRotation(double _r, double _p, double _y) override
Set the local rotation.
Definition: BaseNode.hh:397
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: BaseNode.hh:328
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseNode.hh:308
virtual math::Vector3d WorldPosition() const override
Get the world position.
Definition: BaseNode.hh:451
T dynamic_pointer_cast(T... args)
const Quaternion< T > & Rot() const
virtual void SetUserData(const std::string &_key, Variant _value) override
Store any custom data associated with this node.
Definition: BaseNode.hh:674
virtual void SetLocalPosition(double _x, double _y, double _z) override
Set the local position.
Definition: BaseNode.hh:374
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: Node.hh:47
virtual void PreRenderChildren()
Definition: BaseNode.hh:316
static const Vector3 One
#define ignerr
const Vector3< T > & Pos() const
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: BaseNode.hh:246
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: BaseNode.hh:652
virtual bool HasChild(ConstNodePtr _child) const override
Determine if given node is an attached child.
Definition: BaseNode.hh:631
virtual void RemoveParent() override
Detach this Node from its parent. If this Node does not have a parent, no work will be done...
Definition: BaseNode.hh:231
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: BaseNode.hh:262
virtual void Scale(double _scale) override
Scale the current scale by the given scalar. The given scalar will be assigned to the x...
Definition: BaseNode.hh:595
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: BaseNode.hh:289
BaseNode()
Definition: BaseNode.hh:219
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: BaseNode.hh:666
std::map< std::string, Variant > userData
A map of custom key value data.
Definition: BaseNode.hh:214
Definition: BaseNode.hh:34
math::Vector3d origin
Definition: BaseNode.hh:203
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: BaseNode.hh:280
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: BaseNode.hh:337