Ignition Rendering

API Reference

4.0.0
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 <string>
24 
25 namespace ignition
26 {
27  namespace rendering
28  {
29  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
30  //
31  template <class T>
32  class BaseNode :
33  public virtual Node,
34  public virtual T
35  {
36  protected: BaseNode();
37 
38  public: virtual ~BaseNode();
39 
40  public: virtual NodePtr Parent() const override = 0;
41 
42  // Documentation inherited
43  public: virtual void RemoveParent() override;
44 
45  public: virtual math::Vector3d LocalPosition() const override;
46 
47  public: virtual math::Pose3d LocalPose() const override;
48 
49  public: virtual void SetLocalPose(const math::Pose3d &_pose) override;
50 
51  public: virtual void SetLocalPosition(double _x, double _y, double _z)
52  override;
53 
54  public: virtual void SetLocalPosition(const math::Vector3d &_position)
55  override;
56 
57  public: virtual math::Quaterniond LocalRotation() const override;
58 
59  public: virtual void SetLocalRotation(double _r, double _p, double _y)
60  override;
61 
62  public: virtual void SetLocalRotation(double _w, double _x, double _y,
63  double _z) override;
64 
65  public: virtual void SetLocalRotation(const math::Quaterniond &_rotation)
66  override;
67 
68  public: virtual math::Pose3d WorldPose() const override;
69 
70  public: virtual void SetWorldPose(const math::Pose3d &_pose) override;
71 
72  public: virtual math::Vector3d WorldPosition() const override;
73 
74  public: virtual void SetWorldPosition(double _x, double _y, double _z)
75  override;
76 
77  public: virtual void SetWorldPosition(const math::Vector3d &_position)
78  override;
79 
80  public: virtual math::Quaterniond WorldRotation() const override;
81 
82  public: virtual void SetWorldRotation(double _r, double _p, double _y)
83  override;
84 
85  public: virtual void SetWorldRotation(double _w, double _x, double _y,
86  double _z) override;
87 
88  public: virtual void SetWorldRotation(const math::Quaterniond &_rotation)
89  override;
90 
91  public: virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose)
92  const override;
93 
94  public: virtual math::Vector3d Origin() const override;
95 
96  public: virtual void SetOrigin(double _x, double _y, double _z) override;
97 
98  public: virtual void SetOrigin(const math::Vector3d &_origin) override;
99 
100  // Documentation inherited
101  public: virtual math::Vector3d LocalScale() const override = 0;
102 
103  // Documentation inherited
104  public: virtual void SetLocalScale(double _scale) override;
105 
106  // Documentation inherited
107  public: virtual void SetLocalScale(double _x, double _y, double _z)
108  override;
109 
110  // Documentation inherited
111  public: virtual void SetLocalScale(const math::Vector3d &_scale) override;
112 
113  // Documentation inherited
114  public: virtual math::Vector3d WorldScale() const override;
115 
116  // Documentation inherited
117  public: virtual void SetWorldScale(double _scale) override;
118 
119  // Documentation inherited
120  public: virtual void SetWorldScale(double _x, double _y, double _z)
121  override;
122 
123  // Documentation inherited
124  public: virtual void SetWorldScale(const math::Vector3d &_scale) override;
125 
126  // Documentation inherited
127  public: virtual void Scale(double _scale) override;
128 
129  // Documentation inherited
130  public: virtual void Scale(double _x, double _y, double _z) override;
131 
132  // Documentation inherited
133  public: virtual void Scale(const math::Vector3d &_scale) override;
134 
135  // Documentation inherited
136  public: virtual bool InheritScale() const override = 0;
137 
138  public: virtual void Destroy() override;
139 
140  public: virtual unsigned int ChildCount() const override;
141 
142  public: virtual bool HasChild(ConstNodePtr _child) const override;
143 
144  public: virtual bool HasChildId(unsigned int _id) const override;
145 
146  public: virtual bool HasChildName(const std::string &_name) const
147  override;
148 
149  public: virtual NodePtr ChildById(unsigned int _id) const override;
150 
151  public: virtual NodePtr ChildByName(const std::string &_name) const
152  override;
153 
154  public: virtual NodePtr ChildByIndex(unsigned int _index) const override;
155 
156  public: virtual void AddChild(NodePtr _child) override;
157 
158  public: virtual NodePtr RemoveChild(NodePtr _child) override;
159 
160  public: virtual NodePtr RemoveChildById(unsigned int _id) override;
161 
162  public: virtual NodePtr RemoveChildByName(const std::string &_name)
163  override;
164 
165  public: virtual NodePtr RemoveChildByIndex(unsigned int _index) override;
166 
167  public: virtual void RemoveChildren() override;
168 
169  public: virtual void PreRender() override;
170 
171  protected: virtual void PreRenderChildren();
172 
173  protected: virtual math::Pose3d RawLocalPose() const = 0;
174 
175  protected: virtual void SetRawLocalPose(const math::Pose3d &_pose) = 0;
176 
177  protected: virtual NodeStorePtr Children() const = 0;
178 
179  protected: virtual bool AttachChild(NodePtr _child) = 0;
180 
181  protected: virtual bool DetachChild(NodePtr _child) = 0;
182 
185  protected: virtual void SetLocalScaleImpl(
186  const math::Vector3d &_scale) = 0;
187 
188  protected: math::Vector3d origin;
189  };
190 
192  template <class T>
194  {
195  }
196 
198  template <class T>
200  {
201  }
202 
204  template <class T>
206  {
207  NodePtr parent = this->Parent();
208 
209  if (parent)
210  {
211  auto baseShared = this->shared_from_this();
212  auto thisShared = std::dynamic_pointer_cast<BaseNode<T>>(baseShared);
213  parent->RemoveChild(thisShared);
214  }
215  }
216 
217 
219  template <class T>
221  {
222  if (_child->Id() == this->Id())
223  {
224  ignerr << "Cannot add self as a child node" << std::endl;
225  return;
226  }
227 
228  if (this->AttachChild(_child))
229  {
230  this->Children()->Add(_child);
231  }
232  }
233 
235  template <class T>
237  {
238  NodePtr child = this->Children()->Remove(_child);
239  if (child) this->DetachChild(child);
240  return child;
241  }
242 
244  template <class T>
246  {
247  NodePtr child = this->Children()->RemoveById(_id);
248  if (child) this->DetachChild(child);
249  return child;
250  }
251 
253  template <class T>
255  {
256  NodePtr child = this->Children()->RemoveByName(_name);
257  if (child) this->DetachChild(child);
258  return child;
259  }
260 
262  template <class T>
264  {
265  NodePtr child = this->Children()->RemoveByIndex(_index);
266  if (child) this->DetachChild(child);
267  return child;
268  }
269 
271  template <class T>
273  {
274  for (unsigned int i = this->ChildCount(); i > 0; --i)
275  {
276  this->RemoveChildByIndex(i - 1);
277  }
278  }
279 
281  template <class T>
283  {
284  T::PreRender();
285  this->PreRenderChildren();
286  }
287 
289  template <class T>
291  {
292  unsigned int count = this->ChildCount();
293 
294  for (unsigned int i = 0; i < count; ++i)
295  {
296  this->ChildByIndex(i)->PreRender();
297  }
298  }
299 
301  template <class T>
303  {
304  math::Pose3d pose = this->RawLocalPose();
305  pose.Pos() += pose.Rot() * this->origin;
306  return pose;
307  }
308 
310  template <class T>
312  {
313  math::Pose3d pose = _pose;
314  pose.Pos() = pose.Pos() - pose.Rot() * this->origin;
315  this->SetRawLocalPose(pose);
316  }
317 
319  template <class T>
321  {
322  return this->LocalPose().Pos();
323  }
324 
326  template <class T>
327  void BaseNode<T>::SetLocalPosition(double _x, double _y, double _z)
328  {
329  this->SetLocalPosition(math::Vector3d(_x, _y, _z));
330  }
331 
333  template <class T>
335  {
336  math::Pose3d pose = this->LocalPose();
337  pose.Pos() = _position;
338  this->SetLocalPose(pose);
339  }
340 
342  template <class T>
344  {
345  return this->LocalPose().Rot();
346  }
347 
349  template <class T>
350  void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
351  {
352  this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
353  }
354 
356  template <class T>
357  void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
358  double _z)
359  {
360  this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
361  }
362 
364  template <class T>
366  {
367  math::Pose3d pose = this->LocalPose();
368  pose.Rot() = _rotation;
369  this->SetLocalPose(pose);
370  }
371 
373  template <class T>
375  {
376  NodePtr parent = this->Parent();
377  math::Pose3d pose = this->LocalPose();
378 
379  if (!parent)
380  {
381  return pose;
382  }
383 
384  return pose + parent->WorldPose();
385  }
386 
388  template <class T>
390  {
391  math::Pose3d pose = this->WorldToLocal(_pose);
392  this->SetLocalPose(pose);
393  }
394 
396  template <class T>
397  void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
398  {
399  this->SetWorldPosition(math::Vector3d(_x, _y, _z));
400  }
401 
403  template <class T>
405  {
406  return this->WorldPose().Pos();
407  }
408 
410  template <class T>
412  {
413  math::Pose3d pose = this->WorldPose();
414  pose.Pos() = _position;
415  this->SetWorldPose(pose);
416  }
417 
419  template <class T>
421  {
422  return this->WorldPose().Rot();
423  }
424 
426  template <class T>
427  void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
428  {
429  this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
430  }
431 
433  template <class T>
434  void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
435  double _z)
436  {
437  this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
438  }
439 
441  template <class T>
443  {
444  math::Pose3d pose = this->WorldPose();
445  pose.Rot() = _rotation;
446  this->SetWorldPose(pose);
447  }
448 
450  template <class T>
452  {
453  NodePtr parent = this->Parent();
454 
455  if (!parent)
456  {
457  return _pose;
458  }
459 
460  return _pose - parent->WorldPose();
461  }
462 
464  template <class T>
466  {
467  return this->origin;
468  }
469 
471  template <class T>
472  void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
473  {
474  this->SetOrigin(math::Vector3d(_x, _y, _z));
475  }
476 
478  template <class T>
480  {
481  this->origin = _origin;
482  }
483 
485  template <class T>
486  void BaseNode<T>::SetLocalScale(double _scale)
487  {
488  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
489  }
490 
492  template <class T>
493  void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
494  {
495  this->SetLocalScale(math::Vector3d(_x, _y, _z));
496  }
497 
499  template <class T>
501  {
502  math::Pose3d rawPose = this->LocalPose();
503  this->SetLocalScaleImpl(_scale);
504  this->SetLocalPose(rawPose);
505  }
506 
508  template <class T>
510  {
511  math::Vector3d scale = this->LocalScale();
512 
513  if (!this->InheritScale() || !this->HasParent())
514  {
515  return scale;
516  }
517 
518  return scale * this->Parent()->WorldScale();
519  }
520 
522  template <class T>
523  void BaseNode<T>::SetWorldScale(double _scale)
524  {
525  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
526  }
527 
529  template <class T>
530  void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
531  {
532  this->SetWorldScale(math::Vector3d(_x, _y, _z));
533  }
534 
536  template <class T>
538  {
540  if (this->InheritScale() && this->HasParent())
541  toScale = this->Parent()->WorldScale();
542 
543  this->SetLocalScale(_scale / toScale);
544  }
545 
547  template <class T>
548  void BaseNode<T>::Scale(double _scale)
549  {
550  this->Scale(math::Vector3d(_scale, _scale, _scale));
551  }
552 
554  template <class T>
555  void BaseNode<T>::Scale(double _x, double _y, double _z)
556  {
557  this->Scale(math::Vector3d(_x, _y, _z));
558  }
559 
561  template <class T>
562  void BaseNode<T>::Scale(const math::Vector3d &_scale)
563  {
564  this->SetLocalScale(_scale * this->LocalScale());
565  }
566 
567 
568 
570  template <class T>
572  {
573  T::Destroy();
574  this->RemoveParent();
575  }
576 
578  template <class T>
579  unsigned int BaseNode<T>::ChildCount() const
580  {
581  return this->Children()->Size();
582  }
583 
585  template <class T>
587  {
588  return this->Children()->Contains(_child);
589  }
590 
592  template <class T>
593  bool BaseNode<T>::HasChildId(unsigned int _id) const
594  {
595  return this->Children()->ContainsId(_id);
596  }
597 
599  template <class T>
600  bool BaseNode<T>::HasChildName(const std::string &_name) const
601  {
602  return this->Children()->ContainsName(_name);
603  }
604 
606  template <class T>
607  NodePtr BaseNode<T>::ChildById(unsigned int _id) const
608  {
609  return this->Children()->GetById(_id);
610  }
611 
613  template <class T>
615  {
616  return this->Children()->GetByName(_name);
617  }
618 
620  template <class T>
621  NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
622  {
623  return this->Children()->GetByIndex(_index);
624  }
625  }
626  }
627 }
628 #endif
virtual math::Pose3d WorldPose() const override
Get the world pose.
Definition: BaseNode.hh:374
virtual math::Quaterniond LocalRotation() const override
Get the local rotation.
Definition: BaseNode.hh:343
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:486
virtual math::Quaterniond WorldRotation() const override
Get the world rotation.
Definition: BaseNode.hh:420
virtual unsigned int ChildCount() const override
Get number of child nodes.
Definition: BaseNode.hh:579
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseNode.hh:571
virtual math::Vector3d WorldScale() const override
Get the world scale.
Definition: BaseNode.hh:509
virtual void SetWorldPosition(double _x, double _y, double _z) override
Set the world position.
Definition: BaseNode.hh:397
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:593
virtual void SetWorldRotation(double _r, double _p, double _y) override
Set the world rotation.
Definition: BaseNode.hh:427
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:614
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:245
Represents a single posable node in the scene graph.
Definition: Node.hh:37
virtual ~BaseNode()
Definition: BaseNode.hh:199
virtual math::Vector3d Origin() const override
Get position of origin.
Definition: BaseNode.hh:465
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:472
virtual math::Vector3d LocalPosition() const override
Get the local position.
Definition: BaseNode.hh:320
virtual bool HasChildName(const std::string &_name) const override
Determine if node with given name is an attached child.
Definition: BaseNode.hh:600
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:523
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:272
virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose) const override
Convert given world pose to local pose.
Definition: BaseNode.hh:451
virtual void SetWorldPose(const math::Pose3d &_pose) override
Set the world pose.
Definition: BaseNode.hh:389
virtual void SetLocalRotation(double _r, double _p, double _y) override
Set the local rotation.
Definition: BaseNode.hh:350
virtual math::Pose3d LocalPose() const override
Get the local pose.
Definition: BaseNode.hh:302
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:282
virtual math::Vector3d WorldPosition() const override
Get the world position.
Definition: BaseNode.hh:404
T dynamic_pointer_cast(T... args)
const Quaternion< T > & Rot() const
virtual void SetLocalPosition(double _x, double _y, double _z) override
Set the local position.
Definition: BaseNode.hh:327
virtual void PreRenderChildren()
Definition: BaseNode.hh:290
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:220
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:607
virtual bool HasChild(ConstNodePtr _child) const override
Determine if given node is an attached child.
Definition: BaseNode.hh:586
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:205
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:236
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:548
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:263
BaseNode()
Definition: BaseNode.hh:193
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:621
Definition: BaseNode.hh:32
math::Vector3d origin
Definition: BaseNode.hh:188
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:254
virtual void SetLocalPose(const math::Pose3d &_pose) override
Set the local pose.
Definition: BaseNode.hh:311