Skip to content

Commit

Permalink
[tracks] Un-ordered feature tracking. openMVG#470
Browse files Browse the repository at this point in the history
 - Add union by rank & path compression to the UnionFind implementation.
 - Remove the Lemon library dependency
  • Loading branch information
pmoulon committed Feb 5, 2016
1 parent a0f1446 commit b4b5149
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/openMVG/tracks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
UNIT_TEST(openMVG tracks "${LEMON_LIBRARY}")

UNIT_TEST(openMVG tracks "")

4 changes: 0 additions & 4 deletions src/openMVG/tracks/tracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
#ifndef OPENMVG_TRACKS_H_
#define OPENMVG_TRACKS_H_

#include "lemon/list_graph.h"
#include "lemon/unionfind.h"
using namespace lemon;

#include "openMVG/matching/indMatch.hpp"
#include "openMVG/tracks/union_find.hpp"
#include "openMVG/tracks/flat_pair_map.hpp"
Expand Down
31 changes: 21 additions & 10 deletions src/openMVG/tracks/union_find.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace openMVG {
//--
// A disjoint-set data structure also called a union–find data structure
// or merge–find set, is a data structure that keeps track of a set of elements
// partitioned into a number of disjoint (nonoverlapping) subsets.
// partitioned into a number of disjoint (non-overlapping) subsets.
// It supports two operations:
// - Find: Determine which subset a particular element is in.
// - It returns an item from this set that serves as its "representative";
Expand All @@ -32,7 +32,9 @@ struct UnionFind
// Represent the DS/UF forest thanks to two array:
// A parent 'pointer tree' where each node holds a reference to its parent node
std::vector<unsigned int> m_cc_parent;
// A 'rank/size array' to know the size of each connected component
// A rank array used for union by rank
std::vector<unsigned int> m_cc_rank;
// A 'size array' to know the size of each connected component
std::vector<unsigned int> m_cc_size;

// Init the UF structure with num_cc nodes
Expand All @@ -46,6 +48,8 @@ struct UnionFind
// Parents id have their own CC id {0,n}
m_cc_parent.resize(num_cc);
std::iota(m_cc_parent.begin(), m_cc_parent.end(), 0);
// Rank array (0)
m_cc_rank.resize(num_cc, 0);
}

// Return the number of nodes that have been initialized in the UF tree
Expand All @@ -60,12 +64,10 @@ struct UnionFind
unsigned int i
)
{
while (i != m_cc_parent[i])
{
m_cc_parent[i] = m_cc_parent[m_cc_parent[i]]; // Path compression
i = m_cc_parent[i];
}
return i;
// Recursively set all branch as children of root (Path compression)
if (m_cc_parent[i] != i)
m_cc_parent[i] = Find(m_cc_parent[i]);
return m_cc_parent[i];
}

// Replace sets containing I and J with their union
Expand All @@ -77,8 +79,15 @@ struct UnionFind
{
i = Find(i);
j = Find(j);
if (i==j) return;
if (m_cc_size[i] < m_cc_size[j])
if (i==j)
{ // Already in the same set. Nothing to do
return;
}

// x and y are not already in same set. Merge them.
// Perform an union by rank:
// - always attach the smaller tree to the root of the larger tree
if (m_cc_rank[i] < m_cc_rank[j])
{
m_cc_parent[i] = j;
m_cc_size[j] += m_cc_size[i];
Expand All @@ -87,6 +96,8 @@ struct UnionFind
{
m_cc_parent[j] = i;
m_cc_size[i] += m_cc_size[j];
if (m_cc_rank[i] > m_cc_rank[j])
++m_cc_rank[i];
}
}
};
Expand Down

0 comments on commit b4b5149

Please sign in to comment.