-
Notifications
You must be signed in to change notification settings - Fork 270
/
Util.cc
762 lines (668 loc) · 19.9 KB
/
Util.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
/*
* 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.
*
*/
#ifndef __APPLE__
#if (defined(_MSVC_LANG))
#if (_MSVC_LANG >= 201703L || __cplusplus >= 201703L)
#include <filesystem> // c++17
#else
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
#endif
#elif __GNUC__ < 8
#include <experimental/filesystem>
#else
#include <filesystem>
#endif
#endif
#include <gz/common/Filesystem.hh>
#include <gz/common/StringUtils.hh>
#include <gz/common/Util.hh>
#include <gz/transport/TopicUtils.hh>
#include <sdf/Types.hh>
#include <gz/fuel_tools/Interface.hh>
#include <gz/fuel_tools/ClientConfig.hh>
#include "gz/sim/components/Actor.hh"
#include "gz/sim/components/Collision.hh"
#include "gz/sim/components/Joint.hh"
#include "gz/sim/components/Light.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/ParticleEmitter.hh"
#include "gz/sim/components/Pose.hh"
#include "gz/sim/components/Sensor.hh"
#include "gz/sim/components/SphericalCoordinates.hh"
#include "gz/sim/components/Visual.hh"
#include "gz/sim/components/World.hh"
#include "gz/sim/Util.hh"
namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
//////////////////////////////////////////////////
math::Pose3d worldPose(const Entity &_entity,
const EntityComponentManager &_ecm)
{
auto poseComp = _ecm.Component<components::Pose>(_entity);
if (nullptr == poseComp)
{
ignwarn << "Trying to get world pose from entity [" << _entity
<< "], which doesn't have a pose component" << std::endl;
return math::Pose3d();
}
// work out pose in world frame
math::Pose3d pose = poseComp->Data();
auto p = _ecm.Component<components::ParentEntity>(_entity);
while (p)
{
// get pose of parent entity
auto parentPose = _ecm.Component<components::Pose>(p->Data());
if (!parentPose)
break;
// transform pose
pose = parentPose->Data() * pose;
// keep going up the tree
p = _ecm.Component<components::ParentEntity>(p->Data());
}
return pose;
}
//////////////////////////////////////////////////
std::string scopedName(const Entity &_entity,
const EntityComponentManager &_ecm, const std::string &_delim,
bool _includePrefix)
{
std::string result;
auto entity = _entity;
while (true)
{
// Get entity name
auto nameComp = _ecm.Component<components::Name>(entity);
if (nullptr == nameComp)
break;
auto name = nameComp->Data();
// Get entity type
std::string prefix = entityTypeStr(entity, _ecm);
if (prefix.empty())
{
ignwarn << "Skipping entity [" << name
<< "] when generating scoped name, entity type not known."
<< std::endl;
}
auto parentComp = _ecm.Component<components::ParentEntity>(entity);
if (!prefix.empty())
{
result.insert(0, name);
if (_includePrefix)
{
result.insert(0, _delim);
result.insert(0, prefix);
}
}
if (nullptr == parentComp)
break;
if (!prefix.empty())
result.insert(0, _delim);
entity = parentComp->Data();
}
return result;
}
//////////////////////////////////////////////////
std::unordered_set<Entity> entitiesFromScopedName(
const std::string &_scopedName, const EntityComponentManager &_ecm,
Entity _relativeTo, const std::string &_delim)
{
if (_delim.empty())
{
ignwarn << "Can't process scoped name [" << _scopedName
<< "] with empty delimiter." << std::endl;
return {};
}
// Split names
std::vector<std::string> names;
size_t pos1 = 0;
size_t pos2 = _scopedName.find(_delim);
while (pos2 != std::string::npos)
{
names.push_back(_scopedName.substr(pos1, pos2 - pos1));
pos1 = pos2 + _delim.length();
pos2 = _scopedName.find(_delim, pos1);
}
names.push_back(_scopedName.substr(pos1, _scopedName.size()-pos1));
// Holds current entities that match and is updated for each name
std::vector<Entity> resVector;
// If there's an entity we're relative to, treat it as the first level result
if (_relativeTo != kNullEntity)
{
resVector = {_relativeTo};
}
for (const auto &name : names)
{
std::vector<Entity> current;
if (resVector.empty())
{
current = _ecm.EntitiesByComponents(components::Name(name));
}
else
{
for (auto res : resVector)
{
auto matches = _ecm.EntitiesByComponents(components::Name(name),
components::ParentEntity(res));
std::copy(std::begin(matches), std::end(matches),
std::back_inserter(current));
}
}
if (current.empty())
return {};
resVector = current;
}
return std::unordered_set<Entity>(resVector.begin(), resVector.end());
}
//////////////////////////////////////////////////
ComponentTypeId entityTypeId(const Entity &_entity,
const EntityComponentManager &_ecm)
{
ComponentTypeId type{kComponentTypeIdInvalid};
if (_ecm.Component<components::World>(_entity))
{
type = components::World::typeId;
}
else if (_ecm.Component<components::Model>(_entity))
{
type = components::Model::typeId;
}
else if (_ecm.Component<components::Light>(_entity))
{
type = components::Light::typeId;
}
else if (_ecm.Component<components::Link>(_entity))
{
type = components::Link::typeId;
}
else if (_ecm.Component<components::Collision>(_entity))
{
type = components::Collision::typeId;
}
else if (_ecm.Component<components::Visual>(_entity))
{
type = components::Visual::typeId;
}
else if (_ecm.Component<components::Joint>(_entity))
{
type = components::Joint::typeId;
}
else if (_ecm.Component<components::Sensor>(_entity))
{
type = components::Sensor::typeId;
}
else if (_ecm.Component<components::Actor>(_entity))
{
type = components::Actor::typeId;
}
else if (_ecm.Component<components::ParticleEmitter>(_entity))
{
type = components::ParticleEmitter::typeId;
}
return type;
}
//////////////////////////////////////////////////
std::string entityTypeStr(const Entity &_entity,
const EntityComponentManager &_ecm)
{
std::string type;
if (_ecm.Component<components::World>(_entity))
{
type = "world";
}
else if (_ecm.Component<components::Model>(_entity))
{
type = "model";
}
else if (_ecm.Component<components::Light>(_entity))
{
type = "light";
}
else if (_ecm.Component<components::Link>(_entity))
{
type = "link";
}
else if (_ecm.Component<components::Collision>(_entity))
{
type = "collision";
}
else if (_ecm.Component<components::Visual>(_entity))
{
type = "visual";
}
else if (_ecm.Component<components::Joint>(_entity))
{
type = "joint";
}
else if (_ecm.Component<components::Sensor>(_entity))
{
type = "sensor";
}
else if (_ecm.Component<components::Actor>(_entity))
{
type = "actor";
}
else if (_ecm.Component<components::ParticleEmitter>(_entity))
{
type = "particle_emitter";
}
return type;
}
//////////////////////////////////////////////////
Entity worldEntity(const Entity &_entity,
const EntityComponentManager &_ecm)
{
auto entity = _entity;
while (nullptr == _ecm.Component<components::World>(entity))
{
// Keep going up the tree
auto parentComp = _ecm.Component<components::ParentEntity>(entity);
if (!parentComp)
{
entity = kNullEntity;
break;
}
entity = parentComp->Data();
}
return entity;
}
//////////////////////////////////////////////////
Entity worldEntity(const EntityComponentManager &_ecm)
{
return _ecm.EntityByComponents(components::World());
}
//////////////////////////////////////////////////
std::string removeParentScope(const std::string &_name,
const std::string &_delim)
{
auto sepPos = _name.find(_delim);
if (sepPos == std::string::npos || (sepPos + _delim.size()) > _name.size())
return _name;
return _name.substr(sepPos + _delim.size());
}
//////////////////////////////////////////////////
std::string asFullPath(const std::string &_uri, const std::string &_filePath)
{
// No path, return unmodified
if (_filePath.empty())
{
return _uri;
}
#ifdef __APPLE__
const std::string absPrefix = "/";
// Not a relative path, return unmodified
if (_uri.find("://") != std::string::npos ||
_uri.compare(0, absPrefix.size(), absPrefix) == 0)
{
return _uri;
}
#else
// Not a relative path, return unmodified
#if (defined(_MSVC_LANG))
#if (_MSVC_LANG >= 201703L || __cplusplus >= 201703L)
using namespace std::filesystem;
#else
using namespace std::experimental::filesystem;
#endif
#elif __GNUC__ < 8
using namespace std::experimental::filesystem;
#else
using namespace std::filesystem;
#endif
if (_uri.find("://") != std::string::npos ||
!path(_uri).is_relative())
{
return _uri;
}
#endif
// When SDF is loaded from a string instead of a file
if (std::string(sdf::kSdfStringSource) == _filePath)
{
ignwarn << "Can't resolve full path for relative path ["
<< _uri << "]. Loaded from a data-string." << std::endl;
return _uri;
}
// Remove file name from path
auto path = common::parentPath(_filePath);
auto uri = _uri;
// If path is URI, use "/" separator for all platforms
if (path.find("://") != std::string::npos)
{
std::replace(uri.begin(), uri.end(), '\\', '/');
return path + "/" + uri;
}
// In case relative path doesn't match platform
#ifdef _WIN32
std::replace(uri.begin(), uri.end(), '/', '\\');
#else
std::replace(uri.begin(), uri.end(), '\\', '/');
#endif
// Use platform-specific separator
return common::joinPaths(path, uri);
}
namespace
{
//////////////////////////////////////////////////
/// \brief Helper function to extract paths form an environment variable
/// refactored from `resourcePaths` below.
/// common::SystemPaths::PathsFromEnv is available, but it's behavior is
/// slightly different from this in that it adds trailing `/` to the end of a
/// path if it doesn't have it already.
std::vector<std::string> extractPathsFromEnv(const std::string &_envVar)
{
std::vector<std::string> pathsFromEnv;
char *pathFromEnvCStr = std::getenv(_envVar.c_str());
if (pathFromEnvCStr && *pathFromEnvCStr != '\0')
{
pathsFromEnv =
common::Split(pathFromEnvCStr, common::SystemPaths::Delimiter());
}
return pathsFromEnv;
}
} // namespace
//////////////////////////////////////////////////
std::vector<std::string> resourcePaths()
{
auto gzPaths = extractPathsFromEnv(kResourcePathEnv);
const auto gzSimResourcePaths = extractPathsFromEnv(kResourcePathEnvGzSim);
gzPaths.insert(gzPaths.end(), gzSimResourcePaths.begin(),
gzSimResourcePaths.end());
gzPaths.erase(std::remove_if(gzPaths.begin(), gzPaths.end(),
[](const std::string &_path)
{
return _path.empty();
}), gzPaths.end());
return gzPaths;
}
//////////////////////////////////////////////////
void addResourcePaths(const std::vector<std::string> &_paths)
{
// SDF paths (for <include>s)
std::vector<std::string> sdfPaths;
char *sdfPathCStr = std::getenv(kSdfPathEnv.c_str());
if (sdfPathCStr && *sdfPathCStr != '\0')
{
sdfPaths = common::Split(sdfPathCStr, common::SystemPaths::Delimiter());
}
// Ignition file paths (for <uri>s)
auto systemPaths = common::systemPaths();
std::vector<std::string> ignPaths;
char *ignPathCStr = std::getenv(systemPaths->FilePathEnv().c_str());
if (ignPathCStr && *ignPathCStr != '\0')
{
ignPaths = common::Split(ignPathCStr, common::SystemPaths::Delimiter());
}
// Gazebo resource paths
auto gzPaths = extractPathsFromEnv(kResourcePathEnv);
auto addUniquePaths = [](std::vector<std::string> &_container,
const std::vector<std::string> _pathsToAdd)
{
for (const auto &path : _pathsToAdd)
{
if (std::find(_container.begin(), _container.end(), path) ==
_container.end())
{
_container.push_back(path);
}
}
};
// Add new paths to gzPaths
addUniquePaths(gzPaths, _paths);
// Append Gz paths to SDF / Ign paths
addUniquePaths(sdfPaths, gzPaths);
addUniquePaths(ignPaths, gzPaths);
// Also append paths from GZ_SIM_RESOURCE_PATH
const auto gzSimResourcePaths = extractPathsFromEnv(kResourcePathEnvGzSim);
addUniquePaths(sdfPaths, gzSimResourcePaths);
addUniquePaths(ignPaths, gzSimResourcePaths);
// Update the vars
std::string sdfPathsStr;
for (const auto &path : sdfPaths)
sdfPathsStr += common::SystemPaths::Delimiter() + path;
common::setenv(kSdfPathEnv.c_str(), sdfPathsStr.c_str());
std::string ignPathsStr;
for (const auto &path : ignPaths)
ignPathsStr += common::SystemPaths::Delimiter() + path;
common::setenv(
systemPaths->FilePathEnv().c_str(), ignPathsStr.c_str());
std::string gzPathsStr;
for (const auto &path : gzPaths)
gzPathsStr += common::SystemPaths::Delimiter() + path;
common::setenv(kResourcePathEnv.c_str(), gzPathsStr.c_str());
// Force re-evaluation
// SDF is evaluated at find call
systemPaths->SetFilePathEnv(systemPaths->FilePathEnv());
}
//////////////////////////////////////////////////
gz::sim::Entity topLevelModel(const Entity &_entity,
const EntityComponentManager &_ecm)
{
auto entity = _entity;
// search up the entity tree and find the model with no parent models
// (there is the possibility of nested models)
Entity modelEntity = kNullEntity;
while (entity)
{
if (_ecm.Component<components::Model>(entity))
modelEntity = entity;
// stop searching if we are at the root of the tree
auto parentComp = _ecm.Component<components::ParentEntity>(entity);
if (!parentComp)
break;
entity = parentComp->Data();
}
return modelEntity;
}
//////////////////////////////////////////////////
std::string topicFromScopedName(const Entity &_entity,
const EntityComponentManager &_ecm, bool _excludeWorld)
{
std::string topic = scopedName(_entity, _ecm, "/", true);
if (_excludeWorld)
{
// Exclude the world name. If the entity is a world, then return an
// empty string.
topic = _ecm.Component<components::World>(_entity) ? "" :
removeParentScope(removeParentScope(topic, "/"), "/");
}
return transport::TopicUtils::AsValidTopic("/" + topic);
}
//////////////////////////////////////////////////
std::string validTopic(const std::vector<std::string> &_topics)
{
for (const auto &topic : _topics)
{
auto validTopic = transport::TopicUtils::AsValidTopic(topic);
if (validTopic.empty())
{
ignerr << "Topic [" << topic << "] is invalid, ignoring." << std::endl;
continue;
}
if (validTopic != topic)
{
igndbg << "Topic [" << topic << "] changed to valid topic ["
<< validTopic << "]" << std::endl;
}
return validTopic;
}
return std::string();
}
//////////////////////////////////////////////////
Entity entityFromMsg(const EntityComponentManager &_ecm,
const msgs::Entity &_msg)
{
if (_msg.id() != kNullEntity)
{
return _msg.id();
}
// If there's no ID, check name + type
if (_msg.type() == msgs::Entity::NONE)
{
return kNullEntity;
}
auto entities = entitiesFromScopedName(_msg.name(), _ecm);
if (entities.empty())
{
return kNullEntity;
}
for (const auto &entity : entities)
{
if (_msg.type() == msgs::Entity::LIGHT &&
_ecm.Component<components::Light>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::MODEL &&
_ecm.Component<components::Model>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::LINK &&
_ecm.Component<components::Link>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::VISUAL &&
_ecm.Component<components::Visual>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::COLLISION &&
_ecm.Component<components::Collision>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::SENSOR &&
_ecm.Component<components::Sensor>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::JOINT &&
_ecm.Component<components::Joint>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::ACTOR &&
_ecm.Component<components::Actor>(entity))
{
return entity;
}
if (_msg.type() == msgs::Entity::WORLD &&
_ecm.Component<components::World>(entity))
{
return entity;
}
}
return kNullEntity;
}
//////////////////////////////////////////////////
std::optional<math::Vector3d> sphericalCoordinates(Entity _entity,
const EntityComponentManager &_ecm)
{
auto sphericalCoordinatesComp =
_ecm.Component<components::SphericalCoordinates>(
worldEntity(_entity, _ecm));
if (nullptr == sphericalCoordinatesComp)
{
return std::nullopt;
}
auto xyzPose = worldPose(_entity, _ecm);
// lat / lon / elevation in rad / rad / m
auto rad = sphericalCoordinatesComp->Data().PositionTransform(
xyzPose.Pos(),
math::SphericalCoordinates::LOCAL2,
math::SphericalCoordinates::SPHERICAL);
// Return degrees
return math::Vector3d(IGN_RTOD(rad.X()), IGN_RTOD(rad.Y()), rad.Z());
}
//////////////////////////////////////////////////
// Getting the first .sdf file in the path
std::string findFuelResourceSdf(const std::string &_path)
{
if (!common::exists(_path))
return "";
for (common::DirIter file(_path); file != common::DirIter(); ++file)
{
std::string current(*file);
if (!common::isFile(current))
continue;
auto fileName = common::basename(current);
auto fileExtensionIndex = fileName.rfind(".");
auto fileExtension = fileName.substr(fileExtensionIndex + 1);
if (fileExtension == "sdf")
{
return current;
}
}
return "";
}
//////////////////////////////////////////////////
std::string resolveSdfWorldFile(const std::string &_sdfFile,
const std::string &_fuelResourceCache)
{
std::string filePath;
// Check Fuel if it's a URL
auto sdfUri = common::URI(_sdfFile);
if (sdfUri.Scheme() == "http" || sdfUri.Scheme() == "https")
{
fuel_tools::ClientConfig config;
if (!_fuelResourceCache.empty())
config.SetCacheLocation(_fuelResourceCache);
fuel_tools::FuelClient fuelClient(config);
std::string fuelCachePath;
if (fuelClient.CachedWorld(common::URI(_sdfFile), fuelCachePath))
{
filePath = findFuelResourceSdf(fuelCachePath);
}
else if (auto result = fuelClient.DownloadWorld(
common::URI(_sdfFile), fuelCachePath))
{
filePath = findFuelResourceSdf(fuelCachePath);
}
else
{
ignwarn << "Fuel couldn't download URL [" << _sdfFile
<< "], error: [" << result.ReadableResult() << "]"
<< std::endl;
}
}
if (filePath.empty())
{
common::SystemPaths systemPaths;
// Worlds from environment variable
systemPaths.SetFilePathEnv(kResourcePathEnv);
// Also add paths from GZ_SIM_RESOURCE_PATH
for (const auto &path : extractPathsFromEnv(kResourcePathEnvGzSim))
{
systemPaths.AddFilePaths(path);
}
// Worlds installed with ign-gazebo
systemPaths.AddFilePaths(IGN_GAZEBO_WORLD_INSTALL_DIR);
filePath = systemPaths.FindFile(_sdfFile);
}
return filePath;
}
}
}
}