gtsam
Loading...
Searching...
No Matches
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
19
20#pragma once
21
22#include <utility>
23
26
27namespace gtsam {
28 namespace inference {
29
30 namespace {
31 /* ************************************************************************* */
32 template<class TREE>
33 struct EliminationData {
34 EliminationData* const parentData;
36 EliminationData(EliminationData* _parentData, size_t nChildren) :
37 parentData(_parentData) { childFactors.reserve(nChildren); }
38 };
39
40 /* ************************************************************************* */
41 template<class TREE>
42 EliminationData<TREE> eliminationPreOrderVisitor(
43 const typename TREE::sharedNode& node, EliminationData<TREE>& parentData)
44 {
45 // This function is called before visiting the children. Here, we create this node's data,
46 // which includes a pointer to the parent data and space for the factors of the children.
47 return EliminationData<TREE>(&parentData, node->children.size());
48 }
49
50 /* ************************************************************************* */
51 template<class TREE, class RESULT>
52 struct EliminationPostOrderVisitor
53 {
54 RESULT& result;
55 const typename TREE::Eliminate& eliminationFunction;
56 EliminationPostOrderVisitor(RESULT& result, const typename TREE::Eliminate& eliminationFunction) :
57 result(result), eliminationFunction(eliminationFunction) {}
58 void operator()(const typename TREE::sharedNode& node, EliminationData<TREE>& myData)
59 {
60 // Call eliminate on the node and add the result to the parent's gathered factors
61 typename TREE::sharedFactor childFactor = node->eliminate(result, eliminationFunction, myData.childFactors);
62 if(childFactor && !childFactor->empty())
63 myData.parentData->childFactors.push_back(childFactor);
64 }
65 };
66 }
67
68 /* ************************************************************************* */
72 template<class TREE, class RESULT>
74 EliminateTree(RESULT& result, const TREE& tree, const typename TREE::Eliminate& function)
75 {
76 // Do elimination using a depth-first traversal. During the pre-order visit (see
77 // eliminationPreOrderVisitor), we store a pointer to the parent data (where we'll put the
78 // remaining factor) and reserve a vector of factors to store the children elimination
79 // results. During the post-order visit (see eliminationPostOrderVisitor), we call dense
80 // elimination (using the gathered child factors) and store the result in the parent's
81 // gathered factors.
82 EliminationData<TREE> rootData(0, tree.roots().size());
83 EliminationPostOrderVisitor<TREE,RESULT> visitorPost(result, function);
84 treeTraversal::DepthFirstForest(tree, rootData, eliminationPreOrderVisitor<TREE>, visitorPost);
85
86 // Return remaining factors
87 return rootData.childFactors;
88 }
89
90 }
91}
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:74
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
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:78