00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include <graph_mapping_utils/geometry.h>
00040 #include <graph_mapping_utils/ros.h>
00041 #include <pose_graph/visualization.h>
00042 #include <visualization_msgs/MarkerArray.h>
00043 #include <boost/foreach.hpp>
00044
00045 namespace pose_graph
00046 {
00047
00048 namespace vm=visualization_msgs;
00049 namespace util=graph_mapping_utils;
00050 namespace gm=geometry_msgs;
00051
00052 ConstraintGraphVisualizer::ConstraintGraphVisualizer (bool visualize_node_ids) :
00053 param_nh_("~"), graph_frame_(util::searchParam<std::string>(param_nh_, "optimization_frame", "graph_optimization")),
00054 visualize_node_ids_(visualize_node_ids),
00055 comm_nh_(), marker_pub_(comm_nh_.advertise<vm::Marker> ("visualization_marker", 10)),
00056 marker_array_pub_(comm_nh_.advertise<vm::MarkerArray> ("visualization_marker_array", 10))
00057 {
00058 }
00059
00060
00061 void ConstraintGraphVisualizer::visualize (const ConstraintGraph& g) const
00062 {
00063 vm::Marker edges, node_marker;
00064 vm::MarkerArray nodes;
00065 const unsigned NODE_MARKER_ID_BASE = 1000;
00066
00067 edges.header.frame_id = node_marker.header.frame_id = graph_frame_;
00068 node_marker.id = 1;
00069 edges.id = 0;
00070
00071 node_marker.header.stamp = edges.header.stamp = ros::Time::now();
00072
00073 edges.ns = node_marker.ns = "constraint_graph";
00074 if (visualize_node_ids_)
00075 node_marker.type = vm::Marker::SPHERE;
00076 else
00077 node_marker.type = vm::Marker::SPHERE_LIST;
00078 edges.type = vm::Marker::LINE_LIST;
00079 edges.action = node_marker.action = vm::Marker::ADD;
00080 edges.scale.x = 0.03;
00081 node_marker.scale.x = 0.07;
00082 node_marker.scale.y = 0.07;
00083 node_marker.scale.z = 0.07;
00084 node_marker.pose.orientation.w = 1.0;
00085 edges.color.b = node_marker.color.r = 1.0;
00086 edges.color.a = node_marker.color.a = 1.0;
00087
00088 nodes.markers.reserve(g.allNodes().size());
00089
00090 BOOST_FOREACH (const unsigned n, g.allNodes()) {
00091 if (!g.hasOptimizedPose(n)) {
00092 ROS_DEBUG_STREAM_NAMED ("visualize", "Not visualizing node " << n << " as it has no optimized pose");
00093 continue;
00094 }
00095
00096
00097 const tf::Pose pose = g.getOptimizedPose(n);
00098 const gm::Pose gm_pose = util::toPose(pose);
00099 const unsigned node_ind = nodes.markers.size();
00100 if (visualize_node_ids_)
00101 {
00102 nodes.markers.push_back(node_marker);
00103 nodes.markers[node_ind].pose.position = gm_pose.position;
00104 nodes.markers[node_ind].id = n + NODE_MARKER_ID_BASE;
00105 }
00106 else
00107 {
00108 node_marker.points.push_back(gm_pose.position);
00109 }
00110
00111
00112 BOOST_FOREACH (const unsigned e, g.incidentEdges(n)) {
00113 if (g.incidentNodes(e).first == n) {
00114 edges.points.push_back(gm_pose.position);
00115 const gm::Pose pose2 = util::transformPose(pose, g.getConstraint(e).pose);
00116 edges.points.push_back(pose2.position);
00117 }
00118 }
00119 }
00120
00121 marker_pub_.publish(edges);
00122 if (visualize_node_ids_)
00123 marker_array_pub_.publish(nodes);
00124 else
00125 marker_pub_.publish(node_marker);
00126 }
00127
00128 }