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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:56