-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBaseNode.hh
718 lines (577 loc) · 20.4 KB
/
BaseNode.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* Copyright (C) 2015 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GZ_RENDERING_BASE_BASENODE_HH_
#define GZ_RENDERING_BASE_BASENODE_HH_
#include <map>
#include <string>
#include "gz/rendering/Node.hh"
#include "gz/rendering/Storage.hh"
#include "gz/rendering/base/BaseStorage.hh"
namespace gz
{
namespace rendering
{
inline namespace GZ_RENDERING_VERSION_NAMESPACE {
//
template <class T>
class BaseNode :
public virtual Node,
public virtual T
{
protected: BaseNode();
public: virtual ~BaseNode();
public: virtual NodePtr Parent() const override = 0;
// Documentation inherited
public: virtual void RemoveParent() override;
public: virtual math::Vector3d LocalPosition() const override;
public: virtual math::Pose3d LocalPose() const override;
// Documentation inherited
public: virtual math::Pose3d InitialLocalPose() const override;
public: virtual void SetLocalPose(const math::Pose3d &_pose) override;
public: virtual void SetLocalPosition(double _x, double _y, double _z)
override;
public: virtual void SetLocalPosition(const math::Vector3d &_position)
override;
public: virtual math::Quaterniond LocalRotation() const override;
public: virtual void SetLocalRotation(double _r, double _p, double _y)
override;
public: virtual void SetLocalRotation(double _w, double _x, double _y,
double _z) override;
public: virtual void SetLocalRotation(const math::Quaterniond &_rotation)
override;
public: virtual math::Pose3d WorldPose() const override;
public: virtual void SetWorldPose(const math::Pose3d &_pose) override;
public: virtual math::Vector3d WorldPosition() const override;
public: virtual void SetWorldPosition(double _x, double _y, double _z)
override;
public: virtual void SetWorldPosition(const math::Vector3d &_position)
override;
public: virtual math::Quaterniond WorldRotation() const override;
public: virtual void SetWorldRotation(double _r, double _p, double _y)
override;
public: virtual void SetWorldRotation(double _w, double _x, double _y,
double _z) override;
public: virtual void SetWorldRotation(const math::Quaterniond &_rotation)
override;
public: virtual math::Pose3d WorldToLocal(const math::Pose3d &_pose)
const override;
public: virtual math::Vector3d Origin() const override;
public: virtual void SetOrigin(double _x, double _y, double _z) override;
public: virtual void SetOrigin(const math::Vector3d &_origin) override;
// Documentation inherited
public: virtual math::Vector3d LocalScale() const override = 0;
// Documentation inherited
public: virtual void SetLocalScale(double _scale) override;
// Documentation inherited
public: virtual void SetLocalScale(double _x, double _y, double _z)
override;
// Documentation inherited
public: virtual void SetLocalScale(const math::Vector3d &_scale) override;
// Documentation inherited
public: virtual math::Vector3d WorldScale() const override;
// Documentation inherited
public: virtual void SetWorldScale(double _scale) override;
// Documentation inherited
public: virtual void SetWorldScale(double _x, double _y, double _z)
override;
// Documentation inherited
public: virtual void SetWorldScale(const math::Vector3d &_scale) override;
// Documentation inherited
public: virtual void Scale(double _scale) override;
// Documentation inherited
public: virtual void Scale(double _x, double _y, double _z) override;
// Documentation inherited
public: virtual void Scale(const math::Vector3d &_scale) override;
// Documentation inherited
public: virtual bool InheritScale() const override = 0;
public: virtual void Destroy() override;
public: virtual unsigned int ChildCount() const override;
public: virtual bool HasChild(ConstNodePtr _child) const override;
public: virtual bool HasChildId(unsigned int _id) const override;
public: virtual bool HasChildName(const std::string &_name) const
override;
public: virtual NodePtr ChildById(unsigned int _id) const override;
public: virtual NodePtr ChildByName(const std::string &_name) const
override;
public: virtual NodePtr ChildByIndex(unsigned int _index) const override;
public: virtual void AddChild(NodePtr _child) override;
public: virtual NodePtr RemoveChild(NodePtr _child) override;
public: virtual NodePtr RemoveChildById(unsigned int _id) override;
public: virtual NodePtr RemoveChildByName(const std::string &_name)
override;
public: virtual NodePtr RemoveChildByIndex(unsigned int _index) override;
public: virtual void RemoveChildren() override;
public: virtual void PreRender() override;
// Documentation inherited
public: virtual void SetUserData(const std::string &_key, Variant _value)
override;
// Documentation inherited
public: virtual Variant UserData(const std::string &_key) const override;
// Documentation inherited
public: virtual bool HasUserData(const std::string &_key) const override;
protected: virtual void PreRenderChildren();
protected: virtual math::Pose3d RawLocalPose() const = 0;
protected: virtual void SetRawLocalPose(const math::Pose3d &_pose) = 0;
protected: virtual NodeStorePtr Children() const = 0;
protected: virtual bool AttachChild(NodePtr _child) = 0;
protected: virtual bool DetachChild(NodePtr _child) = 0;
/// \brief Implementation of the SetLocalScale function
/// \param[in] _scale Scale to set the visual to
protected: virtual void SetLocalScaleImpl(
const math::Vector3d &_scale) = 0;
protected: math::Vector3d origin;
/// \brief Flag to indicate whether initial local pose
/// is set for this node.
protected: bool initialLocalPoseSet = false;
/// \brief Initial local pose for this node.
protected: gz::math::Pose3d initialLocalPose =
gz::math::Pose3d::Zero;
/// \brief A map of custom key value data
protected: std::map<std::string, Variant> userData;
};
//////////////////////////////////////////////////
template <class T>
BaseNode<T>::BaseNode()
{
}
//////////////////////////////////////////////////
template <class T>
BaseNode<T>::~BaseNode()
{
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::RemoveParent()
{
NodePtr parent = this->Parent();
if (parent)
{
auto baseShared = this->shared_from_this();
auto thisShared = std::dynamic_pointer_cast<BaseNode<T>>(baseShared);
parent->RemoveChild(thisShared);
}
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::AddChild(NodePtr _child)
{
if (_child->Id() == this->Id())
{
gzerr << "Cannot add self as a child node" << std::endl;
return;
}
if (this->AttachChild(_child))
{
this->Children()->Add(_child);
}
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::RemoveChild(NodePtr _child)
{
NodePtr child = this->Children()->Remove(_child);
if (child) this->DetachChild(child);
return child;
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::RemoveChildById(unsigned int _id)
{
NodePtr child = this->Children()->RemoveById(_id);
if (child) this->DetachChild(child);
return child;
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::RemoveChildByName(const std::string &_name)
{
NodePtr child = this->Children()->RemoveByName(_name);
if (child) this->DetachChild(child);
return child;
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::RemoveChildByIndex(unsigned int _index)
{
NodePtr child = this->Children()->RemoveByIndex(_index);
if (child) this->DetachChild(child);
return child;
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::RemoveChildren()
{
for (unsigned int i = this->ChildCount(); i > 0; --i)
{
this->RemoveChildByIndex(i - 1);
}
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::PreRender()
{
T::PreRender();
this->PreRenderChildren();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::PreRenderChildren()
{
unsigned int count = this->ChildCount();
for (unsigned int i = 0; i < count; ++i)
{
this->ChildByIndex(i)->PreRender();
}
}
//////////////////////////////////////////////////
template <class T>
math::Pose3d BaseNode<T>::LocalPose() const
{
math::Pose3d pose = this->RawLocalPose();
pose.Pos() += pose.Rot() * this->origin;
return pose;
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalPose(const math::Pose3d &_pose)
{
if (!_pose.IsFinite())
{
gzerr << "Unable to set non-finite pose [" << _pose
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}
math::Pose3d pose = _pose;
pose.Pos() = pose.Pos() - pose.Rot() * this->origin;
if (!initialLocalPoseSet)
{
this->initialLocalPose = pose;
this->initialLocalPoseSet = true;
}
this->SetRawLocalPose(pose);
}
//////////////////////////////////////////////////
template <class T>
math::Pose3d BaseNode<T>::InitialLocalPose() const
{
return this->initialLocalPose;
}
//////////////////////////////////////////////////
template <class T>
math::Vector3d BaseNode<T>::LocalPosition() const
{
return this->LocalPose().Pos();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalPosition(double _x, double _y, double _z)
{
this->SetLocalPosition(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalPosition(const math::Vector3d &_position)
{
if (!_position.IsFinite())
{
gzerr << "Unable to set non-finite position [" << _position
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}
math::Pose3d pose = this->LocalPose();
pose.Pos() = _position;
this->SetLocalPose(pose);
}
//////////////////////////////////////////////////
template <class T>
math::Quaterniond BaseNode<T>::LocalRotation() const
{
return this->LocalPose().Rot();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalRotation(double _r, double _p, double _y)
{
this->SetLocalRotation(math::Quaterniond(_r, _p, _y));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalRotation(double _w, double _x, double _y,
double _z)
{
this->SetLocalRotation(math::Quaterniond(_w, _x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalRotation(const math::Quaterniond &_rotation)
{
if (!_rotation.IsFinite())
{
gzerr << "Unable to set non-finite rotation [" << _rotation
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}
math::Pose3d pose = this->LocalPose();
pose.Rot() = _rotation;
this->SetLocalPose(pose);
}
//////////////////////////////////////////////////
template <class T>
math::Pose3d BaseNode<T>::WorldPose() const
{
NodePtr parent = this->Parent();
math::Pose3d pose = this->LocalPose();
if (!parent)
{
return pose;
}
return parent->WorldPose() * pose;
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldPose(const math::Pose3d &_pose)
{
math::Pose3d pose = this->WorldToLocal(_pose);
this->SetLocalPose(pose);
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldPosition(double _x, double _y, double _z)
{
this->SetWorldPosition(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
math::Vector3d BaseNode<T>::WorldPosition() const
{
return this->WorldPose().Pos();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldPosition(const math::Vector3d &_position)
{
math::Pose3d pose = this->WorldPose();
pose.Pos() = _position;
this->SetWorldPose(pose);
}
//////////////////////////////////////////////////
template <class T>
math::Quaterniond BaseNode<T>::WorldRotation() const
{
return this->WorldPose().Rot();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldRotation(double _r, double _p, double _y)
{
this->SetWorldRotation(math::Quaterniond(_r, _p, _y));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldRotation(double _w, double _x, double _y,
double _z)
{
this->SetWorldRotation(math::Quaterniond(_w, _x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldRotation(const math::Quaterniond &_rotation)
{
math::Pose3d pose = this->WorldPose();
pose.Rot() = _rotation;
this->SetWorldPose(pose);
}
//////////////////////////////////////////////////
template <class T>
math::Pose3d BaseNode<T>::WorldToLocal(const math::Pose3d &_pose) const
{
NodePtr parent = this->Parent();
if (!parent)
{
return _pose;
}
return parent->WorldPose().Inverse() * _pose;
}
//////////////////////////////////////////////////
template <class T>
math::Vector3d BaseNode<T>::Origin() const
{
return this->origin;
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetOrigin(double _x, double _y, double _z)
{
this->SetOrigin(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetOrigin(const math::Vector3d &_origin)
{
if (!_origin.IsFinite())
{
gzerr << "Unable to set non-finite origin [" << _origin
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}
this->origin = _origin;
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalScale(double _scale)
{
this->SetLocalScale(math::Vector3d(_scale, _scale, _scale));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalScale(double _x, double _y, double _z)
{
this->SetLocalScale(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetLocalScale(const math::Vector3d &_scale)
{
math::Pose3d rawPose = this->LocalPose();
this->SetLocalScaleImpl(_scale);
this->SetLocalPose(rawPose);
}
//////////////////////////////////////////////////
template <class T>
math::Vector3d BaseNode<T>::WorldScale() const
{
math::Vector3d scale = this->LocalScale();
if (!this->InheritScale() || !this->HasParent())
{
return scale;
}
return scale * this->Parent()->WorldScale();
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldScale(double _scale)
{
this->SetWorldScale(math::Vector3d(_scale, _scale, _scale));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldScale(double _x, double _y, double _z)
{
this->SetWorldScale(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetWorldScale(const math::Vector3d &_scale)
{
math::Vector3d toScale = math::Vector3d::One;
if (this->InheritScale() && this->HasParent())
toScale = this->Parent()->WorldScale();
this->SetLocalScale(_scale / toScale);
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::Scale(double _scale)
{
this->Scale(math::Vector3d(_scale, _scale, _scale));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::Scale(double _x, double _y, double _z)
{
this->Scale(math::Vector3d(_x, _y, _z));
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::Scale(const math::Vector3d &_scale)
{
this->SetLocalScale(_scale * this->LocalScale());
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::Destroy()
{
T::Destroy();
this->RemoveParent();
}
//////////////////////////////////////////////////
template <class T>
unsigned int BaseNode<T>::ChildCount() const
{
return this->Children()->Size();
}
//////////////////////////////////////////////////
template <class T>
bool BaseNode<T>::HasChild(ConstNodePtr _child) const
{
return this->Children()->Contains(_child);
}
//////////////////////////////////////////////////
template <class T>
bool BaseNode<T>::HasChildId(unsigned int _id) const
{
return this->Children()->ContainsId(_id);
}
//////////////////////////////////////////////////
template <class T>
bool BaseNode<T>::HasChildName(const std::string &_name) const
{
return this->Children()->ContainsName(_name);
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::ChildById(unsigned int _id) const
{
return this->Children()->GetById(_id);
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::ChildByName(const std::string &_name) const
{
return this->Children()->GetByName(_name);
}
//////////////////////////////////////////////////
template <class T>
NodePtr BaseNode<T>::ChildByIndex(unsigned int _index) const
{
return this->Children()->GetByIndex(_index);
}
}
//////////////////////////////////////////////////
template <class T>
void BaseNode<T>::SetUserData(const std::string &_key, Variant _value)
{
this->userData[_key] = _value;
}
//////////////////////////////////////////////////
template <class T>
Variant BaseNode<T>::UserData(const std::string &_key) const
{
Variant value;
auto it = this->userData.find(_key);
if (it != this->userData.end())
value = it->second;
return value;
}
//////////////////////////////////////////////////
template <class T>
bool BaseNode<T>::HasUserData(const std::string &_key) const
{
return this->userData.find(_key) != this->userData.end();
}
}
}
#endif