visualization.cpp
Go to the documentation of this file.
00001 #include "surface_perception/visualization.h"
00002 
00003 #include <sstream>
00004 #include <vector>
00005 
00006 #include "ros/ros.h"
00007 #include "visualization_msgs/Marker.h"
00008 #include "visualization_msgs/MarkerArray.h"
00009 
00010 #include "surface_perception/axes_marker.h"
00011 #include "surface_perception/surface_objects.h"
00012 
00013 using visualization_msgs::Marker;
00014 
00015 namespace surface_perception {
00016 SurfaceViz::SurfaceViz(const ros::Publisher& marker_pub)
00017     : marker_pub_(marker_pub), surfaces_(), markers_() {}
00018 
00019 void SurfaceViz::set_surface_objects(
00020     const std::vector<SurfaceObjects>& surfaces) {
00021   surfaces_ = surfaces;
00022 }
00023 
00024 void SurfaceViz::Show() {
00025   Hide();
00026   SurfaceMarkers(surfaces_, &markers_);
00027   for (size_t i = 0; i < markers_.size(); ++i) {
00028     const Marker& marker = markers_[i];
00029     marker_pub_.publish(marker);
00030   }
00031 }
00032 
00033 void SurfaceViz::Hide() {
00034   for (size_t i = 0; i < markers_.size(); ++i) {
00035     Marker marker;
00036     marker.ns = markers_[i].ns;
00037     marker.id = markers_[i].id;
00038     marker.action = Marker::DELETE;
00039     marker_pub_.publish(marker);
00040   }
00041   markers_.clear();
00042 }
00043 
00044 void SurfaceMarker(const Surface& surface, visualization_msgs::Marker* marker) {
00045   marker->type = Marker::CUBE;
00046   marker->header = surface.pose_stamped.header;
00047   marker->pose = surface.pose_stamped.pose;
00048   marker->scale = surface.dimensions;
00049   marker->color.r = 1;
00050   marker->color.b = 1;
00051   marker->color.a = 0.5;
00052 }
00053 
00054 void ObjectMarkers(const std::vector<Object>& objects,
00055                    std::vector<visualization_msgs::Marker>* markers) {
00056   for (size_t i = 0; i < objects.size(); ++i) {
00057     const Object& object = objects[i];
00058     Marker marker;
00059     marker.type = Marker::CUBE;
00060     marker.header = object.pose_stamped.header;
00061     marker.pose = object.pose_stamped.pose;
00062     marker.scale = object.dimensions;
00063     marker.color.g = 1;
00064     marker.color.a = 0.5;
00065     markers->push_back(marker);
00066   }
00067 }
00068 
00069 void SurfaceMarkers(const std::vector<SurfaceObjects>& surfaces,
00070                     std::vector<visualization_msgs::Marker>* markers) {
00071   for (size_t surface_i = 0; surface_i < surfaces.size(); ++surface_i) {
00072     const SurfaceObjects& surface_objects = surfaces[surface_i];
00073     Marker surface_marker;
00074     SurfaceMarker(surface_objects.surface, &surface_marker);
00075     surface_marker.ns = "surface";
00076     surface_marker.id = surface_i;
00077     markers->push_back(surface_marker);
00078 
00079     std::stringstream obj_ns;
00080     obj_ns << "surface_" << surface_i;
00081     std::vector<Marker> object_markers;
00082     ObjectMarkers(surface_objects.objects, &object_markers);
00083 
00084     for (size_t obj_i = 0; obj_i < object_markers.size(); ++obj_i) {
00085       object_markers[obj_i].ns = obj_ns.str();
00086       object_markers[obj_i].id = obj_i;
00087       markers->push_back(object_markers[obj_i]);
00088 
00089       std::stringstream axes_ns;
00090       axes_ns << obj_ns.str() << "_object_" << obj_i;
00091       axes_ns << "_axes";
00092       visualization_msgs::MarkerArray axesMarkers = GetAxesMarkerArray(
00093           axes_ns.str(), object_markers[obj_i].header.frame_id,
00094           object_markers[obj_i].pose,
00095           std::min(object_markers[obj_i].scale.x,
00096                    object_markers[obj_i].scale.y) /
00097               2.0);
00098 
00099       for (size_t axis_i = 0; axis_i < axesMarkers.markers.size(); ++axis_i) {
00100         markers->push_back(axesMarkers.markers[axis_i]);
00101       }
00102     }
00103   }
00104 }
00105 }  // namespace surface_perception


surface_perception
Author(s): Yu-Tang Peng
autogenerated on Thu Jun 6 2019 17:36:21