/
BodyNode.cpp
2362 lines (1978 loc) · 75.4 KB
/
BodyNode.cpp
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011-2015, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Sehoon Ha <sehoon.ha@gmail.com>
* Jeongseok Lee <jslee02@gmail.com>
*
* Georgia Tech Graphics Lab and Humanoid Robotics Lab
*
* Directed by Prof. C. Karen Liu and Prof. Mike Stilman
* <karenliu@cc.gatech.edu> <mstilman@cc.gatech.edu>
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "dart/dynamics/BodyNode.h"
#include <algorithm>
#include <vector>
#include <string>
#include "dart/common/Console.h"
#include "dart/math/Helpers.h"
#include "dart/renderer/RenderInterface.h"
#include "dart/dynamics/Joint.h"
#include "dart/dynamics/Shape.h"
#include "dart/dynamics/Skeleton.h"
#include "dart/dynamics/Marker.h"
#include "dart/dynamics/SoftBodyNode.h"
namespace dart {
namespace dynamics {
//==============================================================================
SkeletonRefCountingBase::SkeletonRefCountingBase()
: mReferenceCount(0),
mLockedSkeleton(std::make_shared<MutexedWeakSkeletonPtr>())
{
// Do nothing
}
//==============================================================================
void SkeletonRefCountingBase::incrementReferenceCount() const
{
int previous = std::atomic_fetch_add(&mReferenceCount, 1);
if(0 == previous)
mReferenceSkeleton = mSkeleton.lock();
}
//==============================================================================
void SkeletonRefCountingBase::decrementReferenceCount() const
{
int previous = std::atomic_fetch_sub(&mReferenceCount, 1);
if(1 == previous)
mReferenceSkeleton = nullptr;
}
//==============================================================================
SkeletonPtr SkeletonRefCountingBase::getSkeleton()
{
return mSkeleton.lock();
}
//==============================================================================
ConstSkeletonPtr SkeletonRefCountingBase::getSkeleton() const
{
return mSkeleton.lock();
}
/// SKEL_SET_FLAGS : Lock a Skeleton pointer and activate dirty flags of X for
/// the tree that this BodyNode belongs to, as well as the flag for the Skeleton
/// overall
#define SKEL_SET_FLAGS( X ) { SkeletonPtr skel = getSkeleton(); if(skel) { \
skel->mTreeCache[mTreeIndex].mDirty. X = true; \
skel->mSkelCache.mDirty. X = true; } }
/// SET_FLAGS : A version of SKEL_SET_FLAGS that assumes a SkeletonPtr named
/// 'skel' has already been locked
#define SET_FLAGS( X ) skel->mTreeCache[mTreeIndex].mDirty. X = true; \
skel->mSkelCache.mDirty. X = true;
/// CHECK_FLAG : Check if the dirty flag X for the tree of this BodyNode is
/// active
#define CHECK_FLAG( X ) skel->mTreeCache[mTreeIndex].mDirty. X
//==============================================================================
typedef std::set<Entity*> EntityPtrSet;
//==============================================================================
template <typename T>
static T getVectorObjectIfAvailable(size_t _index, const std::vector<T>& _vec)
{
assert(_index < _vec.size());
if(_index < _vec.size())
return _vec[_index];
return nullptr;
}
//==============================================================================
size_t BodyNode::msBodyNodeCount = 0;
//==============================================================================
BodyNode::UniqueProperties::UniqueProperties(
const Inertia& _inertia,
const std::vector<ShapePtr>& _collisionShapes,
bool _isCollidable, double _frictionCoeff,
double _restitutionCoeff, bool _gravityMode)
: mInertia(_inertia),
mColShapes(_collisionShapes),
mIsCollidable(_isCollidable),
mFrictionCoeff(_frictionCoeff),
mRestitutionCoeff(_restitutionCoeff),
mGravityMode(_gravityMode)
{
// Do nothing
}
//==============================================================================
BodyNode::Properties::Properties(const Entity::Properties& _entityProperties,
const UniqueProperties& _bodyNodeProperties)
: Entity::Properties(_entityProperties),
UniqueProperties(_bodyNodeProperties)
{
// Do nothing
}
//==============================================================================
BodyNode::~BodyNode()
{
// Release markers
for (std::vector<Marker*>::const_iterator it = mMarkers.begin();
it != mMarkers.end(); ++it)
{
delete (*it);
}
}
//==============================================================================
void BodyNode::setProperties(const Properties& _properties)
{
Entity::setProperties(static_cast<const Entity::Properties&>(_properties));
setProperties(static_cast<const UniqueProperties&>(_properties));
}
//==============================================================================
void BodyNode::setProperties(const UniqueProperties& _properties)
{
setInertia(_properties.mInertia);
setGravityMode(_properties.mGravityMode);
setFrictionCoeff(_properties.mFrictionCoeff);
setRestitutionCoeff(_properties.mRestitutionCoeff);
removeAllCollisionShapes();
for(size_t i=0; i<_properties.mColShapes.size(); ++i)
addCollisionShape(_properties.mColShapes[i]);
mBodyP.mMarkerProperties = _properties.mMarkerProperties;
// Remove current markers
for(Marker* marker : mMarkers)
delete marker;
// Create new markers
mMarkers.clear();
for(const Marker::Properties& marker : mBodyP.mMarkerProperties)
addMarker(new Marker(marker, this));
}
//==============================================================================
BodyNode::Properties BodyNode::getBodyNodeProperties() const
{
return BodyNode::Properties(mEntityP, mBodyP);
}
//==============================================================================
void BodyNode::copy(const BodyNode& _otherBodyNode)
{
if(this == &_otherBodyNode)
return;
setProperties(_otherBodyNode.getBodyNodeProperties());
}
//==============================================================================
void BodyNode::copy(const BodyNode* _otherBodyNode)
{
if(nullptr == _otherBodyNode)
return;
copy(*_otherBodyNode);
}
//==============================================================================
BodyNode& BodyNode::operator=(const BodyNode& _otherBodyNode)
{
copy(_otherBodyNode);
return *this;
}
//==============================================================================
const std::string& BodyNode::setName(const std::string& _name)
{
// If it already has the requested name, do nothing
if(mEntityP.mName == _name)
return mEntityP.mName;
// If the BodyNode belongs to a Skeleton, consult the Skeleton's NameManager
const SkeletonPtr& skel = getSkeleton();
if(skel)
{
skel->mNameMgrForBodyNodes.removeName(mEntityP.mName);
SoftBodyNode* softnode = dynamic_cast<SoftBodyNode*>(this);
if(softnode)
skel->mNameMgrForSoftBodyNodes.removeName(mEntityP.mName);
mEntityP.mName = _name;
skel->addEntryToBodyNodeNameMgr(this);
if(softnode)
skel->addEntryToSoftBodyNodeNameMgr(softnode);
}
else
{
mEntityP.mName = _name;
}
// Return the final name (which might have been altered by the Skeleton's
// NameManager)
return mEntityP.mName;
}
//==============================================================================
const std::string& BodyNode::getName() const
{
return mEntityP.mName;
}
//==============================================================================
void BodyNode::setGravityMode(bool _gravityMode)
{
if (mBodyP.mGravityMode == _gravityMode)
return;
mBodyP.mGravityMode = _gravityMode;
SKEL_SET_FLAGS(mGravityForces);
SKEL_SET_FLAGS(mCoriolisAndGravityForces);
}
//==============================================================================
bool BodyNode::getGravityMode() const
{
return mBodyP.mGravityMode;
}
//==============================================================================
bool BodyNode::isCollidable() const
{
return mBodyP.mIsCollidable;
}
//==============================================================================
void BodyNode::setCollidable(bool _isCollidable)
{
mBodyP.mIsCollidable = _isCollidable;
}
//==============================================================================
void BodyNode::setMass(double _mass)
{
assert(_mass >= 0.0 && "Negative mass is not allowable.");
mBodyP.mInertia.setMass(_mass);
notifyArticulatedInertiaUpdate();
const SkeletonPtr& skel = getSkeleton();
if(skel)
skel->updateTotalMass();
}
//==============================================================================
double BodyNode::getMass() const
{
return mBodyP.mInertia.getMass();
}
//==============================================================================
void BodyNode::setMomentOfInertia(double _Ixx, double _Iyy, double _Izz,
double _Ixy, double _Ixz, double _Iyz)
{
mBodyP.mInertia.setMoment(_Ixx, _Iyy, _Izz,
_Ixy, _Ixz, _Iyz);
notifyArticulatedInertiaUpdate();
}
//==============================================================================
void BodyNode::getMomentOfInertia(double& _Ixx, double& _Iyy, double& _Izz,
double& _Ixy, double& _Ixz, double& _Iyz)
{
_Ixx = mBodyP.mInertia.getParameter(Inertia::I_XX);
_Iyy = mBodyP.mInertia.getParameter(Inertia::I_YY);
_Izz = mBodyP.mInertia.getParameter(Inertia::I_ZZ);
_Ixy = mBodyP.mInertia.getParameter(Inertia::I_XY);
_Ixz = mBodyP.mInertia.getParameter(Inertia::I_XZ);
_Iyz = mBodyP.mInertia.getParameter(Inertia::I_YZ);
}
//==============================================================================
const Eigen::Matrix6d& BodyNode::getSpatialInertia() const
{
return mBodyP.mInertia.getSpatialTensor();
}
//==============================================================================
void BodyNode::setInertia(const Inertia& _inertia)
{
mBodyP.mInertia = _inertia;
notifyArticulatedInertiaUpdate();
const SkeletonPtr& skel = getSkeleton();
if(skel)
skel->updateTotalMass();
}
//==============================================================================
const Inertia& BodyNode::getInertia() const
{
return mBodyP.mInertia;
}
//==============================================================================
const math::Inertia& BodyNode::getArticulatedInertia() const
{
const ConstSkeletonPtr& skel = getSkeleton();
if( skel && CHECK_FLAG(mArticulatedInertia) )
skel->updateArticulatedInertia(mTreeIndex);
return mArtInertia;
}
//==============================================================================
const math::Inertia& BodyNode::getArticulatedInertiaImplicit() const
{
const ConstSkeletonPtr& skel = getSkeleton();
if( skel && CHECK_FLAG(mArticulatedInertia) )
skel->updateArticulatedInertia(mTreeIndex);
return mArtInertiaImplicit;
}
//==============================================================================
void BodyNode::setLocalCOM(const Eigen::Vector3d& _com)
{
mBodyP.mInertia.setLocalCOM(_com);
notifyArticulatedInertiaUpdate();
}
//==============================================================================
const Eigen::Vector3d& BodyNode::getLocalCOM() const
{
return mBodyP.mInertia.getLocalCOM();
}
//==============================================================================
Eigen::Vector3d BodyNode::getCOM(const Frame* _withRespectTo) const
{
return getTransform(_withRespectTo) * getLocalCOM();
}
//==============================================================================
Eigen::Vector3d BodyNode::getCOMLinearVelocity(const Frame* _relativeTo,
const Frame* _inCoordinatesOf) const
{
return getLinearVelocity(getLocalCOM(), _relativeTo, _inCoordinatesOf);
}
//==============================================================================
Eigen::Vector6d BodyNode::getCOMSpatialVelocity() const
{
return getSpatialVelocity(getLocalCOM());
}
//==============================================================================
Eigen::Vector6d BodyNode::getCOMSpatialVelocity(const Frame* _relativeTo,
const Frame* _inCoordinatesOf) const
{
return getSpatialVelocity(getLocalCOM(), _relativeTo, _inCoordinatesOf);
}
//==============================================================================
Eigen::Vector3d BodyNode::getCOMLinearAcceleration(const Frame* _relativeTo,
const Frame* _inCoordinatesOf) const
{
return getLinearAcceleration(getLocalCOM(), _relativeTo, _inCoordinatesOf);
}
//==============================================================================
Eigen::Vector6d BodyNode::getCOMSpatialAcceleration() const
{
return getSpatialAcceleration(getLocalCOM());
}
//==============================================================================
Eigen::Vector6d BodyNode::getCOMSpatialAcceleration(const Frame* _relativeTo,
const Frame* _inCoordinatesOf) const
{
return getSpatialAcceleration(getLocalCOM(), _relativeTo, _inCoordinatesOf);
}
//==============================================================================
void BodyNode::setFrictionCoeff(double _coeff)
{
assert(0.0 <= _coeff
&& "Coefficient of friction should be non-negative value.");
mBodyP.mFrictionCoeff = _coeff;
}
//==============================================================================
double BodyNode::getFrictionCoeff() const
{
return mBodyP.mFrictionCoeff;
}
//==============================================================================
void BodyNode::setRestitutionCoeff(double _coeff)
{
assert(0.0 <= _coeff && _coeff <= 1.0
&& "Coefficient of restitution should be in range of [0, 1].");
mBodyP.mRestitutionCoeff = _coeff;
}
//==============================================================================
double BodyNode::getRestitutionCoeff() const
{
return mBodyP.mRestitutionCoeff;
}
//==============================================================================
void BodyNode::addCollisionShape(const ShapePtr& _shape)
{
if(nullptr == _shape)
{
dtwarn << "[BodyNode::addCollisionShape] Attempting to add a nullptr as a "
<< "collision shape\n";
return;
}
if(_shape->getShapeType() == Shape::LINE_SEGMENT)
{
dtwarn << "[BodyNode::addCollisionShape] Attempting to add a LINE_SEGMENT "
<< "type shape as a collision shape. This is not supported.\n";
return;
}
if(std::find(mBodyP.mColShapes.begin(), mBodyP.mColShapes.end(), _shape)
!= mBodyP.mColShapes.end())
{
dtwarn << "[BodyNode::addCollisionShape] Attempting to add a duplicate "
<< "collision shape.\n";
return;
}
mBodyP.mColShapes.push_back(_shape);
mColShapeAddedSignal.raise(this, _shape);
}
//==============================================================================
void BodyNode::removeCollisionShape(const ShapePtr& _shape)
{
if (nullptr == _shape)
return;
mBodyP.mColShapes.erase(std::remove(mBodyP.mColShapes.begin(),
mBodyP.mColShapes.end(), _shape),
mBodyP.mColShapes.end());
mColShapeRemovedSignal.raise(this, _shape);
}
//==============================================================================
void BodyNode::removeAllCollisionShapes()
{
std::vector<ShapePtr>::iterator it = mBodyP.mColShapes.begin();
while (it != mBodyP.mColShapes.end())
{
removeCollisionShape(*it);
it = mBodyP.mColShapes.begin();
}
}
//==============================================================================
size_t BodyNode::getNumCollisionShapes() const
{
return mBodyP.mColShapes.size();
}
//==============================================================================
ShapePtr BodyNode::getCollisionShape(size_t _index)
{
return getVectorObjectIfAvailable<ShapePtr>(_index, mBodyP.mColShapes);
}
//==============================================================================
ConstShapePtr BodyNode::getCollisionShape(size_t _index) const
{
return getVectorObjectIfAvailable<ShapePtr>(_index, mBodyP.mColShapes);
}
//==============================================================================
size_t BodyNode::getIndexInSkeleton() const
{
return mIndexInSkeleton;
}
//==============================================================================
size_t BodyNode::getIndexInTree() const
{
return mIndexInTree;
}
//==============================================================================
size_t BodyNode::getTreeIndex() const
{
return mTreeIndex;
}
//==============================================================================
static bool checkSkeletonNodeAgreement(
const BodyNode* _bodyNode,
const ConstSkeletonPtr& _newSkeleton, const BodyNode* _newParent,
const std::string& _function,
const std::string& _operation)
{
if(nullptr == _newSkeleton)
{
dterr << "[BodyNode::" << _function << "] Attempting to " << _operation
<< " a BodyNode tree starting " << "from [" << _bodyNode->getName()
<< "] in the Skeleton named [" << _bodyNode->getSkeleton()->getName()
<< "] into a nullptr Skeleton.\n";
return false;
}
if(_newParent && _newSkeleton != _newParent->getSkeleton())
{
dterr << "[BodyNode::" << _function << "] Mismatch between the specified "
<< "Skeleton [" << _newSkeleton->getName() << "] (" << _newSkeleton
<< ") and the specified new parent BodyNode ["
<< _newParent->getName() << "] whose actual Skeleton is named ["
<< _newParent->getSkeleton()->getName() << "] ("
<< _newParent->getSkeleton() << ") while attempting to " << _operation
<< " the BodyNode [" << _bodyNode->getName() << "] from the "
<< "Skeleton named [" << _bodyNode->getSkeleton()->getName() << "] ("
<< _bodyNode->getSkeleton() << ").\n";
return false;
}
return true;
}
//==============================================================================
SkeletonPtr BodyNode::remove(const std::string& _name)
{
return split(_name);
}
//==============================================================================
bool BodyNode::moveTo(BodyNode* _newParent)
{
if(nullptr == _newParent)
return getSkeleton()->moveBodyNodeTree(
getParentJoint(), this, getSkeleton(), nullptr);
else
return getSkeleton()->moveBodyNodeTree(
getParentJoint(), this, _newParent->getSkeleton(), _newParent);
}
//==============================================================================
bool BodyNode::moveTo(const SkeletonPtr& _newSkeleton, BodyNode* _newParent)
{
if(checkSkeletonNodeAgreement(
this, _newSkeleton, _newParent, "moveTo", "move"))
{
return getSkeleton()->moveBodyNodeTree(
getParentJoint(), this, _newSkeleton, _newParent);
}
return false;
}
//==============================================================================
SkeletonPtr BodyNode::split(const std::string& _skeletonName)
{
const SkeletonPtr& skel =
Skeleton::create(getSkeleton()->getSkeletonProperties());
skel->setName(_skeletonName);
moveTo(skel, nullptr);
return skel;
}
//==============================================================================
std::pair<Joint*, BodyNode*> BodyNode::copyTo(BodyNode* _newParent,
bool _recursive)
{
if(nullptr == _newParent)
return getSkeleton()->cloneBodyNodeTree(
nullptr, this, getSkeleton(), nullptr, _recursive);
else
return getSkeleton()->cloneBodyNodeTree(
nullptr, this, _newParent->getSkeleton(), _newParent, _recursive);
}
//==============================================================================
std::pair<Joint*, BodyNode*> BodyNode::copyTo(const SkeletonPtr& _newSkeleton,
BodyNode* _newParent,
bool _recursive) const
{
if(checkSkeletonNodeAgreement(
this, _newSkeleton, _newParent, "copyTo", "copy"))
{
return getSkeleton()->cloneBodyNodeTree(
nullptr, this, _newSkeleton, _newParent, _recursive);
}
return std::pair<Joint*, BodyNode*>(nullptr, nullptr);
}
//==============================================================================
SkeletonPtr BodyNode::copyAs(const std::string& _skeletonName,
bool _recursive) const
{
const SkeletonPtr& skel =
Skeleton::create(getSkeleton()->getSkeletonProperties());
skel->setName(_skeletonName);
copyTo(skel, nullptr, _recursive);
return skel;
}
//==============================================================================
Joint* BodyNode::getParentJoint()
{
return mParentJoint;
}
//==============================================================================
const Joint* BodyNode::getParentJoint() const
{
return mParentJoint;
}
//==============================================================================
BodyNode* BodyNode::getParentBodyNode()
{
return mParentBodyNode;
}
//==============================================================================
const BodyNode* BodyNode::getParentBodyNode() const
{
return mParentBodyNode;
}
//==============================================================================
void BodyNode::addChildBodyNode(BodyNode* _body)
{
assert(_body != nullptr);
if(std::find(mChildBodyNodes.begin(), mChildBodyNodes.end(), _body) !=
mChildBodyNodes.end())
{
dtwarn << "[BodyNode::addChildBodyNode] Attempting to add a BodyNode '"
<< _body->getName() << "' as a child BodyNode of '" << getName()
<< "', which is already its parent." << std::endl;
return;
}
mChildBodyNodes.push_back(_body);
_body->mParentBodyNode = this;
_body->changeParentFrame(this);
}
//==============================================================================
size_t BodyNode::getNumChildBodyNodes() const
{
return mChildBodyNodes.size();
}
//==============================================================================
BodyNode* BodyNode::getChildBodyNode(size_t _index)
{
return getVectorObjectIfAvailable<BodyNode*>(_index, mChildBodyNodes);
}
//==============================================================================
const BodyNode* BodyNode::getChildBodyNode(size_t _index) const
{
return getVectorObjectIfAvailable<BodyNode*>(_index, mChildBodyNodes);
}
//==============================================================================
size_t BodyNode::getNumChildJoints() const
{
return mChildBodyNodes.size();
}
//==============================================================================
Joint* BodyNode::getChildJoint(size_t _index)
{
BodyNode* childBodyNode = getChildBodyNode(_index);
if(childBodyNode)
return childBodyNode->getParentJoint();
else
return nullptr;
}
//==============================================================================
const Joint* BodyNode::getChildJoint(size_t _index) const
{
return const_cast<BodyNode*>(this)->getChildJoint(_index);
}
//==============================================================================
void BodyNode::addMarker(Marker* _marker)
{
mMarkers.push_back(_marker);
const SkeletonPtr& skel = getSkeleton();
if(skel)
skel->addEntryToMarkerNameMgr(_marker);
}
//==============================================================================
size_t BodyNode::getNumMarkers() const
{
return mMarkers.size();
}
//==============================================================================
Marker* BodyNode::getMarker(size_t _index)
{
return getVectorObjectIfAvailable<Marker*>(_index, mMarkers);
}
//==============================================================================
const Marker* BodyNode::getMarker(size_t _index) const
{
return getVectorObjectIfAvailable<Marker*>(_index, mMarkers);
}
//==============================================================================
bool BodyNode::dependsOn(size_t _genCoordIndex) const
{
return std::binary_search(mDependentGenCoordIndices.begin(),
mDependentGenCoordIndices.end(),
_genCoordIndex);
}
//==============================================================================
size_t BodyNode::getNumDependentGenCoords() const
{
return mDependentGenCoordIndices.size();
}
//==============================================================================
size_t BodyNode::getDependentGenCoordIndex(size_t _arrayIndex) const
{
assert(_arrayIndex < mDependentGenCoordIndices.size());
return mDependentGenCoordIndices[_arrayIndex];
}
//==============================================================================
const std::vector<size_t>& BodyNode::getDependentGenCoordIndices() const
{
return mDependentGenCoordIndices;
}
//==============================================================================
const std::vector<DegreeOfFreedom*>& BodyNode::getDependentDofs()
{
return mDependentDofs;
}
//==============================================================================
const std::vector<const DegreeOfFreedom*>& BodyNode::getDependentDofs() const
{
return mConstDependentDofs;
}
//==============================================================================
const Eigen::Isometry3d& BodyNode::getRelativeTransform() const
{
return mParentJoint->getLocalTransform();
}
//==============================================================================
const Eigen::Vector6d& BodyNode::getRelativeSpatialVelocity() const
{
return mParentJoint->getLocalSpatialVelocity();
}
//==============================================================================
const Eigen::Vector6d& BodyNode::getRelativeSpatialAcceleration() const
{
return mParentJoint->getLocalSpatialAcceleration();
}
//==============================================================================
const Eigen::Vector6d& BodyNode::getPrimaryRelativeAcceleration() const
{
return mParentJoint->getLocalPrimaryAcceleration();
}
//==============================================================================
const Eigen::Vector6d& BodyNode::getPartialAcceleration() const
{
if(mIsPartialAccelerationDirty)
updatePartialAcceleration();
return mPartialAcceleration;
}
//==============================================================================
const math::Jacobian& BodyNode::getJacobian() const
{
if (mIsBodyJacobianDirty)
updateBodyJacobian();
return mBodyJacobian;
}
//==============================================================================
math::Jacobian BodyNode::getJacobian(const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobian();
else if(_inCoordinatesOf->isWorld())
return getWorldJacobian();
return math::AdRJac(getTransform(_inCoordinatesOf), getJacobian());
}
//==============================================================================
math::Jacobian BodyNode::getJacobian(const Eigen::Vector3d& _offset) const
{
math::Jacobian J = getJacobian();
J.bottomRows<3>() += J.topRows<3>().colwise().cross(_offset);
return J;
}
//==============================================================================
math::Jacobian BodyNode::getJacobian(const Eigen::Vector3d& _offset,
const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobian(_offset);
else if(_inCoordinatesOf->isWorld())
return getWorldJacobian(_offset);
Eigen::Isometry3d T = getTransform(_inCoordinatesOf);
T.translation() = - T.linear() * _offset;
return math::AdTJac(T, getJacobian());
}
//==============================================================================
const math::Jacobian& BodyNode::getWorldJacobian() const
{
if(mIsWorldJacobianDirty)
updateWorldJacobian();
return mWorldJacobian;
}
//==============================================================================
math::Jacobian BodyNode::getWorldJacobian(const Eigen::Vector3d& _offset) const
{
math::Jacobian J = getWorldJacobian();
J.bottomRows<3>() += J.topRows<3>().colwise().cross(
getWorldTransform().linear() * _offset);
return J;
}
//==============================================================================
math::LinearJacobian BodyNode::getLinearJacobian(
const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobian().bottomRows<3>();
else if(_inCoordinatesOf->isWorld())
return getWorldJacobian().bottomRows<3>();
return getTransform(_inCoordinatesOf).linear() * getJacobian().bottomRows<3>();
}
//==============================================================================
math::LinearJacobian BodyNode::getLinearJacobian(const Eigen::Vector3d& _offset,
const Frame* _inCoordinatesOf) const
{
const math::Jacobian& J = getJacobian();
math::LinearJacobian JLinear;
JLinear = J.bottomRows<3>() + J.topRows<3>().colwise().cross(_offset);
if(this == _inCoordinatesOf)
return JLinear;
return getTransform(_inCoordinatesOf).linear() * JLinear;
}
//==============================================================================
math::AngularJacobian BodyNode::getAngularJacobian(
const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobian().topRows<3>();
else if(_inCoordinatesOf->isWorld())
return getWorldJacobian().topRows<3>();
return getTransform(_inCoordinatesOf).linear() * getJacobian().topRows<3>();
}
//==============================================================================
const math::Jacobian& BodyNode::getJacobianSpatialDeriv() const
{
if(mIsBodyJacobianSpatialDerivDirty)
updateBodyJacobianSpatialDeriv();
return mBodyJacobianSpatialDeriv;
}
//==============================================================================
math::Jacobian BodyNode::getJacobianSpatialDeriv(const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobianSpatialDeriv();
return math::AdRJac(getTransform(_inCoordinatesOf), getJacobianSpatialDeriv());
}
//==============================================================================
math::Jacobian BodyNode::getJacobianSpatialDeriv(const Eigen::Vector3d& _offset) const
{
math::Jacobian J_d = getJacobianSpatialDeriv();
J_d.bottomRows<3>() += J_d.topRows<3>().colwise().cross(_offset);
return J_d;
}
//==============================================================================
math::Jacobian BodyNode::getJacobianSpatialDeriv(const Eigen::Vector3d& _offset,
const Frame* _inCoordinatesOf) const
{
if(this == _inCoordinatesOf)
return getJacobianSpatialDeriv(_offset);
Eigen::Isometry3d T = getTransform(_inCoordinatesOf);
T.translation() = T.linear() * -_offset;
return math::AdTJac(T, getJacobianSpatialDeriv());
}
//==============================================================================
const math::Jacobian& BodyNode::getJacobianClassicDeriv() const
{
if(mIsWorldJacobianClassicDerivDirty)
updateWorldJacobianClassicDeriv();
return mWorldJacobianClassicDeriv;
}
//==============================================================================
math::Jacobian BodyNode::getJacobianClassicDeriv(const Frame* _inCoordinatesOf) const
{