gtsam 4.1.1
gtsam
inference-inst.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3* GTSAM Copyright 2010, Georgia Tech Research Corporation,
4* Atlanta, Georgia 30332-0415
5* All Rights Reserved
6* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8* See LICENSE for the license information
9
10* -------------------------------------------------------------------------- */
11
20#pragma once
21
22#include <boost/shared_ptr.hpp>
23#include <utility>
24
27
28namespace gtsam {
29 namespace inference {
30
31 namespace {
32 /* ************************************************************************* */
33 template<class TREE>
34 struct EliminationData {
35 EliminationData* const parentData;
36 FastVector<typename TREE::sharedFactor> childFactors;
37 EliminationData(EliminationData* _parentData, size_t nChildren) :
38 parentData(_parentData) { childFactors.reserve(nChildren); }
39 };
40
41 /* ************************************************************************* */
42 template<class TREE>
43 EliminationData<TREE> eliminationPreOrderVisitor(
44 const typename TREE::sharedNode& node, EliminationData<TREE>& parentData)
45 {
46 // This function is called before visiting the children. Here, we create this node's data,
47 // which includes a pointer to the parent data and space for the factors of the children.
48 return EliminationData<TREE>(&parentData, node->children.size());
49 }
50
51 /* ************************************************************************* */
52 template<class TREE, class RESULT>
53 struct EliminationPostOrderVisitor
54 {
55 RESULT& result;
56 const typename TREE::Eliminate& eliminationFunction;
57 EliminationPostOrderVisitor(RESULT& result, const typename TREE::Eliminate& eliminationFunction) :
58 result(result), eliminationFunction(eliminationFunction) {}
59 void operator()(const typename TREE::sharedNode& node, EliminationData<TREE>& myData)
60 {
61 // Call eliminate on the node and add the result to the parent's gathered factors
62 typename TREE::sharedFactor childFactor = node->eliminate(result, eliminationFunction, myData.childFactors);
63 if(childFactor && !childFactor->empty())
64 myData.parentData->childFactors.push_back(childFactor);
65 }
66 };
67 }
68
69 /* ************************************************************************* */
73 template<class TREE, class RESULT>
74 FastVector<typename TREE::sharedFactor>
75 EliminateTree(RESULT& result, const TREE& tree, const typename TREE::Eliminate& function)
76 {
77 // Do elimination using a depth-first traversal. During the pre-order visit (see
78 // eliminationPreOrderVisitor), we store a pointer to the parent data (where we'll put the
79 // remaining factor) and reserve a vector of factors to store the children elimination
80 // results. During the post-order visit (see eliminationPostOrderVisitor), we call dense
81 // elimination (using the gathered child factors) and store the result in the parent's
82 // gathered factors.
83 EliminationData<TREE> rootData(0, tree.roots().size());
84 EliminationPostOrderVisitor<TREE,RESULT> visitorPost(result, function);
85 treeTraversal::DepthFirstForest(tree, rootData, eliminationPreOrderVisitor<TREE>, visitorPost);
86
87 // Return remaining factors
88 return rootData.childFactors;
89 }
90
91 }
92}
A thin wrapper around std::vector that uses a custom allocator.
FastVector< typename TREE::sharedFactor > EliminateTree(RESULT &result, const TREE &tree, const typename TREE::Eliminate &function)
Eliminate an elimination tree or a Bayes tree (used internally).
Definition: inference-inst.h:75
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
Traverse a forest depth-first with pre-order and post-order visits.
Definition: treeTraversal-inst.h:77