Skip to content

Commit 62e40ca

Browse files
authored
Merge pull request #1379 from gazebosim/merge_13_14_20240306
Merge 13 -> 14
2 parents f360776 + ff9b3ad commit 62e40ca

File tree

14 files changed

+297
-19
lines changed

14 files changed

+297
-19
lines changed

include/sdf/ParserConfig.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class SDFORMAT_VISIBLE ParserConfig
215215
/// and merge the child link into the parent.
216216
public: bool URDFPreserveFixedJoint() const;
217217

218+
/// \brief Set the storeResolvedURIs flag value.
219+
/// \sa SetStoreResolvedURIs
220+
public: void SetStoreResovledURIs(bool _resolveURI);
221+
218222
/// \brief Set the storeResolvedURIs flag value.
219223
/// \param[in] _resolveURI True to make the parser attempt to resolve any
220224
/// URIs found and store them. False to preserve original URIs
@@ -224,7 +228,7 @@ class SDFORMAT_VISIBLE ParserConfig
224228
/// If the FindFileCallback provides a non-empty string, the URI will be
225229
/// stored in the DOM object, and the original (unresolved) URI will be
226230
/// stored in the underlying Element.
227-
public: void SetStoreResovledURIs(bool _resolveURI);
231+
public: void SetStoreResolvedURIs(bool _resolveURI);
228232

229233
/// \brief Get the storeResolvedURIs flag value.
230234
/// \return True if the parser will attempt to resolve any URIs found and

src/Heightmap.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717

18+
#include <filesystem>
1819
#include <vector>
1920

2021
#include "Utils.hh"
@@ -133,9 +134,15 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config)
133134

134135
if (_sdf->HasElement("diffuse"))
135136
{
137+
std::unordered_set<std::string> paths;
138+
if (!this->dataPtr->sdf->FilePath().empty())
139+
{
140+
paths.insert(std::filesystem::path(
141+
this->dataPtr->sdf->FilePath()).parent_path().string());
142+
}
136143
this->dataPtr->diffuse = resolveURI(
137144
_sdf->Get<std::string>(errors, "diffuse", this->dataPtr->diffuse).first,
138-
_config, errors);
145+
_config, errors, paths);
139146
}
140147
else
141148
{
@@ -145,9 +152,15 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config)
145152

146153
if (_sdf->HasElement("normal"))
147154
{
155+
std::unordered_set<std::string> paths;
156+
if (!this->dataPtr->sdf->FilePath().empty())
157+
{
158+
paths.insert(std::filesystem::path(
159+
this->dataPtr->sdf->FilePath()).parent_path().string());
160+
}
148161
this->dataPtr->normal = resolveURI(
149162
_sdf->Get<std::string>(errors, "normal", this->dataPtr->normal).first,
150-
_config, errors);
163+
_config, errors, paths);
151164
}
152165
else
153166
{
@@ -326,9 +339,15 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config)
326339

327340
if (_sdf->HasElement("uri"))
328341
{
342+
std::unordered_set<std::string> paths;
343+
if (!this->dataPtr->filePath.empty())
344+
{
345+
paths.insert(std::filesystem::path(
346+
this->dataPtr->filePath).parent_path().string());
347+
}
329348
this->dataPtr->uri = resolveURI(
330349
_sdf->Get<std::string>(errors, "uri", "").first,
331-
_config, errors);
350+
_config, errors, paths);
332351
}
333352
else
334353
{

src/Material.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
#include <string>
17+
18+
#include <filesystem>
1819
#include <optional>
20+
#include <string>
1921
#include <vector>
2022
#include <gz/math/Vector3.hh>
2123

@@ -122,7 +124,14 @@ Errors Material::Load(sdf::ElementPtr _sdf, const sdf::ParserConfig &_config)
122124
"<uri> element is empty."});
123125
}
124126

125-
this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors);
127+
std::unordered_set<std::string> paths;
128+
if (!this->dataPtr->filePath.empty())
129+
{
130+
paths.insert(std::filesystem::path(
131+
this->dataPtr->filePath).parent_path().string());
132+
}
133+
this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors,
134+
paths);
126135

127136
std::pair<std::string, bool> namePair =
128137
elem->Get<std::string>(errors, "name", "");

src/Mesh.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
17+
#include <filesystem>
1818
#include <optional>
1919

2020
#include <gz/math/Inertial.hh>
@@ -89,9 +89,15 @@ Errors Mesh::Load(ElementPtr _sdf, const ParserConfig &_config)
8989

9090
if (_sdf->HasElement("uri"))
9191
{
92+
std::unordered_set<std::string> paths;
93+
if (!this->dataPtr->filePath.empty())
94+
{
95+
paths.insert(std::filesystem::path(
96+
this->dataPtr->filePath).parent_path().string());
97+
}
9298
this->dataPtr->uri = resolveURI(
9399
_sdf->Get<std::string>(errors, "uri", "").first,
94-
_config, errors);
100+
_config, errors, paths);
95101
}
96102
else
97103
{

src/ParserConfig.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ bool ParserConfig::URDFPreserveFixedJoint() const
220220

221221
/////////////////////////////////////////////////
222222
void ParserConfig::SetStoreResovledURIs(bool _resolveURI)
223+
{
224+
this->SetStoreResolvedURIs(_resolveURI);
225+
}
226+
227+
/////////////////////////////////////////////////
228+
void ParserConfig::SetStoreResolvedURIs(bool _resolveURI)
223229
{
224230
this->dataPtr->storeResolvedURIs = _resolveURI;
225231
}
@@ -229,4 +235,3 @@ bool ParserConfig::StoreResolvedURIs() const
229235
{
230236
return this->dataPtr->storeResolvedURIs;
231237
}
232-

src/Sky.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*
1616
*/
17+
#include <filesystem>
18+
1719
#include "sdf/parser.hh"
1820
#include "sdf/Sky.hh"
1921
#include "Utils.hh"
@@ -201,9 +203,15 @@ Errors Sky::Load(ElementPtr _sdf, const ParserConfig &_config)
201203

202204
if (_sdf->HasElement("cubemap_uri"))
203205
{
206+
std::unordered_set<std::string> paths;
207+
if (!_sdf->FilePath().empty())
208+
{
209+
paths.insert(std::filesystem::path(
210+
_sdf->FilePath()).parent_path().string());
211+
}
204212
this->dataPtr->cubemapUri = resolveURI(
205213
_sdf->Get<std::string>(errors, "cubemap_uri", "").first,
206-
_config, errors);
214+
_config, errors, paths);
207215
}
208216

209217
if ( _sdf->HasElement("clouds"))

src/Utils.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
* limitations under the License.
1515
*
1616
*/
17+
#include <filesystem>
1718
#include <limits>
1819
#include <string>
1920
#include <utility>
2021
#include "sdf/Assert.hh"
22+
#include "sdf/Filesystem.hh"
2123
#include "sdf/SDFImpl.hh"
2224
#include "Utils.hh"
2325

@@ -382,11 +384,28 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml,
382384

383385
/////////////////////////////////////////////////
384386
std::string resolveURI(const std::string &_inputURI,
385-
const sdf::ParserConfig &_config, sdf::Errors &_errors)
387+
const sdf::ParserConfig &_config, sdf::Errors &_errors,
388+
const std::unordered_set<std::string> &_searchPaths)
386389
{
387390
std::string resolvedURI = _inputURI;
388391
if (_config.StoreResolvedURIs())
389392
{
393+
std::string sep("://");
394+
if (!_searchPaths.empty() &&
395+
_inputURI.find(sep) == std::string::npos &&
396+
!std::filesystem::path(_inputURI).is_absolute())
397+
{
398+
for (const auto &sp : _searchPaths)
399+
{
400+
// find file by searching local path but do not use callbacks
401+
std::string fullPath = sdf::filesystem::append(sp, _inputURI);
402+
resolvedURI = sdf::findFile(fullPath, true, false);
403+
if (!resolvedURI.empty())
404+
return resolvedURI;
405+
}
406+
}
407+
408+
// find file by searching local path and use registered callbacks
390409
resolvedURI = sdf::findFile(_inputURI, true, true, _config);
391410
if (resolvedURI.empty())
392411
{

src/Utils.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <utility>
2424
#include <vector>
2525
#include <tinyxml2.h>
26+
#include <unordered_set>
2627
#include "sdf/Error.hh"
2728
#include "sdf/Element.hh"
2829
#include "sdf/InterfaceElements.hh"
@@ -262,10 +263,14 @@ namespace sdf
262263
/// \param[in] _inputURI URI from parsed SDF file to resolve
263264
/// \param[in] _config Parser configuration to use to resolve
264265
/// \param[in, out] _errors Error vector to append to if resolution fails
266+
/// \param[in] _searchPaths Optional additional search paths (directory)
267+
/// when resolving URI
265268
/// \return Resolved URI or Original URI, depending on parser configuration
266269
std::string resolveURI(const std::string &_inputURI,
267270
const sdf::ParserConfig &_config,
268-
sdf::Errors &_errors);
271+
sdf::Errors &_errors,
272+
const std::unordered_set<std::string>
273+
&_searchPaths = {});
269274
}
270275
}
271276
#endif

test/integration/model/double_pendulum.sdf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<material>
1717
<script>
1818
<uri>file://media/materials/scripts/gazebo.material</uri>
19-
<name>Gazebo/Grey</name>
19+
<name>Gazebo/Green</name>
2020
</script>
2121
</material>
2222
</visual>
@@ -28,7 +28,7 @@
2828
<material>
2929
<script>
3030
<uri>file://media/materials/scripts/gazebo.material</uri>
31-
<name>Gazebo/Grey</name>
31+
<name>Gazebo/Blue</name>
3232
</script>
3333
</material>
3434
</visual>
@@ -96,7 +96,7 @@
9696
<material>
9797
<script>
9898
<uri>file://media/materials/scripts/gazebo.material</uri>
99-
<name>Gazebo/Grey</name>
99+
<name>Gazebo/TransparentGreen</name>
100100
</script>
101101
</material>
102102
</visual>
@@ -146,7 +146,7 @@
146146
<material>
147147
<script>
148148
<uri>file://media/materials/scripts/gazebo.material</uri>
149-
<name>Gazebo/Grey</name>
149+
<name>Gazebo/Red</name>
150150
</script>
151151
</material>
152152
</visual>

0 commit comments

Comments
 (0)