DART 6.13.2
Loading...
Searching...
No Matches
XmlHelpers-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, 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_DETAIL_XMLHELPERS_IMPL_HPP_
34#define DART_UTILS_DETAIL_XMLHELPERS_IMPL_HPP_
35
38
39namespace dart::utils {
40
41//==============================================================================
42template <typename S, int N>
43std::string toString(const Eigen::Matrix<S, N, 1>& v)
44{
45 std::stringstream ss;
46 ss << v.transpose();
47 return ss.str();
48}
49
50//==============================================================================
51template <typename S>
52std::string toString(
53 const Eigen::Transform<S, 3, Eigen::Isometry>& v,
54 const std::string& rotationType)
55{
56 Eigen::Matrix<S, 3, 1> angles;
57 if (rotationType == "intrinsic")
58 {
59 angles = math::matrixToEulerXYZ(v.rotation());
60 }
61 else if (rotationType == "extrinsic")
62 {
63 angles = math::matrixToEulerZYX(v.rotation()).reverse();
64 }
65 else
66 {
68 "Unsupported rotation type [{}]. Assuming intrinsic.", rotationType);
69 angles = math::matrixToEulerXYZ(v.rotation());
70 }
71
72 std::stringstream ss;
73 ss.precision(6);
74 ss << v.translation().transpose() << " ";
75 ss << angles;
76
77 return ss.str();
78}
79
80//==============================================================================
81template <std::size_t N>
82Eigen::Matrix<double, N, 1> toVectorNd(const std::string& str)
83{
84 const std::vector<std::string> pieces = common::split(common::trim(str));
85 const std::size_t sizeToRead = std::min(N, pieces.size());
86 if (pieces.size() < N)
87 {
88 dterr << "Failed to read a vector because the dimension '" << pieces.size()
89 << "' is less than the expectation '" << N << "'.\n";
90 }
91 else if (pieces.size() > N)
92 {
93 dterr << "Failed to read a vector because the dimension '" << pieces.size()
94 << "' is greater than the expectation '" << N << "'.\n";
95 }
96
97 Eigen::Matrix<double, N, 1> ret = Eigen::Matrix<double, N, 1>::Zero();
98
99 for (std::size_t i = 0; i < sizeToRead; ++i)
100 {
101 if (pieces[i] != "")
102 {
103 try
104 {
105 ret[i] = toDouble(pieces[i]);
106 }
107 catch (std::exception& e)
108 {
109 dterr << "value [" << pieces[i]
110 << "] is not a valid double for Eigen::Vector" << N << "d[" << i
111 << "]: " << e.what() << "\n";
112 }
113 }
114 }
115
116 return ret;
117}
118
119//==============================================================================
120template <std::size_t N>
121Eigen::Matrix<double, N, 1> getAttributeVectorNd(
122 const tinyxml2::XMLElement* element, const std::string& attributeName)
123{
124 const std::string val = getAttributeString(element, attributeName);
125 return toVectorNd<N>(val);
126}
127
128//==============================================================================
129template <typename ElementType>
131 ElementPtr parentElement, const std::string& childElementName)
132 : mParentElement(parentElement),
133 mChildElementName(childElementName),
134 mCurrentElement(nullptr)
135{
136 // Do nothing
137}
138
139//==============================================================================
140template <typename ElementType>
145
146//==============================================================================
147template <typename ElementType>
149{
150 if (!mParentElement)
151 return false;
152
153 if (mCurrentElement)
154 {
155 mCurrentElement
156 = mCurrentElement->NextSiblingElement(mChildElementName.c_str());
157 }
158 else
159 {
160 mCurrentElement
161 = mParentElement->FirstChildElement(mChildElementName.c_str());
162 }
163
164 if (!valid())
165 mParentElement = nullptr;
166
167 return valid();
168}
169
170//==============================================================================
171template <typename ElementType>
174{
175 return mCurrentElement;
176}
177
178//==============================================================================
179template <typename ElementType>
182{
183 return mCurrentElement;
184}
185
186//==============================================================================
187template <typename ElementType>
190{
191 return *mCurrentElement;
192}
193
194//==============================================================================
195template <typename ElementType>
198{
199 // If they point at the same node, then the names must match
200 return (this->mParentElement == rhs.mParentElement)
201 && (this->mCurrentElement == rhs.mCurrentElement)
202 && (this->mCurrentElement != nullptr
203 || (this->mChildElementName == rhs.mChildElementName));
204}
205
206//==============================================================================
207template <typename ElementType>
211{
212 this->mParentElement = rhs.mParentElement;
213 this->mChildElementName = rhs.mChildElementName;
214 this->mCurrentElement = rhs.mCurrentElement;
215
216 return *this;
217}
218
219//==============================================================================
220template <typename ElementType>
222{
223 return mCurrentElement != nullptr;
224}
225
226} // namespace dart::utils
227
228#endif // #ifndef DART_UTILS_DETAIL_XMLHELPERS_IMPL_HPP_
#define dterr
Output an error message.
Definition Console.hpp:49
#define DART_ERROR(...)
Definition Logging.hpp:76
bool valid
Definition SkelParser.cpp:1674
TemplatedElementEnumerator is a convenience class to help visiting all the child elements of given pa...
Definition XmlHelpers.hpp:179
bool next()
Set the current element to the next sibling element or to the first child element of given parent ele...
Definition XmlHelpers-impl.hpp:148
ElementPtr operator->() const
Dereference operator.
Definition XmlHelpers-impl.hpp:181
ElementType & ElementRef
Definition XmlHelpers.hpp:182
~TemplatedElementEnumerator()
Destructor.
Definition XmlHelpers-impl.hpp:141
ElementRef operator*() const
Dereference operator.
Definition XmlHelpers-impl.hpp:189
ElementPtr get() const
Get the current element.
Definition XmlHelpers-impl.hpp:173
bool operator==(const TemplatedElementEnumerator< ElementType > &rhs) const
Equality operator.
Definition XmlHelpers-impl.hpp:196
ElementPtr mCurrentElement
Currently visiting child element.
Definition XmlHelpers.hpp:224
bool valid() const
Returns true if the current element is valid (not a nullptr)
Definition XmlHelpers-impl.hpp:221
std::string mChildElementName
Child element name.
Definition XmlHelpers.hpp:221
TemplatedElementEnumerator< ElementType > & operator=(const TemplatedElementEnumerator< ElementType > &rhs)
Assignment operator.
Definition XmlHelpers-impl.hpp:209
ElementType * ElementPtr
Definition XmlHelpers.hpp:181
TemplatedElementEnumerator(ElementPtr parentElement, const std::string &childElementName)
Constructor that takes parent element and.
Definition XmlHelpers-impl.hpp:130
ElementPtr mParentElement
Parent element.
Definition XmlHelpers.hpp:218
std::vector< std::string > split(const std::string &str, const std::string &delimiters)
Splits string given delimiters.
Definition String.cpp:86
std::string trim(const std::string &str, const std::string &whitespaces)
Trims both sides of string.
Definition String.cpp:66
Eigen::Vector3d matrixToEulerXYZ(const Eigen::Matrix3d &_R)
get the Euler XYZ angle from R
Definition Geometry.cpp:117
Eigen::Vector3d matrixToEulerZYX(const Eigen::Matrix3d &_R)
get the Euler ZYX angle from R
Definition Geometry.cpp:151
Definition C3D.cpp:43
Eigen::Matrix< double, N, 1 > toVectorNd(const std::string &str)
Definition XmlHelpers-impl.hpp:82
double toDouble(const std::string &str)
Definition XmlHelpers.cpp:117
Eigen::Matrix< double, N, 1 > getAttributeVectorNd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers-impl.hpp:121
std::string toString(const Eigen::Matrix< S, N, 1 > &v)
Definition XmlHelpers-impl.hpp:43
std::string getAttributeString(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition XmlHelpers.cpp:732