Ignition Rendering

API Reference

5.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 
316  if (!pose.IsFinite())
317  {
318  ignerr << "Unable to set pose of a node: "
319  << "non-finite (nan, inf) values detected." << std::endl;
320  return;
321  }
322 
323  this->SetRawLocalPose(pose);
324  }
325 
327  template <class T>
329  {
330  return this->LocalPose().Pos();
331  }
332 
334  template <class T>
335  void BaseNode<T>::SetLocalPosition(double _x, double _y, double _z)
336  {
337  this->SetLocalPosition(math::Vector3d(_x, _y, _z));
338  }
339 
341  template <class T>
343  {
344  math::Pose3d pose = this->LocalPose();
345  pose.Pos() = _position;
346  this->SetLocalPose(pose);
347  }
348 
350  template <class T>
352  {
353  return this->LocalPose().Rot();
354  }
355 
357  template <class T>
358  void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
359  {
360  this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
361  }
362 
364  template <class T>
365  void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
366  double _z)
367  {
368  this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
369  }
370 
372  template <class T>
374  {
375  math::Pose3d pose = this->LocalPose();
376  pose.Rot() = _rotation;
377  this->SetLocalPose(pose);
378  }
379 
381  template <class T>
383  {
384  NodePtr parent = this->Parent();
385  math::Pose3d pose = this->LocalPose();
386 
387  if (!parent)
388  {
389  return pose;
390  }
391 
392  return pose + parent->WorldPose();
393  }
394 
396  template <class T>
398  {
399  math::Pose3d pose = this->WorldToLocal(_pose);
400  this->SetLocalPose(pose);
401  }
402 
404  template <class T>
405  void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
406  {
407  this->SetWorldPosition(math::Vector3d(_x, _y, _z));
408  }
409 
411  template <class T>
413  {
414  return this->WorldPose().Pos();
415  }
416 
418  template <class T>
420  {
421  math::Pose3d pose = this->WorldPose();
422  pose.Pos() = _position;
423  this->SetWorldPose(pose);
424  }
425 
427  template <class T>
429  {
430  return this->WorldPose().Rot();
431  }
432 
434  template <class T>
435  void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
436  {
437  this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
438  }
439 
441  template <class T>
442  void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
443  double _z)
444  {
445  this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
446  }
447 
449  template <class T>
451  {
452  math::Pose3d pose = this->WorldPose();
453  pose.Rot() = _rotation;
454  this->SetWorldPose(pose);
455  }
456 
458  template <class T>
460  {
461  NodePtr parent = this->Parent();
462 
463  if (!parent)
464  {
465  return _pose;
466  }
467 
468  return _pose - parent->WorldPose();
469  }
470 
472  template <class T>
474  {
475  return this->origin;
476  }
477 
479  template <class T>
480  void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
481  {
482  this->SetOrigin(math::Vector3d(_x, _y, _z));
483  }
484 
486  template <class T>
488  {
489  this->origin = _origin;
490  }
491 
493  template <class T>
494  void BaseNode<T>::SetLocalScale(double _scale)
495  {
496  this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
497  }
498 
500  template <class T>
501  void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
502  {
503  this->SetLocalScale(math::Vector3d(_x, _y, _z));
504  }
505 
507  template <class T>
509  {
510  math::Pose3d rawPose = this->LocalPose();
511  this->SetLocalScaleImpl(_scale);
512  this->SetLocalPose(rawPose);
513  }
514 
516  template <class T>
518  {
519  math::Vector3d scale = this->LocalScale();
520 
521  if (!this->InheritScale() || !this->HasParent())
522  {
523  return scale;
524  }
525 
526  return scale * this->Parent()->WorldScale();
527  }
528 
530  template <class T>
531  void BaseNode<T>::SetWorldScale(double _scale)
532  {
533  this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
534  }
535 
537  template <class T>
538  void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
539  {
540  this->SetWorldScale(math::Vector3d(_x, _y, _z));
541  }
542 
544  template <class T>
546  {
548  if (this->InheritScale() && this->HasParent())
549  toScale = this->Parent()->WorldScale();
550 
551  this->SetLocalScale(_scale / toScale);
552  }
553 
555  template <class T>
556  void BaseNode<T>::Scale(double _scale)
557  {
558  this->Scale(math::Vector3d(_scale, _scale, _scale));
559  }
560 
562  template <class T>
563  void BaseNode<T>::Scale(double _x, double _y, double _z)
564  {
565  this->Scale(math::Vector3d(_x, _y, _z));
566  }
567 
569  template <class T>
570  void BaseNode<T>::Scale(const math::Vector3d &_scale)
571  {
572  this->SetLocalScale(_scale * this->LocalScale());
573  }
574 
575 
576 
578  template <class T>
580  {
581  T::Destroy();
582  this->RemoveParent();
583  }
584 
586  template <class T>
587  unsigned int BaseNode<T>::ChildCount() const
588  {
589  return this->Children()->Size();
590  }
591 
593  template <class T>
595  {
596  return this->Children()->Contains(_child);
597  }
598 
600  template <class T>
601  bool BaseNode<T>::HasChildId(unsigned int _id) const
602  {
603  return this->Children()->ContainsId(_id);
604  }
605 
607  template <class T>
608  bool BaseNode<T>::HasChildName(const std::string &_name) const
609  {
610  return this->Children()->ContainsName(_name);
611  }
612 
614  template <class T>
615  NodePtr BaseNode<T>::ChildById(unsigned int _id) const
616  {
617  return this->Children()->GetById(_id);
618  }
619 
621  template <class T>
623  {
624  return this->Children()->GetByName(_name);
625  }
626 
628  template <class T>
629  NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
630  {
631  return this->Children()->GetByIndex(_index);
632  }
633  }
634  }
635 }
636 #endif
virtual math::Pose3d WorldPose() const override
Get the world pose.
Definition: BaseNode.hh:382
virtual math::Quaterniond LocalRotation() const override
Get the local rotation.
Definition: BaseNode.hh:351
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:494
virtual math::Quaterniond WorldRotation() const override
Get the world rotation.
Definition: BaseNode.hh:428
virtual unsigned int ChildCount() const override
Get number of child nodes.
Definition: BaseNode.hh:587
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: BaseNode.hh:579
virtual math::Vector3d WorldScale() const override
Get the world scale.
Definition: BaseNode.hh:517
virtual void SetWorldPosition(double _x, double _y, double _z) override
Set the world position.
Definition: BaseNode.hh:405
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:601
virtual void SetWorldRotation(double _r, double _p, double _y) override
Set the world rotation.
Definition: BaseNode.hh:435
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:622
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:473
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:480
virtual math::Vector3d LocalPosition() const override
Get the local position.
Definition: BaseNode.hh:328
virtual bool HasChildName(const std::string &_name) const override
Determine if node with given name is an attached child.
Definition: BaseNode.hh:608
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:531
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:459
virtual void SetWorldPose(const math::Pose3d &_pose) override
Set the world pose.
Definition: BaseNode.hh:397
virtual void SetLocalRotation(double _r, double _p, double _y) override
Set the local rotation.
Definition: BaseNode.hh:358
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:412
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:335
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:615
virtual bool HasChild(ConstNodePtr _child) const override
Determine if given node is an attached child.
Definition: BaseNode.hh:594
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:556
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:629
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