object_descriptor.cpp
Go to the documentation of this file.
1 
21 #include <rviz/mesh_loader.h>
22 
23 #include <ros/console.h>
24 
25 #include <boost/lexical_cast.hpp>
26 #include <ros_uri/ros_uri.hpp>
27 
28 #include "object_descriptor.h"
29 #include <rapidxml.hpp>
30 #include <rapidxml_utils.hpp>
31 #include "util.h"
32 #include "rotation_axis.h"
33 
34 
36 
37 ObjectDescriptor::ObjectDescriptor(std::string folder_path, std::string xml_path, int instance_count, bool use_pose_val) : valid_object_(false), instance_count_(instance_count), use_pose_val_(use_pose_val)
38 {
39  std::string package_path = folder_path;
40  folder_path = ros_uri::absolute_path(folder_path);
41  try {
42  rapidxml::file<> xmlFile(xml_path.c_str());
43  rapidxml::xml_document<> doc;
44  doc.parse<0>(xmlFile.data());
45 
46 
47  rapidxml::xml_node<> *first_node = doc.first_node();
48  if (first_node) {
49  rapidxml::xml_node<> *node_name = first_node->first_node("name");
50  rapidxml::xml_node<> *node_surface_model = first_node->first_node("surface_model");
51  rapidxml::xml_node<> *node_mesh = first_node->first_node("mesh");
52  rapidxml::xml_node<> *node_rotation_type = first_node->first_node("rotation_model_type");
53  rapidxml::xml_node<> *node_orientation = first_node->first_node("model_orientation");
54  rapidxml::xml_node<> *node_diameter = first_node->first_node("diameter");
55  rapidxml::xml_node<> *node_score3D = first_node->first_node("score3D");
56  rapidxml::xml_node<> *node_desc_models = first_node->first_node("descriptor_models");
57 
58 
59  if (node_name && node_surface_model && node_mesh && node_rotation_type && node_orientation && node_diameter && node_score3D && node_desc_models) {
60 
61  name_ = node_name->value();
62  surface_model_ = HalconCpp::HSurfaceModel((folder_path + node_surface_model->value()).c_str());
63 
64  mesh_name_ = package_path + node_mesh->value();
65  rotation_model_type_ = boost::lexical_cast<int>(node_rotation_type->value());
66  orientation_ = parseStringVector(node_orientation->value(), " ,");
67  diameter_ = boost::lexical_cast<double>(node_diameter->value());
68  score_3D_ = boost::lexical_cast<double>(node_score3D->value());
69  std::vector<ObjectViewDescriptor> object_descs;
70  for (rapidxml::xml_node<> *node_desc_model = node_desc_models->first_node("descriptor_model"); node_desc_model; node_desc_model = node_desc_model->next_sibling("descriptor_model")) {
71  rapidxml::xml_node<> *node_desc_model_name = node_desc_model->first_node("name");
72  rapidxml::xml_node<> *node_desc_model_orientation = node_desc_model->first_node("view_orientation");
73  rapidxml::xml_node<> *node_desc_model_score2D = node_desc_model->first_node("score2D");
74  rapidxml::xml_node<> *node_desc_model_use_color = node_desc_model->first_node("use_color");
75  rapidxml::xml_node<> *node_desc_model_vertical_offset = node_desc_model->first_node("vertical_texture_offset");
76  rapidxml::xml_node<> *node_desc_model_horizontal_offset = node_desc_model->first_node("horizontal_texture_offset");
77  rapidxml::xml_node<> *node_desc_model_invertible = node_desc_model->first_node("invertible");
78  rapidxml::xml_node<> *node_desc_model_bounding_box = node_desc_model->first_node("bounding_box");
79  if (node_desc_model_name && node_desc_model_orientation && node_desc_model_score2D && node_desc_model_use_color && node_desc_model_use_color && node_desc_model_vertical_offset && node_desc_model_horizontal_offset && node_desc_model_invertible && node_desc_model_bounding_box) {
80  std::vector<RotationAxis> axes;
81  std::vector<Eigen::Vector2i> box_corners;
83  rapidxml::xml_node<> *node_desc_model_axes = node_desc_model->first_node("rotation_axes");
84  if (node_desc_model_axes) {
85  rapidxml::xml_node<> *axis_1 = node_desc_model_axes->first_node("axis_1");
86  if (axis_1 && axis_1->first_attribute("angle")) {
87  double angle_1 = boost::lexical_cast<double>(axis_1->first_attribute("angle")->value());
88  RotationAxis rot_axis_1(angle_1, parseStringVector(axis_1->value(), " ,"));
89  axes.push_back(rot_axis_1);
90  } else {
91  //axis_1 invalid
92  return;
93  }
95  rapidxml::xml_node<> *axis_2 = node_desc_model_axes->first_node("axis_2");
96  if (axis_2 && axis_2->first_attribute("angle")) {
97  double angle_2 = boost::lexical_cast<double>(axis_2->first_attribute("angle")->value());
98  RotationAxis rot_axis_2(angle_2, parseStringVector(axis_2->value(), " ,"));
99  axes.push_back(rot_axis_2);
100  } else {
101  //axis_2 invalid
102  return;
103  }
104  }
105  } else {
106  //no axes node
107  return;
108  }
109  }
110  if ((int)(axes.size()) != rotation_model_type_) {
111  //axes creation error
112  return;
113  }
114 
115  for (rapidxml::xml_node<> *node_corner_point = node_desc_model_bounding_box->first_node("corner_point"); node_corner_point; node_corner_point = node_corner_point->next_sibling("corner_point")) {
116  box_corners.push_back(parseStringVector2i(node_corner_point->value(), " ,"));
117  }
118  if (box_corners.size() != 4) {
119  //invalid bounding box
120  return;
121  }
122 
123  HalconCpp::HDescriptorModel desc_mdl((folder_path + node_desc_model_name->value()).c_str());
124  Eigen::Vector3d desc_orientation = parseStringVector(node_desc_model_orientation->value(), " ,");
125  double desc_score2D = boost::lexical_cast<double>(node_desc_model_score2D->value());
126  bool desc_use_color = boost::lexical_cast<bool>(node_desc_model_use_color->value());
127  int desc_vertical_offset = boost::lexical_cast<int>(node_desc_model_vertical_offset->value());
128  int desc_horizontal_offset = boost::lexical_cast<int>(node_desc_model_horizontal_offset->value());
129  bool desc_invertible = boost::lexical_cast<bool>(node_desc_model_invertible->value());
130  object_descs.push_back(ObjectViewDescriptor(desc_mdl, desc_orientation, desc_score2D, desc_use_color, desc_vertical_offset, desc_horizontal_offset, desc_invertible, axes, box_corners));
131  }
132  }
133  model_view_descriptors_ = object_descs;
134  valid_object_ = true;
135  } else {
136  ROS_ERROR_STREAM("Could not create object: " << folder_path << " (node missing)");
137  }
138  }
139  } catch(std::runtime_error err) {
140  ROS_ERROR_STREAM("Could not create object: " << folder_path << " (XML-parser error)");
141  } catch(HalconCpp::HException exc) {
142  ROS_ERROR_STREAM(exc.ErrorText());
143  ROS_ERROR_STREAM("Could not create object: " << folder_path << " (Halcon error)");
144  } catch(boost::bad_lexical_cast) {
145  ROS_ERROR_STREAM("Could not create object: " << folder_path << " (Boost cast error)");
146  } catch (rapidxml::parse_error err) {
147  ROS_ERROR_STREAM("Could not create object: " << folder_path << " (XML-parser error)");
148  }
149 }
150 
151 bool ObjectDescriptor::isValid() const { return valid_object_; }
152 
153 std::string ObjectDescriptor::getName() const { return name_; }
154 
155 HalconCpp::HSurfaceModel ObjectDescriptor::getSurfaceModel() const { return surface_model_; }
156 
157 std::string ObjectDescriptor::getMesh() const { return mesh_name_; }
158 
160 
161 Eigen::Vector3d ObjectDescriptor::getOrientation() const { return orientation_; }
162 
163 double ObjectDescriptor::getDiameter() const { return diameter_; }
164 
165 double ObjectDescriptor::getScore3D() const { return score_3D_; }
166 
167 std::vector<ObjectViewDescriptor> ObjectDescriptor::getModelViewDescriptors() const { return model_view_descriptors_; }
168 
170 
172 
173 
174 void ObjectDescriptor::setCount(int count) {
175  if (count > 0) {
176  this->instance_count_ = count;
177  }
178 }
179 
180 void ObjectDescriptor::setUsePoseVal(bool usePoseVal) {this->use_pose_val_ = usePoseVal; }
181 
182 }
std::vector< ObjectViewDescriptor > model_view_descriptors_
Eigen::Vector3d parseStringVector(std::string input_string, std::string delim)
Parses the given string and returns the 3D-double-vector described by it.
Definition: util.cpp:37
bool isValid() const
Checks whether this object can be used for recognition.
std::vector< ObjectViewDescriptor > getModelViewDescriptors() const
Eigen::Vector2i parseStringVector2i(std::string input_string, std::string delim)
Parses the given string and returns the 2D-int-vector described by it.
Definition: util.cpp:52
#define ROS_ERROR_STREAM(args)
ObjectDescriptor(std::string folder_path, std::string xml_path, int instance_count, bool use_pose_val)
The constructor of this class.


asr_descriptor_surface_based_recognition
Author(s): Allgeyer Tobias, Hutmacher Robin, Meißner Pascal
autogenerated on Mon Dec 16 2019 03:31:15