14 #include <mrpt/poses/CPose2D.h> 15 #include <mrpt/poses/CPose3D.h> 16 #include <mrpt/math/lightweight_geom_data.h> 17 #include <mrpt/system/string_utils.h> 19 #include <mrpt/version.h> 20 #if MRPT_VERSION<0x199 21 #include <mrpt/utils/utils_defs.h> 22 #include <mrpt/utils/TColor.h> 23 using mrpt::utils::TColor;
24 using mrpt::utils::DEG2RAD;
25 using mrpt::utils::RAD2DEG;
27 #include <mrpt/core/format.h> 28 #include <mrpt/core/bits_math.h> 29 #include <mrpt/img/TColor.h> 30 using mrpt::img::TColor;
36 using namespace mvsim;
42 void TParamEntry::parse(
43 const std::string& str,
const std::string& varName,
44 const char* function_name_context)
const 48 if (std::string(frmt) == std::string(
"%s"))
51 if (1 != ::sscanf(str.c_str(), frmt, auxStr))
52 throw std::runtime_error(
54 "%s Error parsing '%s'='%s' (Expected format:'%s')",
55 function_name_context, varName.c_str(), str.c_str(), frmt));
56 std::string& str = *
reinterpret_cast<std::string*
>(val);
57 str = mrpt::system::trim(auxStr);
60 else if (std::string(frmt) == std::string(
"%lf_deg"))
62 if (1 != ::sscanf(str.c_str(), frmt, val))
63 throw std::runtime_error(
65 "%s Error parsing attribute '%s'='%s' (Expected " 67 function_name_context, varName.c_str(), str.c_str(), frmt));
68 double& ang = *
reinterpret_cast<double*
>(val);
72 else if (std::string(frmt) == std::string(
"%bool"))
74 bool& bool_val = *
reinterpret_cast<bool*
>(val);
76 const std::string sStr =
77 mrpt::system::lowerCase(mrpt::system::trim(std::string(str)));
78 if (sStr ==
"1" || sStr ==
"true")
80 else if (sStr ==
"0" || sStr ==
"false")
83 throw std::runtime_error(
85 "%s Error parsing 'bool' attribute '%s'='%s' (Expected " 87 function_name_context, varName.c_str(), str.c_str()));
90 else if (std::string(frmt) == std::string(
"%color"))
93 if (!(str.size() > 1 && str[0] ==
'#'))
94 throw std::runtime_error(
96 "%s Error parsing '%s'='%s' (Expected " 97 "format:'#RRGGBB[AA]')",
98 function_name_context, varName.c_str(), str.c_str()));
100 unsigned int r, g, b, a = 0xff;
101 int ret = ::sscanf(str.c_str() + 1,
"%2x%2x%2x%2x", &r, &g, &b, &a);
102 if (ret != 3 && ret != 4)
103 throw std::runtime_error(
105 "%s Error parsing '%s'='%s' (Expected " 106 "format:'#RRGGBB[AA]')",
107 function_name_context, varName.c_str(), str.c_str()));
108 TColor& col = *
reinterpret_cast<TColor*
>(val);
109 col = TColor(r, g, b, a);
113 else if (!strncmp(frmt,
"%pose2d", strlen(
"%pose2d")))
116 int ret = ::sscanf(str.c_str(),
"%lf %lf %lf", &x, &y, &yaw);
118 throw std::runtime_error(
120 "%s Error parsing '%s'='%s' (Expected format:'X Y " 122 function_name_context, varName.c_str(), str.c_str()));
127 const mrpt::poses::CPose2D p(x, y, yaw);
130 if (!strcmp(frmt,
"%pose2d"))
132 mrpt::poses::CPose2D& pp =
133 *
reinterpret_cast<mrpt::poses::CPose2D*
>(val);
136 else if (!strcmp(frmt,
"%pose2d_ptr3d"))
138 mrpt::poses::CPose3D& pp =
139 *
reinterpret_cast<mrpt::poses::CPose3D*
>(val);
140 pp = mrpt::poses::CPose3D(p);
143 throw std::runtime_error(
145 "%s Error: Unknown format specifier '%s'",
146 function_name_context, frmt));
151 if (1 != ::sscanf(str.c_str(), frmt, val))
152 throw std::runtime_error(
154 "%s Error parsing attribute '%s'='%s' (Expected " 156 function_name_context, varName.c_str(), str.c_str(), frmt));
162 const std::map<std::string, TParamEntry>& params,
163 const char* function_name_context)
165 for (std::map<std::string, TParamEntry>::const_iterator it = params.begin();
166 it != params.end(); ++it)
170 if (attr && attr->
value())
172 attr->
value(), attr->
name(), function_name_context);
178 const std::map<std::string, TParamEntry>& params,
179 const char* function_name_context)
181 std::map<std::string, TParamEntry>::const_iterator it_param =
182 params.find(xml_node.
name());
184 if (it_param != params.end())
187 it_param->second.parse(
188 xml_node.
value(), xml_node.
name(), function_name_context);
198 const std::map<std::string, TParamEntry>& params,
199 const char* function_name_context)
215 const std::string& s,
bool allow_missing_angle,
216 double default_angle_radians)
219 v.
vals[2] = RAD2DEG(default_angle_radians);
222 ::sscanf(s.c_str(),
"%lf %lf %lf", &v.
vals[0], &v.
vals[1], &v.
vals[2]);
227 if ((na != 3 && !allow_missing_angle) ||
228 (na != 2 && na != 3 && allow_missing_angle))
229 throw std::runtime_error(
230 mrpt::format(
"Malformed pose string: '%s'", s.c_str()));
241 const char* function_name_context)
246 pt_node = pt_node->next_sibling(
"pt"))
248 if (!pt_node->value())
249 throw std::runtime_error(
251 "%s Error: <pt> node seems empty.", function_name_context));
253 mrpt::math::TPoint2D pt;
254 const char* str_val = pt_node->value();
255 if (2 != ::sscanf(str_val,
"%lf %lf", &pt.x, &pt.y))
256 throw std::runtime_error(
258 "%s Error parsing <pt> node: '%s' (Expected format:'<pt>X " 260 function_name_context, str_val));
262 out_poly.push_back(pt);
265 if (out_poly.size() < 3)
266 throw std::runtime_error(
268 "%s Error: <shape> node requires 3 or more <pt>X Y</pt> " 270 function_name_context));
void parse_xmlnode_shape(const rapidxml::xml_node< char > &xml_node, mrpt::math::TPolygon2D &out_poly, const char *function_name_context="")
bool parse_xmlnode_as_param(const rapidxml::xml_node< char > &xml_node, const std::map< std::string, TParamEntry > ¶ms, const char *function_name_context="")
TFSIMD_FORCE_INLINE const tfScalar & y() const
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
void parse_xmlnode_attribs(const rapidxml::xml_node< char > &xml_node, const std::map< std::string, TParamEntry > ¶ms, const char *function_name_context="")
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const std::map< std::string, TParamEntry > ¶ms, const char *function_name_context="")
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
vec3 parseXYPHI(const std::string &s, bool allow_missing_angle=false, double default_angle_radians=0.0)
xml_node< Ch > * next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const