forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SdfGenerator_TEST.cc
1061 lines (948 loc) · 35 KB
/
SdfGenerator_TEST.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
863
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) 2020 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 <gtest/gtest.h>
#include <tinyxml2.h>
#include <gz/common/Console.hh>
#include <gz/fuel_tools/ClientConfig.hh>
#include <gz/fuel_tools/Interface.hh>
#include <gz/utils/ExtraTestMacros.hh>
#include <sdf/Model.hh>
#include <sdf/Root.hh>
#include <sdf/sdf.hh>
#include "gz/sim/EventManager.hh"
#include "gz/sim/SdfEntityCreator.hh"
#include "gz/sim/components/Collision.hh"
#include "gz/sim/components/Joint.hh"
#include "gz/sim/components/Link.hh"
#include "gz/sim/components/Model.hh"
#include "gz/sim/components/Name.hh"
#include "gz/sim/components/ParentEntity.hh"
#include "gz/sim/components/Pose.hh"
#include "gz/sim/components/Sensor.hh"
#include "gz/sim/components/Visual.hh"
#include "gz/sim/components/World.hh"
#include "test_config.hh"
#include "helpers/UniqueTestDirectoryEnv.hh"
#include "helpers/EnvTestFixture.hh"
#include "SdfGenerator.hh"
using namespace gz;
using namespace sim;
/////////////////////////////////////////////////
/// \breif Checks if elemA is a subset of elemB
static testing::AssertionResult isSubset(const sdf::ElementPtr &_elemA,
const sdf::ElementPtr &_elemB)
{
if (_elemA->GetName() != _elemB->GetName())
{
return testing::AssertionFailure()
<< "Mismatch in element name: '" << _elemA->GetName() << "' vs '"
<< _elemB->GetName() << "'";
}
if (_elemA->GetAttributeCount() != _elemB->GetAttributeCount())
{
return testing::AssertionFailure()
<< "Mismatch in attribute count for " << _elemA->GetName() << ": "
<< _elemA->GetAttributeCount() << " vs "
<< _elemB->GetAttributeCount();
}
// Compare attributes
for (std::size_t i = 0; i < _elemA->GetAttributeCount(); i)
{
sdf::ParamPtr attrA = _elemA->GetAttribute(i);
sdf::ParamPtr attrB = _elemB->GetAttribute(attrA->GetKey());
if (attrA->GetTypeName() != attrB->GetTypeName())
{
return testing::AssertionFailure()
<< "Mismatch in attribute type for " << _elemA->GetName() << "/[@"
<< attrA->GetKey() << "]: '" << attrA->GetTypeName() << "' vs '"
<< attrB->GetTypeName() << "'";
}
if (attrA->GetAsString() != attrB->GetAsString())
{
return testing::AssertionFailure()
<< "Mismatch in attribute as string for " << _elemA->GetName()
<< "/[@" << attrA->GetKey() << "]: '" << attrA->GetAsString()
<< "' vs '" << attrB->GetAsString() << "'";
}
}
// Compare values
{
sdf::ParamPtr valA = _elemA->GetValue();
if (nullptr != valA)
{
sdf::ParamPtr valB = _elemB->GetValue();
if (nullptr == valB)
{
return testing::AssertionFailure()
<< "Value of " << _elemB->GetName() << " null";
}
if (valA->GetTypeName() != valB->GetTypeName())
{
return testing::AssertionFailure()
<< "Mismatch in value type for " << _elemA->GetName() << ": '"
<< valA->GetTypeName() << "' vs '" << valB->GetTypeName() << "'";
}
if (valA->GetTypeName() == "pose")
{
math::Pose3d poseA, poseB;
valA->Get(poseA);
valA->Get(poseB);
if (poseA != poseB)
{
return testing::AssertionFailure()
<< "Mismatch in value as Pose: '" << poseA << "' vs '" << poseB
<< "'";
}
}
else if (valA->GetTypeName() == "double")
{
double dblA, dblB;
valA->Get(dblA);
valB->Get(dblB);
if (!math::equal(dblA, dblB))
{
return testing::AssertionFailure()
<< "Mismatch in value as double: '" << dblA << "' vs '"
<< dblB << "'";
}
}
else if (valA->GetTypeName() == "float")
{
float fltA, fltB;
valA->Get(fltA);
valB->Get(fltB);
if (!math::equal(fltA, fltB))
{
return testing::AssertionFailure()
<< "Mismatch in value as float: '" << fltA << "' vs '"
<< fltB << "'";
}
}
else if (valA->GetTypeName() == "bool")
{
bool boolA, boolB;
valA->Get(boolA);
valB->Get(boolB);
if (boolA != boolB)
{
return testing::AssertionFailure()
<< "Mismatch in value as bool: '" << boolA << "' vs '"
<< boolB << "'";
}
}
else if (valA->GetAsString() != valB->GetAsString())
{
return testing::AssertionFailure()
<< "Mismatch in value as string: '" << valA->GetAsString()
<< "' vs '" << valB->GetAsString() << "'";
}
}
}
auto elemBClone = _elemB->Clone();
for (sdf::ElementPtr childElemA = _elemA->GetFirstElement(); childElemA;
childElemA = childElemA->GetNextElement())
{
bool result = false;
// Since we want to ignore order of elements, we have to compare against
// every element in _elemB.
for (sdf::ElementPtr childElemB = elemBClone->GetFirstElement(); childElemB;
childElemB = childElemB->GetNextElement())
{
result = result || isSubset(childElemA, childElemB);
if (result)
{
// Remove the matching element so that we don't compare against it
// again.
elemBClone->RemoveChild(childElemB);
break;
}
}
if (!result)
{
// Ignore missing pose values if the pose is zero.
sdf::ParamPtr childValA = childElemA->GetValue();
if (childValA->GetTypeName() == "pose")
{
math::Pose3d childValPose;
childValA->Get(childValPose);
if (childValPose == math::Pose3d::Zero)
return testing::AssertionSuccess();
}
else if (childValA->GetTypeName() == "bool")
{
bool childValBool;
childValA->Get(childValBool);
if (!childValBool && (childElemA->GetName() == "static" ||
childElemA->GetName() == "self_collide" ||
childElemA->GetName() == "enable_wind" ))
{
return testing::AssertionSuccess();
}
}
return testing::AssertionFailure()
<< "No matching child element in element B for child element '"
<< childElemA->GetName() << "' in element A";
}
}
return testing::AssertionSuccess();
}
/////////////////////////////////////////////////
TEST(CompareElements, CompareWithDuplicateElements)
{
const std::string m1Sdf = R"(
<sdf version="1.7">
<model name="M1">
<pose>1 0 0 0 0 0</pose>
</model>
</sdf>
)";
const std::string m1CompTestSdf = R"(
<sdf version="1.7">
<model name="M1">
<pose>1 0 0 0 0 0</pose>
<pose>0 1 0 0 0 0</pose>
</model>
</sdf>
)";
auto m1 = std::make_shared<sdf::Element>();
auto m1CompTest = std::make_shared<sdf::Element>();
sdf::initFile("model.sdf", m1);
sdf::initFile("model.sdf", m1CompTest);
sdf::readString(m1Sdf, m1);
sdf::readString(m1CompTestSdf, m1CompTest);
EXPECT_TRUE(isSubset(m1, m1CompTest));
EXPECT_FALSE(isSubset(m1CompTest, m1));
}
/////////////////////////////////////////////////
class ElementUpdateFixture : public InternalFixture<::testing::Test>
{
public: void SetUp() override
{
InternalFixture::SetUp();
fuel_tools::ClientConfig config;
config.SetCacheLocation(test::UniqueTestDirectoryEnv::Path());
this->fuelClient = std::make_unique<fuel_tools::FuelClient>(config);
auto fuelCb = [&](const std::string &_uri)
{
auto out =
fuel_tools::fetchResourceWithClient(_uri, *this->fuelClient.get());
if (!out.empty())
{
this->includeUriMap[out] = _uri;
}
return out;
};
// Configure SDF to fetch assets from Gazebo Fuel.
sdf::setFindCallback(fuelCb);
creator = std::make_unique<SdfEntityCreator>(this->ecm, this->evm);
}
public: virtual void LoadWorld(const std::string &_path)
{
auto errors = this->root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
_path));
ASSERT_EQ(1u, root.WorldCount());
this->world = root.WorldByIndex(0);
ASSERT_NE(nullptr, this->world);
this->creator->CreateEntities(world);
}
public: virtual void LoadWorldString(const std::string &_worldSdf)
{
auto errors = this->root.LoadSdfString(_worldSdf);
ASSERT_EQ(1u, root.WorldCount());
this->world = root.WorldByIndex(0);
ASSERT_NE(nullptr, this->world);
this->creator->CreateEntities(world);
}
// Helper function to get a model by name from an sdf::World
const sdf::Model *ModelByName(const std::string &_name)
{
for (std::size_t i = 0; i < this->world->ModelCount(); i)
{
if (_name == this->world->ModelByIndex(i)->Name())
{
return this->world->ModelByIndex(i);
}
}
return nullptr;
}
public: EntityComponentManager ecm;
public: EventManager evm;
public: sdf::Root root;
public: const sdf::World *world{nullptr};
public: std::unique_ptr<SdfEntityCreator> creator;
public: msgs::SdfGeneratorConfig sdfGenConfig;
public: sdf_generator::IncludeUriMap includeUriMap;
public: std::unique_ptr<fuel_tools::FuelClient> fuelClient;
};
/////////////////////////////////////////////////
class ModelElementFixture : public ElementUpdateFixture
{
public: sdf::ElementPtr CreateModelElem(const std::string &_modelName)
{
using namespace sdf_generator;
Entity model = this->ecm.EntityByComponents(
components::Model(), components::Name(_modelName));
if (kNullEntity == model)
return nullptr;
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("model.sdf", elem);
EXPECT_TRUE(updateModelElement(elem, this->ecm, model));
return elem;
}
public: void TestModel(const std::string &_modelName)
{
auto elem = this->CreateModelElem(_modelName);
ASSERT_NE(nullptr, elem);
const sdf::Model *sdfModel = this->ModelByName(_modelName);
ASSERT_NE(nullptr, sdfModel);
EXPECT_TRUE(isSubset(sdfModel->Element(), elem));
EXPECT_TRUE(isSubset(elem, sdfModel->Element()));
}
};
/////////////////////////////////////////////////
TEST_F(ModelElementFixture, ModelsInline)
{
this->LoadWorld(common::joinPaths("test", "worlds", "shapes.sdf"));
this->TestModel("box");
this->TestModel("capsule");
this->TestModel("cylinder");
this->TestModel("ellipsoid");
this->TestModel("sphere");
}
/////////////////////////////////////////////////
TEST_F(ModelElementFixture, ModelIncluded)
{
this->LoadWorld(common::joinPaths("test", "worlds", "save_world.sdf"));
this->TestModel("backpack1");
this->TestModel("backpack2");
}
/////////////////////////////////////////////////
TEST_F(ModelElementFixture, ModelComponentUpdate)
{
this->LoadWorld(common::joinPaths("test", "worlds", "shapes.sdf"));
std::string modelName = "box";
Entity modelEntity = this->ecm.EntityByComponents(
components::Model(), components::Name(modelName));
auto *poseComp = this->ecm.Component<components::Pose>(modelEntity);
math::Pose3d newPose{0.1, 0.2, 0.3, 0, 0, 0};
EXPECT_NE(poseComp->Data(), newPose);
*poseComp = components::Pose(newPose);
auto modelElem = this->CreateModelElem(modelName);
ASSERT_NE(nullptr, modelElem);
// Check that the generated element has the new pose
EXPECT_EQ(newPose, modelElem->Get<math::Pose3d>("pose"));
}
/////////////////////////////////////////////////
/// Test to show that CopyFrom and MergeFrom don't work.
TEST_F(ElementUpdateFixture, ConfigOverrideCopyOrMerge)
{
{
msgs::SdfGeneratorConfig::EntityGeneratorConfig globalConfig, modelConfig;
globalConfig.mutable_expand_include_tags()->set_data(true);
globalConfig.mutable_save_fuel_version()->set_data(true);
modelConfig.mutable_expand_include_tags()->set_data(false);
{
auto combinedConfig = globalConfig;
std::cout << "Before Merge global:\n"
<< globalConfig.DebugString() << std::endl;
std::cout << "Before Merge model:\n"
<< modelConfig.DebugString() << std::endl;
combinedConfig.MergeFrom(modelConfig);
std::cout << "After Merge:\n"
<< combinedConfig.DebugString() << std::endl;
}
{
auto combinedConfig = modelConfig;
std::cout << "Before Merge2 global:\n"
<< globalConfig.DebugString() << std::endl;
std::cout << "Before Merge2 model:\n"
<< modelConfig.DebugString() << std::endl;
combinedConfig.MergeFrom(globalConfig);
std::cout << "After Merge:\n"
<< combinedConfig.DebugString() << std::endl;
}
{
auto combinedConfig = globalConfig;
std::cout << "Before Copy global:\n"
<< globalConfig.DebugString() << std::endl;
std::cout << "Before Copy model:\n"
<< modelConfig.DebugString() << std::endl;
combinedConfig.CopyFrom(modelConfig);
std::cout << "After Copy:\n" << combinedConfig.DebugString() << std::endl;
}
}
{
msgs::SdfGeneratorConfig::EntityGeneratorConfig globalConfig, modelConfig;
globalConfig.mutable_expand_include_tags()->set_data(false);
globalConfig.mutable_save_fuel_version()->set_data(false);
modelConfig.mutable_save_fuel_version()->set_data(true);
{
auto combinedConfig = globalConfig;
std::cout << "Before Merge global:\n"
<< globalConfig.DebugString() << std::endl;
std::cout << "Before Merge model:\n"
<< modelConfig.DebugString() << std::endl;
combinedConfig.MergeFrom(modelConfig);
std::cout << "After Merge:\n"
<< combinedConfig.DebugString() << std::endl;
}
{
auto combinedConfig = globalConfig;
std::cout << "Before Copy global:\n"
<< globalConfig.DebugString() << std::endl;
std::cout << "Before Copy model:\n"
<< modelConfig.DebugString() << std::endl;
combinedConfig.CopyFrom(modelConfig);
std::cout << "After Copy:\n" << combinedConfig.DebugString() << std::endl;
}
}
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, ConfigOverride)
{
this->LoadWorld(common::joinPaths("test", "worlds", "save_world.sdf"));
Entity worldEntity = this->ecm.EntityByComponents(components::World());
{
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(true);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_save_fuel_version()
->set_data(true);
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::backpack2"]
.mutable_expand_include_tags()
->set_data(false);
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
ASSERT_TRUE(elem->HasElement("include"));
auto inclElem = elem->GetElement("include");
EXPECT_EQ("backpack2", inclElem->Get<std::string>("name"));
}
{
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(false);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_save_fuel_version()
->set_data(false);
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::backpack2"]
.mutable_save_fuel_version()
->set_data(true);
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
ASSERT_TRUE(elem->HasElement("include"));
auto inclElem = elem->GetElement("include");
EXPECT_EQ("backpack1", inclElem->Get<std::string>("name"));
inclElem = inclElem->GetNextElement("include");
EXPECT_EQ("backpack2", inclElem->Get<std::string>("name"));
auto uri = inclElem->Get<std::string>("uri");
EXPECT_FALSE(uri.empty());
const std::string version = common::split(uri, "/").back();
int64_t versionParsed = -1;
EXPECT_NO_THROW(versionParsed = std::stol(version));
EXPECT_NE(-1, versionParsed);
}
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsInline)
{
this->LoadWorld(common::joinPaths("test", "worlds", "shapes.sdf"));
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
sdf_generator::updateWorldElement(elem, this->ecm, worldEntity);
EXPECT_TRUE(isSubset(this->root.WorldByIndex(0)->Element(), elem));
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsIncludedExpanded)
{
this->LoadWorld(common::joinPaths("test", "worlds", "save_world.sdf"));
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
auto origWorldElem = this->root.WorldByIndex(0)->Element();
EXPECT_TRUE(isSubset(origWorldElem, elem))
<< "Original: " << origWorldElem->ToString("")
<< "\n Generated: " << elem->ToString("");
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldComponentUpdate)
{
const std::string worldFile{"test/worlds/shapes.sdf"};
this->LoadWorld(worldFile);
std::string modelName = "box";
Entity modelEntity = this->ecm.EntityByComponents(
components::Model(), components::Name(modelName));
auto *poseComp = this->ecm.Component<components::Pose>(modelEntity);
math::Pose3d newPose{0.1, 0.2, 0.3, 0, 0, 0};
EXPECT_NE(poseComp->Data(), newPose);
*poseComp = components::Pose(newPose);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
sdf_generator::updateWorldElement(elem, this->ecm, worldEntity);
ASSERT_TRUE(elem->HasElement("model"));
auto modelElem = elem->GetElement("model");
for (; modelElem; modelElem->GetNextElement("model"))
{
if (modelName == modelElem->Get<std::string>("name"))
break;
}
// Check that the generated element has the new pose
EXPECT_EQ(newPose, modelElem->Get<math::Pose3d>("pose"));
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsIncludedNotExpanded)
{
const auto worldFile = common::joinPaths("test", "worlds", "save_world.sdf");
this->LoadWorld(worldFile);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(false);
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::backpack3"]
.mutable_save_fuel_version()
->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
tinyxml2::XMLDocument originalSdfDoc;
EXPECT_EQ(tinyxml2::XML_SUCCESS,
originalSdfDoc.LoadFile(
common::joinPaths(PROJECT_SOURCE_PATH, worldFile).c_str()));
tinyxml2::XMLDocument genSdfDoc;
genSdfDoc.Parse(elem->ToString("").c_str());
// Compare elements from the original sdf xml and the generated xml
auto origWorld = originalSdfDoc.RootElement()->FirstChildElement("world");
ASSERT_NE(nullptr, origWorld);
auto genWorld = genSdfDoc.RootElement();
ASSERT_NE(nullptr, genWorld);
// Check name
{
ASSERT_NE(nullptr, origWorld->Attribute("name"));
ASSERT_NE(nullptr, genWorld->Attribute("name"));
EXPECT_STREQ(origWorld->Attribute("name"), genWorld->Attribute("name"));
}
// Check include tag
auto origInclude = origWorld->FirstChildElement("include");
ASSERT_NE(nullptr, origInclude);
auto genInclude = genWorld->FirstChildElement("include");
for (; nullptr != origInclude;
origInclude = origInclude->NextSiblingElement("include"),
genInclude = genInclude->NextSiblingElement("include"))
{
ASSERT_NE(nullptr, genInclude);
auto origUri = origInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, origUri);
auto genUri = genInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, genUri);
EXPECT_STREQ(origUri->GetText(), genUri->GetText());
}
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsIncludedWithInvalidUris)
{
const std::string goodUri =
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack/3";
// These are URIs that are potentially problematic.
const std::vector<std::string> fuelUris = {
// Thes following two URIs are valid, but have a trailing '/'
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack/",
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack/3/",
// Thes following two URIs are invalid, and will not be saved
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack/"
"notInt",
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack/"
"notInt/",
};
std::string worldSdf = R"(
<?xml version="1.0" ?>
<sdf version="1.7">
<world name="invalid_uris">
<include>
<uri>)" fuelUris[0] R"(</uri>
<name>backpack1</name>
</include>
<include>
<uri>)" fuelUris[1] R"(</uri>
<name>backpack2</name>
</include>
<include>
<uri>)" fuelUris[2] R"(</uri>
<name>backpack3</name>
</include>
<include>
<uri>)" fuelUris[3] R"(</uri>
<name>backpack3</name>
</include>
</world>
</sdf>
)";
this->LoadWorldString(worldSdf);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
auto globalEntityConfig =
this->sdfGenConfig.mutable_global_entity_gen_config();
globalEntityConfig->mutable_expand_include_tags()->set_data(false);
globalEntityConfig->mutable_save_fuel_version()->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
std::size_t count = 0;
EXPECT_TRUE(elem->HasElement("include"));
for (sdf::ElementPtr incl = elem->GetElement("include"); incl;
incl = incl->GetNextElement("include"), count)
{
EXPECT_TRUE(incl->HasElement("uri"));
EXPECT_EQ(goodUri, incl->Get<std::string>("uri"));
}
EXPECT_EQ(2u, count);
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsIncludedWithNonFuelUris)
{
const std::vector<std::string> includeUris = {
"https://fuel.gazebosim.org/1.0/openroboticstest/models/backpack",
std::string("file://") PROJECT_SOURCE_PATH
"/test/worlds/models/sphere"};
std::string worldSdf = R"(
<?xml version="1.0" ?>
<sdf version="1.7">
<world name="nonfuel_uris">
<include>
<uri>)" includeUris[0] R"(</uri>
<name>model1</name>
</include>
<include>
<uri>)" includeUris[1] R"(</uri>
<name>model2</name>
</include>
<model name="model3">
<link name="link3"/>
</model>
</world>
</sdf>
)";
this->LoadWorldString(worldSdf);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
// There should be three models
EXPECT_EQ(3u, this->ecm.EntitiesByComponents(components::Model()).size());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(false);
// Model2 is not a Fuel model, so this should have no effect.
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::model2"]
.mutable_save_fuel_version()
->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
std::cout << elem->ToString("") << std::endl;
std::size_t count = 0;
EXPECT_TRUE(elem->HasElement("include"));
for (sdf::ElementPtr incl = elem->GetElement("include"); incl;
incl = incl->GetNextElement("include"), count)
{
EXPECT_TRUE(incl->HasElement("uri"));
EXPECT_EQ(includeUris[count], incl->Get<std::string>("uri"));
}
EXPECT_EQ(2u, count);
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsIncludedWithOneExpanded)
{
const auto worldFile = common::joinPaths("test", "worlds", "save_world.sdf");
this->LoadWorld(worldFile);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(false);
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::backpack2"]
.mutable_expand_include_tags()
->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
tinyxml2::XMLDocument originalSdfDoc;
EXPECT_EQ(tinyxml2::XML_SUCCESS,
originalSdfDoc.LoadFile(
common::joinPaths(PROJECT_SOURCE_PATH, worldFile).c_str()));
tinyxml2::XMLDocument genSdfDoc;
sdf::PrintConfig config;
config.SetOutPrecision(6);
genSdfDoc.Parse(elem->ToString("", config).c_str());
// Compare elements from the original sdf xml and the generated xml
auto origWorld = originalSdfDoc.RootElement()->FirstChildElement("world");
ASSERT_NE(nullptr, origWorld);
auto genWorld = genSdfDoc.RootElement();
ASSERT_NE(nullptr, genWorld);
// Check include tag
auto origInclude = origWorld->FirstChildElement("include");
ASSERT_NE(nullptr, origInclude);
auto genInclude = genWorld->FirstChildElement("include");
ASSERT_NE(nullptr, genInclude);
{
auto origUri = origInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, origUri);
auto genUri = genInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, genUri);
EXPECT_STREQ(origUri->GetText(), genUri->GetText());
}
tinyxml2::XMLElement *backpack2Model{nullptr};
for (auto modelElem = genWorld->FirstChildElement("model"); modelElem;
modelElem = modelElem->NextSiblingElement("model"))
{
if (std::string("backpack2") == modelElem->Attribute("name"))
{
backpack2Model = modelElem;
break;
}
}
// Check that backpack2 is expanded
ASSERT_NE(nullptr, backpack2Model);
ASSERT_NE(nullptr, backpack2Model->Attribute("name"));
EXPECT_STREQ(backpack2Model->Attribute("name"), "backpack2");
ASSERT_NE(nullptr, backpack2Model->FirstChildElement("pose"));
EXPECT_STREQ(backpack2Model->FirstChildElement("pose")->GetText(),
"1 2 3 0.1 0.2 0.3");
}
/////////////////////////////////////////////////
TEST_F(ElementUpdateFixture, WorldWithModelsExpandedWithOneIncluded)
{
const std::string worldFile{"test/worlds/save_world.sdf"};
this->LoadWorld(worldFile);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(true);
auto &modelGenConfig =
*this->sdfGenConfig.mutable_override_entity_gen_configs();
modelGenConfig["save_world::backpack2"]
.mutable_expand_include_tags()
->set_data(false);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
tinyxml2::XMLDocument originalSdfDoc;
EXPECT_EQ(tinyxml2::XML_SUCCESS,
originalSdfDoc.LoadFile(
common::joinPaths(PROJECT_SOURCE_PATH, worldFile).c_str()));
tinyxml2::XMLDocument genSdfDoc;
genSdfDoc.Parse(elem->ToString("").c_str());
// Compare elements from the original sdf xml and the generated xml
auto origWorld = originalSdfDoc.RootElement()->FirstChildElement("world");
ASSERT_NE(nullptr, origWorld);
auto genWorld = genSdfDoc.RootElement();
ASSERT_NE(nullptr, genWorld);
// Check include tag
auto origInclude = origWorld->FirstChildElement("include");
ASSERT_NE(nullptr, origInclude);
auto genInclude = genWorld->FirstChildElement("include");
ASSERT_NE(nullptr, genInclude);
{
auto origUri = origInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, origUri);
auto genUri = genInclude->FirstChildElement("uri");
ASSERT_NE(nullptr, genUri);
EXPECT_STREQ(origUri->GetText(), genUri->GetText());
}
tinyxml2::XMLElement *backpack2Model{nullptr};
for (auto modelElem = genWorld->FirstChildElement("model"); modelElem;
modelElem = modelElem->NextSiblingElement("model"))
{
if (std::string("backpack2") == modelElem->Attribute("name"))
{
backpack2Model = modelElem;
break;
}
}
// Check that backpack2 is not expanded
EXPECT_EQ(nullptr, backpack2Model);
}
/////////////////////////////////////////////////
// See https://github.com/gazebosim/gz-sim/issues/1175
TEST_F(ElementUpdateFixture,
GZ_UTILS_TEST_DISABLED_ON_WIN32(WorldWithModelsUsingRelativeResourceURIs))
{
const auto includeUri = std::string("file://") PROJECT_SOURCE_PATH
"/test/worlds/models/relative_resource_uri";
std::string worldSdf = R"(
<?xml version="1.0" ?>
<sdf version="1.7">
<world name="test_relative_resource">
<include>
<uri>)" includeUri R"(</uri>
</include>
</world>
</sdf>
)";
this->LoadWorldString(worldSdf);
Entity worldEntity = this->ecm.EntityByComponents(components::World());
EXPECT_EQ(1u, this->ecm.EntitiesByComponents(components::Model()).size());
auto elem = std::make_shared<sdf::Element>();
sdf::initFile("world.sdf", elem);
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(true);
sdf_generator::updateWorldElement(
elem, this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
auto link = elem->GetElement("model")->GetElement("link");
for (auto elemType : {"visual", "collision"})
{
std::size_t testCount{0};
for (auto testElem = link->GetElement(elemType); testElem;
testElem = testElem->GetNextElement(elemType))
{
auto mesh = testElem->GetElement("geometry")->GetElement("mesh");
ASSERT_TRUE(mesh->HasElement("uri"));
auto uriStr = mesh->Get<std::string>("uri");
EXPECT_EQ(includeUri "/meshes/box.dae", uriStr);
testCount;
}
EXPECT_EQ(2u, testCount);
}
}
using GenerateWorldFixture = ElementUpdateFixture;
/////////////////////////////////////////////////
TEST_F(GenerateWorldFixture, ModelsInline)
{
this->LoadWorld(common::joinPaths("test", "worlds", "save_world.sdf"));
Entity worldEntity = this->ecm.EntityByComponents(components::World());
// Check with expandIncludeTags = true
{
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(true);
const std::optional<std::string> worldStr = sdf_generator::generateWorld(
this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
ASSERT_TRUE(worldStr.has_value());
sdf::Root newRoot;
newRoot.LoadSdfString(*worldStr);
EXPECT_TRUE(isSubset(newRoot.Element(), this->root.Element()));
EXPECT_TRUE(isSubset(this->root.Element(), newRoot.Element()));
}
// Check with expandIncludeTags = false
{
this->sdfGenConfig.mutable_global_entity_gen_config()
->mutable_expand_include_tags()
->set_data(false);
const std::optional<std::string> worldStr = sdf_generator::generateWorld(
this->ecm, worldEntity, this->includeUriMap, this->sdfGenConfig);
ASSERT_TRUE(worldStr.has_value());
// std::cout << "Generated world:" << std::endl;
// std::cout << worldStr << std::endl;
sdf::Root newRoot;
newRoot.LoadSdfString(*worldStr);
EXPECT_TRUE(isSubset(newRoot.Element(), this->root.Element()));
EXPECT_TRUE(isSubset(this->root.Element(), newRoot.Element()));
}
}
/////////////////////////////////////////////////
TEST_F(GenerateWorldFixture, PoseWithAttributes)
{
const auto includeUri = std::string("file://") PROJECT_SOURCE_PATH
"/test/worlds/models/relative_resource_uri";
std::string worldSdf = R"(
<?xml version="1.0" ?>
<sdf version="1.9">
<world name="test_pose_attributes">
<include>
<uri>)" includeUri R"(</uri>
<pose degrees="true">1 2 3 90 0 0</pose>
<name>model1</name>
</include>
<include>
<uri>)" includeUri R"(</uri>
<pose rotation_format="quat_xyzw">1 2 3 0 0 0 1</pose>