MUQ  0.4.3
FlannCache.h
Go to the documentation of this file.
1 #ifndef FLANNCACHE_H_
2 #define FLANNCACHE_H_
3 
4 #include "MUQ/config.h"
5 
6 #include <deque>
7 
8 #include <nanoflann.hpp>
9 
11 
12 #include "MUQ/Modeling/ModPiece.h"
13 
14 namespace muq {
15  namespace Modeling {
16 
17  class FlannCache;
18 
19  template <class Distance = nanoflann::metric_L2, typename IndexType = size_t>
21 
22  friend class FlannCache;
23 
25  typedef typename Distance::template traits<double,self_t>::distance_t metric_t;
26  typedef nanoflann::KDTreeSingleIndexDynamicAdaptor< metric_t,self_t,-1,IndexType> index_t;
27 
28  std::shared_ptr<index_t> index;
29  std::deque<Eigen::VectorXd> m_data;
30 
32  inline DynamicKDTreeAdaptor(const int dim, const int leaf_max_size = 10) {
33  index = std::make_shared<index_t>(dim, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size ) );
34  }
35 
36  inline void UpdateIndex(const int leaf_max_size = 10) {
37  assert(m_data.size()>0);
38  index = std::make_shared<index_t>(m_data.at(0).size(), *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size ) );
39  index->addPoints(0, m_data.size()-1);
40  }
41 
42  inline virtual ~DynamicKDTreeAdaptor() {}
43 
44  inline void add(Eigen::VectorXd const& newPt) {
45  m_data.push_back(newPt);
46  index->addPoints(m_data.size()-1, m_data.size()-1);
47  }
48 
54  inline std::pair<std::vector<IndexType>, std::vector<double>> query(Eigen::VectorXd const& query_point, const size_t num_closest, const int nChecks_IGNORED = 10) const {
55  std::vector<IndexType> out_indices(num_closest);
56  std::vector<double> out_distances_sq(num_closest);
57 
58  nanoflann::KNNResultSet<double,IndexType> resultSet(num_closest);
59  resultSet.init(&out_indices[0], &out_distances_sq[0]);
60  #if MUQ_NANOFLAN_PARAMS_COMPILES
61  index->findNeighbors(resultSet, query_point.data(), nanoflann::SearchParams());
62  #else
63  index->findNeighbors(resultSet, query_point.data(), nanoflann::SearchParameters());
64  #endif
65  return std::make_pair(out_indices, out_distances_sq);
66  }
67 
68  inline const self_t & derived() const {
69  return *this;
70  }
71 
72  inline self_t& derived() {
73  return *this;
74  }
75 
76  // Must return the number of data points
77  inline size_t kdtree_get_point_count() const {
78  return m_data.size();
79  }
80 
81  // Returns the dim'th component of the idx'th point in the class:
82  inline double kdtree_get_pt(const size_t idx, int dim) const {
83  assert(idx<m_data.size());
84  assert(dim<m_data[idx].size());
85 
86  return m_data[idx][dim];
87  }
88 
89  // Optional bounding-box computation: return false to default to a standard bbox computation loop.
90  // Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again.
91  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)
92  template <class BBOX>
93  inline bool kdtree_get_bbox(BBOX & /*bb*/) const {
94  return false;
95  }
96  }; // end of DynamicKDTreeAdaptor
97 
98 
100 
103  class FlannCache : public ModPiece {
104  public:
105 
110  FlannCache(std::shared_ptr<ModPiece> function/* = nullptr*/);
111 
112  ~FlannCache();
113 
115 
119  int InCache(Eigen::VectorXd const& input) const;
120 
122 
126  std::vector<Eigen::VectorXd> Add(std::vector<Eigen::VectorXd> const& inputs);
127 
129 
133  void Add(std::vector<Eigen::VectorXd> const& inputs, std::vector<Eigen::VectorXd> const& results);
134 
136 
140  const Eigen::VectorXd at(unsigned int const index) const;
141 
143 
147  Eigen::VectorXd at(unsigned int const index);
148 
150  Eigen::VectorXd const& OutputValue(unsigned int index) const;
151 
153 
157  Eigen::VectorXd Add(Eigen::VectorXd const& input);
158 
160 
165  unsigned int Add(Eigen::VectorXd const& input, Eigen::VectorXd const& result);
166 
168 
171  void Remove(Eigen::VectorXd const& input);
172 
174 
178  size_t NearestNeighborIndex(Eigen::VectorXd const& point) const;
179 
181 
187  void NearestNeighbors(Eigen::VectorXd const& point,
188  unsigned int const k,
189  std::vector<Eigen::VectorXd>& neighbors,
190  std::vector<Eigen::VectorXd>& result) const;
191 
193 
198  void NearestNeighbors(Eigen::VectorXd const& point,
199  unsigned int const k,
200  std::vector<Eigen::VectorXd>& neighbors) const;
201 
203 
206  unsigned int Size() const;
207 
209 
212  Eigen::VectorXd Centroid() const;
213 
215 
218  std::shared_ptr<ModPiece> Function() const;
219 
220  private:
221 
223 
226  void UpdateCentroid(Eigen::VectorXd const& point);
227 
228  // The vector of previous results
229  std::vector<Eigen::VectorXd> outputCache;
230 
231  virtual void EvaluateImpl(ref_vector<Eigen::VectorXd> const& inputs) override;
232 
234  std::shared_ptr<ModPiece> function;
235 
237  std::shared_ptr<DynamicKDTreeAdaptor<>> kdTree;
238 
240 
249  Eigen::VectorXd centroid;
250  };
251  } // namespace Utilities
252 } // namespace muq
253 
254 #endif
Create a cache of model evaluations (input/output pairs)
Definition: FlannCache.h:103
std::vector< Eigen::VectorXd > outputCache
Definition: FlannCache.h:229
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Definition: FlannCache.cpp:17
void UpdateCentroid(Eigen::VectorXd const &point)
Update the centroid when a new point is added to the cache.
Definition: FlannCache.cpp:193
std::vector< Eigen::VectorXd > Add(std::vector< Eigen::VectorXd > const &inputs)
Add new points to the cache.
Definition: FlannCache.cpp:146
std::shared_ptr< DynamicKDTreeAdaptor<> > kdTree
The nearest neighbor index, used to perform searches.
Definition: FlannCache.h:237
void NearestNeighbors(Eigen::VectorXd const &point, unsigned int const k, std::vector< Eigen::VectorXd > &neighbors) const
Find the nearest neighbors.
Definition: FlannCache.cpp:104
FlannCache(std::shared_ptr< ModPiece > function)
Definition: FlannCache.cpp:5
Eigen::VectorXd Centroid() const
Get the centroid of the cache.
Definition: FlannCache.cpp:197
unsigned int Size() const
Get the size of the cache.
Definition: FlannCache.cpp:141
const Eigen::VectorXd at(unsigned int const index) const
Get an input point from the cache.
Definition: FlannCache.cpp:179
Eigen::VectorXd centroid
The centroid of the input addPoints.
Definition: FlannCache.h:249
size_t NearestNeighborIndex(Eigen::VectorXd const &point) const
The index of the nearest neighbor.
Definition: FlannCache.cpp:91
void Remove(Eigen::VectorXd const &input)
Remove point from the cache.
Definition: FlannCache.cpp:76
int InCache(Eigen::VectorXd const &input) const
Determine if an entry is in the cache.
Definition: FlannCache.cpp:28
std::shared_ptr< ModPiece > Function() const
Get the underlying function.
Definition: FlannCache.cpp:199
Eigen::VectorXd const & OutputValue(unsigned int index) const
Returns the model for a specific cache index.
Definition: FlannCache.cpp:189
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37
bool kdtree_get_bbox(BBOX &) const
Definition: FlannCache.h:93
void UpdateIndex(const int leaf_max_size=10)
Definition: FlannCache.h:36
nanoflann::KDTreeSingleIndexDynamicAdaptor< metric_t, self_t,-1, IndexType > index_t
Definition: FlannCache.h:26
Distance::template traits< double, self_t >::distance_t metric_t
Definition: FlannCache.h:25
DynamicKDTreeAdaptor(const int dim, const int leaf_max_size=10)
Constructor: takes a const ref to the vector of vectors object with the data points.
Definition: FlannCache.h:32
double kdtree_get_pt(const size_t idx, int dim) const
Definition: FlannCache.h:82
std::shared_ptr< index_t > index
Definition: FlannCache.h:28
std::deque< Eigen::VectorXd > m_data
The kd-tree index for the user to call its methods as usual with any other FLANN index.
Definition: FlannCache.h:29
DynamicKDTreeAdaptor< Distance, IndexType > self_t
Definition: FlannCache.h:24
const self_t & derived() const
Definition: FlannCache.h:68
std::pair< std::vector< IndexType >, std::vector< double > > query(Eigen::VectorXd const &query_point, const size_t num_closest, const int nChecks_IGNORED=10) const
Definition: FlannCache.h:54
void add(Eigen::VectorXd const &newPt)
Definition: FlannCache.h:44