-
Notifications
You must be signed in to change notification settings - Fork 270
/
SdfEntityCreator.cc
993 lines (845 loc) · 31.8 KB
/
SdfEntityCreator.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
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
/*
* Copyright (C) 2019 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 <ignition/common/Console.hh>
#include <ignition/common/Profiler.hh>
#include <sdf/Types.hh>
#include "ignition/gazebo/Events.hh"
#include "ignition/gazebo/SdfEntityCreator.hh"
#include "ignition/gazebo/components/Actor.hh"
#include "ignition/gazebo/components/AirPressureSensor.hh"
#include "ignition/gazebo/components/Altimeter.hh"
#include "ignition/gazebo/components/AngularVelocity.hh"
#include "ignition/gazebo/components/Atmosphere.hh"
#include "ignition/gazebo/components/Camera.hh"
#include "ignition/gazebo/components/CanonicalLink.hh"
#include "ignition/gazebo/components/CastShadows.hh"
#include "ignition/gazebo/components/ChildLinkName.hh"
#include "ignition/gazebo/components/Collision.hh"
#include "ignition/gazebo/components/ContactSensor.hh"
#include "ignition/gazebo/components/CustomSensor.hh"
#include "ignition/gazebo/components/DepthCamera.hh"
#include "ignition/gazebo/components/ForceTorque.hh"
#include "ignition/gazebo/components/Geometry.hh"
#include "ignition/gazebo/components/GpuLidar.hh"
#include "ignition/gazebo/components/Gravity.hh"
#include "ignition/gazebo/components/Imu.hh"
#include "ignition/gazebo/components/Inertial.hh"
#include "ignition/gazebo/components/Joint.hh"
#include "ignition/gazebo/components/JointAxis.hh"
#include "ignition/gazebo/components/JointType.hh"
#include "ignition/gazebo/components/LaserRetro.hh"
#include "ignition/gazebo/components/Lidar.hh"
#include "ignition/gazebo/components/Light.hh"
#include "ignition/gazebo/components/LightType.hh"
#include "ignition/gazebo/components/LinearAcceleration.hh"
#include "ignition/gazebo/components/LinearVelocity.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/components/LogicalCamera.hh"
#include "ignition/gazebo/components/MagneticField.hh"
#include "ignition/gazebo/components/Magnetometer.hh"
#include "ignition/gazebo/components/Material.hh"
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/NavSat.hh"
#include "ignition/gazebo/components/ParentLinkName.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
#include <ignition/gazebo/components/ParticleEmitter.hh>
#include "ignition/gazebo/components/Physics.hh"
#include "ignition/gazebo/components/Pose.hh"
#include "ignition/gazebo/components/RgbdCamera.hh"
#include "ignition/gazebo/components/Scene.hh"
#include "ignition/gazebo/components/SegmentationCamera.hh"
#include "ignition/gazebo/components/SelfCollide.hh"
#include "ignition/gazebo/components/Sensor.hh"
#include "ignition/gazebo/components/SourceFilePath.hh"
#include "ignition/gazebo/components/SphericalCoordinates.hh"
#include "ignition/gazebo/components/Static.hh"
#include "ignition/gazebo/components/ThermalCamera.hh"
#include "ignition/gazebo/components/ThreadPitch.hh"
#include "ignition/gazebo/components/Transparency.hh"
#include "ignition/gazebo/components/Visibility.hh"
#include "ignition/gazebo/components/Visual.hh"
#include "ignition/gazebo/components/WindMode.hh"
#include "ignition/gazebo/components/World.hh"
class ignition::gazebo::SdfEntityCreatorPrivate
{
/// \brief Pointer to entity component manager. We don't assume ownership.
public: EntityComponentManager *ecm{nullptr};
/// \brief Pointer to event manager. We don't assume ownership.
public: EventManager *eventManager{nullptr};
/// \brief Keep track of new sensors being added, so we load their plugins
/// only after we have their scoped name.
public: std::map<Entity, sdf::ElementPtr> newSensors;
/// \brief Keep track of new models being added, so we load their plugins
/// only after we have their scoped name.
public: std::map<Entity, sdf::ElementPtr> newModels;
/// \brief Keep track of new visuals being added, so we load their plugins
/// only after we have their scoped name.
public: std::map<Entity, sdf::ElementPtr> newVisuals;
};
using namespace ignition;
using namespace gazebo;
/////////////////////////////////////////////////
/// \brief Resolve the pose of an SDF DOM object with respect to its relative_to
/// frame. If that fails, return the raw pose
static math::Pose3d ResolveSdfPose(const sdf::SemanticPose &_semPose)
{
math::Pose3d pose;
::sdf::Errors errors = _semPose.Resolve(pose);
if (!errors.empty())
{
pose = _semPose.RawPose();
}
return pose;
}
/////////////////////////////////////////////////
static std::optional<sdf::JointAxis> ResolveJointAxis(
const sdf::JointAxis &_unresolvedAxis)
{
math::Vector3d axisXyz;
const sdf::Errors resolveAxisErrors = _unresolvedAxis.ResolveXyz(axisXyz);
if (!resolveAxisErrors.empty())
{
ignerr << "Failed to resolve axis" << std::endl;
return std::nullopt;
}
sdf::JointAxis resolvedAxis = _unresolvedAxis;
const sdf::Errors setXyzErrors = resolvedAxis.SetXyz(axisXyz);
if (!setXyzErrors.empty())
{
ignerr << "Failed to resolve axis" << std::endl;
return std::nullopt;
}
resolvedAxis.SetXyzExpressedIn("");
return resolvedAxis;
}
//////////////////////////////////////////////////
/// \brief Find a descendent child link entity by name.
/// \param[in] _name The relative name of the link with "::" as the scope
/// delimiter
/// \param[in] _model Model entity that defines the scope
/// \param[in] _ecm Entity component manager
/// \return The Entity of the descendent link or kNullEntity if link was not
/// found
static Entity FindDescendentLinkEntityByName(const std::string &_name,
const Entity &_model,
const EntityComponentManager &_ecm)
{
auto ind = _name.find(sdf::kSdfScopeDelimiter);
std::vector<Entity> candidates;
if (ind != std::string::npos)
{
candidates = _ecm.ChildrenByComponents(
_model, components::Model(), components::Name(_name.substr(0, ind)));
if (candidates.size() != 1 || (ind + 2 >= _name.size()))
{
return kNullEntity;
}
return FindDescendentLinkEntityByName(_name.substr(ind + 2),
candidates.front(), _ecm);
}
else
{
candidates = _ecm.ChildrenByComponents(_model, components::Link(),
components::Name(_name));
if (candidates.size() != 1)
{
return kNullEntity;
}
return candidates.front();
}
}
//////////////////////////////////////////////////
SdfEntityCreator::SdfEntityCreator(EntityComponentManager &_ecm,
EventManager &_eventManager)
: dataPtr(std::make_unique<SdfEntityCreatorPrivate>())
{
this->dataPtr->ecm = &_ecm;
this->dataPtr->eventManager = &_eventManager;
}
/////////////////////////////////////////////////
SdfEntityCreator::SdfEntityCreator(const SdfEntityCreator &_creator)
: dataPtr(std::make_unique<SdfEntityCreatorPrivate>(*_creator.dataPtr))
{
}
/////////////////////////////////////////////////
SdfEntityCreator::SdfEntityCreator(SdfEntityCreator &&_creator) noexcept
= default;
//////////////////////////////////////////////////
SdfEntityCreator::~SdfEntityCreator() = default;
/////////////////////////////////////////////////
SdfEntityCreator &SdfEntityCreator::operator=(const SdfEntityCreator &_creator)
{
*this->dataPtr = (*_creator.dataPtr);
return *this;
}
/////////////////////////////////////////////////
SdfEntityCreator &SdfEntityCreator::operator=(SdfEntityCreator &&_creator)
noexcept = default;
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::World *_world)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::World)");
// World entity
Entity worldEntity = this->dataPtr->ecm->CreateEntity();
// World components
this->dataPtr->ecm->CreateComponent(worldEntity, components::World());
this->dataPtr->ecm->CreateComponent(worldEntity,
components::Name(_world->Name()));
// scene
if (_world->Scene())
{
this->dataPtr->ecm->CreateComponent(worldEntity,
components::Scene(*_world->Scene()));
}
// atmosphere
if (_world->Atmosphere())
{
this->dataPtr->ecm->CreateComponent(worldEntity,
components::Atmosphere(*_world->Atmosphere()));
}
// spherical coordinates
if (_world->SphericalCoordinates())
{
this->dataPtr->ecm->CreateComponent(worldEntity,
components::SphericalCoordinates(*_world->SphericalCoordinates()));
}
// Models
for (uint64_t modelIndex = 0; modelIndex < _world->ModelCount();
++modelIndex)
{
auto model = _world->ModelByIndex(modelIndex);
auto modelEntity = this->CreateEntities(model);
this->SetParent(modelEntity, worldEntity);
}
// Actors
for (uint64_t actorIndex = 0; actorIndex < _world->ActorCount();
++actorIndex)
{
auto actor = _world->ActorByIndex(actorIndex);
auto actorEntity = this->CreateEntities(actor);
this->SetParent(actorEntity, worldEntity);
}
// Lights
for (uint64_t lightIndex = 0; lightIndex < _world->LightCount();
++lightIndex)
{
auto light = _world->LightByIndex(lightIndex);
auto lightEntity = this->CreateEntities(light);
this->SetParent(lightEntity, worldEntity);
}
// Gravity
this->dataPtr->ecm->CreateComponent(worldEntity,
components::Gravity(_world->Gravity()));
// Physics
// \todo(anyone) Support picking a specific physics profile
auto physics = _world->PhysicsByIndex(0);
if (!physics)
{
physics = _world->PhysicsDefault();
}
this->dataPtr->ecm->CreateComponent(worldEntity,
components::Physics(*physics));
// Populate physics options that aren't accessible outside the Element()
// See https://github.com/osrf/sdformat/issues/508
if (physics->Element() && physics->Element()->HasElement("dart"))
{
auto dartElem = physics->Element()->GetElement("dart");
if (dartElem->HasElement("collision_detector"))
{
auto collisionDetector =
dartElem->Get<std::string>("collision_detector");
this->dataPtr->ecm->CreateComponent(worldEntity,
components::PhysicsCollisionDetector(collisionDetector));
}
if (dartElem->HasElement("solver") &&
dartElem->GetElement("solver")->HasElement("solver_type"))
{
auto solver =
dartElem->GetElement("solver")->Get<std::string>("solver_type");
this->dataPtr->ecm->CreateComponent(worldEntity,
components::PhysicsSolver(solver));
}
}
// MagneticField
this->dataPtr->ecm->CreateComponent(worldEntity,
components::MagneticField(_world->MagneticField()));
this->dataPtr->eventManager->Emit<events::LoadPlugins>(worldEntity,
_world->Element());
// Store the world's SDF DOM to be used when saving the world to file
this->dataPtr->ecm->CreateComponent(
worldEntity, components::WorldSdf(*_world));
return worldEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Model *_model)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Model)");
auto ent = this->CreateEntities(_model, false);
// Load all model plugins afterwards, so we get scoped name for nested models.
for (const auto &[entity, element] : this->dataPtr->newModels)
{
this->dataPtr->eventManager->Emit<events::LoadPlugins>(entity, element);
}
this->dataPtr->newModels.clear();
// Load sensor plugins after model, so we get scoped name.
for (const auto &[entity, element] : this->dataPtr->newSensors)
{
this->dataPtr->eventManager->Emit<events::LoadPlugins>(entity, element);
}
this->dataPtr->newSensors.clear();
// Load visual plugins after model, so we get scoped name.
for (const auto &[entity, element] : this->dataPtr->newVisuals)
{
this->dataPtr->eventManager->Emit<events::LoadPlugins>(entity, element);
}
this->dataPtr->newVisuals.clear();
return ent;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Model *_model,
bool _staticParent)
{
// Entity
Entity modelEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(modelEntity, components::Model());
this->dataPtr->ecm->CreateComponent(modelEntity,
components::Pose(ResolveSdfPose(_model->SemanticPose())));
this->dataPtr->ecm->CreateComponent(modelEntity,
components::Name(_model->Name()));
bool isStatic = _model->Static() || _staticParent;
this->dataPtr->ecm->CreateComponent(modelEntity,
components::Static(isStatic));
this->dataPtr->ecm->CreateComponent(
modelEntity, components::WindMode(_model->EnableWind()));
this->dataPtr->ecm->CreateComponent(
modelEntity, components::SelfCollide(_model->SelfCollide()));
this->dataPtr->ecm->CreateComponent(
modelEntity, components::SourceFilePath(_model->Element()->FilePath()));
// NOTE: Pose components of links, visuals, and collisions are expressed in
// the parent frame until we get frames working.
// Links
const auto *canonicalLink = _model->CanonicalLink();
for (uint64_t linkIndex = 0; linkIndex < _model->LinkCount();
++linkIndex)
{
auto link = _model->LinkByIndex(linkIndex);
auto linkEntity = this->CreateEntities(link);
this->SetParent(linkEntity, modelEntity);
if (canonicalLink == link)
{
this->dataPtr->ecm->CreateComponent(linkEntity,
components::CanonicalLink());
}
// Set wind mode if the link didn't override it
if (!this->dataPtr->ecm->Component<components::WindMode>(linkEntity))
{
this->dataPtr->ecm->CreateComponent(
linkEntity, components::WindMode(_model->EnableWind()));
}
}
// Joints
for (uint64_t jointIndex = 0; jointIndex < _model->JointCount();
++jointIndex)
{
auto joint = _model->JointByIndex(jointIndex);
auto jointEntity = this->CreateEntities(joint);
this->SetParent(jointEntity, modelEntity);
}
// Nested Models
for (uint64_t modelIndex = 0; modelIndex < _model->ModelCount();
++modelIndex)
{
auto nestedModel = _model->ModelByIndex(modelIndex);
auto nestedModelEntity = this->CreateEntities(nestedModel, isStatic);
this->SetParent(nestedModelEntity, modelEntity);
}
// Find canonical link
const auto canonicalLinkPair = _model->CanonicalLinkAndRelativeName();
if (canonicalLinkPair.first)
{
Entity canonicalLinkEntity = FindDescendentLinkEntityByName(
canonicalLinkPair.second, modelEntity, *this->dataPtr->ecm);
if (kNullEntity != canonicalLinkEntity)
{
this->dataPtr->ecm->CreateComponent(
modelEntity, components::ModelCanonicalLink(canonicalLinkEntity));
}
else
{
ignerr << "Could not find the canonical link entity for "
<< canonicalLinkPair.second << "\n";
}
}
else if(!isStatic)
{
ignerr << "Could not resolve the canonical link for " << _model->Name()
<< "\n";
}
// Store the model's SDF DOM to be used when saving the world to file
this->dataPtr->ecm->CreateComponent(
modelEntity, components::ModelSdf(*_model));
// Keep track of models so we can load their plugins after loading the entire
// model and having its full scoped name.
this->dataPtr->newModels[modelEntity] = _model->Element();
return modelEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Actor *_actor)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Actor)");
// Entity
Entity actorEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(actorEntity, components::Actor(*_actor));
this->dataPtr->ecm->CreateComponent(actorEntity,
components::Pose(_actor->RawPose()));
this->dataPtr->ecm->CreateComponent(actorEntity,
components::Name(_actor->Name()));
// Actor plugins
this->dataPtr->eventManager->Emit<events::LoadPlugins>(actorEntity,
_actor->Element());
return actorEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Light *_light)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Light)");
// Entity
Entity lightEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(lightEntity, components::Light(*_light));
this->dataPtr->ecm->CreateComponent(lightEntity,
components::Pose(ResolveSdfPose(_light->SemanticPose())));
this->dataPtr->ecm->CreateComponent(lightEntity,
components::Name(_light->Name()));
this->dataPtr->ecm->CreateComponent(lightEntity,
components::LightType(convert(_light->Type())));
return lightEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Link *_link)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Link)");
// Entity
Entity linkEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(linkEntity, components::Link());
this->dataPtr->ecm->CreateComponent(linkEntity,
components::Pose(ResolveSdfPose(_link->SemanticPose())));
this->dataPtr->ecm->CreateComponent(linkEntity,
components::Name(_link->Name()));
this->dataPtr->ecm->CreateComponent(linkEntity,
components::Inertial(_link->Inertial()));
if (_link->EnableWind())
{
this->dataPtr->ecm->CreateComponent(
linkEntity, components::WindMode(_link->EnableWind()));
}
// Visuals
for (uint64_t visualIndex = 0; visualIndex < _link->VisualCount();
++visualIndex)
{
auto visual = _link->VisualByIndex(visualIndex);
auto visualEntity = this->CreateEntities(visual);
this->SetParent(visualEntity, linkEntity);
}
// Collisions
for (uint64_t collisionIndex = 0; collisionIndex < _link->CollisionCount();
++collisionIndex)
{
auto collision = _link->CollisionByIndex(collisionIndex);
auto collisionEntity = this->CreateEntities(collision);
this->SetParent(collisionEntity, linkEntity);
}
// Lights
for (uint64_t lightIndex = 0; lightIndex < _link->LightCount();
++lightIndex)
{
auto light = _link->LightByIndex(lightIndex);
auto lightEntity = this->CreateEntities(light);
this->SetParent(lightEntity, linkEntity);
}
// Sensors
for (uint64_t sensorIndex = 0; sensorIndex < _link->SensorCount();
++sensorIndex)
{
auto sensor = _link->SensorByIndex(sensorIndex);
auto sensorEntity = this->CreateEntities(sensor);
this->SetParent(sensorEntity, linkEntity);
}
// Particle emitters
for (uint64_t emitterIndex = 0; emitterIndex < _link->ParticleEmitterCount();
++emitterIndex)
{
auto emitter = _link->ParticleEmitterByIndex(emitterIndex);
auto emitterEntity = this->CreateEntities(emitter);
this->SetParent(emitterEntity, linkEntity);
}
return linkEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Joint *_joint)
{
return this->CreateEntities(_joint, false);
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Joint *_joint,
bool _resolved)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Joint)");
// Entity
Entity jointEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(jointEntity,
components::Joint());
this->dataPtr->ecm->CreateComponent(jointEntity,
components::JointType(_joint->Type()));
// Sensors
for (uint64_t sensorIndex = 0; sensorIndex < _joint->SensorCount();
++sensorIndex)
{
auto sensor = _joint->SensorByIndex(sensorIndex);
auto sensorEntity = this->CreateEntities(sensor);
this->SetParent(sensorEntity, jointEntity);
}
if (_joint->Axis(0))
{
auto resolvedAxis = ResolveJointAxis(*_joint->Axis(0));
if (!resolvedAxis)
{
ignerr << "Failed to resolve joint axis 0 for joint '" << _joint->Name()
<< "'" << std::endl;
return kNullEntity;
}
this->dataPtr->ecm->CreateComponent(jointEntity,
components::JointAxis(std::move(*resolvedAxis)));
}
if (_joint->Axis(1))
{
auto resolvedAxis = ResolveJointAxis(*_joint->Axis(1));
if (!resolvedAxis)
{
ignerr << "Failed to resolve joint axis 1 for joint '" << _joint->Name()
<< "'" << std::endl;
return kNullEntity;
}
this->dataPtr->ecm->CreateComponent(jointEntity,
components::JointAxis2(std::move(*resolvedAxis)));
}
this->dataPtr->ecm->CreateComponent(jointEntity,
components::Pose(ResolveSdfPose(_joint->SemanticPose())));
this->dataPtr->ecm->CreateComponent(jointEntity ,
components::Name(_joint->Name()));
this->dataPtr->ecm->CreateComponent(jointEntity ,
components::ThreadPitch(_joint->ThreadPitch()));
std::string resolvedParentLinkName;
if (_resolved)
{
resolvedParentLinkName = _joint->ParentLinkName();
}
else
{
const auto resolveParentErrors =
_joint->ResolveParentLink(resolvedParentLinkName);
if (!resolveParentErrors.empty())
{
ignerr << "Failed to resolve parent link for joint '" << _joint->Name()
<< "' with parent name '" << _joint->ParentLinkName() << "'"
<< std::endl;
for (const auto &error : resolveParentErrors)
{
ignerr << error << std::endl;
}
return kNullEntity;
}
}
this->dataPtr->ecm->CreateComponent(
jointEntity, components::ParentLinkName(resolvedParentLinkName));
std::string resolvedChildLinkName;
if (_resolved)
{
resolvedChildLinkName = _joint->ChildLinkName();
}
else
{
const auto resolveChildErrors =
_joint->ResolveChildLink(resolvedChildLinkName);
if (!resolveChildErrors.empty())
{
ignerr << "Failed to resolve child link for joint '" << _joint->Name()
<< "' with child name '" << _joint->ChildLinkName() << "'"
<< std::endl;
for (const auto &error : resolveChildErrors)
{
ignerr << error << std::endl;
}
return kNullEntity;
}
}
this->dataPtr->ecm->CreateComponent(
jointEntity, components::ChildLinkName(resolvedChildLinkName));
return jointEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Visual)");
// Entity
Entity visualEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(visualEntity, components::Visual());
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Pose(ResolveSdfPose(_visual->SemanticPose())));
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Name(_visual->Name()));
this->dataPtr->ecm->CreateComponent(visualEntity,
components::CastShadows(_visual->CastShadows()));
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Transparency(_visual->Transparency()));
this->dataPtr->ecm->CreateComponent(visualEntity,
components::VisibilityFlags(_visual->VisibilityFlags()));
if (_visual->HasLaserRetro())
{
this->dataPtr->ecm->CreateComponent(visualEntity,
components::LaserRetro(_visual->LaserRetro()));
}
if (_visual->Geom())
{
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Geometry(*_visual->Geom()));
}
// \todo(louise) Populate with default material if undefined
if (_visual->Material())
{
this->dataPtr->ecm->CreateComponent(visualEntity,
components::Material(*_visual->Material()));
}
// store the plugin in a component
if (_visual->Element())
{
sdf::ElementPtr pluginElem = _visual->Element()->FindElement("plugin");
if (pluginElem)
{
this->dataPtr->ecm->CreateComponent(visualEntity,
components::VisualPlugin(pluginElem));
}
}
// Keep track of visuals so we can load their plugins after loading the
// entire model and having its full scoped name.
this->dataPtr->newVisuals[visualEntity] = _visual->Element();
return visualEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::ParticleEmitter *_emitter)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::ParticleEmitter)");
// Entity
Entity emitterEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(emitterEntity,
components::ParticleEmitter(convert<msgs::ParticleEmitter>(*_emitter)));
this->dataPtr->ecm->CreateComponent(emitterEntity,
components::Pose(ResolveSdfPose(_emitter->SemanticPose())));
this->dataPtr->ecm->CreateComponent(emitterEntity,
components::Name(_emitter->Name()));
return emitterEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Collision *_collision)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Collision)");
// Entity
Entity collisionEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(collisionEntity,
components::Collision());
this->dataPtr->ecm->CreateComponent(collisionEntity,
components::Pose(ResolveSdfPose(_collision->SemanticPose())));
this->dataPtr->ecm->CreateComponent(collisionEntity,
components::Name(_collision->Name()));
if (_collision->Geom())
{
this->dataPtr->ecm->CreateComponent(collisionEntity,
components::Geometry(*_collision->Geom()));
}
this->dataPtr->ecm->CreateComponent(collisionEntity,
components::CollisionElement(*_collision));
return collisionEntity;
}
//////////////////////////////////////////////////
Entity SdfEntityCreator::CreateEntities(const sdf::Sensor *_sensor)
{
IGN_PROFILE("SdfEntityCreator::CreateEntities(sdf::Sensor)");
// Entity
Entity sensorEntity = this->dataPtr->ecm->CreateEntity();
// Components
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Sensor());
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Pose(ResolveSdfPose(_sensor->SemanticPose())));
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Name(_sensor->Name()));
if (_sensor->Type() == sdf::SensorType::CAMERA)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Camera(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::GPU_LIDAR)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::GpuLidar(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::LIDAR)
{
// \todo(anyone) Implement CPU-based lidar
// this->dataPtr->ecm->CreateComponent(sensorEntity,
// components::Lidar(*_sensor));
ignwarn << "Sensor type LIDAR not supported yet. Try using"
<< "a GPU LIDAR instead." << std::endl;
}
else if (_sensor->Type() == sdf::SensorType::DEPTH_CAMERA)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::DepthCamera(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::RGBD_CAMERA)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::RgbdCamera(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::THERMAL_CAMERA)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::ThermalCamera(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::SEGMENTATION_CAMERA)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::SegmentationCamera(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::AIR_PRESSURE)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::AirPressureSensor(*_sensor));
// create components to be filled by physics
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldPose(math::Pose3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::ALTIMETER)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Altimeter(*_sensor));
// create components to be filled by physics
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldPose(math::Pose3d::Zero));
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldLinearVelocity(math::Vector3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::GPS ||
_sensor->Type() == sdf::SensorType::NAVSAT)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::NavSat(*_sensor));
// Create components to be filled by physics.
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldLinearVelocity(math::Vector3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::IMU)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Imu(*_sensor));
// create components to be filled by physics
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldPose(math::Pose3d::Zero));
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::AngularVelocity(math::Vector3d::Zero));
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::LinearAcceleration(math::Vector3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::FORCE_TORQUE)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::ForceTorque(*_sensor));
}
else if (_sensor->Type() == sdf::SensorType::LOGICAL_CAMERA)
{
auto elem = _sensor->Element();
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::LogicalCamera(elem));
// create components to be filled by physics
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldPose(math::Pose3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::MAGNETOMETER)
{
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::Magnetometer(*_sensor));
// create components to be filled by physics
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::WorldPose(math::Pose3d::Zero));
}
else if (_sensor->Type() == sdf::SensorType::CONTACT)
{
auto elem = _sensor->Element();
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::ContactSensor(elem));
// We will let the contact system create the necessary components for
// physics to populate.
}
else if (_sensor->Type() == sdf::SensorType::CUSTOM)
{
auto elem = _sensor->Element();
this->dataPtr->ecm->CreateComponent(sensorEntity,
components::CustomSensor(*_sensor));
}
else
{
ignwarn << "Sensor type [" << static_cast<int>(_sensor->Type())
<< "] not supported yet." << std::endl;
}
// Keep track of sensors so we can load their plugins after loading the entire
// model and having its full scoped name.
this->dataPtr->newSensors[sensorEntity] = _sensor->Element();
return sensorEntity;
}
//////////////////////////////////////////////////
void SdfEntityCreator::RequestRemoveEntity(Entity _entity, bool _recursive)
{
// Leave children parentless
if (!_recursive)
{
auto childEntities = this->dataPtr->ecm->ChildrenByComponents(_entity,
components::ParentEntity(_entity));
for (const auto childEntity : childEntities)
{
this->dataPtr->ecm->RemoveComponent<components::ParentEntity>(
childEntity);
}
}
this->dataPtr->ecm->RequestRemoveEntity(_entity, _recursive);
}
//////////////////////////////////////////////////
void SdfEntityCreator::SetParent(Entity _child, Entity _parent)
{
// TODO(louise) Figure out a way to avoid duplication while keeping all
// state in components and also keeping a convenient graph in the ECM
this->dataPtr->ecm->SetParentEntity(_child, _parent);
this->dataPtr->ecm->CreateComponent(_child,
components::ParentEntity(_parent));
}