Berkeley SfM
Public Member Functions | Private Attributes | List of all members
bsfm::FlannDescriptorKDTree Class Reference

#include <flann_descriptor_kdtree.h>

Public Member Functions

 FlannDescriptorKDTree ()
 
 ~FlannDescriptorKDTree ()
 
void AddDescriptor (Descriptor &descriptor)
 
void AddDescriptors (std::vector< Descriptor > &descriptors)
 
bool NearestNeighbor (Descriptor &query, int &nn_index, double &nn_distance)
 

Private Attributes

std::shared_ptr< flann::Index< flann::L2< double > > > index_
 

Detailed Description

Definition at line 60 of file flann_descriptor_kdtree.h.

Constructor & Destructor Documentation

bsfm::FlannDescriptorKDTree::FlannDescriptorKDTree ( )

Definition at line 42 of file flann_descriptor_kdtree.cpp.

42 {}
bsfm::FlannDescriptorKDTree::~FlannDescriptorKDTree ( )

Definition at line 44 of file flann_descriptor_kdtree.cpp.

44  {
45  // Free memory from descriptors in the kd tree.
46  if (index_ != nullptr) {
47  for (size_t ii = 0; ii < index_->size(); ++ii) {
48  double* descriptor = index_->getPoint(ii);
49  delete[] descriptor;
50  }
51  }
52 }
std::shared_ptr< flann::Index< flann::L2< double > > > index_

Member Function Documentation

void bsfm::FlannDescriptorKDTree::AddDescriptor ( Descriptor descriptor)

Definition at line 55 of file flann_descriptor_kdtree.cpp.

55  {
56 
57  // Copy the input descriptor into FLANN's Matrix type.
58  const size_t cols = descriptor.size();
59  flann::Matrix<double> flann_descriptor(new double[cols], 1, cols);
60  for (size_t ii = 0; ii < cols; ++ii) {
61  flann_descriptor[0][ii] = descriptor(ii);
62  }
63 
64  // If this is the first point in the index, create the index and exit.
65  if (index_ == nullptr) {
66  // Single kd-tree. No approximation.
67  const int kNumRandomizedKDTrees = 1;
68  index_.reset(new flann::Index<flann::L2<double> >(
69  flann_descriptor, flann::KDTreeIndexParams(kNumRandomizedKDTrees)));
70  index_->buildIndex();
71  return;
72  }
73 
74  // If the index is already created, add the data point to the index. Rebuild
75  // every time the index doubles in size to occasionally rebalance the kd tree.
76  const int kRebuildThreshold = 2;
77  index_->addPoints(flann_descriptor, kRebuildThreshold);
78 }
std::shared_ptr< flann::Index< flann::L2< double > > > index_
void bsfm::FlannDescriptorKDTree::AddDescriptors ( std::vector< Descriptor > &  descriptors)

Definition at line 81 of file flann_descriptor_kdtree.cpp.

82  {
83  for (auto& descriptor : descriptors) {
84  AddDescriptor(descriptor);
85  }
86 }
void AddDescriptor(Descriptor &descriptor)
bool bsfm::FlannDescriptorKDTree::NearestNeighbor ( Descriptor query,
int &  nn_index,
double &  nn_distance 
)

Definition at line 89 of file flann_descriptor_kdtree.cpp.

90  {
91  if (index_ == nullptr) {
92  VLOG(1) << "Index has not been built. Descriptors must be added before "
93  "querying the kd tree";
94  return false;
95  }
96 
97  // Convert the input descriptor to the FLANN format. We can use Eigen's memory
98  // here, since we will have our answer before leaving function scope.
99  flann::Matrix<double> flann_query(query.data(), 1, index_->veclen());
100 
101  // Search the kd tree for the nearest neighbor to the query.
102  std::vector< std::vector<int> > query_match_indices;
103  std::vector< std::vector<double> > query_distances;
104 
105  const int kOneNearestNeighbor = 1;
106  int num_neighbors_found = index_->knnSearch(
107  flann_query, query_match_indices, query_distances, kOneNearestNeighbor,
108  flann::SearchParams(flann::FLANN_CHECKS_UNLIMITED) /* no approx */);
109 
110  // If we found a nearest neighbor, assign output.
111  if (num_neighbors_found > 0) {
112  nn_index = query_match_indices[0][0];
113  nn_distance = query_distances[0][0];
114  }
115 
116  return num_neighbors_found > 0;
117 }
std::shared_ptr< flann::Index< flann::L2< double > > > index_

Member Data Documentation

std::shared_ptr< flann::Index<flann::L2<double> > > bsfm::FlannDescriptorKDTree::index_
private

Definition at line 78 of file flann_descriptor_kdtree.h.


The documentation for this class was generated from the following files: