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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:01