Gazebo Rendering

API Reference

8.2.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 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 gz
28 {
29  namespace rendering
30  {
31  inline namespace GZ_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: gz::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  gzerr << "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  if (!_pose.IsFinite())
340  {
341  gzerr << "Unable to set non-finite pose [" << _pose
342  << "] to node [" << this->Name() << "]" << std::endl;
343  return;
344  }
345 
346  math::Pose3d pose = _pose;
347  pose.Pos() = pose.Pos() - pose.Rot() * this->origin;
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  if (!_position.IsFinite())
384  {
385  gzerr << "Unable to set non-finite position [" << _position
386  << "] to node [" << this->Name() << "]" << std::endl;
387  return;
388  }
389 
390  math::Pose3d pose = this->LocalPose();
391  pose.Pos() = _position;
392  this->SetLocalPose(pose);
393  }
394 
396  template <class T>
398  {
399  return this->LocalPose().Rot();
400  }
401 
403  template <class T>
404  void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
405  {
406  this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
407  }
408 
410  template <class T>
411  void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
412  double _z)
413  {
414  this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
415  }
416 
418  template <class T>
420  {
421  if (!_rotation.IsFinite())
422  {
423  gzerr << "Unable to set non-finite rotation [" << _rotation
424  << "] to node [" << this->Name() << "]" << std::endl;
425  return;
426  }
427 
428  math::Pose3d pose = this->LocalPose();
429  pose.Rot() = _rotation;
430  this->SetLocalPose(pose);
431  }
432 
434  template <class T>
436  {
437  NodePtr parent = this->Parent();
438  math::Pose3d pose = this->LocalPose();
439 
440  if (!parent)
441  {
442  return pose;
443  }
444 
445  return parent->WorldPose() * pose;
446  }
447 
449  template <class T>
451  {
452  math::Pose3d pose = this->WorldToLocal(_pose);
453  this->SetLocalPose(pose);
454  }
455 
457  template <class T>
458  void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
459  {
460  this->SetWorldPosition(math::Vector3d(_x, _y, _z));
461  }
462 
464  template <class T>
466  {
467  return this->WorldPose().Pos();
468  }
469 
471  template <class T>
473  {
474  math::Pose3d pose = this->WorldPose();
475  pose.Pos() = _position;
476  this->SetWorldPose(pose);
477  }
478 
480  template <class T>
482  {
483  return this->WorldPose().Rot();
484  }
485 
487  template <class T>
488  void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
489  {
490  this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
491  }
492 
494  template <class T>
495  void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
496  double _z)
497  {
498  this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
499  }
500 
502  template <class T>
504  {
505  math::Pose3d pose = this->WorldPose();
506  pose.Rot() = _rotation;
507  this->SetWorldPose(pose);
508  }
509 
511  template <class T>
513  {
514  NodePtr parent = this->Parent();
515 
516  if (!parent)
517  {
518  return _pose;
519  }
520 
521  return parent->WorldPose().Inverse() * _pose;
522  }
523 
525  template <class T>
527  {
528  return this->origin;
529  }
530 
532  template <class T>
533  void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
534  {
535  this->SetOrigin(math::Vector3d(_x, _y, _z));
536  }
537 
539  template <class T>
541  {
542  if (!_origin.IsFinite())
543  {
544  gzerr << "Unable to set non-finite origin [" << _origin
545  << "] to node [" << this->Name() << "]" << std::endl;
546  return;
547  }
548  this->origin = _origin;
549  }
550 
552  template <class T>
553  void BaseNode<T>::SetLocalScale(double _scale)
554  {
555  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
556  }
557 
559  template <class T>
560  void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
561  {
562  this->SetLocalScale(math::Vector3d(_x, _y, _z));
563  }
564 
566  template <class T>
568  {
569  math::Pose3d rawPose = this->LocalPose();
570  this->SetLocalScaleImpl(_scale);
571  this->SetLocalPose(rawPose);
572  }
573 
575  template <class T>
577  {
578  math::Vector3d scale = this->LocalScale();
579 
580  if (!this->InheritScale() || !this->HasParent())
581  {
582  return scale;
583  }
584 
585  return scale * this->Parent()->WorldScale();
586  }
587 
589  template <class T>
590  void BaseNode<T>::SetWorldScale(double _scale)
591  {
592  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
593  }
594 
596  template <class T>
597  void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
598  {
599  this->SetWorldScale(math::Vector3d(_x, _y, _z));
600  }
601 
603  template <class T>
605  {
607  if (this->InheritScale() && this->HasParent())
608  toScale = this->Parent()->WorldScale();
609 
610  this->SetLocalScale(_scale / toScale);
611  }
612 
614  template <class T>
615  void BaseNode<T>::Scale(double _scale)
616  {
617  this->Scale(math::Vector3d(_scale, _scale, _scale));
618  }
619 
621  template <class T>
622  void BaseNode<T>::Scale(double _x, double _y, double _z)
623  {
624  this->Scale(math::Vector3d(_x, _y, _z));
625  }
626 
628  template <class T>
629  void BaseNode<T>::Scale(const math::Vector3d &_scale)
630  {
631  this->SetLocalScale(_scale * this->LocalScale());
632  }
633 
635  template <class T>
637  {
638  T::Destroy();
639  this->RemoveParent();
640  }
641 
643  template <class T>
644  unsigned int BaseNode<T>::ChildCount() const
645  {
646  return this->Children()->Size();
647  }
648 
650  template <class T>
652  {
653  return this->Children()->Contains(_child);
654  }
655 
657  template <class T>
658  bool BaseNode<T>::HasChildId(unsigned int _id) const
659  {
660  return this->Children()->ContainsId(_id);
661  }
662 
664  template <class T>
665  bool BaseNode<T>::HasChildName(const std::string &_name) const
666  {
667  return this->Children()->ContainsName(_name);
668  }
669 
671  template <class T>
672  NodePtr BaseNode<T>::ChildById(unsigned int _id) const
673  {
674  return this->Children()->GetById(_id);
675  }
676 
678  template <class T>
680  {
681  return this->Children()->GetByName(_name);
682  }
683 
685  template <class T>
686  NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
687  {
688  return this->Children()->GetByIndex(_index);
689  }
690  }
691 
693  template <class T>
694  void BaseNode<T>::SetUserData(const std::string &_key, Variant _value)
695  {
696  this->userData[_key] = _value;
697  }
698 
700  template <class T>
702  {
703  Variant value;
704  auto it = this->userData.find(_key);
705  if (it != this->userData.end())
706  value = it->second;
707  return value;
708  }
709 
711  template <class T>
712  bool BaseNode<T>::HasUserData(const std::string &_key) const
713  {
714  return this->userData.find(_key) != this->userData.end();
715  }
716  }
717 }
718 #endif