Skip to content

Commit

Permalink
Merge from sdf13
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Mar 7, 2024
2 parents f360776 + 2246db8 commit ff9b3ad
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 19 deletions.
6 changes: 5 additions & 1 deletion include/sdf/ParserConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ class SDFORMAT_VISIBLE ParserConfig
/// and merge the child link into the parent.
public: bool URDFPreserveFixedJoint() const;

/// \brief Set the storeResolvedURIs flag value.
/// \sa SetStoreResolvedURIs
public: void SetStoreResovledURIs(bool _resolveURI);

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

/// \brief Get the storeResolvedURIs flag value.
/// \return True if the parser will attempt to resolve any URIs found and
Expand Down
25 changes: 22 additions & 3 deletions src/Heightmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

#include <filesystem>
#include <vector>

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

if (_sdf->HasElement("diffuse"))
{
std::unordered_set<std::string> paths;
if (!this->dataPtr->sdf->FilePath().empty())
{
paths.insert(std::filesystem::path(
this->dataPtr->sdf->FilePath()).parent_path().string());
}
this->dataPtr->diffuse = resolveURI(
_sdf->Get<std::string>(errors, "diffuse", this->dataPtr->diffuse).first,
_config, errors);
_config, errors, paths);
}
else
{
Expand All @@ -145,9 +152,15 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("normal"))
{
std::unordered_set<std::string> paths;
if (!this->dataPtr->sdf->FilePath().empty())
{
paths.insert(std::filesystem::path(
this->dataPtr->sdf->FilePath()).parent_path().string());
}
this->dataPtr->normal = resolveURI(
_sdf->Get<std::string>(errors, "normal", this->dataPtr->normal).first,
_config, errors);
_config, errors, paths);
}
else
{
Expand Down Expand Up @@ -326,9 +339,15 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("uri"))
{
std::unordered_set<std::string> paths;
if (!this->dataPtr->filePath.empty())
{
paths.insert(std::filesystem::path(
this->dataPtr->filePath).parent_path().string());
}
this->dataPtr->uri = resolveURI(
_sdf->Get<std::string>(errors, "uri", "").first,
_config, errors);
_config, errors, paths);
}
else
{
Expand Down
13 changes: 11 additions & 2 deletions src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*
*/
#include <string>

#include <filesystem>
#include <optional>
#include <string>
#include <vector>
#include <gz/math/Vector3.hh>

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

this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors);
std::unordered_set<std::string> paths;
if (!this->dataPtr->filePath.empty())
{
paths.insert(std::filesystem::path(
this->dataPtr->filePath).parent_path().string());
}
this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors,
paths);

std::pair<std::string, bool> namePair =
elem->Get<std::string>(errors, "name", "");
Expand Down
10 changes: 8 additions & 2 deletions src/Mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*
*/

#include <filesystem>
#include <optional>

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

if (_sdf->HasElement("uri"))
{
std::unordered_set<std::string> paths;
if (!this->dataPtr->filePath.empty())
{
paths.insert(std::filesystem::path(
this->dataPtr->filePath).parent_path().string());
}
this->dataPtr->uri = resolveURI(
_sdf->Get<std::string>(errors, "uri", "").first,
_config, errors);
_config, errors, paths);
}
else
{
Expand Down
7 changes: 6 additions & 1 deletion src/ParserConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ bool ParserConfig::URDFPreserveFixedJoint() const

/////////////////////////////////////////////////
void ParserConfig::SetStoreResovledURIs(bool _resolveURI)
{
this->SetStoreResolvedURIs(_resolveURI);
}

Check warning on line 225 in src/ParserConfig.cc

View check run for this annotation

Codecov / codecov/patch

src/ParserConfig.cc#L224-L225

Added lines #L224 - L225 were not covered by tests

/////////////////////////////////////////////////
void ParserConfig::SetStoreResolvedURIs(bool _resolveURI)
{
this->dataPtr->storeResolvedURIs = _resolveURI;
}
Expand All @@ -229,4 +235,3 @@ bool ParserConfig::StoreResolvedURIs() const
{
return this->dataPtr->storeResolvedURIs;
}

10 changes: 9 additions & 1 deletion src/Sky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*
*/
#include <filesystem>

#include "sdf/parser.hh"
#include "sdf/Sky.hh"
#include "Utils.hh"
Expand Down Expand Up @@ -201,9 +203,15 @@ Errors Sky::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("cubemap_uri"))
{
std::unordered_set<std::string> paths;
if (!_sdf->FilePath().empty())
{
paths.insert(std::filesystem::path(
_sdf->FilePath()).parent_path().string());
}
this->dataPtr->cubemapUri = resolveURI(
_sdf->Get<std::string>(errors, "cubemap_uri", "").first,
_config, errors);
_config, errors, paths);
}

if ( _sdf->HasElement("clouds"))
Expand Down
21 changes: 20 additions & 1 deletion src/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*
*/
#include <filesystem>
#include <limits>
#include <string>
#include <utility>
#include "sdf/Assert.hh"
#include "sdf/Filesystem.hh"
#include "sdf/SDFImpl.hh"
#include "Utils.hh"

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

/////////////////////////////////////////////////
std::string resolveURI(const std::string &_inputURI,
const sdf::ParserConfig &_config, sdf::Errors &_errors)
const sdf::ParserConfig &_config, sdf::Errors &_errors,
const std::unordered_set<std::string> &_searchPaths)
{
std::string resolvedURI = _inputURI;
if (_config.StoreResolvedURIs())
{
std::string sep("://");
if (!_searchPaths.empty() &&
_inputURI.find(sep) == std::string::npos &&
!std::filesystem::path(_inputURI).is_absolute())
{
for (const auto &sp : _searchPaths)
{
// find file by searching local path but do not use callbacks
std::string fullPath = sdf::filesystem::append(sp, _inputURI);
resolvedURI = sdf::findFile(fullPath, true, false);
if (!resolvedURI.empty())
return resolvedURI;
}
}

// find file by searching local path and use registered callbacks
resolvedURI = sdf::findFile(_inputURI, true, true, _config);
if (resolvedURI.empty())
{
Expand Down
7 changes: 6 additions & 1 deletion src/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>
#include <vector>
#include <tinyxml2.h>
#include <unordered_set>
#include "sdf/Error.hh"
#include "sdf/Element.hh"
#include "sdf/InterfaceElements.hh"
Expand Down Expand Up @@ -262,10 +263,14 @@ namespace sdf
/// \param[in] _inputURI URI from parsed SDF file to resolve
/// \param[in] _config Parser configuration to use to resolve
/// \param[in, out] _errors Error vector to append to if resolution fails
/// \param[in] _searchPaths Optional additional search paths (directory)
/// when resolving URI
/// \return Resolved URI or Original URI, depending on parser configuration
std::string resolveURI(const std::string &_inputURI,
const sdf::ParserConfig &_config,
sdf::Errors &_errors);
sdf::Errors &_errors,
const std::unordered_set<std::string>
&_searchPaths = {});
}
}
#endif
8 changes: 4 additions & 4 deletions test/integration/model/double_pendulum.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
<name>Gazebo/Green</name>
</script>
</material>
</visual>
Expand All @@ -28,7 +28,7 @@
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
<name>Gazebo/Blue</name>
</script>
</material>
</visual>
Expand Down Expand Up @@ -96,7 +96,7 @@
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
<name>Gazebo/TransparentGreen</name>
</script>
</material>
</visual>
Expand Down Expand Up @@ -146,7 +146,7 @@
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
<name>Gazebo/Red</name>
</script>
</material>
</visual>
Expand Down
Loading

0 comments on commit ff9b3ad

Please sign in to comment.