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 <pose_graph/message_conversion.h>
00040 #include <graph_mapping_utils/geometry.h>
00041 #include <boost/foreach.hpp>
00042
00043 namespace pose_graph
00044 {
00045
00046 msg::ConstraintGraphMessage constraintGraphToMessage (const ConstraintGraph& g)
00047 {
00048 msg::ConstraintGraphMessage m;
00049 NodeSet nodes = g.allNodes();
00050 ROS_DEBUG_STREAM_NAMED ("message_conversion", "Converting graph with " << nodes.size() <<
00051 " nodes to message");
00052 m.nodes.clear();
00053 m.edges.clear();
00054 BOOST_FOREACH (const unsigned n, nodes) {
00055 msg::Node node;
00056 node.id = n;
00057 if (g.hasOptimizedPose(n)) {
00058 node.optimized_pose.resize(1);
00059 node.optimized_pose[0] = graph_mapping_utils::toPose(g.getOptimizedPose(n));
00060 }
00061 m.nodes.push_back(node);
00062 }
00063
00064 BOOST_FOREACH (const unsigned e, g.allEdges()) {
00065 msg::Edge edge;
00066 edge.id = e;
00067 std::pair<unsigned, unsigned> nodes = g.incidentNodes(e);
00068 edge.constraint.src = nodes.first;
00069 edge.constraint.dest = nodes.second;
00070 edge.constraint.constraint = g.getConstraint(e);
00071 m.edges.push_back(edge);
00072 }
00073 return m;
00074 }
00075
00076 ConstraintGraph constraintGraphFromMessage (const msg::ConstraintGraphMessage& m)
00077 {
00078 ConstraintGraph g;
00079 ROS_DEBUG_STREAM_NAMED ("message_conversion", "Converting graph from message with " << m.nodes.size() <<
00080 " nodes ");
00081 ROS_ASSERT (g.allNodes().empty());
00082 BOOST_FOREACH (const msg::Node& n, m.nodes) {
00083 const unsigned id = n.id;
00084 g.addNode(id);
00085 if (n.optimized_pose.size() == 1)
00086 g.setOptimizedPose(id, graph_mapping_utils::toPose(n.optimized_pose[0]));
00087 }
00088
00089 BOOST_FOREACH (const msg::Edge& e, m.edges)
00090 g.addEdge(e.id, e.constraint.src, e.constraint.dest, e.constraint.constraint);
00091
00092 return g;
00093 }
00094
00095 void applyDiff (ConstraintGraph* g, const msg::ConstraintGraphDiff& diff)
00096 {
00097 ROS_ASSERT (diff.new_nodes.size() == diff.new_node_timestamps.size());
00098 BOOST_FOREACH (const msg::Node& n, diff.new_nodes) {
00099 g->addNode(n.id);
00100 }
00101 BOOST_FOREACH (const msg::Edge& e, diff.new_edges) {
00102 g->addEdge(e.id, e.constraint.src, e.constraint.dest, e.constraint.constraint);
00103 }
00104 }
00105
00106
00107 }
00108
00109