-
Notifications
You must be signed in to change notification settings - Fork 94
/
Model.cc
879 lines (772 loc) · 26.6 KB
/
Model.cc
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
/*
* Copyright 2018 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.
*
*/
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
#include <ignition/math/Pose3.hh>
#include <ignition/math/SemanticVersion.hh>
#include "sdf/Error.hh"
#include "sdf/Frame.hh"
#include "sdf/Joint.hh"
#include "sdf/Link.hh"
#include "sdf/Model.hh"
#include "sdf/Types.hh"
#include "FrameSemantics.hh"
#include "Utils.hh"
using namespace sdf;
class sdf::ModelPrivate
{
/// \brief Name of the model.
public: std::string name = "";
/// \brief True if this model is specified as static, false otherwise.
public: bool isStatic = false;
/// \brief True if this model should self-collide, false otherwise.
public: bool selfCollide = false;
/// \brief True if this model is allowed to conserve processing power by not
/// updating when it's at rest.
public: bool allowAutoDisable = true;
/// \brief True if this model should be subject to wind, false otherwise.
public: bool enableWind = false;
/// \brief Name of the canonical link.
public: std::string canonicalLink = "";
/// \brief Name of the placement frame
public: std::string placementFrameName = "";
/// \brief Pose of the model
public: ignition::math::Pose3d pose = ignition::math::Pose3d::Zero;
/// \brief Frame of the pose.
public: std::string poseRelativeTo = "";
/// \brief The links specified in this model.
public: std::vector<Link> links;
/// \brief The joints specified in this model.
public: std::vector<Joint> joints;
/// \brief The frames specified in this model.
public: std::vector<Frame> frames;
/// \brief The nested models specified in this model.
public: std::vector<Model> models;
/// \brief The SDF element pointer used during load.
public: sdf::ElementPtr sdf;
/// \brief Frame Attached-To Graph constructed during Load.
public: std::shared_ptr<sdf::FrameAttachedToGraph> frameAttachedToGraph;
/// \brief Pose Relative-To Graph constructed during Load.
public: std::shared_ptr<sdf::PoseRelativeToGraph> poseGraph;
/// \brief Pose Relative-To Graph in parent (world or __model__) scope.
public: std::weak_ptr<const sdf::PoseRelativeToGraph> parentPoseGraph;
/// \brief Scope name of parent Pose Relative-To Graph (world or __model__).
public: std::string parentPoseGraphScopeName;
};
/////////////////////////////////////////////////
Model::Model()
: dataPtr(new ModelPrivate)
{
}
/////////////////////////////////////////////////
Model::~Model()
{
delete this->dataPtr;
this->dataPtr = nullptr;
}
/////////////////////////////////////////////////
Model::Model(const Model &_model)
: dataPtr(new ModelPrivate(*_model.dataPtr))
{
if (_model.dataPtr->frameAttachedToGraph)
{
this->dataPtr->frameAttachedToGraph =
std::make_shared<sdf::FrameAttachedToGraph>(
*_model.dataPtr->frameAttachedToGraph);
}
if (_model.dataPtr->poseGraph)
{
this->dataPtr->poseGraph = std::make_shared<sdf::PoseRelativeToGraph>(
*_model.dataPtr->poseGraph);
}
for (auto &link : this->dataPtr->links)
{
link.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
for (auto &model : this->dataPtr->models)
{
model.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
for (auto &joint : this->dataPtr->joints)
{
joint.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
joint.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
for (auto &frame : this->dataPtr->frames)
{
frame.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
frame.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
}
/////////////////////////////////////////////////
Model::Model(Model &&_model) noexcept
: dataPtr(std::exchange(_model.dataPtr, nullptr))
{
}
/////////////////////////////////////////////////
Model &Model::operator=(const Model &_model)
{
return *this = Model(_model);
}
/////////////////////////////////////////////////
Model &Model::operator=(Model &&_model)
{
std::swap(this->dataPtr, _model.dataPtr);
return *this;
}
/////////////////////////////////////////////////
Errors Model::Load(ElementPtr _sdf)
{
Errors errors;
this->dataPtr->sdf = _sdf;
ignition::math::SemanticVersion sdfVersion(_sdf->OriginalVersion());
// Check that the provided SDF element is a <model>
// This is an error that cannot be recovered, so return an error.
if (_sdf->GetName() != "model")
{
errors.push_back({ErrorCode::ELEMENT_INCORRECT_TYPE,
"Attempting to load a Model, but the provided SDF element is not a "
"<model>."});
return errors;
}
// Read the models's name
if (!loadName(_sdf, this->dataPtr->name))
{
errors.push_back({ErrorCode::ATTRIBUTE_MISSING,
"A model name is required, but the name is not set."});
}
// Check that the model's name is valid
if (isReservedName(this->dataPtr->name))
{
errors.push_back({ErrorCode::RESERVED_NAME,
"The supplied model name [" + this->dataPtr->name +
"] is reserved."});
}
// Read the model's canonical_link attribute
if (_sdf->HasAttribute("canonical_link"))
{
auto pair = _sdf->Get<std::string>("canonical_link", "");
if (pair.second)
{
this->dataPtr->canonicalLink = pair.first;
}
}
this->dataPtr->placementFrameName = _sdf->Get<std::string>("placement_frame",
this->dataPtr->placementFrameName).first;
this->dataPtr->isStatic = _sdf->Get<bool>("static", false).first;
this->dataPtr->selfCollide = _sdf->Get<bool>("self_collide", false).first;
this->dataPtr->allowAutoDisable =
_sdf->Get<bool>("allow_auto_disable", true).first;
this->dataPtr->enableWind = _sdf->Get<bool>("enable_wind", false).first;
// Load the pose. Ignore the return value since the model pose is optional.
loadPose(_sdf, this->dataPtr->pose, this->dataPtr->poseRelativeTo);
if (!_sdf->HasUniqueChildNames())
{
sdfwarn << "Non-unique names detected in XML children of model with name["
<< this->Name() << "].\n";
}
// Set of implicit and explicit frame names in this model for tracking
// name collisions
std::unordered_set<std::string> frameNames;
// Load nested models.
Errors nestedModelLoadErrors = loadUniqueRepeated<Model>(_sdf, "model",
this->dataPtr->models);
errors.insert(errors.end(),
nestedModelLoadErrors.begin(),
nestedModelLoadErrors.end());
// Nested models are loaded first, and loadUniqueRepeated ensures there are no
// duplicate names, so these names can be added to frameNames without
// checking uniqueness.
for (const auto &model : this->dataPtr->models)
{
frameNames.insert(model.Name());
}
// Load all the links.
Errors linkLoadErrors = loadUniqueRepeated<Link>(_sdf, "link",
this->dataPtr->links);
errors.insert(errors.end(), linkLoadErrors.begin(), linkLoadErrors.end());
// Check links for name collisions and modify and warn if so.
for (auto &link : this->dataPtr->links)
{
std::string linkName = link.Name();
if (frameNames.count(linkName) > 0)
{
// This link has a name collision
if (sdfVersion < ignition::math::SemanticVersion(1, 7))
{
// This came from an old file, so try to workaround by renaming link
linkName += "_link";
int i = 0;
while (frameNames.count(linkName) > 0)
{
linkName = link.Name() + "_link" + std::to_string(i++);
}
sdfwarn << "Link with name [" << link.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision, changing link name to ["
<< linkName << "].\n";
link.SetName(linkName);
}
else
{
sdferr << "Link with name [" << link.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision. Please rename this link.\n";
}
}
frameNames.insert(linkName);
}
// If the model is not static and has no nested models:
// Require at least one link so the implicit model frame can be attached to
// something.
if (!this->Static() && this->dataPtr->links.empty() &&
this->dataPtr->models.empty())
{
errors.push_back({ErrorCode::MODEL_WITHOUT_LINK,
"A model must have at least one link."});
}
// Load all the joints.
Errors jointLoadErrors = loadUniqueRepeated<Joint>(_sdf, "joint",
this->dataPtr->joints);
errors.insert(errors.end(), jointLoadErrors.begin(), jointLoadErrors.end());
// Check joints for name collisions and modify and warn if so.
for (auto &joint : this->dataPtr->joints)
{
std::string jointName = joint.Name();
if (frameNames.count(jointName) > 0)
{
// This joint has a name collision
if (sdfVersion < ignition::math::SemanticVersion(1, 7))
{
// This came from an old file, so try to workaround by renaming joint
jointName += "_joint";
int i = 0;
while (frameNames.count(jointName) > 0)
{
jointName = joint.Name() + "_joint" + std::to_string(i++);
}
sdfwarn << "Joint with name [" << joint.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision, changing joint name to ["
<< jointName << "].\n";
joint.SetName(jointName);
}
else
{
sdferr << "Joint with name [" << joint.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision. Please rename this joint.\n";
}
}
frameNames.insert(jointName);
}
// Load all the frames.
Errors frameLoadErrors = loadUniqueRepeated<Frame>(_sdf, "frame",
this->dataPtr->frames);
errors.insert(errors.end(), frameLoadErrors.begin(), frameLoadErrors.end());
// Check frames for name collisions and modify and warn if so.
for (auto &frame : this->dataPtr->frames)
{
std::string frameName = frame.Name();
if (frameNames.count(frameName) > 0)
{
// This joint has a name collision
if (sdfVersion < ignition::math::SemanticVersion(1, 7))
{
// This came from an old file, so try to workaround by renaming frame
frameName += "_frame";
int i = 0;
while (frameNames.count(frameName) > 0)
{
frameName = frame.Name() + "_frame" + std::to_string(i++);
}
sdfwarn << "Frame with name [" << frame.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision, changing frame name to ["
<< frameName << "].\n";
frame.SetName(frameName);
}
else
{
sdferr << "Frame with name [" << frame.Name() << "] "
<< "in model with name [" << this->Name() << "] "
<< "has a name collision. Please rename this frame.\n";
}
}
frameNames.insert(frameName);
}
// Build the graphs.
// Build the FrameAttachedToGraph if the model is not static.
// Re-enable this when the buildFrameAttachedToGraph implementation handles
// static models.
if (!this->Static())
{
this->dataPtr->frameAttachedToGraph
= std::make_shared<FrameAttachedToGraph>();
Errors frameAttachedToGraphErrors =
buildFrameAttachedToGraph(*this->dataPtr->frameAttachedToGraph, this);
errors.insert(errors.end(), frameAttachedToGraphErrors.begin(),
frameAttachedToGraphErrors.end());
Errors validateFrameAttachedGraphErrors =
validateFrameAttachedToGraph(*this->dataPtr->frameAttachedToGraph);
errors.insert(errors.end(), validateFrameAttachedGraphErrors.begin(),
validateFrameAttachedGraphErrors.end());
for (auto &joint : this->dataPtr->joints)
{
joint.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
}
for (auto &frame : this->dataPtr->frames)
{
frame.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
}
}
// Build the PoseRelativeToGraph
this->dataPtr->poseGraph = std::make_shared<PoseRelativeToGraph>();
Errors poseGraphErrors =
buildPoseRelativeToGraph(*this->dataPtr->poseGraph, this);
errors.insert(errors.end(), poseGraphErrors.begin(),
poseGraphErrors.end());
Errors validatePoseGraphErrors =
validatePoseRelativeToGraph(*this->dataPtr->poseGraph);
errors.insert(errors.end(), validatePoseGraphErrors.begin(),
validatePoseGraphErrors.end());
for (auto &link : this->dataPtr->links)
{
link.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
for (auto &model : this->dataPtr->models)
{
Errors setPoseRelativeToGraphErrors =
model.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
errors.insert(errors.end(), setPoseRelativeToGraphErrors.begin(),
setPoseRelativeToGraphErrors.end());
}
for (auto &joint : this->dataPtr->joints)
{
joint.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
for (auto &frame : this->dataPtr->frames)
{
frame.SetPoseRelativeToGraph(this->dataPtr->poseGraph);
}
// Update the model pose to account for the placement frame.
if (!this->dataPtr->placementFrameName.empty())
{
ignition::math::Pose3d X_MPf;
sdf::Errors resolveErrors =
sdf::resolvePose(X_MPf, *this->dataPtr->poseGraph,
this->dataPtr->placementFrameName, "__model__");
if (resolveErrors.empty())
{
// Before this update, i.e, as specified in the SDFormat, the model pose
// (X_RPf) is the pose of the placement frame (Pf) relative to a frame (R)
// in the parent scope of the model. However, when this model (M) is
// inserted into a pose graph of the parent scope, only the pose (X_RM)
// of the __model__ frame can be used. Thus, the model pose has to be
// updated to X_RM.
//
// Note that X_RPf is the raw pose specified in //model/pose before this
// update.
const auto &X_RPf = this->dataPtr->pose;
auto &X_RM = this->dataPtr->pose;
X_RM = X_RPf * X_MPf.Inverse();
}
else
{
errors.insert(errors.end(), resolveErrors.begin(), resolveErrors.end());
}
}
// The placement_frame attributes of included child models are currently lost
// during parsing because the parser expands the included models into the
// parent model. As a temporary workardound to preserve this information, the
// parser injects <model name>::__placement_frame__ for every included child
// model. This serves as an identifier that the model frame of the child model
// should be treated as if it has its placement_frame attribute set.
// This will not be necessary when included nested models work as directly
// nested models. See
// https://github.com/osrf/sdformat/issues/319#issuecomment-665214004
// TODO (addisu) Remove placementFrameIdentifier once PR addressing
// https://github.com/osrf/sdformat/issues/284 lands
for (auto &frame : this->dataPtr->frames)
{
auto placementSubstrInd = frame.Name().rfind("::__placement_frame__");
if (placementSubstrInd != std::string::npos)
{
const std::string childModelName =
frame.Name().substr(0, placementSubstrInd);
// Find the model frame associated with this placement frame
const Frame *childModelFrame =
this->FrameByName(childModelName + "::__model__");
if (nullptr != childModelFrame)
{
// The RawPose of the child model frame is the desired pose of the
// placement frame relative to a frame (R) in the scope of the parent
// model. We don't need to resolve the pose because the relative_to
// frame remains unchanged after the pose update here. i.e, the updated
// pose of the child model frame will still be relative to R.
const auto &X_RPf = childModelFrame->RawPose();
ignition::math::Pose3d X_MPf;
sdf::Errors resolveErrors =
frame.SemanticPose().Resolve(X_MPf, childModelFrame->Name());
errors.insert(errors.end(), resolveErrors.begin(), resolveErrors.end());
// We need to update childModelFrame's pose relative to the parent
// frame as well as the corresponding edge in the pose graph because
// just updating childModelFrame doesn't update the pose graph.
auto X_RM = X_RPf * X_MPf.Inverse();
frame.SetRawPose(X_RM);
sdf::updateGraphPose(
*this->dataPtr->poseGraph, childModelFrame->Name(), X_RM);
}
else
{
errors.push_back({ErrorCode::MODEL_PLACEMENT_FRAME_INVALID,
"Found a __placement_frame__ for child model with name [" +
childModelName + "], but the model does not exist in the" +
" parent model with name [" + this->Name() + "]"});
}
}
}
return errors;
}
/////////////////////////////////////////////////
std::string Model::Name() const
{
return this->dataPtr->name;
}
/////////////////////////////////////////////////
void Model::SetName(const std::string &_name)
{
this->dataPtr->name = _name;
}
/////////////////////////////////////////////////
bool Model::Static() const
{
return this->dataPtr->isStatic;
}
/////////////////////////////////////////////////
void Model::SetStatic(const bool _static)
{
this->dataPtr->isStatic = _static;
}
/////////////////////////////////////////////////
bool Model::SelfCollide() const
{
return this->dataPtr->selfCollide;
}
/////////////////////////////////////////////////
void Model::SetSelfCollide(const bool _selfCollide)
{
this->dataPtr->selfCollide = _selfCollide;
}
/////////////////////////////////////////////////
bool Model::AllowAutoDisable() const
{
return this->dataPtr->allowAutoDisable;
}
/////////////////////////////////////////////////
void Model::SetAllowAutoDisable(const bool _allowAutoDisable)
{
this->dataPtr->allowAutoDisable = _allowAutoDisable;
}
/////////////////////////////////////////////////
bool Model::EnableWind() const
{
return this->dataPtr->enableWind;
}
/////////////////////////////////////////////////
void Model::SetEnableWind(const bool _enableWind)
{
this->dataPtr->enableWind =_enableWind;
}
/////////////////////////////////////////////////
uint64_t Model::LinkCount() const
{
return this->dataPtr->links.size();
}
/////////////////////////////////////////////////
const Link *Model::LinkByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->links.size())
return &this->dataPtr->links[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool Model::LinkNameExists(const std::string &_name) const
{
return nullptr != this->LinkByName(_name);
}
/////////////////////////////////////////////////
uint64_t Model::JointCount() const
{
return this->dataPtr->joints.size();
}
/////////////////////////////////////////////////
const Joint *Model::JointByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->joints.size())
return &this->dataPtr->joints[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool Model::JointNameExists(const std::string &_name) const
{
return nullptr != this->JointByName(_name);
}
/////////////////////////////////////////////////
const Joint *Model::JointByName(const std::string &_name) const
{
auto index = _name.rfind("::");
if (index != std::string::npos)
{
const Model *model = this->ModelByName(_name.substr(0, index));
if (nullptr != model)
{
return model->JointByName(_name.substr(index + 2));
}
// The nested model name preceding the last "::" could not be found.
// For now, try to find a link that matches _name exactly.
// When "::" are reserved and not allowed in names, then uncomment
// the following line to return a nullptr.
// return nullptr;
}
for (auto const &j : this->dataPtr->joints)
{
if (j.Name() == _name)
{
return &j;
}
}
return nullptr;
}
/////////////////////////////////////////////////
uint64_t Model::FrameCount() const
{
return this->dataPtr->frames.size();
}
/////////////////////////////////////////////////
const Frame *Model::FrameByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->frames.size())
return &this->dataPtr->frames[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool Model::FrameNameExists(const std::string &_name) const
{
return nullptr != this->FrameByName(_name);
}
/////////////////////////////////////////////////
const Frame *Model::FrameByName(const std::string &_name) const
{
auto index = _name.rfind("::");
if (index != std::string::npos)
{
const Model *model = this->ModelByName(_name.substr(0, index));
if (nullptr != model)
{
return model->FrameByName(_name.substr(index + 2));
}
// The nested model name preceding the last "::" could not be found.
// For now, try to find a link that matches _name exactly.
// When "::" are reserved and not allowed in names, then uncomment
// the following line to return a nullptr.
// return nullptr;
}
for (auto const &f : this->dataPtr->frames)
{
if (f.Name() == _name)
{
return &f;
}
}
return nullptr;
}
/////////////////////////////////////////////////
uint64_t Model::ModelCount() const
{
return this->dataPtr->models.size();
}
/////////////////////////////////////////////////
const Model *Model::ModelByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->models.size())
return &this->dataPtr->models[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool Model::ModelNameExists(const std::string &_name) const
{
return nullptr != this->ModelByName(_name);
}
/////////////////////////////////////////////////
const Model *Model::ModelByName(const std::string &_name) const
{
auto index = _name.find("::");
const std::string nextModelName = _name.substr(0, index);
const Model *nextModel = nullptr;
for (auto const &m : this->dataPtr->models)
{
if (m.Name() == nextModelName)
{
nextModel = &m;
break;
}
}
if (nullptr != nextModel && index != std::string::npos)
{
return nextModel->ModelByName(_name.substr(index + 2));
}
return nextModel;
}
/////////////////////////////////////////////////
const Link *Model::CanonicalLink() const
{
return this->CanonicalLinkAndRelativeName().first;
}
/////////////////////////////////////////////////
std::pair<const Link*, std::string> Model::CanonicalLinkAndRelativeName() const
{
if (this->CanonicalLinkName().empty())
{
if (this->LinkCount() > 0)
{
auto firstLink = this->LinkByIndex(0);
return std::make_pair(firstLink, firstLink->Name());
}
else if (this->ModelCount() > 0)
{
// Recursively choose the canonical link of the first nested model
// (depth first search).
auto firstModel = this->ModelByIndex(0);
auto canonicalLinkAndName = firstModel->CanonicalLinkAndRelativeName();
// Prepend firstModelName if a valid link is found.
if (nullptr != canonicalLinkAndName.first)
{
canonicalLinkAndName.second =
firstModel->Name() + "::" + canonicalLinkAndName.second;
}
return canonicalLinkAndName;
}
else
{
return std::make_pair(nullptr, "");
}
}
else
{
return std::make_pair(this->LinkByName(this->CanonicalLinkName()),
this->CanonicalLinkName());
}
}
/////////////////////////////////////////////////
const std::string &Model::CanonicalLinkName() const
{
return this->dataPtr->canonicalLink;
}
/////////////////////////////////////////////////
void Model::SetCanonicalLinkName(const std::string &_canonicalLink)
{
this->dataPtr->canonicalLink = _canonicalLink;
}
/////////////////////////////////////////////////
const std::string &Model::PlacementFrameName() const
{
return this->dataPtr->placementFrameName;
}
/////////////////////////////////////////////////
void Model::SetPlacementFrameName(const std::string &_placementFrame)
{
this->dataPtr->placementFrameName = _placementFrame;
}
/////////////////////////////////////////////////
const ignition::math::Pose3d &Model::RawPose() const
{
return this->dataPtr->pose;
}
/////////////////////////////////////////////////
const std::string &Model::PoseRelativeTo() const
{
return this->dataPtr->poseRelativeTo;
}
/////////////////////////////////////////////////
void Model::SetRawPose(const ignition::math::Pose3d &_pose)
{
this->dataPtr->pose = _pose;
}
/////////////////////////////////////////////////
void Model::SetPoseRelativeTo(const std::string &_frame)
{
this->dataPtr->poseRelativeTo = _frame;
}
/////////////////////////////////////////////////
Errors Model::SetPoseRelativeToGraph(
std::weak_ptr<const PoseRelativeToGraph> _graph)
{
Errors errors;
auto graph = _graph.lock();
if (!graph)
{
errors.push_back({ErrorCode::POSE_RELATIVE_TO_GRAPH_ERROR,
"Tried to set PoseRelativeToGraph with invalid pointer."});
return errors;
}
this->dataPtr->parentPoseGraphScopeName = graph->sourceName;
this->dataPtr->parentPoseGraph = _graph;
return errors;
}
/////////////////////////////////////////////////
sdf::SemanticPose Model::SemanticPose() const
{
return sdf::SemanticPose(
this->dataPtr->pose,
this->dataPtr->poseRelativeTo,
this->dataPtr->parentPoseGraphScopeName,
this->dataPtr->parentPoseGraph);
}
/////////////////////////////////////////////////
const Link *Model::LinkByName(const std::string &_name) const
{
auto index = _name.rfind("::");
if (index != std::string::npos)
{
const Model *model = this->ModelByName(_name.substr(0, index));
if (nullptr != model)
{
return model->LinkByName(_name.substr(index + 2));
}
// The nested model name preceding the last "::" could not be found.
// For now, try to find a link that matches _name exactly.
// When "::" are reserved and not allowed in names, then uncomment
// the following line to return a nullptr.
// return nullptr;
}
for (auto const &l : this->dataPtr->links)
{
if (l.Name() == _name)
{
return &l;
}
}
return nullptr;
}
/////////////////////////////////////////////////
sdf::ElementPtr Model::Element() const
{
return this->dataPtr->sdf;
}