testDSFMap.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 
19 #include <gtsam/base/DSFMap.h>
20 
21 #include <boost/assign/std/list.hpp>
22 #include <boost/assign/std/set.hpp>
23 using namespace boost::assign;
24 
26 
27 #include <iostream>
28 
29 using namespace std;
30 using namespace gtsam;
31 
32 /* ************************************************************************* */
33 TEST(DSFMap, find) {
34  DSFMap<size_t> dsf;
35  EXPECT(dsf.find(0)==0);
36  EXPECT(dsf.find(2)==2);
37  EXPECT(dsf.find(0)==0);
38  EXPECT(dsf.find(2)==2);
39  EXPECT(dsf.find(0) != dsf.find(2));
40 }
41 
42 /* ************************************************************************* */
43 TEST(DSFMap, merge) {
44  DSFMap<size_t> dsf;
45  dsf.merge(0,2);
46  EXPECT(dsf.find(0) == dsf.find(2));
47 }
48 
49 /* ************************************************************************* */
50 TEST(DSFMap, merge2) {
51  DSFMap<size_t> dsf;
52  dsf.merge(2,0);
53  EXPECT(dsf.find(0) == dsf.find(2));
54 }
55 
56 /* ************************************************************************* */
57 TEST(DSFMap, merge3) {
58  DSFMap<size_t> dsf;
59  dsf.merge(0,1);
60  dsf.merge(1,2);
61  EXPECT(dsf.find(0) == dsf.find(2));
62 }
63 
64 /* ************************************************************************* */
65 TEST(DSFMap, mergePairwiseMatches) {
66 
67  // Create some "matches"
68  typedef pair<size_t,size_t> Match;
69  list<Match> matches;
70  matches += Match(1,2), Match(2,3), Match(4,5), Match(4,6);
71 
72  // Merge matches
73  DSFMap<size_t> dsf;
74  for(const Match& m: matches)
75  dsf.merge(m.first,m.second);
76 
77  // Each point is now associated with a set, represented by one of its members
78  size_t rep1 = dsf.find(1), rep2 = dsf.find(4);
79  EXPECT_LONGS_EQUAL(rep1,dsf.find(2));
80  EXPECT_LONGS_EQUAL(rep1,dsf.find(3));
81  EXPECT_LONGS_EQUAL(rep2,dsf.find(5));
82  EXPECT_LONGS_EQUAL(rep2,dsf.find(6));
83 }
84 
85 /* ************************************************************************* */
86 TEST(DSFMap, mergePairwiseMatches2) {
87 
88  // Create some measurements with image index and feature index
89  typedef pair<size_t,size_t> Measurement;
90  Measurement m11(1,1),m12(1,2),m14(1,4); // in image 1
91  Measurement m22(2,2),m23(2,3),m25(2,5),m26(2,6); // in image 2
92 
93  // Add them all
94  list<Measurement> measurements;
95  measurements += m11,m12,m14, m22,m23,m25,m26;
96 
97  // Create some "matches"
98  typedef pair<Measurement,Measurement> Match;
99  list<Match> matches;
100  matches += Match(m11,m22), Match(m12,m23), Match(m14,m25), Match(m14,m26);
101 
102  // Merge matches
104  for(const Match& m: matches)
105  dsf.merge(m.first,m.second);
106 
107  // Check that sets are merged correctly
108  EXPECT(dsf.find(m22)==dsf.find(m11));
109  EXPECT(dsf.find(m23)==dsf.find(m12));
110  EXPECT(dsf.find(m25)==dsf.find(m14));
111  EXPECT(dsf.find(m26)==dsf.find(m14));
112 }
113 
114 /* ************************************************************************* */
115 TEST(DSFMap, sets){
116  // Create some "matches"
117  typedef pair<size_t,size_t> Match;
118  list<Match> matches;
119  matches += Match(1,2), Match(2,3), Match(4,5), Match(4,6);
120 
121  // Merge matches
122  DSFMap<size_t> dsf;
123  for(const Match& m: matches)
124  dsf.merge(m.first,m.second);
125 
126  map<size_t, set<size_t> > sets = dsf.sets();
127  set<size_t> s1, s2;
128  s1 += 1,2,3;
129  s2 += 4,5,6;
130 
131  /*for(key_pair st: sets){
132  cout << "Set " << st.first << " :{";
133  for(const size_t s: st.second)
134  cout << s << ", ";
135  cout << "}" << endl;
136  }*/
137 
138  EXPECT(s1 == sets[1]);
139  EXPECT(s2 == sets[4]);
140 }
141 
142 /* ************************************************************************* */
143 TEST(DSFMap, findIndexPair) {
144  DSFMap<IndexPair> dsf;
145  EXPECT(dsf.find(IndexPair(1,2))==IndexPair(1,2));
146  EXPECT(dsf.find(IndexPair(1,2)) != dsf.find(IndexPair(1,3)));
147 }
148 /* ************************************************************************* */
149 int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
150 /* ************************************************************************* */
151 
Matrix3f m
TEST(DSFMap, find)
Definition: testDSFMap.cpp:33
static int runAllTests(TestResult &result)
void merge(const KEY &x, const KEY &y)
Merge two sets.
Definition: DSFMap.h:87
KEY find(const KEY &key) const
Given key, find the representative key for the set in which it lives.
Definition: DSFMap.h:81
Definition: Half.h:150
GenericMeasurement< Point2, Point2 > Measurement
Definition: simulated2D.h:263
int main()
Definition: testDSFMap.cpp:149
#define EXPECT(condition)
Definition: Test.h:151
Small utility class for representing a wrappable pairs of ints.
Definition: DSFMap.h:117
traits
Definition: chartTesting.h:28
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:155
Allow for arbitrary type in DSF.
std::map< KEY, Set > sets() const
return all sets, i.e. a partition of all elements
Definition: DSFMap.h:105


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:26