ed
xml_shape_parser.cpp
Go to the documentation of this file.
1 #include "xml_shape_parser.h"
2 
3 #include <geolib/datatypes.h>
5 
6 #include <tinyxml2.h>
7 
8 #include <vector>
9 #include <string>
10 #include <sstream>
11 
12 // ----------------------------------------------------------------------------------------------------
13 
14 std::vector<double> parseArray(const tinyxml2::XMLElement* xml_elem)
15 {
16  std::string txt = xml_elem->GetText();
17 
19 
20  std::string word;
21  std::stringstream stream(txt);
22  while(getline(stream, word, ' '))
23  {
24  double d = 0;
25  std::istringstream istr(word);
26  istr >> d;
27  v.push_back(d);
28  }
29 
30  return v;
31 }
32 
33 // ----------------------------------------------------------------------------------------------------
34 
36 {
37  std::stringstream s_error;
38 
39  tinyxml2::XMLDocument doc;
40  doc.LoadFile(filename.c_str());
41 
42  if (doc.Error())
43  {
44  s_error << "While parsing '" << filename << "': " << std::endl << std::endl
45  << doc.ErrorStr() << " at line " << doc.ErrorLineNum() << std::endl;
46  error = s_error.str();
47  return geo::ShapePtr();
48  }
49 
50  const tinyxml2::XMLElement* model_xml = doc.FirstChildElement("model");
51  if (!model_xml)
52  {
53  s_error << "Could not find 'model' element" << std::endl;
54  return geo::ShapePtr(new geo::Shape());
55  }
56 
58 
59  const tinyxml2::XMLElement* shape_xml = model_xml->FirstChildElement();
60  while (shape_xml)
61  {
63 
64  // parse properties valid for all shapes
65  const tinyxml2::XMLElement* xyz_xml = shape_xml->FirstChildElement("xyz");
66  if (xyz_xml)
67  {
68  std::vector<double> xyz = parseArray(xyz_xml);
69  pose.setOrigin(geo::Vector3(xyz[0], xyz[1], xyz[2]));
70  }
71 
72  const tinyxml2::XMLElement* rpy_xml = shape_xml->FirstChildElement("rpy");
73  if (rpy_xml)
74  {
75  std::vector<double> rpy = parseArray(rpy_xml);
76  if (fabs(rpy[0]) > 0.0001 || fabs(rpy[1]) > 0.0001 || fabs(rpy[2]) > 0.0001)
77  {
78  geo::Matrix3 rot;
79  rot.setRPY(rpy[0], rpy[1], rpy[2]);
80  pose.setBasis(rot);
81  }
82  }
83 
85  const tinyxml2::XMLElement* size_xml = shape_xml->FirstChildElement("size");
86  if (size_xml)
87  size = parseArray(size_xml);
88 
89  std::string shape_type = shape_xml->Value();
90  if (shape_type == "box") {
91  const tinyxml2::XMLElement* min_xml = shape_xml->FirstChildElement("min");
92  const tinyxml2::XMLElement* max_xml = shape_xml->FirstChildElement("max");
93 
94  if (min_xml && max_xml)
95  {
96  std::vector<double> min = parseArray(min_xml);
97  std::vector<double> max = parseArray(max_xml);
98 
99  if (min.size() == 3 && max.size() == 3)
100  {
101  shape->addShape(geo::Box(geo::Vector3(min[0], min[1], min[2]),
102  geo::Vector3(max[0], max[1], max[2])), pose);
103  }
104  }
105  else if (!size.empty())
106  {
107  geo::Vector3 v_size(size[0], size[1], size[2]);
108  shape->addShape(geo::Box(-v_size / 2, v_size / 2), pose);
109  }
110  else
111  {
112  s_error << "In definition '" << filename << "': shape '" << shape_type << "' has no size property" << std::endl;
113  }
114 
115  } else {
116  s_error << "In definition '" << filename << "': Unknown shape type: '" << shape_type << "'" << std::endl;
117  }
118 
119  shape_xml = shape_xml->NextSiblingElement();
120  }
121 
122  error = s_error.str();
123  if (!error.empty())
124  return geo::ShapePtr();
125 
126  return shape;
127 }
datatypes.h
sstream
geo::ShapePtr
std::shared_ptr< Shape > ShapePtr
std::string
std::shared_ptr
geo::Transform3T::setOrigin
void setOrigin(const Vec3T< T > &v)
geo::Transform3T::identity
static Transform3T identity()
vector
geo::Transform3T::setBasis
void setBasis(const Mat3T< T > &r)
geo::Box
std::stringstream
std::istringstream
geo::Transform3T
std::vector::push_back
T push_back(T... args)
xml_shape_parser.h
ed::log::error
std::ostream & error()
Definition: logging.cpp:74
parseXMLShape
geo::ShapePtr parseXMLShape(const std::string &filename, std::string &error)
Definition: xml_shape_parser.cpp:35
std::string::c_str
T c_str(T... args)
CompositeShape.h
geo::Vector3
geo::Mat3T
geo::CompositeShape
std::endl
T endl(T... args)
parseArray
std::vector< double > parseArray(const tinyxml2::XMLElement *xml_elem)
Definition: xml_shape_parser.cpp:14
std::stringstream::str
T str(T... args)
geo::Mat3T::setRPY
void setRPY(T roll, T pitch, T yaw)
geo::Shape
string