forked from gazebosim/sdformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cc
643 lines (551 loc) · 16.9 KB
/
World.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
/*
* Copyright 2017 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 <string>
#include <unordered_set>
#include <vector>
#include <ignition/math/Vector3.hh>
#include "sdf/Actor.hh"
#include "sdf/Frame.hh"
#include "sdf/Light.hh"
#include "sdf/Model.hh"
#include "sdf/Physics.hh"
#include "sdf/Types.hh"
#include "sdf/World.hh"
#include "FrameSemantics.hh"
#include "ScopedGraph.hh"
#include "Utils.hh"
using namespace sdf;
class sdf::WorldPrivate
{
/// \brief Default constructor
public: WorldPrivate() = default;
/// \brief Copy constructor
/// \param[in] _worldPrivate Joint axis to move.
public: explicit WorldPrivate(const WorldPrivate &_worldPrivate);
// Delete copy assignment so it is not accidentally used
public: WorldPrivate &operator=(const WorldPrivate &) = delete;
/// \brief Pointer to an atmosphere model.
public: std::unique_ptr<Atmosphere> atmosphere;
/// \brief Audio device name
public: std::string audioDevice = "default";
/// \brief Gravity vector.
public: ignition::math::Vector3d gravity =
ignition::math::Vector3d(0, 0, -9.80665);
/// \brief Pointer to Gui parameters.
public: std::unique_ptr<Gui> gui;
/// \brief Pointer to Sene parameters.
public: std::unique_ptr<Scene> scene;
/// \brief The frames specified in this world.
public: std::vector<Frame> frames;
/// \brief The lights specified in this world.
public: std::vector<Light> lights;
/// \brief The actors specified in this world.
public: std::vector<Actor> actors;
/// \brief Magnetic field.
public: ignition::math::Vector3d magneticField =
ignition::math::Vector3d(5.5645e-6, 22.8758e-6, -42.3884e-6);
/// \brief The models specified in this world.
public: std::vector<Model> models;
/// \brief Name of the world.
public: std::string name = "";
/// \brief The physics profiles specified in this world.
public: std::vector<Physics> physics;
/// \brief The SDF element pointer used during load.
public: sdf::ElementPtr sdf;
/// \brief Linear velocity of wind.
public: ignition::math::Vector3d windLinearVelocity =
ignition::math::Vector3d::Zero;
/// \brief Scoped Frame Attached-To graph that points to a graph owned
/// by this world.
public: sdf::ScopedGraph<sdf::FrameAttachedToGraph> frameAttachedToGraph;
/// \brief Scoped Pose Relative-To graph that points to a graph owned by this
/// world.
public: sdf::ScopedGraph<sdf::PoseRelativeToGraph> poseRelativeToGraph;
};
/////////////////////////////////////////////////
WorldPrivate::WorldPrivate(const WorldPrivate &_worldPrivate)
: audioDevice(_worldPrivate.audioDevice),
gravity(_worldPrivate.gravity),
frames(_worldPrivate.frames),
lights(_worldPrivate.lights),
actors(_worldPrivate.actors),
magneticField(_worldPrivate.magneticField),
models(_worldPrivate.models),
name(_worldPrivate.name),
physics(_worldPrivate.physics),
sdf(_worldPrivate.sdf),
windLinearVelocity(_worldPrivate.windLinearVelocity),
frameAttachedToGraph(_worldPrivate.frameAttachedToGraph),
poseRelativeToGraph(_worldPrivate.poseRelativeToGraph)
{
if (_worldPrivate.atmosphere)
{
this->atmosphere =
std::make_unique<Atmosphere>(*(_worldPrivate.atmosphere));
}
if (_worldPrivate.gui)
{
this->gui = std::make_unique<Gui>(*(_worldPrivate.gui));
}
if (_worldPrivate.scene)
{
this->scene = std::make_unique<Scene>(*(_worldPrivate.scene));
}
}
/////////////////////////////////////////////////
World::World()
: dataPtr(new WorldPrivate)
{
this->dataPtr->physics.emplace_back(Physics());
}
/////////////////////////////////////////////////
World::~World()
{
delete this->dataPtr;
this->dataPtr = nullptr;
}
/////////////////////////////////////////////////
World::World(const World &_world)
: dataPtr(new WorldPrivate(*_world.dataPtr))
{
}
/////////////////////////////////////////////////
World::World(World &&_world) noexcept
: dataPtr(std::exchange(_world.dataPtr, nullptr))
{
}
/////////////////////////////////////////////////
World &World::operator=(const World &_world)
{
return *this = World(_world);
}
/////////////////////////////////////////////////
World &World::operator=(World &&_world)
{
std::swap(this->dataPtr, _world.dataPtr);
return *this;
}
/////////////////////////////////////////////////
Errors World::Load(sdf::ElementPtr _sdf)
{
Errors errors;
this->dataPtr->sdf = _sdf;
// Check that the provided SDF element is a <world>
// This is an error that cannot be recovered, so return an error.
if (_sdf->GetName() != "world")
{
errors.push_back({ErrorCode::ELEMENT_INCORRECT_TYPE,
"Attempting to load a World, but the provided SDF element is not a "
"<world>."});
return errors;
}
// Read the world's name
if (!loadName(_sdf, this->dataPtr->name))
{
errors.push_back({ErrorCode::ATTRIBUTE_MISSING,
"A world name is required, but the name is not set."});
}
// Check that the world's name is valid
if (isReservedName(this->dataPtr->name))
{
errors.push_back({ErrorCode::RESERVED_NAME,
"The supplied world name [" + this->dataPtr->name +
"] is reserved."});
}
// Read the audio element
if (_sdf->HasElement("audio"))
{
sdf::ElementPtr elem = _sdf->GetElement("audio");
this->dataPtr->audioDevice = elem->Get<std::string>("device",
this->dataPtr->audioDevice).first;
}
// Read the wind element
if (_sdf->HasElement("wind"))
{
sdf::ElementPtr elem = _sdf->GetElement("wind");
this->dataPtr->windLinearVelocity =
elem->Get<ignition::math::Vector3d>("linear_velocity",
this->dataPtr->windLinearVelocity).first;
}
// Read the atmosphere element
if (_sdf->HasElement("atmosphere"))
{
this->dataPtr->atmosphere.reset(new sdf::Atmosphere());
Errors atmosphereLoadErrors =
this->dataPtr->atmosphere->Load(_sdf->GetElement("atmosphere"));
errors.insert(errors.end(), atmosphereLoadErrors.begin(),
atmosphereLoadErrors.end());
}
// Read gravity.
this->dataPtr->gravity = _sdf->Get<ignition::math::Vector3d>("gravity",
this->dataPtr->gravity).first;
// Read the magnetic field.
this->dataPtr->magneticField =
_sdf->Get<ignition::math::Vector3d>("magnetic_field",
this->dataPtr->magneticField).first;
if (!_sdf->HasUniqueChildNames())
{
sdfwarn << "Non-unique names detected in XML children of world 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 all the models.
Errors modelLoadErrors =
loadUniqueRepeated<Model>(_sdf, "model", this->dataPtr->models);
errors.insert(errors.end(), modelLoadErrors.begin(), modelLoadErrors.end());
// 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 physics.
if (_sdf->HasElement("physics"))
{
this->dataPtr->physics.clear();
Errors physicsLoadErrors = loadUniqueRepeated<Physics>(_sdf, "physics",
this->dataPtr->physics);
errors.insert(errors.end(), physicsLoadErrors.begin(),
physicsLoadErrors.end());
}
// Load all the actors.
Errors actorLoadErrors = loadUniqueRepeated<Actor>(_sdf, "actor",
this->dataPtr->actors);
errors.insert(errors.end(), actorLoadErrors.begin(), actorLoadErrors.end());
// Load all the lights.
Errors lightLoadErrors = loadUniqueRepeated<Light>(_sdf, "light",
this->dataPtr->lights);
errors.insert(errors.end(), lightLoadErrors.begin(), lightLoadErrors.end());
// 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)
{
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 world with name [" << this->Name() << "] "
<< "has a name collision, changing frame name to ["
<< frameName << "].\n";
frame.SetName(frameName);
}
frameNames.insert(frameName);
}
// Load the Gui
if (_sdf->HasElement("gui"))
{
this->dataPtr->gui.reset(new sdf::Gui());
Errors guiLoadErrors = this->dataPtr->gui->Load(_sdf->GetElement("gui"));
errors.insert(errors.end(), guiLoadErrors.begin(), guiLoadErrors.end());
}
// Load the Scene
if (_sdf->HasElement("scene"))
{
this->dataPtr->scene.reset(new sdf::Scene());
Errors sceneLoadErrors =
this->dataPtr->scene->Load(_sdf->GetElement("scene"));
errors.insert(errors.end(), sceneLoadErrors.begin(), sceneLoadErrors.end());
}
return errors;
}
/////////////////////////////////////////////////
std::string World::Name() const
{
return this->dataPtr->name;
}
/////////////////////////////////////////////////
void World::SetName(const std::string &_name) const
{
this->dataPtr->name = _name;
}
/////////////////////////////////////////////////
std::string World::AudioDevice() const
{
return this->dataPtr->audioDevice;
}
/////////////////////////////////////////////////
void World::SetAudioDevice(const std::string &_device)
{
this->dataPtr->audioDevice = _device;
}
/////////////////////////////////////////////////
ignition::math::Vector3d World::WindLinearVelocity() const
{
return this->dataPtr->windLinearVelocity;
}
/////////////////////////////////////////////////
void World::SetWindLinearVelocity(const ignition::math::Vector3d &_wind)
{
this->dataPtr->windLinearVelocity = _wind;
}
/////////////////////////////////////////////////
ignition::math::Vector3d World::Gravity() const
{
return this->dataPtr->gravity;
}
/////////////////////////////////////////////////
void World::SetGravity(const ignition::math::Vector3d &_gravity)
{
this->dataPtr->gravity = _gravity;
}
/////////////////////////////////////////////////
ignition::math::Vector3d World::MagneticField() const
{
return this->dataPtr->magneticField;
}
/////////////////////////////////////////////////
void World::SetMagneticField(const ignition::math::Vector3d &_mag)
{
this->dataPtr->magneticField = _mag;
}
/////////////////////////////////////////////////
uint64_t World::ModelCount() const
{
return this->dataPtr->models.size();
}
/////////////////////////////////////////////////
const Model *World::ModelByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->models.size())
return &this->dataPtr->models[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool World::ModelNameExists(const std::string &_name) const
{
for (auto const &m : this->dataPtr->models)
{
if (m.Name() == _name)
{
return true;
}
}
return false;
}
/////////////////////////////////////////////////
const Model *World::ModelByName(const std::string &_name) const
{
for (auto const &m : this->dataPtr->models)
{
if (m.Name() == _name)
{
return &m;
}
}
return nullptr;
}
/////////////////////////////////////////////////
const sdf::Atmosphere *World::Atmosphere() const
{
return this->dataPtr->atmosphere.get();
}
/////////////////////////////////////////////////
void World::SetAtmosphere(const sdf::Atmosphere &_atmosphere) const
{
this->dataPtr->atmosphere.reset(new sdf::Atmosphere(_atmosphere));
}
/////////////////////////////////////////////////
sdf::Gui *World::Gui() const
{
return this->dataPtr->gui.get();
}
/////////////////////////////////////////////////
void World::SetGui(const sdf::Gui &_gui)
{
return this->dataPtr->gui.reset(new sdf::Gui(_gui));
}
/////////////////////////////////////////////////
const sdf::Scene *World::Scene() const
{
return this->dataPtr->scene.get();
}
/////////////////////////////////////////////////
void World::SetScene(const sdf::Scene &_scene)
{
return this->dataPtr->scene.reset(new sdf::Scene(_scene));
}
/////////////////////////////////////////////////
sdf::ElementPtr World::Element() const
{
return this->dataPtr->sdf;
}
/////////////////////////////////////////////////
uint64_t World::FrameCount() const
{
return this->dataPtr->frames.size();
}
/////////////////////////////////////////////////
const Frame *World::FrameByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->frames.size())
return &this->dataPtr->frames[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool World::FrameNameExists(const std::string &_name) const
{
for (auto const &f : this->dataPtr->frames)
{
if (f.Name() == _name)
{
return true;
}
}
return false;
}
/////////////////////////////////////////////////
const Frame *World::FrameByName(const std::string &_name) const
{
for (auto const &f : this->dataPtr->frames)
{
if (f.Name() == _name)
{
return &f;
}
}
return nullptr;
}
/////////////////////////////////////////////////
uint64_t World::LightCount() const
{
return this->dataPtr->lights.size();
}
/////////////////////////////////////////////////
const Light *World::LightByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->lights.size())
return &this->dataPtr->lights[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool World::LightNameExists(const std::string &_name) const
{
for (auto const &l : this->dataPtr->lights)
{
if (l.Name() == _name)
{
return true;
}
}
return false;
}
/////////////////////////////////////////////////
uint64_t World::ActorCount() const
{
return this->dataPtr->actors.size();
}
/////////////////////////////////////////////////
const Actor *World::ActorByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->actors.size())
return &this->dataPtr->actors[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool World::ActorNameExists(const std::string &_name) const
{
for (auto const &a : this->dataPtr->actors)
{
if (a.Name() == _name)
{
return true;
}
}
return false;
}
//////////////////////////////////////////////////
uint64_t World::PhysicsCount() const
{
return this->dataPtr->physics.size();
}
//////////////////////////////////////////////////
const Physics *World::PhysicsByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->physics.size())
return &this->dataPtr->physics[_index];
return nullptr;
}
//////////////////////////////////////////////////
const Physics *World::PhysicsDefault() const
{
if (!this->dataPtr->physics.empty())
{
for (const Physics &physics : this->dataPtr->physics)
{
if (physics.IsDefault())
return &physics;
}
return &this->dataPtr->physics.at(0);
}
return nullptr;
}
//////////////////////////////////////////////////
bool World::PhysicsNameExists(const std::string &_name) const
{
for (const Physics &physics : this->dataPtr->physics)
{
if (physics.Name() == _name)
{
return true;
}
}
return false;
}
/////////////////////////////////////////////////
void World::SetPoseRelativeToGraph(sdf::ScopedGraph<PoseRelativeToGraph> _graph)
{
this->dataPtr->poseRelativeToGraph = _graph;
for (auto &model : this->dataPtr->models)
{
model.SetPoseRelativeToGraph(this->dataPtr->poseRelativeToGraph);
}
for (auto &frame : this->dataPtr->frames)
{
frame.SetPoseRelativeToGraph(this->dataPtr->poseRelativeToGraph);
}
for (auto &light : this->dataPtr->lights)
{
light.SetXmlParentName("world");
light.SetPoseRelativeToGraph(this->dataPtr->poseRelativeToGraph);
}
}
/////////////////////////////////////////////////
void World::SetFrameAttachedToGraph(
sdf::ScopedGraph<FrameAttachedToGraph> _graph)
{
this->dataPtr->frameAttachedToGraph = _graph;
for (auto &frame : this->dataPtr->frames)
{
frame.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
}
for (auto &model : this->dataPtr->models)
{
model.SetFrameAttachedToGraph(this->dataPtr->frameAttachedToGraph);
}
}