testVariableIndex.cpp
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 
22 
24 
25 #include <boost/assign/std/list.hpp>
26 #include <boost/assign/list_of.hpp>
27 using namespace boost::assign;
28 
29 using namespace std;
30 using namespace gtsam;
31 
32 /* ************************************************************************* */
33 // 2 small symbolic graphs shared by all tests
34 
37  fg1.push_factor(0, 1);
38  fg1.push_factor(0, 2);
39  fg1.push_factor(5, 9);
40  fg1.push_factor(2, 3);
41  return fg1;
42 }
43 
46  fg2.push_factor(1, 3);
47  fg2.push_factor(2, 4);
48  fg2.push_factor(3, 5);
49  fg2.push_factor(5, 6);
50  return fg2;
51 }
52 
53 /* ************************************************************************* */
54 TEST(VariableIndex, augment) {
55  auto fg1 = testGraph1(), fg2 = testGraph2();
56  SymbolicFactorGraph fgCombined;
57  fgCombined.push_back(fg1);
58  fgCombined.push_back(fg2);
59 
60  VariableIndex expected(fgCombined);
61  VariableIndex actual(fg1);
62  actual.augment(fg2);
63 
64  LONGS_EQUAL(8, actual.size());
65  LONGS_EQUAL(16, actual.nEntries());
66  LONGS_EQUAL(8, actual.nFactors());
67  EXPECT(assert_equal(expected, actual));
68 }
69 
70 /* ************************************************************************* */
71 TEST(VariableIndex, augment2) {
72 
73  auto fg1 = testGraph1(), fg2 = testGraph2();
74 
75  SymbolicFactorGraph fgCombined;
76  fgCombined.push_back(fg1);
77  fgCombined.push_back(SymbolicFactor::shared_ptr()); // Add an extra empty factor
78  fgCombined.push_back(fg2);
79 
80  VariableIndex expected(fgCombined);
81 
82  FactorIndices newIndices = list_of(5)(6)(7)(8);
83  VariableIndex actual(fg1);
84  actual.augment(fg2, newIndices);
85 
86  LONGS_EQUAL(8, actual.size());
87  LONGS_EQUAL(16, actual.nEntries());
88  LONGS_EQUAL(9, actual.nFactors());
89  EXPECT(assert_equal(expected, actual));
90 }
91 
92 /* ************************************************************************* */
93 TEST(VariableIndex, remove) {
94 
95  auto fg1 = testGraph1(), fg2 = testGraph2();
96 
97  SymbolicFactorGraph fgCombined; fgCombined.push_back(fg1); fgCombined.push_back(fg2);
98 
99  // Create a factor graph containing only the factors from fg2 and with null
100  // factors in the place of those of fg1, so that the factor indices are correct.
101  SymbolicFactorGraph fg2removed(fgCombined);
102  fg2removed.remove(0); fg2removed.remove(1); fg2removed.remove(2); fg2removed.remove(3);
103 
104  // The expected VariableIndex has the same factor indices as fgCombined but
105  // with entries from fg1 removed, and still has all 10 variables.
106  VariableIndex expected(fg2removed);
107  VariableIndex actual(fgCombined);
108  vector<size_t> indices;
109  indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(3);
110  actual.remove(indices.begin(), indices.end(), fg1);
111  std::list<Key> unusedVariables; unusedVariables += 0, 9;
112  actual.removeUnusedVariables(unusedVariables.begin(), unusedVariables.end());
113 
114  CHECK(assert_equal(expected, actual));
115 }
116 
117 /* ************************************************************************* */
118 TEST(VariableIndex, deep_copy) {
119 
120  auto fg1 = testGraph1(), fg2 = testGraph2();
121 
122  // Create original graph and VariableIndex
123  SymbolicFactorGraph fgOriginal; fgOriginal.push_back(fg1); fgOriginal.push_back(fg2);
124  VariableIndex original(fgOriginal);
125  VariableIndex expectedOriginal(fgOriginal);
126 
127  // Create a factor graph containing only the factors from fg2 and with null
128  // factors in the place of those of fg1, so that the factor indices are correct.
129  SymbolicFactorGraph fg2removed(fgOriginal);
130  fg2removed.remove(0); fg2removed.remove(1); fg2removed.remove(2); fg2removed.remove(3);
131  VariableIndex expectedRemoved(fg2removed);
132 
133  // Create a clone and modify the clone - the original should not change
134  VariableIndex clone(original);
135  vector<size_t> indices;
136  indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(3);
137  clone.remove(indices.begin(), indices.end(), fg1);
138  std::list<Key> unusedVariables; unusedVariables += 0, 9;
139  clone.removeUnusedVariables(unusedVariables.begin(), unusedVariables.end());
140 
141  // When modifying the clone, the original should have stayed the same
142  EXPECT(assert_equal(expectedOriginal, original));
143  EXPECT(assert_equal(expectedRemoved, clone));
144 }
145 
146 /* ************************************************************************* */
147 int main() {
148  TestResult tr;
149  return TestRegistry::runAllTests(tr);
150 }
151 /* ************************************************************************* */
Provides additional testing facilities for common data structures.
#define CHECK(condition)
Definition: Test.h:109
static int runAllTests(TestResult &result)
void augment(const FG &factors, boost::optional< const FactorIndices & > newFactorIndices=boost::none)
Matrix expected
Definition: testMatrix.cpp:974
SymbolicFactorGraph testGraph2()
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
TEST(VariableIndex, augment)
Definition: Half.h:150
void remove(size_t i)
Definition: FactorGraph.h:362
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:166
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition: VariableIndex.h:80
SymbolicFactorGraph testGraph1()
void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG &factors)
int main()
#define EXPECT(condition)
Definition: Test.h:151
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition: VariableIndex.h:86
boost::shared_ptr< This > shared_ptr
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:135
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:83
void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey)
Remove unused empty variables (in debug mode verifies they are empty).


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:50:25