ism_voting_visualizer_rviz.cpp
Go to the documentation of this file.
1 
18 //Header include
20 
21 //other includes
22 #include <map>
23 #include <tuple>
24 
25 
26 namespace VIZ
27 {
28 void ISMVotingVisualizerRVIZ::addVisualization(const ISM::PatternNameAndVotingSpaceTuple& voting_space, std::vector<ISM::RecognitionResultPtr> results)
29 {
30  ISM::RecognitionResultPtr result;
31  for(std::size_t i = 0; i < results.size(); i++){
32  if(results[i]->patternName == scene_name_){
33  result = results[i];
34  break;
35  }
36  }
37  if(result == NULL){
38  ROS_ERROR("Recognition Result doesn't contain result of pattern %s.\nSkipping visualization.", pattern_name_.c_str());
39  return;
40  }
41 
42  if(voting_space.first.compare(pattern_name_) != 0){
43  ROS_ERROR("Votingspace doesn't contain pattern %s.\nSkipping visualization.", pattern_name_.c_str());
44  return;
45  }
46  ROS_INFO("Drawing votes of scene \"%s\" pattern \"%s\"", scene_name_.c_str(), pattern_name_.c_str());
47 
48  ISM::VotingSpacePtr matching_voting_space_ptr = voting_space.second;
49  std::map<ISM::ObjectPtr, std::vector<ISM::PosePtr>> voter_to_poses_map = getSortedVotedPoses(matching_voting_space_ptr);
50  std::vector<ISM::PosePtr> poses_ptr;
51  for(auto iter: voter_to_poses_map){
52  poses_ptr.insert(poses_ptr.end(), iter.second.begin(), iter.second.end());
53  }
54 
55 
56  MarkerArray bin_markers = generateBinAndGridMarker(matching_voting_space_ptr);
57  MarkerArray vote_markers = generateVoteMarker(voter_to_poses_map);
58  MarkerArray ref_markers = generateRefMarker(getRecognitionResultPtrOfPattern(result), poses_ptr);
59 
60  addMarker(vote_markers);
61  addMarker(bin_markers);
62  addMarker(ref_markers);
64 
65 }
67  Marker ret_marker = VizHelperRVIZ::createMarkerWithoutTypeAndPose(base_frame_, "result_votes_overlay", 0, line_scale_, 0, 0, VizHelperRVIZ::createColorRGBA(0.0, 0.0, 0.0, 1.0), marker_lifetime_);
68  ret_marker.type = Marker::LINE_LIST;
69 
70  for (ISM::SummarizedVotedPosePtr& summarized_vote : result->summarizedVotes)
71  {
72  ret_marker.points.push_back(VizHelperRVIZ::pointToPointMsg(summarized_vote.first->pose->point));
73  ret_marker.points.push_back(VizHelperRVIZ::pointToPointMsg(summarized_vote.first->source->pose->point));
74  }
75 
76  return ret_marker;
77 }
78 
79 
80 MarkerArray ISMVotingVisualizerRVIZ::generateRefMarker(ISM::RecognitionResultPtr result, std::vector<ISM::PosePtr> voted_poses){
81  MarkerArray ret_markers;
82  ColorRGBA confidence_color = VizHelperRVIZ::confidenceToColor(result->confidence);
83  confidence_color.a = confidence_sphere_opacity_;
84 
85  ret_markers.markers.push_back(VizHelperRVIZ::createCylinderMarker(result->referencePose, base_frame_, "reference", 0,
87  ret_markers.markers.push_back(VizHelperRVIZ::createSphereMarker(result->referencePose->point, base_frame_, "confidence_sphere", 0,
88  sqrt(3) * bin_size_, confidence_color, marker_lifetime_));
89  int id = 0;
90  ret_markers.markers.push_back(VizHelperRVIZ::createLineArrow(result->referencePose->point, voted_poses, base_frame_, "votesToReferenceLines", id,
91  line_scale_ * 0.1, VizHelperRVIZ::createColorRGBA(0.0, 0.0, 0.0, 0.15), marker_lifetime_));
92  ret_markers.markers.push_back(generateResultVoteOverlay(result));
93 
94  return ret_markers;
95 }
96 
97 MarkerArray ISMVotingVisualizerRVIZ::generateBinAndGridMarker(ISM::VotingSpacePtr voting_space_ptr){
98  MarkerArray ret_markers;
99  std::map<std::tuple<double, double, double>, int> bin_to_votecount_map;
100 
101  //tuple<minX, minY, minZ, maxX, maxY, maxZ>
102  std::tuple<int, int, int, int, int, int> grid_bb {INT_MAX, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX, -INT_MAX};
103  double x;
104  double y;
105  double z;
106  ISM::XIndexToYIndex grid = voting_space_ptr->voteGrid;
107 
108  for(ISM::XIndexToYIndex::iterator x_iter = grid.begin(); x_iter!=grid.end(); ++x_iter){
109  for(ISM::YIndexToZIndex::iterator y_iter = x_iter->second.begin(); y_iter!=x_iter->second.end(); ++y_iter){
110  for(ISM::ZIndexToVotingBinPtr::iterator z_iter = y_iter->second.begin(); z_iter!=y_iter->second.end(); ++z_iter){
111  for(ISM::TypeToInnerMap::iterator type_iter = z_iter->second->votes.begin(); type_iter != z_iter->second->votes.end(); ++type_iter){
112  for(ISM::IdToVoteMap::iterator map_iter = type_iter->second.begin(); map_iter !=type_iter->second.end(); ++map_iter){
113  x = x_iter->first * bin_size_;
114  y = y_iter->first * bin_size_;
115  z = z_iter->first * bin_size_;
116 
117  std::get<0>(grid_bb) = std::min(x_iter->first, (int)std::get<0>(grid_bb));
118  std::get<1>(grid_bb) = std::min(y_iter->first, (int)std::get<1>(grid_bb));
119  std::get<2>(grid_bb) = std::min(z_iter->first, (int)std::get<2>(grid_bb));
120 
121 
122  std::get<3>(grid_bb) = std::max(x_iter->first, std::get<3>(grid_bb));
123  std::get<4>(grid_bb) = std::max(y_iter->first, std::get<4>(grid_bb));
124  std::get<5>(grid_bb) = std::max(z_iter->first, std::get<5>(grid_bb));
125 
126 
127  if(bin_to_votecount_map.find(std::make_tuple(x, y, z)) == bin_to_votecount_map.end()){
128  bin_to_votecount_map.insert(std::make_pair(std::make_tuple(x, y, z), 0));
129  }
130  bin_to_votecount_map[std::make_tuple(x,y,z)] += map_iter->second.size();
131  }
132  }
133  }
134  }
135  }
136 
137  int max_count = (int)-INFINITY;
138  int min_count = (int)INFINITY;
139 
140  int i = 0;
141  for(std::map<std::tuple<double, double, double>, int>::iterator iter=bin_to_votecount_map.begin(); iter!=bin_to_votecount_map.end(); ++iter) {
142  if(iter->second > max_count){
143  max_count = iter->second;
144  }
145  if(iter->second < min_count){
146  min_count = iter->second;
147  }
148  }
149  ColorRGBA color;
150  Marker temp_marker;
151  for(std::map<std::tuple<double, double, double>, int>::iterator iter = bin_to_votecount_map.begin(); iter!=bin_to_votecount_map.end(); ++iter) {
152  color = VizHelperRVIZ::hsvToRGBA((120 * (min_count + iter->second) / max_count), 1.0, 1.0);
153  color.a = bin_opacity_;
154 
155  double scale = bin_size_ * bin_scale_;
156 
157  temp_marker = VizHelperRVIZ::createMarkerWithoutTypeAndPose(base_frame_, "binMarker", i, scale, scale, scale, color, marker_lifetime_);
158  temp_marker.type = Marker::CUBE;
159  temp_marker.action = Marker::ADD;
160  temp_marker.pose.position = VizHelperRVIZ::createPoint(std::get<0>(iter->first), std::get<1>(iter->first), std::get<2>(iter->first));
161  ret_markers.markers.push_back(temp_marker);
162 
163  i++;
164  }
165 
166  ret_markers.markers.push_back(generateGridMarker(grid_bb));
167  return ret_markers;
168 }
169 
170 
171 MarkerArray ISMVotingVisualizerRVIZ::generateVoteMarker(std::map<ISM::ObjectPtr, std::vector<ISM::PosePtr>> voter_to_poses_map){
172  MarkerArray ret_markers;
173  MarkerArray temp_arrow_markers;
174  Marker temp_voter_marker;
175  std_msgs::ColorRGBA vote_color;
176  std::string voter_namespace;
177 
178  std::map<std::string, std::vector< std::pair<ISM::PointPtr, std::vector<ISM::PosePtr> > > > united_votes_of_voter;
179 
180  for(auto iter: voter_to_poses_map)
181  {
182  if(united_votes_of_voter.find(iter.first->type) == united_votes_of_voter.end()){
183  std::vector< std::pair<ISM::PointPtr, std::vector<ISM::PosePtr> > > temp_vector;
184  united_votes_of_voter[iter.first->type] = temp_vector;
185  }
186  united_votes_of_voter[iter.first->type].push_back(std::make_pair(iter.first->pose->point, iter.second));
187  }
188 
189 
190  int i = 1;
191  for(auto iter: united_votes_of_voter)
192  {
193  voter_namespace = "votes_of_" + iter.first;
194  vote_color = VizHelperRVIZ::hsvToRGBA(120 + (240.0 / (united_votes_of_voter.size() + 1)) * i , 1.0, 1.0);
195  vote_color.a = vote_opacity_;
196 
197  temp_arrow_markers = VizHelperRVIZ::createCoordinateArrow(iter.second, base_frame_, voter_namespace,
199  ret_markers.markers.insert(ret_markers.markers.end(), temp_arrow_markers.markers.begin(), temp_arrow_markers.markers.end());
200 
201  for(size_t j = 0; j < iter.second.size(); j++){
202  temp_voter_marker = VizHelperRVIZ::createSphereMarker(iter.second[j].first, base_frame_, iter.first, j,
204  temp_voter_marker.color.a = 1.0;
205  ret_markers.markers.push_back(temp_voter_marker);
206  }
207 
208  i++;
209  }
210 
211  return ret_markers;
212 }
213 
214 Marker ISMVotingVisualizerRVIZ::generateGridMarker(std::tuple<int, int, int, int, int, int> grid_bb){
217  ret_marker.type = Marker::LINE_LIST;
218 
219 
220  double x_min = std::get<0>(grid_bb) * bin_size_ - bin_size_ / 2.0;
221  double y_min = std::get<1>(grid_bb) * bin_size_ - bin_size_ / 2.0;
222  double z_min = std::get<2>(grid_bb) * bin_size_ - bin_size_ / 2.0;
223 
224  double x_max = std::get<3>(grid_bb) * bin_size_ + bin_size_ / 2.0;
225  double y_max = std::get<4>(grid_bb) * bin_size_ + bin_size_ / 2.0;
226  double z_max = std::get<5>(grid_bb) * bin_size_ + bin_size_ / 2.0;
227 
228  //prevents missing lines caused by double comparison inaccuracy
229  double epsilon = grid_scale_ * 0.01;
230 
231 
232  for(double y = y_min; y < y_max + epsilon; y += bin_size_){
233  for(double z = z_min; z < z_max + epsilon; z += bin_size_){
234  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x_max, y, z));
235  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x_min, y, z));
236  }
237  }
238  for(double x = x_min; x < x_max + epsilon; x += bin_size_){
239  for(double z = z_min; z < z_max + epsilon; z += bin_size_){
240  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x ,y_max, z));
241  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x, y_min, z));
242  }
243  }
244 
245  for(double x = x_min; x < x_max + epsilon; x += bin_size_){
246  for(double y = y_min; y < y_max + epsilon; y += bin_size_){
247  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x, y, z_max));
248  ret_marker.points.push_back(VizHelperRVIZ::createPoint(x, y, z_min));
249  }
250  }
251 
252  return ret_marker;
253 }
254 
255 
256 std::map<ISM::ObjectPtr, std::vector<ISM::PosePtr>> ISMVotingVisualizerRVIZ::getSortedVotedPoses(ISM::VotingSpacePtr voting_space_ptr)
257 {
258  std::map<ISM::ObjectPtr, std::vector<ISM::PosePtr>> voter_to_poses_map;
259  ISM::XIndexToYIndex grid = voting_space_ptr->voteGrid;
260 
261  for(ISM::XIndexToYIndex::iterator x_iter = grid.begin(); x_iter!=grid.end(); ++x_iter){
262  for(ISM::YIndexToZIndex::iterator y_iter = x_iter->second.begin(); y_iter != x_iter->second.end(); ++y_iter){
263  for(ISM::ZIndexToVotingBinPtr::iterator z_iter = y_iter->second.begin(); z_iter != y_iter->second.end(); ++z_iter){
264  for(ISM::TypeToInnerMap::iterator type_iter = z_iter->second->votes.begin(); type_iter != z_iter->second->votes.end(); ++type_iter){
265  for(ISM::IdToVoteMap::iterator map_iter = type_iter->second.begin(); map_iter != type_iter->second.end(); ++map_iter){
266  for(ISM::VotedPosePtrs::iterator vote_iter = map_iter->second.begin(); vote_iter != map_iter->second.end(); ++vote_iter){
267  //Maps votes to Voters
268  if(voter_to_poses_map.find((*vote_iter)->source) != voter_to_poses_map.end()){
269  voter_to_poses_map[(*vote_iter)->source].push_back((*vote_iter)->pose);
270  }else{
271  std::vector<ISM::PosePtr> temp_vec;
272  voter_to_poses_map.insert(std::make_pair((*vote_iter)->source, temp_vec));
273  voter_to_poses_map[(*vote_iter)->source].push_back((*vote_iter)->pose);
274  }
275  }
276  }
277  }
278  }
279  }
280  }
281  return voter_to_poses_map;
282 }
283 
284 ISM::RecognitionResultPtr ISMVotingVisualizerRVIZ::getRecognitionResultPtrOfPattern(ISM::RecognitionResultPtr result){
285  if(result->patternName == pattern_name_){
286  return result;
287  }else{
288  for(ISM::RecognitionResultPtr subPattern : result->subPatterns) {
289  ISM::RecognitionResultPtr temp_recognition_result_ptr = getRecognitionResultPtrOfPattern(subPattern);
290  if(temp_recognition_result_ptr != NULL){
291  return temp_recognition_result_ptr;
292  }
293  }
294  }
295  return NULL;
296 }
297 
298 }
static Marker createCylinderMarker(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int id, float radius, float height, ColorRGBA color, double markerLifetime)
double epsilon
static ColorRGBA hsvToRGBA(double hue, double saturation, double value)
visualization_msgs::Marker Marker
static geometry_msgs::Point pointToPointMsg(ISM::PointPtr point)
TFSIMD_FORCE_INLINE const tfScalar & y() const
visualization_msgs::Marker generateGridMarker(std::tuple< int, int, int, int, int, int > grid_bb)
static Marker createSphereMarker(ISM::PointPtr point, std::string baseFrame, std::string markerNamespace, int id, float radius, ColorRGBA color, double markerLifetime)
static MarkerArray createCoordinateArrow(std::vector< std::pair< ISM::PointPtr, std::vector< ISM::PosePtr > > > poses_vec, std::string baseFrame, std::string markerNamespace, float arrowScale, float axisScale, ColorRGBA color, double markerLifetime)
visualization_msgs::Marker generateResultVoteOverlay(ISM::RecognitionResultPtr result)
std::map< ISM::ObjectPtr, std::vector< ISM::PosePtr > > getSortedVotedPoses(ISM::VotingSpacePtr voting_space_ptr)
static ColorRGBA createColorRGBA(float red, float green, float blue, float alpha)
ISM::RecognitionResultPtr getRecognitionResultPtrOfPattern(ISM::RecognitionResultPtr result)
#define ROS_INFO(...)
std_msgs::ColorRGBA ColorRGBA
TFSIMD_FORCE_INLINE const tfScalar & x() const
visualization_msgs::MarkerArray generateVoteMarker(std::map< ISM::ObjectPtr, std::vector< ISM::PosePtr >> voter_to_poses_map)
visualization_msgs::MarkerArray MarkerArray
static geometry_msgs::Point createPoint(double x, double y, double z)
TFSIMD_FORCE_INLINE const tfScalar & z() const
void addMarker(visualization_msgs::Marker marker)
visualization_msgs::MarkerArray generateRefMarker(ISM::RecognitionResultPtr result, std::vector< ISM::PosePtr > voted_poses)
void addVisualization(const ISM::PatternNameAndVotingSpaceTuple &voting_space, std::vector< ISM::RecognitionResultPtr > results)
static Marker createMarkerWithoutTypeAndPose(std::string baseFrame, std::string markerNamespace, int id, float xScale, float yScale, float zScale, ColorRGBA color, double markerLifetime)
visualization_msgs::MarkerArray generateBinAndGridMarker(ISM::VotingSpacePtr voting_space_ptr)
static Marker createLineArrow(ISM::PointPtr fromPoint, std::vector< ISM::PosePtr > toPoses, std::string baseFrame, std::string markerNamespace, int &id, float arrowScale, ColorRGBA color, double markerLifetime)
static ColorRGBA confidenceToColor(double confidence)
#define ROS_ERROR(...)


asr_ism_visualizations
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Meißner Pascal, Reckling Reno, Stöckle Patrick, Trautmann Jeremias
autogenerated on Fri Nov 8 2019 03:28:47