ism_result_visualizer_rviz.cpp
Go to the documentation of this file.
1 
17 //Header include
19 
20 //Ilcas includes
21 #include <ISM/utility/GeometryHelper.hpp>
22 #include <ISM/common_type/Pose.hpp>
23 #include <ISM/common_type/Object.hpp>
24 #include <ISM/common_type/RecognitionResult.hpp>
27 
28 //ros includes
29 #include <ros/package.h>
30 
31 //other includes
32 #include <algorithm>
33 
34 namespace VIZ
35 {
36 
37 
38 void ISMResultVisualizerRVIZ::addVisualization(const ISM::RecognitionResultPtr recognition_result)
39 {
40 
41  visualization_msgs::MarkerArray current_marker_array = getMarkersFromResult(recognition_result);
42 
43  addMarker(current_marker_array);
45 }
46 
49 
50  ISM::PosePtr id = ISM::PosePtr(new ISM::Pose(new ISM::Point(), new ISM::Quaternion()));
51  std::vector<ISM::PosePtr> poses;
52  poses.push_back(id);
53 
54  id->quat = (ISM::GeometryHelper::getQuatFromRPY(id->quat, 0, 0, 180));
55  temp_marker = VizHelperRVIZ::createCoordinateMarkerWithAngle(id, "map", "test_marker", 1, 0.1, 0.01, 0);
56 
57  return temp_marker;
58 }
59 
61 {
63  ism_to_depth_map_.clear();
64  ism_to_parent_ism_map_.clear();
65  last_ref_pose_ = nullptr;
66  z_offset_ = use_z_max_ ? -INFINITY : INFINITY;
67 
68  if (result->referencePose == nullptr)
69  {
70  return ret_markers;
71  }
72 
73  std::vector<ISM::RecognitionResultPtr> ism_tree = traverseTree(result, 0);
74  int max_depth = 0;
75  for(std::pair<std::string, int> ism_to_depth_map_iter : ism_to_depth_map_){
76  max_depth = std::max(max_depth, ism_to_depth_map_iter.second);
77  }
78  max_depth += 1;
79 
80  int i = 1;
81  int j = 0;
82  std::stringstream ns_stream;
83  ns_stream << "ism_" << result->patternName << "_" << scene_iterator_ << "";
84  std::string marker_namespace = ns_stream.str();
85  Marker temp_marker;
86 
87  ref_pose_ = result->referencePose;
88  for(ISM::RecognitionResultPtr leaf : ism_tree) {
89  std::string leaf_pattern_name = leaf->patternName;
90  int inverse_depth = max_depth - ism_to_depth_map_[leaf_pattern_name];
91  float ref_scaling = (leaf_pattern_name == result->patternName) ? ism_marker_scale_ * 2 : ism_marker_scale_ / 2;
92  ISM::PosePtr from_pose_ptr = getAdjustedPose(leaf->referencePose, inverse_depth, false);
93  ISM::PointPtr from_point_ptr = from_pose_ptr->point;
94 
95  temp_marker = VizHelperRVIZ::createSphereMarker(from_point_ptr, base_frame_, marker_namespace, ++j, ref_scaling, VizHelperRVIZ::confidenceToColor(leaf->confidence), marker_lifetime_);
96  ret_markers.markers.push_back(temp_marker);
97 
98  if(last_ref_pose_ != nullptr){
99  //if ISM is not Root ISM
100  from_pose_ptr->quat =(calculateOrientation(getAdjustedPose(ism_to_parent_ism_map_[leaf_pattern_name], inverse_depth +1, false)->point, from_point_ptr));
101  temp_marker = VizHelperRVIZ::createConeMarker(from_pose_ptr, base_frame_, marker_namespace, ++j, ref_scaling * 0.9, VizHelperRVIZ::confidenceToColor(leaf->confidence), marker_lifetime_);
102  ret_markers.markers.push_back(temp_marker);
103  }
104 
105  ColorRGBA arrow_color = VizHelperRVIZ::createColorRGBA(0, 0, 0, 0);
107  if(ism_to_color_map_.find(result->patternName) == ism_to_color_map_.end()){
108  ism_to_color_map_.insert(std::make_pair(result->patternName, VizHelperRVIZ::hsvToRGBA(120 + (240.0 / (scene_count_ + 1)) * (ism_to_color_map_.size() + 1) , 1.0, 1.0)));
109  }
110  arrow_color = ism_to_color_map_[result->patternName];
111  }else{
112  arrow_color = VizHelperRVIZ::hsvToRGBA(120 + (240.0 / (ism_tree.size() + 1)) * i , 1.0, 1.0);
113  }
114  std::vector<ISM::PosePtr> to_poses;
115 
116  for(ISM::ObjectPtr object : leaf->recognizedSet->objects){
117 
118  to_poses.push_back(getAdjustedPose(object->pose, inverse_depth, true));
119 
120  if(ism_to_depth_map_.find(object->type) != ism_to_depth_map_.end()){
121  continue;
122  }
123 
124  if(ism_to_depth_map_.find(object->type + object->observedId) == ism_to_depth_map_.end()){
125  ISM::PosePtr adjusted_pose_ptr = getAdjustedPose(object->pose, inverse_depth, true);
126 
127 
128  //Meshmarker
129  if(!object->ressourcePath.empty()){
130 
131  Marker mesh_marker = VizHelperRVIZ::createMeshMarker(getAdjustedPose(object->pose, inverse_depth, true), base_frame_, marker_namespace, ++j, VizHelperRVIZ::getColorOfObject(object), marker_lifetime_, object->ressourcePath.c_str());
132 
133  mesh_marker.scale.x = 0.0001;
134  mesh_marker.scale.y = 0.0001;
135  mesh_marker.scale.z = 0.0001;
136  ret_markers.markers.push_back(mesh_marker);
137  }
138 
139  //Cylinder
140  double height = use_z_max_ ? std::abs(object->pose->point->eigen.z() - std::max(adjusted_pose_ptr->point->eigen.z(), z_offset_)) : std::abs(z_offset_ - std::min(object->pose->point->eigen.z(), adjusted_pose_ptr->point->eigen.z()));
141 
142  ISM::PosePtr cylinder_pose_ptr = calculateCylinderPose(object->pose, adjusted_pose_ptr->point, height);
143 
144 
145  if(cylinder_map_.find(object->type + object->observedId) == cylinder_map_.end()){
146  cylinder_map_.insert(std::make_pair(object->type + object->observedId, std::make_pair(cylinder_pose_ptr, height)));
147  }else{
148  if(cylinder_map_[object->type + object->observedId].second < height){
149  cylinder_map_[object->type + object->observedId] = std::make_pair(cylinder_pose_ptr, height);
150  }
151  }
152 
153  //Cubearrow
154  adjusted_pose_ptr->quat =(calculateOrientation(adjusted_pose_ptr->point, from_point_ptr, true));
155  arrow_color.a = 0.5;
156  MarkerArray cube_markers = VizHelperRVIZ::createCubeArrow(adjusted_pose_ptr, base_frame_, marker_namespace, ++j, ism_marker_scale_ / 5, arrow_color, VizHelperRVIZ::confidenceToColor(object->weight), marker_lifetime_);
157  ret_markers.markers.insert(ret_markers.markers.end(), cube_markers.markers.begin(), cube_markers.markers.end());
158 
159  }
160  }
161  arrow_color.a = 1;
162  temp_marker = VizHelperRVIZ::createLineArrow(from_point_ptr, to_poses, base_frame_, marker_namespace, ++j, line_scale_, arrow_color, marker_lifetime_);
163  ret_markers.markers.push_back(temp_marker);
164  last_ref_pose_ = from_point_ptr;
165  i++;
166  }
167  scene_iterator_++;
168 
170 
171  int ii = 0;
172  for(auto iter : cylinder_map_){
173  Marker cylinder_marker = VizHelperRVIZ::createCylinderMarker(iter.second.first, base_frame_, "ism_cylinder", ii++, line_scale_ * 2, iter.second.second,
174  VizHelperRVIZ::createColorRGBA(0.0, 0.0, 0.0, 0.25), marker_lifetime_);
175  ret_markers.markers.push_back(cylinder_marker);
176  }
177  }
178  return ret_markers;
179 }
180 
181 ISM::PosePtr ISMResultVisualizerRVIZ::calculateCylinderPose(const ISM::PosePtr from_pose_ptr, const ISM::PointPtr to_point_ptr, double height){
182  ISM::PosePtr ret_pose_ptr = ISM::PosePtr(new ISM::Pose());
183 
184  ret_pose_ptr->quat =(calculateOrientation(from_pose_ptr->point, to_point_ptr));
185  ret_pose_ptr->point->eigen.z() =(std::min(z_offset_, from_pose_ptr->point->eigen.z()) + height / 2);
186  ret_pose_ptr->point->eigen.y() = (from_pose_ptr->point->eigen.y());
187  ret_pose_ptr->point->eigen.x() = (from_pose_ptr->point->eigen.x());
188 
189  return ret_pose_ptr;
190 }
191 
192 
193 ISM::QuaternionPtr ISMResultVisualizerRVIZ::calculateOrientation(ISM::PointPtr from_point_ptr, ISM::PointPtr to_point_ptr, bool pose_relative){
194 
195  //TODO use for cylinder
196  pose_relative = false;
197 
198  double x = from_point_ptr->eigen.x() - to_point_ptr->eigen.x();
199  double y = from_point_ptr->eigen.y() - to_point_ptr->eigen.y();
200  double z = from_point_ptr->eigen.z() - to_point_ptr->eigen.z();
201 
202  double az = atan2(-x, -z);
203  double alt = atan2(y, sqrt(x*x + z*z));
204 
205  if(z <= 0){
206  alt = -alt;
207  az = -az;
208  }
209 
210  az = az < -M_PI / 2 || az > M_PI / 2 ? M_PI - az : az;
211  alt = alt < -M_PI / 2 || alt > M_PI / 2 ? M_PI - alt : alt;
212 
213 
214  Eigen::AngleAxisd rY(-az, Eigen::Vector3d::UnitY());
215  Eigen::AngleAxisd rX(-alt, Eigen::Vector3d::UnitX());
216 
217  return ISM::GeometryHelper::eigenQuatToQuat(rY * rX);
218 }
219 
220 
221 std::vector<ISM::RecognitionResultPtr> ISMResultVisualizerRVIZ::traverseTree(ISM::RecognitionResultPtr result, int depth){
222  std::vector<ISM::RecognitionResultPtr> ism_tree;
223  ism_tree.push_back(result);
224  ism_to_depth_map_[result->patternName] = depth;
225 
226  for(ISM::ObjectPtr object : result->recognizedSet->objects){
227  z_offset_ = use_z_max_? std::max(z_offset_, object->pose->point->eigen.z()) : std::min(z_offset_, object->pose->point->eigen.z());
228  }
229 
230  depth++;
231  for(ISM::RecognitionResultPtr subPattern : result->subPatterns) {
232  ism_to_parent_ism_map_[subPattern->patternName] = result->referencePose;
233 
234  std::vector<ISM::RecognitionResultPtr> childTrees = traverseTree(subPattern, depth);
235  ism_tree.insert(ism_tree.end(), childTrees.begin(), childTrees.end());
236  }
237  return ism_tree;
238 }
239 
240 ISM::PosePtr ISMResultVisualizerRVIZ::getAdjustedPose(ISM::PosePtr pose, int depth, bool is_child)
241 {
242  double delta_offset = is_child ? -tree_depth_size_ / 2 : tree_depth_size_ / 2;
243  ISM::PosePtr ret_pose = ISM::PosePtr(new ISM::Pose());
244  ret_pose->quat =ISM::QuaternionPtr(pose->quat);
245 
246  double z_offset = ignore_z_offset_ ? z_offset_ : pose->point->eigen.z();
247 
248  if(is_pose_relative_){
249  Eigen::Quaternion<double> rotation = ISM::GeometryHelper::quatToEigenQuat(ref_pose_->quat);
250  rotation.normalize();
251  Eigen::Vector3d translation(0.0, 0.0, (-depth * tree_depth_size_) - delta_offset);
252  Eigen::Vector3d offset = rotation._transformVector(translation);
253 
254  const ISM::PointPtr ret_point_ptr = ISM::PointPtr(new ISM::Point(
255  pose->point->eigen.x() + offset[0],
256  pose->point->eigen.y() + offset[1] ,
257  pose->point->eigen.z() + offset[2]));
258  ret_pose->point = ret_point_ptr;
259  }else{
260  const ISM::PointPtr ret_point_ptr = ISM::PointPtr(new ISM::Point(
261  pose->point->eigen.x(),
262  pose->point->eigen.y(),
263  z_offset + depth * tree_depth_size_ + delta_offset));
264  ret_pose->point = ret_point_ptr;
265  }
266 
267  return ret_pose;
268 }
269 }
270 
271 
void addVisualization(const ISM::RecognitionResultPtr recognition_result)
static Marker createCylinderMarker(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int id, float radius, float height, ColorRGBA color, double markerLifetime)
static ColorRGBA hsvToRGBA(double hue, double saturation, double value)
static MarkerArray createCubeArrow(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int &id, float scale, ColorRGBA cubeColor, ColorRGBA arrowColor, double markerLifetime)
static MarkerArray createCoordinateMarkerWithAngle(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int id, float length, float openAngle, double markerLifetime)
visualization_msgs::MarkerArray genTestMarker()
static ColorRGBA getColorOfObject(const ISM::Object &object)
static Marker createConeMarker(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int id, float radius, ColorRGBA color, double markerLifetime)
visualization_msgs::Marker Marker
std::map< std::string, ISM::PosePtr > ism_to_parent_ism_map_
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< ISM::RecognitionResultPtr > traverseTree(ISM::RecognitionResultPtr result, int depth)
static Marker createSphereMarker(ISM::PointPtr point, std::string baseFrame, std::string markerNamespace, int id, float radius, ColorRGBA color, double markerLifetime)
static ColorRGBA createColorRGBA(float red, float green, float blue, float alpha)
std_msgs::ColorRGBA ColorRGBA
TFSIMD_FORCE_INLINE const tfScalar & x() const
visualization_msgs::MarkerArray MarkerArray
TFSIMD_FORCE_INLINE const tfScalar & z() const
void addMarker(visualization_msgs::Marker marker)
static Marker createMeshMarker(ISM::PosePtr pose, std::string baseFrame, std::string markerNamespace, int id, ColorRGBA color, double markerLifetime, std::string resourcePath)
ISM::PosePtr getAdjustedPose(ISM::PosePtr pose, int depth, bool is_child)
Helper method to translate a point. So we can see the tree depth objects residing in trough their cor...
ISM::QuaternionPtr calculateOrientation(ISM::PointPtr from_point_ptr, ISM::PointPtr to_point_ptr, bool pose_relative=false)
std::map< std::string, int > ism_to_depth_map_
std::map< std::string, VIZ::ColorRGBA > ism_to_color_map_
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)
visualization_msgs::MarkerArray getMarkersFromResult(const ISM::RecognitionResultPtr result)
std::map< std::string, std::pair< ISM::PosePtr, double > > cylinder_map_
ISM::PosePtr calculateCylinderPose(const ISM::PosePtr from_pose_ptr, const ISM::PointPtr to_point_ptr, double height)


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