22 #pragma GCC diagnostic push 23 #pragma GCC diagnostic ignored "-Wunused-variable" 26 #include <boost/graph/breadth_first_search.hpp> 28 #pragma GCC diagnostic pop 30 #include <boost/graph/prim_minimum_spanning_tree.hpp> 41 template <
typename Vertex,
typename Graph>
void discover_vertex(Vertex v,
const Graph& g)
const {
42 KEY key = boost::get(boost::vertex_name, g, v);
43 ordering_.push_front(key);
45 std::list<KEY>& ordering_;
52 typedef typename SGraph<KEY>::Vertex SVertex;
56 std::map<KEY, SVertex> key2vertex;
57 boost::tie(g, root, key2vertex) = gtsam::predecessorMap2Graph<SGraph<KEY>, SVertex, KEY>(p_map);
62 boost::breadth_first_search(g, root, boost::visitor(vis));
67 template<
class G,
class F,
class KEY>
71 typedef typename boost::graph_traits<SDGraph<KEY> >::vertex_descriptor BoostVertex;
72 std::map<KEY, BoostVertex> key2vertex;
73 typename G::const_iterator itFactor;
76 for(itFactor=graph.begin(); itFactor!=graph.end(); itFactor++) {
79 if ((*itFactor)->keys().size() != 2)
83 boost::shared_ptr<F> factor = boost::dynamic_pointer_cast<F>(*itFactor);
85 if (!factor)
continue;
88 KEY key1 = factor->keys()[0];
89 KEY key2 = factor->keys()[1];
94 if (key2vertex.find(key1) == key2vertex.end()) {
95 v1 = add_vertex(key1, g);
96 key2vertex.insert(std::pair<KEY,KEY>(key1, v1));
98 v1 = key2vertex[key1];
101 if (key2vertex.find(key2) == key2vertex.end()) {
102 v2 = add_vertex(key2, g);
103 key2vertex.insert(std::pair<KEY,KEY>(key2, v2));
105 v2 = key2vertex[key2];
108 boost::property<boost::edge_weight_t, double> edge_property(1.0);
109 boost::add_edge(v1, v2, edge_property, g);
116 template<
class G,
class V,
class KEY>
117 boost::tuple<G, V, std::map<KEY,V> >
121 std::map<KEY, V> key2vertex;
123 bool foundRoot =
false;
124 for(
auto child_parent: p_map) {
126 std::tie(child,parent) = child_parent;
127 if (key2vertex.find(child) == key2vertex.end()) {
128 v1 = add_vertex(child, g);
129 key2vertex.insert(std::make_pair(child, v1));
131 v1 = key2vertex[child];
133 if (key2vertex.find(parent) == key2vertex.end()) {
134 v2 = add_vertex(parent, g);
135 key2vertex.insert(std::make_pair(parent, v2));
137 v2 = key2vertex[parent];
143 boost::add_edge(v2, v1, g);
147 throw std::invalid_argument(
"predecessorMap2Graph: invalid predecessor map!");
149 return boost::tuple<G, V, std::map<KEY, V> >(g, root, key2vertex);
153 template <
class V,
class POSE,
class KEY>
157 boost::shared_ptr<Values> config_;
163 template <
typename Edge,
typename Graph>
void tree_edge(Edge edge,
const Graph& g)
const {
164 KEY key_from = boost::get(boost::vertex_name, g, boost::source(edge, g));
165 KEY key_to = boost::get(boost::vertex_name, g, boost::target(edge, g));
166 POSE relativePose = boost::get(boost::edge_weight, g, edge);
167 config_->insert(key_to, config_->at<POSE>(key_from).compose(relativePose));
173 template<
class G,
class Factor,
class POSE,
class KEY>
175 const POSE& rootPose) {
178 typedef typename boost::adjacency_list<
179 boost::vecS, boost::vecS, boost::directedS,
180 boost::property<boost::vertex_name_t, KEY>,
181 boost::property<boost::edge_weight_t, POSE> > PoseGraph;
182 typedef typename boost::graph_traits<PoseGraph>::vertex_descriptor PoseVertex;
183 typedef typename boost::graph_traits<PoseGraph>::edge_descriptor PoseEdge;
187 std::map<KEY, PoseVertex> key2vertex;
188 boost::tie(g, root, key2vertex) =
189 predecessorMap2Graph<PoseGraph, PoseVertex, KEY>(tree);
192 PoseEdge edge12, edge21;
194 for(
typename G::sharedFactor nl_factor: graph) {
196 if (nl_factor->keys().size() > 2)
197 throw std::invalid_argument(
"composePoses: only support factors with at most two keys");
200 boost::shared_ptr<Factor> factor = boost::dynamic_pointer_cast<Factor>(nl_factor);
201 if (!factor)
continue;
203 KEY key1 = factor->key1();
204 KEY key2 = factor->key2();
206 PoseVertex v1 = key2vertex.find(key1)->second;
207 PoseVertex v2 = key2vertex.find(key2)->second;
209 POSE l1Xl2 = factor->measured();
210 boost::tie(edge12, found1) = boost::edge(v1, v2, g);
211 boost::tie(edge21, found2) = boost::edge(v2, v1, g);
212 if (found1 && found2)
throw std::invalid_argument (
"composePoses: invalid spanning tree");
213 if (!found1 && !found2)
continue;
215 boost::put(boost::edge_weight, g, edge12, l1Xl2);
217 boost::put(boost::edge_weight, g, edge21, l1Xl2.inverse());
221 boost::shared_ptr<Values> config(
new Values);
222 KEY rootKey = boost::get(boost::vertex_name, g, root);
223 config->insert(rootKey, rootPose);
225 boost::breadth_first_search(g, root, boost::visitor(vis));
231 template<
class G,
class KEY,
class FACTOR2>
235 SDGraph<KEY> g = gtsam::toBoostGraph<G, FACTOR2, KEY>(fg);
238 std::vector<typename SDGraph<KEY>::Vertex> p_map(boost::num_vertices(g));
239 prim_minimum_spanning_tree(g, &p_map[0]);
244 for(
const typename SDGraph<KEY>::Vertex& vi: p_map){
245 KEY key = boost::get(boost::vertex_name, g, *itVertex);
246 KEY parent = boost::get(boost::vertex_name, g, vi);
254 template<
class G,
class KEY,
class FACTOR2>
257 typedef typename G::sharedFactor F ;
259 for(
const F& factor: g)
261 if (factor->keys().size() > 2)
262 throw(std::invalid_argument(
"split: only support factors with at most two keys"));
264 if (factor->keys().size() == 1) {
265 Ab1.push_back(factor);
269 boost::shared_ptr<FACTOR2> factor2 = boost::dynamic_pointer_cast<
271 if (!factor2)
continue;
273 KEY key1 = factor2->key1();
274 KEY key2 = factor2->key2();
276 if ((tree.find(key1) != tree.end() &&
277 tree.find(key1)->second.compare(key2) == 0) ||
278 (tree.find(key2) != tree.end() &&
279 tree.find(key2)->second.compare(key1)== 0) )
280 Ab1.push_back(factor2);
282 Ab2.push_back(factor2);
PredecessorMap< KEY > findMinimumSpanningTree(const G &fg)
find the minimum spanning tree using boost graph library
Definition: graph-inl.h:232
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
Definition: graph-inl.h:154
void insert(const KEY &key, const KEY &parent)
convenience insert so we can pass ints for TypedSymbol keys
Definition: graph.h:61
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Split the graph into two parts: one corresponds to the given spanning tree, and the other corresponds...
Definition: graph-inl.h:255
boost::shared_ptr< Values > composePoses(const G &graph, const PredecessorMap< KEY > &tree, const POSE &rootPose)
Compose the poses by following the chain specified by the spanning tree.
Definition: graph-inl.h:174
std::list< KEY > predecessorMap2Keys(const PredecessorMap< KEY > &p_map)
Generate a list of keys from a spanning tree represented by its predecessor map.
Definition: graph-inl.h:50
Definition: graph-inl.h:38
boost::tuple< G, V, std::map< KEY, V > > predecessorMap2Graph(const PredecessorMap< KEY > &p_map)
Build takes a predecessor map, and builds a directed graph corresponding to the tree.
Definition: graph-inl.h:118
SDGraph is undirected graph with variable keys and double edge weights.
Definition: graph.h:38
Graph algorithm using boost library.
SDGraph< KEY > toBoostGraph(const G &graph)
Convert the factor graph to an SDGraph G = Graph type F = Factor type Key = Key type.
Definition: graph-inl.h:68
Map from variable key to parent key.
Definition: graph.h:58
Global functions in a separate testing namespace.
Definition: chartTesting.h:28