voteViewer.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <boost/filesystem.hpp>
20 #include <boost/algorithm/string.hpp>
21 #include <boost/lexical_cast.hpp>
22 
23 #include <rapidxml.hpp>
24 #include <rapidxml_utils.hpp>
25 
26 //Pkg includes
27 #include <ros/ros.h>
28 #include <asr_ism_visualizations/ObjectModelVisualizerRVIZ.hpp>
29 #include <asr_ism_visualizations/vote_visualizer_rviz.hpp>
30 #include <asr_ism_visualizations/VizHelperRVIZ.hpp>
31 
32 //ISM includes
33 #include <ISM/utility/GeometryHelper.hpp>
34 #include <ISM/utility/TableHelper.hpp>
35 #include <ISM/utility/Util.hpp>
36 
37 
38 
40 {
41  public:
42  VoteViewer(): nh_("~")
43  {
44  std::string db_filename;
45  std::string base_frame;
46  std::string visualization_topic;
47  getNodeParameters(db_filename, base_frame, visualization_topic);
48  ISM::TableHelperPtr table_helper(new ISM::TableHelper(db_filename));
49  if(!table_helper->modelDataExists())
50  {
51  ISM::printRed("The database \"" + db_filename + "\" doesn't contain a model!\n");
52  exit(0);
53  }
54 
55  init(table_helper);
56 
57  visualization_publisher_ = nh_.advertise<visualization_msgs::MarkerArray>(visualization_topic, 10000);
58  object_model_visualizer_ = new VIZ::ObjectModelVisualizerRVIZ(visualization_publisher_, base_frame, "", 0);
59  vote_visualizer_ = new VIZ::VoteVisualizerRVIZ(visualization_publisher_, base_frame, "", 0);
60 
62  }
63 
64 
65  void printObjects()
66  {
67  ISM::printGreen("\n###############################################\n");
68  std::cout << scene_name_ << " contains following sub-ISMs and objects (NAME & ID):\n";
69  for (std::pair<const std::string, std::map<std::string, std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>>>& object_to_votes : pattern_to_object_to_votes_)
70  {
71  std::cout << "\t" << object_to_votes.first << ":\n";
72  for (std::pair<const std::string, std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>>& object_name : object_to_votes.second)
73  {
74  for (std::pair<const std::string, std::vector<ISM::VoteSpecifierPtr>>& object_id : object_name.second)
75  {
76  std::cout << "\t\t" << object_name.first << " " << object_id.first << std::endl;
77  }
78  }
79  }
80  ISM::printGreen("###############################################\n");
81  }
82 
83 
85  objects_.clear();
86  object_to_votes_.clear();
87 
88  if(config_file_path_ == "")
89  {
90  ISM::printRed("Couldn't load object constellation from xml, because the parameter \"config_file_path\" is not specified!\n");
91  exit(0);
92  }
93 
94  std::string xml_path = config_file_path_;
95  ROS_DEBUG_STREAM("Path to objects.xml: " << xml_path);
96 
97  try {
98  rapidxml::file<> xmlFile(xml_path.c_str());
99  rapidxml::xml_document<> doc;
100  doc.parse<0>(xmlFile.data());
101 
102  rapidxml::xml_node<> *root_node = doc.first_node();
103  if (root_node)
104  {
105  rapidxml::xml_node<> *child_node = root_node->first_node();
106  while (child_node)
107  {
108  rapidxml::xml_attribute<> *type_attribute = child_node->first_attribute("type");
109  rapidxml::xml_attribute<> *id_attribute = child_node->first_attribute("id");
110  rapidxml::xml_attribute<> *mesh_attribute = child_node->first_attribute("mesh");
111  rapidxml::xml_attribute<> *angles_attribute = child_node->first_attribute("angles");
112  if (type_attribute && id_attribute && mesh_attribute && angles_attribute)
113  {
114  std::string type = type_attribute->value();
115  std::string mesh = mesh_attribute->value();
116  std::string id = id_attribute->value();
117  std::string pose_string = child_node->value();
118  std::string angles = angles_attribute->value();
119  ISM::PosePtr pose = ISM::PosePtr(new ISM::Pose());
120  if (parsePoseString(pose_string, pose, " ,", angles))
121  {
122  addObjectAndVotes(ISM::ObjectPtr(new ISM::Object(type, pose, id, mesh)), true);
123  }
124  }
125  child_node = child_node->next_sibling();
126  }
127  }
128  } catch(std::runtime_error err) {
129  ROS_DEBUG_STREAM("Can't parse xml-file. Runtime error: " << err.what());
130  } catch (rapidxml::parse_error err) {
131  ROS_DEBUG_STREAM("Can't parse xml-file Parse error: " << err.what());
132  }
133 
134  if (objects_.empty())
135  {
136  ISM::printRed("No objects loaded! Check path specified in the launch-file!\n");
137  ros::shutdown();
138  }
139  else
140  {
141  ISM::printGreen("Objects loaded!\n");
142  }
143  }
144 
145 
146 
148  {
149  object_model_visualizer_->clearAllMarkerOfTopic();
150  vote_visualizer_->clearAllMarkerOfTopic();
151 
152  object_model_visualizer_->drawObjectModels(objects_);
153  vote_visualizer_->addVisualization(object_to_votes_);
154  }
155 
156 
157 
159  {
160  ros::Rate refresh_rate(1); // 1 hz
161  while (ros::ok())
162  {
163  std::string obj_name;
164  std::string obj_id;
165  ISM::printYellow("\nNEW SELECTION:\n");
166  std::cout << "Enter object NAME: ";
167  std::getline (std::cin, obj_name);
168  std::cout << "Enter object ID: ";
169  std::getline (std::cin, obj_id);
170 
171  bool exist = this->objectExist(obj_name, obj_id);
172  if (exist)
173  {
174  ISM::printGreen("SELECTED ");
175  std::cout << obj_name << " with the ID " << obj_id << std::endl;
176 
177  ISM::PosePtr origin(new ISM::Pose(new ISM::Point(0.0, 0.0, 0.0), new ISM::Quaternion(1.0, 0.0, 0.0, 0.0)));
178  std::string resource_path = (type_to_resource_path_.find(obj_name) == type_to_resource_path_.end()) ? "" : type_to_resource_path_[obj_name].string();
179  ISM::ObjectPtr obj(ISM::ObjectPtr(new ISM::Object(obj_name, origin, obj_id, resource_path)));
180 
181  objects_.clear();
182  object_to_votes_.clear();
183  addObjectAndVotes(obj);
185  }
186  else
187  {
188  ISM::printRed("NO SUCH OBJECT!\n");
189  }
190 
191  ros::spinOnce();
192  refresh_rate.sleep();
193  }
194  }
195 
196  private:
197 
198  void getNodeParameters(std::string& db_filename, std::string& base_frame, std::string& visualization_topic)
199  {
200  if (!nh_.getParam("dbfilename", db_filename))
201  {
202  db_filename = "";
203  }
204  ROS_INFO_STREAM("dbfilename: " << db_filename);
205 
206  if (!nh_.getParam("visualization_topic", visualization_topic))
207  {
208  visualization_topic = "";
209  }
210  ROS_INFO_STREAM("visualization_topic: " << visualization_topic);
211 
212  if (!nh_.getParam("baseFrame", base_frame))
213  {
214  base_frame = "/map";
215  }
216  ROS_INFO_STREAM("baseFrame: " << base_frame);
217 
218  if (!nh_.getParam("sceneName", scene_name_))
219  {
220  scene_name_ = "";
221  }
222  ROS_INFO_STREAM("sceneName: " << scene_name_);
223 
224  if (!nh_.getParam("config_file_path", config_file_path_))
225  {
226  config_file_path_ = "";
227  }
228  ROS_INFO_STREAM("config_file_path: " << config_file_path_);
229  }
230 
231 
232  void init(ISM::TableHelperPtr table_helper)
233  {
234  std::vector<std::string> pattern_names = table_helper->getModelPatternNames();
235  pattern_to_object_to_votes_ = ISM::PatternToObjectToVoteMap();
236  bool pattern_exist = false;
237 
238  for (std::string pattern_name : pattern_names)
239  {
240  if(pattern_name.find(scene_name_) == 0)
241  {
242  std::set<std::pair<std::string, std::string>> objects = table_helper->getObjectTypesAndIdsBelongingToPattern(pattern_name);
243  pattern_to_object_to_votes_[pattern_name] = table_helper->getVoteSpecifiersForPatternAndObjects(pattern_name, objects);
244 
245  pattern_exist = pattern_exist || !pattern_to_object_to_votes_[pattern_name].empty();
246  }
247  }
248 
249  if (!pattern_exist)
250  {
251  ISM::printRed("No pattern in the database, with the name: " + scene_name_ + "\n");
252  exit(0);
253  }
254 
255  type_to_resource_path_ = table_helper->getRessourcePaths();
256  }
257 
258 
259  bool parsePoseString(std::string pose_in, ISM::PosePtr &pose_out, std::string delim, std::string angles) {
260  std::vector<std::string> strvec;
261 
262  boost::algorithm::trim_if(pose_in, boost::algorithm::is_any_of(delim));
263  boost::algorithm::split(strvec, pose_in, boost::algorithm::is_any_of(delim), boost::algorithm::token_compress_on);
264  if (strvec.size() == 6 || strvec.size() == 7) {
265  try {
266  pose_out->point->eigen.x() = boost::lexical_cast<double>(strvec[0]);
267  pose_out->point->eigen.y() = boost::lexical_cast<double>(strvec[1]);
268  pose_out->point->eigen.z() = boost::lexical_cast<double>(strvec[2]);
269 
270  if(angles == "quaternion" && strvec.size() == 7)
271  {
272  pose_out->quat->eigen.w() = boost::lexical_cast<double>(strvec[3]);
273  pose_out->quat->eigen.x() = boost::lexical_cast<double>(strvec[4]);
274  pose_out->quat->eigen.y() = boost::lexical_cast<double>(strvec[5]);
275  pose_out->quat->eigen.z() = boost::lexical_cast<double>(strvec[6]);
276  }
277  else if(angles == "euler" && strvec.size() == 6)
278  {
279  double euler0,euler1,euler2;
280  euler0 = boost::lexical_cast<double>(strvec[3]);
281  euler1 = boost::lexical_cast<double>(strvec[4]);
282  euler2 = boost::lexical_cast<double>(strvec[5]);
283 
284  Eigen::Matrix3d rotationMatrix;
285  rotationMatrix = Eigen::AngleAxisd(euler0 * (M_PI / 180), Eigen::Vector3d::UnitX())
286  * Eigen::AngleAxisd(euler1 * (M_PI / 180), Eigen::Vector3d::UnitY())
287  * Eigen::AngleAxisd(euler2 * (M_PI / 180), Eigen::Vector3d::UnitZ());
288 
289  Eigen::Quaternion<double> result(rotationMatrix);
290  pose_out->quat->eigen.w() = result.w();
291  pose_out->quat->eigen.x() = result.x();
292  pose_out->quat->eigen.y() = result.y();
293  pose_out->quat->eigen.z() = result.z();
294  }
295  else
296  {
297  ROS_ERROR("Invalid XML syntax.");
298  nh_.shutdown();
299  }
300 
301  return true;
302  } catch (boost::bad_lexical_cast err) {
303  ROS_DEBUG_STREAM("Can't cast node-value. Cast error: " << err.what());
304  }
305  }
306  return false;
307  }
308 
309 
310  void addObjectAndVotes(ISM::ObjectPtr object, bool recursive = false)
311  {
312  ROS_DEBUG_STREAM("Add Object " << object->type << " with id " << object->observedId << ". [addObjectAndVotes(..)]");
313  objects_.push_back(object);
314 
315  for (ISM::PatternToObjectToVoteMap::iterator pat_it = pattern_to_object_to_votes_.begin(); pat_it != pattern_to_object_to_votes_.end(); ++pat_it)
316  {
317  ISM::ObjectToVoteMap::iterator obj_name_it = pat_it->second.find(object->type);
318  if (obj_name_it != pat_it->second.end())
319  {
320  std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>::iterator obj_id_it = obj_name_it->second.find(object->observedId);
321  if (obj_id_it != obj_name_it->second.end())
322  {
323  if(obj_id_it->second.size() > 0)
324  {
325  object_to_votes_[object].insert(object_to_votes_[object].begin(), obj_id_it->second.begin(), obj_id_it->second.end());
326 
327  // if recursive adding is true and the object has a selfvote so add sub_ism object with same pose.
328  if(recursive && pat_it->first.find("sub") != std::string::npos && ISM::GeometryHelper::isSelfVote(obj_id_it->second[0]))
329  {
330  ROS_DEBUG_STREAM("Object " << object->type << " with id " << object->observedId << " is the reference object of sub-ism " << pat_it->first << ". [addObjectAndVotes(..)]");
331  ISM::ObjectPtr obj(ISM::ObjectPtr(new ISM::Object(pat_it->first, object->pose, "")));
332  addObjectAndVotes(obj, true);
333  }
334  }
335  }
336  }
337  }
338 
339  if (object_to_votes_.find(object) != object_to_votes_.end())
340  {
341  ROS_DEBUG_STREAM("For Object " << object->type << " with id " << object->observedId << " are " << object_to_votes_[object].size()<< " votes in the model for " << scene_name_ << ". [addObjectAndVotes(..)]");
342  }
343  else
344  {
345  ROS_DEBUG_STREAM("No votes for Object " << object->type << " with id " << object->observedId << " in the model for " << scene_name_ << ". [addObjectAndVotes(..)]");
346  }
347  }
348 
349 
350  bool objectExist(std::string& obj_name, std::string& obj_id)
351  {
352  for (ISM::PatternToObjectToVoteMap::iterator pat_it = pattern_to_object_to_votes_.begin(); pat_it != pattern_to_object_to_votes_.end(); ++pat_it)
353  {
354  ISM::ObjectToVoteMap::iterator obj_name_it = pat_it->second.find(obj_name);
355  if (obj_name_it != pat_it->second.end())
356  {
357  std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>::iterator obj_id_it = obj_name_it->second.find(obj_id);
358  if (obj_id_it != obj_name_it->second.end())
359  {
360  return true;
361  }
362  }
363  }
364 
365  return false;
366  }
367 
368 
370  {
371  unsigned int previous_number = number_of_subscriber_;
373  if(number_of_subscriber_ > 0 && (number_of_subscriber_ > previous_number))
374  {
375  ISM::printGreen("Publish vote marker!\n");
376  vote_visualizer_->publishCollectedMarkers();
377  object_model_visualizer_->publishCollectedMarkers();
378  std::cout<<"\n";
379  }
380  }
381 
382 
383  private:
386 
387  std::string scene_name_;
388  ISM::PatternToObjectToVoteMap pattern_to_object_to_votes_;
389  std::vector<ISM::ObjectPtr> objects_;
390  std::map<ISM::ObjectPtr, std::vector<ISM::VoteSpecifierPtr>> object_to_votes_;
391  std::map<std::string, boost::filesystem::path> type_to_resource_path_;
392 
393  std::string config_file_path_;
394 
395  //Visualization stuff
397  VIZ::ObjectModelVisualizerRVIZ* object_model_visualizer_;
398  VIZ::VoteVisualizerRVIZ* vote_visualizer_;
399  unsigned int number_of_subscriber_;
400 
401 };
402 
403 int main (int argc, char **argv)
404 {
405  //Usual ros node stuff
406  ros::init(argc, argv, "modelViewer");
407  VoteViewer* vote_viewer = new VoteViewer();
408 
409  vote_viewer->printObjects();
410 
411  while (true)
412  {
413  std::string load_config_query;
414  ISM::printYellow("\nDo you want to use object constellation from an xml-file?\n");
415  std::cout << "[y]es or [n]o: ";
416  std::getline (std::cin, load_config_query);
417  if (load_config_query == "y")
418  {
419  vote_viewer->loadObjectsFromXML();
420  vote_viewer->generateVisualization();
421  ros::spin(); // spin so visualization will be refreshed if needed
422  break;
423  }
424  else if (load_config_query == "n")
425  {
426  vote_viewer->spinSelectionMode();
427  break;
428  }
429  else
430  {
431  std::cout << load_config_query << " is not valid, try again!" << std::endl;
432  }
433  }
434 
435 
436  delete vote_viewer;
437  return 0;
438 }
void loadObjectsFromXML()
Definition: voteViewer.cpp:84
void init(ISM::TableHelperPtr table_helper)
Definition: voteViewer.cpp:232
ISM::PatternToObjectToVoteMap pattern_to_object_to_votes_
Definition: voteViewer.cpp:388
VIZ::VoteVisualizerRVIZ * vote_visualizer_
Definition: voteViewer.cpp:398
void spinSelectionMode()
Definition: voteViewer.cpp:158
void addObjectAndVotes(ISM::ObjectPtr object, bool recursive=false)
Definition: voteViewer.cpp:310
void refreshVisualization(const ros::TimerEvent &e)
Definition: voteViewer.cpp:369
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool parsePoseString(std::string pose_in, ISM::PosePtr &pose_out, std::string delim, std::string angles)
Definition: voteViewer.cpp:259
unsigned int number_of_subscriber_
Definition: voteViewer.cpp:399
ros::Timer timer_
Definition: voteViewer.cpp:385
int main(int argc, char **argv)
Definition: voteViewer.cpp:403
std::map< std::string, boost::filesystem::path > type_to_resource_path_
Definition: voteViewer.cpp:391
ROSCPP_DECL void spin(Spinner &spinner)
std::map< ISM::ObjectPtr, std::vector< ISM::VoteSpecifierPtr > > object_to_votes_
Definition: voteViewer.cpp:390
ROSCPP_DECL bool ok()
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
VIZ::ObjectModelVisualizerRVIZ * object_model_visualizer_
Definition: voteViewer.cpp:397
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle nh_
Definition: voteViewer.cpp:384
#define ROS_DEBUG_STREAM(args)
std::string config_file_path_
Definition: voteViewer.cpp:393
bool sleep()
std::vector< ISM::ObjectPtr > objects_
Definition: voteViewer.cpp:389
bool objectExist(std::string &obj_name, std::string &obj_id)
Definition: voteViewer.cpp:350
std::string scene_name_
Definition: voteViewer.cpp:387
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()
void getNodeParameters(std::string &db_filename, std::string &base_frame, std::string &visualization_topic)
Definition: voteViewer.cpp:198
ros::Publisher visualization_publisher_
Definition: voteViewer.cpp:396
#define ROS_ERROR(...)
void generateVisualization()
Definition: voteViewer.cpp:147
void printObjects()
Definition: voteViewer.cpp:65


asr_ism
Author(s): Borella Jocelyn, Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Thu Jan 9 2020 07:20:58