-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathRoot.cc
444 lines (378 loc) · 13 KB
/
Root.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
/*
* 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 <variant>
#include <vector>
#include <utility>
#include "sdf/Actor.hh"
#include "sdf/Error.hh"
#include "sdf/Light.hh"
#include "sdf/Model.hh"
#include "sdf/Root.hh"
#include "sdf/Types.hh"
#include "sdf/World.hh"
#include "sdf/parser.hh"
#include "sdf/sdf_config.h"
#include "FrameSemantics.hh"
#include "ScopedGraph.hh"
#include "Utils.hh"
using namespace sdf;
/// \brief Private data for sdf::Root
class sdf::Root::Implementation
{
/// \brief Version string
public: std::string version = "";
/// \brief The worlds specified under the root SDF element
public: std::vector<World> worlds;
/// \brief A model, light or actor under the root SDF element
public: std::variant<std::monostate, sdf::Model, sdf::Light, sdf::Actor>
modelLightOrActor;
/// \brief Frame Attached-To Graphs constructed when loading Worlds.
public: std::vector<sdf::ScopedGraph<FrameAttachedToGraph>>
worldFrameAttachedToGraphs;
/// \brief Frame Attached-To Graphs constructed when loading a Model.
public: sdf::ScopedGraph<FrameAttachedToGraph> modelFrameAttachedToGraph;
/// \brief Pose Relative-To Graphs constructed when loading Worlds.
public: std::vector<sdf::ScopedGraph<PoseRelativeToGraph>>
worldPoseRelativeToGraphs;
/// \brief Pose Relative-To Graph constructed when loading a Model.
public: sdf::ScopedGraph<PoseRelativeToGraph> modelPoseRelativeToGraph;
/// \brief The SDF element pointer generated during load.
public: sdf::ElementPtr sdf;
};
/////////////////////////////////////////////////
template <typename T>
sdf::ScopedGraph<FrameAttachedToGraph> createFrameAttachedToGraph(
const T &_domObj, sdf::Errors &_errors)
{
auto frameGraph = sdf::ScopedGraph<FrameAttachedToGraph>(
std::make_shared<FrameAttachedToGraph>());
sdf::Errors buildErrors =
sdf::buildFrameAttachedToGraph(frameGraph, &_domObj);
_errors.insert(_errors.end(), buildErrors.begin(), buildErrors.end());
sdf::Errors validateErrors = sdf::validateFrameAttachedToGraph(frameGraph);
_errors.insert(_errors.end(), validateErrors.begin(), validateErrors.end());
return frameGraph;
}
/////////////////////////////////////////////////
template <typename T>
sdf::ScopedGraph<FrameAttachedToGraph> addFrameAttachedToGraph(
std::vector<sdf::ScopedGraph<sdf::FrameAttachedToGraph>> &_graphList,
const T &_domObj, sdf::Errors &_errors)
{
auto frameGraph = createFrameAttachedToGraph(_domObj, _errors);
_graphList.push_back(frameGraph);
return frameGraph;
}
/////////////////////////////////////////////////
template <typename T>
ScopedGraph<PoseRelativeToGraph> createPoseRelativeToGraph(
const T &_domObj, Errors &_errors)
{
auto poseGraph = ScopedGraph<PoseRelativeToGraph>(
std::make_shared<sdf::PoseRelativeToGraph>());
Errors buildErrors = buildPoseRelativeToGraph(poseGraph, &_domObj);
_errors.insert(_errors.end(), buildErrors.begin(), buildErrors.end());
Errors validateErrors = validatePoseRelativeToGraph(poseGraph);
_errors.insert(_errors.end(), validateErrors.begin(), validateErrors.end());
return poseGraph;
}
/////////////////////////////////////////////////
template <typename T>
ScopedGraph<PoseRelativeToGraph> addPoseRelativeToGraph(
std::vector<sdf::ScopedGraph<sdf::PoseRelativeToGraph>> &_graphList,
const T &_domObj, Errors &_errors)
{
auto poseGraph = createPoseRelativeToGraph(_domObj, _errors);
_graphList.push_back(poseGraph);
return poseGraph;
}
/////////////////////////////////////////////////
Root::Root()
: dataPtr(ignition::utils::MakeUniqueImpl<Implementation>())
{
}
/////////////////////////////////////////////////
Errors Root::Load(const std::string &_filename)
{
return this->Load(_filename, ParserConfig::GlobalConfig());
}
/////////////////////////////////////////////////
Errors Root::Load(const std::string &_filename, const ParserConfig &_config)
{
Errors errors;
// Read an SDF file, and store the result in sdfParsed.
SDFPtr sdfParsed = readFile(_filename, _config, errors);
// Return if we were not able to read the file.
if (!sdfParsed)
{
errors.push_back(
{ErrorCode::FILE_READ, "Unable to read file:" + _filename});
return errors;
}
Errors loadErrors = this->Load(sdfParsed, _config);
errors.insert(errors.end(), loadErrors.begin(), loadErrors.end());
return errors;
}
/////////////////////////////////////////////////
Errors Root::LoadSdfString(const std::string &_sdf)
{
return this->LoadSdfString(_sdf, ParserConfig::GlobalConfig());
}
/////////////////////////////////////////////////
Errors Root::LoadSdfString(const std::string &_sdf, const ParserConfig &_config)
{
Errors errors;
SDFPtr sdfParsed(new SDF());
init(sdfParsed);
// Read an SDF string, and store the result in sdfParsed.
if (!readString(_sdf, _config, sdfParsed, errors))
{
errors.push_back(
{ErrorCode::STRING_READ, "Unable to read SDF string: " + _sdf});
return errors;
}
Errors loadErrors = this->Load(sdfParsed, _config);
errors.insert(errors.end(), loadErrors.begin(), loadErrors.end());
return errors;
}
/////////////////////////////////////////////////
Errors Root::Load(SDFPtr _sdf)
{
return this->Load(_sdf, ParserConfig::GlobalConfig());
}
/////////////////////////////////////////////////
Errors Root::Load(SDFPtr _sdf, const ParserConfig &_config)
{
Errors errors;
this->dataPtr->sdf = _sdf->Root();
// Get the SDF version.
std::pair<std::string, bool> versionPair =
this->dataPtr->sdf->Get<std::string>("version", SDF_VERSION);
// Check that the version exists. Exit if the version is missing.
// readFile will fail if the version is missing, so this
// check should never be triggered.
if (!versionPair.second)
{
errors.push_back(
{ErrorCode::ATTRIBUTE_MISSING, "SDF does not have a version."});
return errors;
}
// Check that the version is the latest, since this is assumed by the DOM API
if (SDF_PROTOCOL_VERSION != versionPair.first)
{
errors.push_back(
{ErrorCode::ATTRIBUTE_INVALID,
"SDF version attribute[" + versionPair.first + "] should match "
"the latest version[" + SDF_PROTOCOL_VERSION + "] when loading DOM "
"objects."});
return errors;
}
this->dataPtr->version = versionPair.first;
// Read all the worlds
if (this->dataPtr->sdf->HasElement("world"))
{
ElementPtr elem = this->dataPtr->sdf->GetElement("world");
while (elem)
{
World world;
Errors worldErrors = world.Load(elem, _config);
// Build the graphs.
auto frameAttachedToGraph = addFrameAttachedToGraph(
this->dataPtr->worldFrameAttachedToGraphs, world, worldErrors);
world.SetFrameAttachedToGraph(frameAttachedToGraph);
auto poseRelativeToGraph = addPoseRelativeToGraph(
this->dataPtr->worldPoseRelativeToGraphs, world, worldErrors);
world.SetPoseRelativeToGraph(poseRelativeToGraph);
// Attempt to load the world
if (worldErrors.empty())
{
// Check that the world's name does not exist.
if (this->WorldNameExists(world.Name()))
{
errors.push_back({ErrorCode::DUPLICATE_NAME,
"World with name[" + world.Name() + "] already exists."
" Each world must have a unique name. Skipping this world."});
}
}
else
{
std::move(worldErrors.begin(), worldErrors.end(),
std::back_inserter(errors));
errors.push_back({ErrorCode::ELEMENT_INVALID,
"Failed to load a world."});
}
this->dataPtr->worlds.push_back(std::move(world));
elem = elem->GetNextElement("world");
}
}
// Load all the models.
std::vector<sdf::Model> models;
Errors modelLoadErrors = loadUniqueRepeated<sdf::Model>(
this->dataPtr->sdf, "model", models, _config);
errors.insert(errors.end(), modelLoadErrors.begin(), modelLoadErrors.end());
if (!models.empty())
{
if (models.size() > 1)
{
errors.emplace_back(
ErrorCode::ELEMENT_INCORRECT_TYPE,
"Root object can only contain one model. Using the first one found");
}
this->dataPtr->modelLightOrActor = std::move(models.front());
sdf::Model &model = std::get<sdf::Model>(this->dataPtr->modelLightOrActor);
// Build the graphs.
this->dataPtr->modelFrameAttachedToGraph =
createFrameAttachedToGraph(model, errors);
model.SetFrameAttachedToGraph(this->dataPtr->modelFrameAttachedToGraph);
this->dataPtr->modelPoseRelativeToGraph =
createPoseRelativeToGraph(model, errors);
model.SetPoseRelativeToGraph(this->dataPtr->modelPoseRelativeToGraph);
}
// Load all the lights.
std::vector<sdf::Light> lights;
Errors lightLoadErrors =
loadUniqueRepeated<sdf::Light>(this->dataPtr->sdf, "light", lights);
errors.insert(errors.end(), lightLoadErrors.begin(), lightLoadErrors.end());
if (!lights.empty())
{
if (lights.size() > 1)
{
errors.emplace_back(
ErrorCode::ELEMENT_INCORRECT_TYPE,
"Root object can only contain one light. Using the first one found.");
}
if (std::holds_alternative<std::monostate>(
this->dataPtr->modelLightOrActor))
{
this->dataPtr->modelLightOrActor = std::move(lights.front());
}
else
{
errors.emplace_back(
ErrorCode::ELEMENT_INCORRECT_TYPE,
"Root object can only contain one of model, light or actor, but "
"found more than one type of element. Skipping this light.");
}
}
// Load all the actors.
std::vector<sdf::Actor> actors;
Errors actorLoadErrors =
loadUniqueRepeated<sdf::Actor>(this->dataPtr->sdf, "actor", actors);
errors.insert(errors.end(), actorLoadErrors.begin(), actorLoadErrors.end());
if (!actors.empty())
{
if (actors.size() > 1)
{
errors.emplace_back(
ErrorCode::ELEMENT_INCORRECT_TYPE,
"Root object can only contain one actor. Using the first one found.");
}
if (std::holds_alternative<std::monostate>(
this->dataPtr->modelLightOrActor))
{
this->dataPtr->modelLightOrActor = std::move(actors.front());
}
else
{
errors.emplace_back(
ErrorCode::ELEMENT_INCORRECT_TYPE,
"Root object can only contain one of model, light or actor, but "
"found more than one type of element. Skipping this actor.");
}
}
// Check that Joint parent and child names resolve to valid and
// different frames.
checkJointParentChildNames(this, errors);
return errors;
}
/////////////////////////////////////////////////
std::string Root::Version() const
{
return this->dataPtr->version;
}
/////////////////////////////////////////////////
void Root::SetVersion(const std::string &_version)
{
this->dataPtr->version = _version;
}
/////////////////////////////////////////////////
uint64_t Root::WorldCount() const
{
return this->dataPtr->worlds.size();
}
/////////////////////////////////////////////////
const World *Root::WorldByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->worlds.size())
return &this->dataPtr->worlds[_index];
return nullptr;
}
/////////////////////////////////////////////////
World *Root::WorldByIndex(const uint64_t _index)
{
if (_index < this->dataPtr->worlds.size())
return &this->dataPtr->worlds[_index];
return nullptr;
}
/////////////////////////////////////////////////
bool Root::WorldNameExists(const std::string &_name) const
{
for (auto const &w : this->dataPtr->worlds)
{
if (w.Name() == _name)
{
return true;
}
}
return false;
}
/////////////////////////////////////////////////
const Model *Root::Model() const
{
return std::get_if<sdf::Model>(&this->dataPtr->modelLightOrActor);
}
/////////////////////////////////////////////////
const Light *Root::Light() const
{
return std::get_if<sdf::Light>(&this->dataPtr->modelLightOrActor);
}
/////////////////////////////////////////////////
const Actor *Root::Actor() const
{
return std::get_if<sdf::Actor>(&this->dataPtr->modelLightOrActor);
}
/////////////////////////////////////////////////
sdf::ElementPtr Root::Element() const
{
return this->dataPtr->sdf;
}
/////////////////////////////////////////////////
bool Root::AddWorld(const World &_world)
{
if (!this->WorldNameExists(_world.Name()))
{
this->dataPtr->worlds.push_back(_world);
return true;
}
return false;
}
/////////////////////////////////////////////////
void Root::ClearWorlds()
{
this->dataPtr->worlds.clear();
}