Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Apr 30, 2020
1 parent a134e53 commit 3829a40
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 271 deletions.
19 changes: 12 additions & 7 deletions include/sdf/parser_urdf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ namespace sdf
public: ~URDF2SDF();

/// \brief convert urdf xml document string to sdf xml document
/// \param[in] _xmlDoc a tinyxml document containing the urdf model
/// \return a tinyxml document containing sdf of the model
public: tinyxml2::XMLDocument InitModelDoc(tinyxml2::XMLDocument* _xmlDoc);
/// \param[in] _xmlDoc document containing the urdf model.
/// \param[inout] _sdfXmlDoc document to populate with the sdf model.
public: void InitModelDoc(const tinyxml2::XMLDocument* _xmlDoc,
tinyxml2::XMLDocument *_sdfXmlDoc);

/// \brief convert urdf file to sdf xml document
/// \param[in] _urdfStr a string containing filename of the urdf model
/// \param[in] _urdfStr a string containing filename of the urdf model.
/// \param[inout] _sdfXmlDoc document to populate with the sdf model.
/// \return a tinyxml document containing sdf of the model
public: tinyxml2::XMLDocument InitModelFile(const std::string &_filename);
public: void InitModelFile(const std::string &_filename,
tinyxml2::XMLDocument *_sdfXmlDoc);

/// \brief convert urdf string to sdf xml document, with option to enforce
/// limits.
/// \param[in] _urdfStr a string containing model urdf
/// \param[inout] _sdfXmlDoc document to populate with the sdf model.
/// \param[in] _enforceLimits option to enforce joint limits
/// \return a tinyxml document containing sdf of the model
public: tinyxml2::XMLDocument InitModelString(const std::string &_urdfStr,
bool _enforceLimits = true);
public: void InitModelString(const std::string &_urdfStr,
tinyxml2::XMLDocument *_sdfXmlDoc,
bool _enforceLimits = true);

/// \brief Return true if the filename is a URDF model.
/// \param[in] _filename File to check.
Expand Down
8 changes: 5 additions & 3 deletions src/SDFExtension.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace sdf
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
using XMLDocumentPtr= std::shared_ptr<tinyxml2::XMLDocument>;
using XMLElementPtr = std::shared_ptr<tinyxml2::XMLElement>;

/// \internal
/// \brief A class for holding sdf extension elements in urdf
Expand All @@ -57,7 +59,7 @@ namespace sdf
public: std::string material;

/// \brief blobs of xml to be copied into the visual sdf element
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > visual_blobs;
public: std::vector<XMLDocumentPtr> visual_blobs;

/// \brief blobs of xml to be copied into the collision sdf element
/// An example might be:
Expand All @@ -84,7 +86,7 @@ namespace sdf
/// </gazebo>
/// where all the contents of `<collision>` element is copied into the
/// resulting collision sdf.
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > collision_blobs;
public: std::vector<XMLDocumentPtr> collision_blobs;

// body, default off
public: bool setStaticFlag;
Expand Down Expand Up @@ -118,7 +120,7 @@ namespace sdf
public: bool implicitSpringDamper;

// blobs into body or robot
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > blobs;
public: std::vector<XMLDocumentPtr> blobs;

SDF_SUPPRESS_DEPRECATED_BEGIN
friend class URDF2SDF;
Expand Down
20 changes: 20 additions & 0 deletions src/XmlUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ tinyxml2::XMLNode* DeepClone(tinyxml2::XMLDocument *_doc, const tinyxml2::XMLNod
return copy;
}

/////////////////////////////////////////////////
std::string &TrimStringLeft(std::string &_s)
{
_s.erase(_s.begin(),find_if_not(_s.begin(),_s.end(),[](int c){return isspace(c);}));
return _s;
}

/////////////////////////////////////////////////
std::string &TrimStringRight(std::string &_s)
{
_s.erase(find_if_not(_s.rbegin(),_s.rend(),[](int c){return isspace(c);}).base(), _s.end());
return _s;
}

/////////////////////////////////////////////////
std::string TrimString(const std::string &_s)
{
std::string t = _s;
return TrimStringLeft(TrimStringRight(t));
}
}
}

5 changes: 5 additions & 0 deletions src/XmlUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace sdf
/// \param[in] _src The node to deep copy
/// \returns The newly copied node
tinyxml2::XMLNode* DeepClone(tinyxml2::XMLDocument *_doc, const tinyxml2::XMLNode *_src);

/// \brief Trim whitespace from a string
/// \param[in] _s String to trim
/// \returns String _s with whitespace trimmed from both ends
std::string TrimString(const std::string &_s);
}
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ bool readStringInternal(const std::string &_xmlString, SDFPtr _sdf,
SDF_SUPPRESS_DEPRECATED_BEGIN
URDF2SDF u2g;
SDF_SUPPRESS_DEPRECATED_END
tinyxml2::XMLDocument doc = u2g.InitModelString(_xmlString);
tinyxml2::XMLDocument doc;
u2g.InitModelString(&doc, _xmlString);

if (sdf::readDoc(&doc, _sdf, "urdf string", _convert, _errors))
{
sdfdbg << "Parsing from urdf.\n";
Expand Down
Loading

0 comments on commit 3829a40

Please sign in to comment.