DART 6.10.1
Loading...
Searching...
No Matches
XmlHelpers.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_UTILS_XMLHELPERS_HPP_
34#define DART_UTILS_XMLHELPERS_HPP_
35
36#include <string>
37
38#include <Eigen/Dense>
39#include <boost/algorithm/string.hpp>
40#include <boost/lexical_cast.hpp>
41#include <tinyxml2.h>
42
47
48namespace dart {
49namespace utils {
50
51std::string toString(bool v);
52std::string toString(int v);
53std::string toString(unsigned int v);
54std::string toString(float v);
55std::string toString(double v);
56std::string toString(char v);
57std::string toString(const Eigen::Vector2d& v);
58std::string toString(const Eigen::Vector3d& v);
59std::string toString(const Eigen::Vector3i& v);
60std::string toString(const Eigen::Vector6d& v);
61std::string toString(const Eigen::VectorXd& v);
62std::string toString(const Eigen::Isometry3d& v);
63
64bool toBool(const std::string& str);
65int toInt(const std::string& str);
66unsigned int toUInt(const std::string& str);
67float toFloat(const std::string& str);
68double toDouble(const std::string& str);
69char toChar(const std::string& str);
70Eigen::Vector2d toVector2d(const std::string& str);
71Eigen::Vector2i toVector2i(const std::string& str);
72Eigen::Vector3d toVector3d(const std::string& str);
73Eigen::Vector3i toVector3i(const std::string& str);
74Eigen::Vector4d toVector4d(const std::string& str);
75Eigen::Vector6d toVector6d(const std::string& str);
76Eigen::VectorXd toVectorXd(const std::string& str);
77template <std::size_t N>
78Eigen::Matrix<double, N, 1> toVectorNd(const std::string& str)
79{
80 Eigen::Matrix<double, N, 1> ret = Eigen::Matrix<double, N, 1>::Zero();
81
82 std::vector<std::string> pieces;
83 std::string trimedStr = boost::trim_copy(str);
84 boost::split(
85 pieces, trimedStr, boost::is_any_of(" "), boost::token_compress_on);
86 std::size_t sizeToRead = std::min(N, pieces.size());
87 if (pieces.size() < N)
88 {
89 dterr << "Failed to read a vector because the dimension '" << pieces.size()
90 << "' is less than the expectation '" << N << "'.\n";
91 }
92 else if (pieces.size() > N)
93 {
94 dterr << "Failed to read a vector because the dimension '" << pieces.size()
95 << "' is greater than the expectation '" << N << "'.\n";
96 }
97
98 for (std::size_t i = 0; i < sizeToRead; ++i)
99 {
100 if (pieces[i] != "")
101 {
102 try
103 {
104 ret(i) = boost::lexical_cast<double>(pieces[i].c_str());
105 }
106 catch (boost::bad_lexical_cast& e)
107 {
108 dterr << "value [" << pieces[i]
109 << "] is not a valid double for Eigen::Vector" << N << "d[" << i
110 << "]: " << e.what() << "\n";
111 }
112 }
113 }
114
115 return ret;
116}
117// TODO: The definition of _str is not clear for transform (see: #250)
118Eigen::Isometry3d toIsometry3d(const std::string& str);
119Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string& str);
120
121std::string getValueString(
122 const tinyxml2::XMLElement* parentElement, const std::string& name);
123bool getValueBool(
124 const tinyxml2::XMLElement* parentElement, const std::string& name);
125int getValueInt(
126 const tinyxml2::XMLElement* parentElement, const std::string& name);
127unsigned int getValueUInt(
128 const tinyxml2::XMLElement* parentElement, const std::string& name);
129float getValueFloat(
130 const tinyxml2::XMLElement* parentElement, const std::string& name);
131double getValueDouble(
132 const tinyxml2::XMLElement* parentElement, const std::string& name);
133char getValueChar(
134 const tinyxml2::XMLElement* parentElement, const std::string& name);
135Eigen::Vector2d getValueVector2d(
136 const tinyxml2::XMLElement* parentElement, const std::string& name);
137Eigen::Vector3d getValueVector3d(
138 const tinyxml2::XMLElement* parentElement, const std::string& name);
139Eigen::Vector3i getValueVector3i(
140 const tinyxml2::XMLElement* parentElement, const std::string& name);
142 const tinyxml2::XMLElement* parentElement, const std::string& name);
143Eigen::VectorXd getValueVectorXd(
144 const tinyxml2::XMLElement* parentElement, const std::string& name);
145Eigen::Isometry3d getValueIsometry3d(
146 const tinyxml2::XMLElement* parentElement, const std::string& name);
148 const tinyxml2::XMLElement* parentElement, const std::string& name);
149
150// TODO(JS): Deprecate
151void openXMLFile(
152 tinyxml2::XMLDocument& doc,
153 const common::Uri& uri,
154 const common::ResourceRetrieverPtr& retriever = nullptr);
155
156bool readXmlFile(
157 tinyxml2::XMLDocument& doc,
158 const common::Uri& uri,
159 const common::ResourceRetrieverPtr& retrieverOrNullPtr = nullptr);
160
161bool hasElement(
162 const tinyxml2::XMLElement* parentElement, const std::string& name);
163
164const tinyxml2::XMLElement* getElement(
165 const tinyxml2::XMLElement* parentElement, const std::string& name);
166
167tinyxml2::XMLElement* getElement(
168 tinyxml2::XMLElement* parentElement, const std::string& name);
169
170bool hasAttribute(const tinyxml2::XMLElement* element, const char* const name);
171
172// Please use getAttributeString() instead.
174std::string getAttribute(tinyxml2::XMLElement* element, const char* const name);
175
176// Please use getAttributeDouble() instead.
178void getAttribute(
179 tinyxml2::XMLElement* element, const char* const name, double* d);
180
181std::string getAttributeString(
182 const tinyxml2::XMLElement* element, const std::string& attributeName);
184 const tinyxml2::XMLElement* element, const std::string& attributeName);
186 const tinyxml2::XMLElement* element, const std::string& attributeName);
187unsigned int getAttributeUInt(
188 const tinyxml2::XMLElement* element, const std::string& attributeName);
190 const tinyxml2::XMLElement* element, const std::string& attributeName);
191double getAttributeDouble(
192 const tinyxml2::XMLElement* element, const std::string& attributeName);
194 const tinyxml2::XMLElement* element, const std::string& attributeName);
196 const tinyxml2::XMLElement* element, const std::string& attributeName);
198 const tinyxml2::XMLElement* element, const std::string& attributeName);
200 const tinyxml2::XMLElement* element, const std::string& attributeName);
202 const tinyxml2::XMLElement* element, const std::string& attributeName);
204 const tinyxml2::XMLElement* element, const std::string& attributeName);
206 const tinyxml2::XMLElement* element, const std::string& attributeName);
207template <std::size_t N>
208Eigen::Matrix<double, N, 1> getAttributeVectorNd(
209 const tinyxml2::XMLElement* element, const std::string& attributeName)
210{
211 const std::string val = getAttributeString(element, attributeName);
212 return toVectorNd<N>(val);
213}
214
218template <typename ElementType>
220{
221protected:
222 using ElementPtr = ElementType*;
223 using ElementRef = ElementType&;
224
225public:
228 ElementPtr parentElement, const std::string& childElementName)
229 : mParentElement(parentElement),
230 mChildElementName(childElementName),
231 mCurrentElement(nullptr)
232 {
233 }
234
239
242 bool next()
243 {
244 if (!mParentElement)
245 return false;
246
247 if (mCurrentElement)
248 {
250 = mCurrentElement->NextSiblingElement(mChildElementName.c_str());
251 }
252 else
253 {
255 = mParentElement->FirstChildElement(mChildElementName.c_str());
256 }
257
258 if (!valid())
259 mParentElement = nullptr;
260
261 return valid();
262 }
263
266 {
267 return mCurrentElement;
268 }
269
272 {
273 return mCurrentElement;
274 }
275
278 {
279 return *mCurrentElement;
280 }
281
284 {
285 // If they point at the same node, then the names must match
286 return (this->mParentElement == rhs.mParentElement)
287 && (this->mCurrentElement == rhs.mCurrentElement)
288 && (this->mCurrentElement != nullptr
289 || (this->mChildElementName == rhs.mChildElementName));
290 }
291
302
303private:
305 bool valid() const
306 {
307 return mCurrentElement != nullptr;
308 }
309
310private:
313
315 std::string mChildElementName;
316
319};
320
321// ElementEnumerator is for iterating elements for
325
326bool copyNode(tinyxml2::XMLNode* destParent, const tinyxml2::XMLNode& src);
327
328bool copyChildNodes(
329 tinyxml2::XMLNode* destParent, const tinyxml2::XMLNode& src);
330
331} // namespace utils
332} // namespace dart
333
334#endif // #ifndef DART_UTILS_XMLHELPERS_HPP_
#define dterr
Output an error message.
Definition Console.hpp:49
#define DART_DEPRECATED(version)
Definition Deprecated.hpp:51
std::string * name
Definition SkelParser.cpp:1697
TemplatedElementEnumerator is a convenience class to help visiting all the child elements of given pa...
Definition XmlHelpers.hpp:220
bool next()
Set the current element to the next sibling element or to the first child element of given parent ele...
Definition XmlHelpers.hpp:242
ElementType & ElementRef
Definition XmlHelpers.hpp:223
ElementPtr get() const
Get the current element.
Definition XmlHelpers.hpp:265
~TemplatedElementEnumerator()
Destructor.
Definition XmlHelpers.hpp:236
ElementPtr operator->() const
Dereference operator.
Definition XmlHelpers.hpp:271
bool operator==(const TemplatedElementEnumerator< ElementType > &rhs) const
Equality operator.
Definition XmlHelpers.hpp:283
ElementPtr mCurrentElement
Currently visiting child element.
Definition XmlHelpers.hpp:318
bool valid() const
Returns true if the current element is valid (not a nullptr)
Definition XmlHelpers.hpp:305
std::string mChildElementName
Child element name.
Definition XmlHelpers.hpp:315
ElementRef operator*() const
Dereference operator.
Definition XmlHelpers.hpp:277
TemplatedElementEnumerator< ElementType > & operator=(const TemplatedElementEnumerator< ElementType > &rhs)
Assignment operator.
Definition XmlHelpers.hpp:293
ElementType * ElementPtr
Definition XmlHelpers.hpp:222
TemplatedElementEnumerator(ElementPtr parentElement, const std::string &childElementName)
Constructor that takes parent element and.
Definition XmlHelpers.hpp:227
ElementPtr mParentElement
Parent element.
Definition XmlHelpers.hpp:312
Definition Random-impl.hpp:92
Matrix< double, 6, 1 > Vector6d
Definition MathTypes.hpp:49
std::shared_ptr< ResourceRetriever > ResourceRetrieverPtr
Definition ResourceRetriever.hpp:76
bool hasElement(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:650
double getAttributeDouble(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:883
bool getValueBool(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:472
unsigned int toUInt(const std::string &str)
Definition XmlHelpers.cpp:151
Eigen::Vector6d getValueVector6d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:590
Eigen::Matrix< double, N, 1 > toVectorNd(const std::string &str)
Definition XmlHelpers.hpp:78
Eigen::Vector4d toVector4d(const std::string &str)
Definition XmlHelpers.cpp:298
Eigen::Isometry3d getValueIsometry3dWithExtrinsicRotation(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:638
bool copyChildNodes(tinyxml2::XMLNode *destParent, const tinyxml2::XMLNode &src)
Definition XmlHelpers.cpp:999
unsigned int getValueUInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:506
Eigen::Isometry3d toIsometry3d(const std::string &str)
Definition XmlHelpers.cpp:391
Eigen::Vector3d getAttributeVector3d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:928
Eigen::Vector2i toVector2i(const std::string &str)
Definition XmlHelpers.cpp:205
char getAttributeChar(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:901
Eigen::VectorXd getValueVectorXd(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:602
const tinyxml2::XMLElement * getElement(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:661
Eigen::Vector3d toVector3d(const std::string &str)
Definition XmlHelpers.cpp:236
double toDouble(const std::string &str)
Definition XmlHelpers.cpp:163
float getAttributeFloat(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:865
Eigen::Vector6d getAttributeVector6d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:946
bool copyNode(tinyxml2::XMLNode *destParent, const tinyxml2::XMLNode &src)
Definition XmlHelpers.cpp:964
Eigen::Matrix< double, N, 1 > getAttributeVectorNd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.hpp:208
std::string getAttribute(tinyxml2::XMLElement *element, const char *const name)
Definition XmlHelpers.cpp:780
Eigen::Isometry3d getValueIsometry3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:626
Eigen::Vector4d getAttributeVector4d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:937
int toInt(const std::string &str)
Definition XmlHelpers.cpp:145
Eigen::Vector3d getValueVector3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:566
Eigen::VectorXd getAttributeVectorXd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:955
Eigen::Vector2d toVector2d(const std::string &str)
Definition XmlHelpers.cpp:174
float getValueFloat(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:518
Eigen::Vector3i toVector3i(const std::string &str)
Definition XmlHelpers.cpp:267
Eigen::VectorXd toVectorXd(const std::string &str)
Definition XmlHelpers.cpp:360
Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string &str)
Definition XmlHelpers.cpp:424
float toFloat(const std::string &str)
Definition XmlHelpers.cpp:157
int getValueInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:494
std::string getAttributeString(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:793
char toChar(const std::string &str)
Definition XmlHelpers.cpp:168
Eigen::Vector3i getValueVector3i(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:578
Eigen::Vector2d getValueVector2d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:554
std::string toString(bool v)
Definition XmlHelpers.cpp:49
char getValueChar(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:542
std::string getValueString(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:460
Eigen::Vector2d getAttributeVector2d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:919
bool toBool(const std::string &str)
Definition XmlHelpers.cpp:130
bool getAttributeBool(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:810
double getValueDouble(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition XmlHelpers.cpp:530
Eigen::Vector6d toVector6d(const std::string &str)
Definition XmlHelpers.cpp:329
int getAttributeInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:828
bool readXmlFile(tinyxml2::XMLDocument &doc, const common::Uri &uri, const common::ResourceRetrieverPtr &retrieverOrNullPtr)
Definition XmlHelpers.cpp:749
bool hasAttribute(const tinyxml2::XMLElement *element, const char *const name)
Definition XmlHelpers.cpp:773
void openXMLFile(tinyxml2::XMLDocument &doc, const common::Uri &uri, const common::ResourceRetrieverPtr &retrieverOrNullPtr)
Definition XmlHelpers.cpp:727
Eigen::Vector2i getAttributeVector2i(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:910
unsigned int getAttributeUInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:846
Definition BulletCollisionDetector.cpp:65
Definition SharedLibraryManager.hpp:46
The Uri struct provides URI parsing and merging functionality based on RFC 3986.
Definition Uri.hpp:87