nabo_experimental.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2010--2011, Stephane Magnenat, ASL, ETHZ, Switzerland
4 You can contact the author at <stephane at magnenat dot net>
5 
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of the <organization> nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 */
31 
32 #ifndef __NABO_EXPERIMENTAL_H
33 #define __NABO_EXPERIMENTAL_H
34 
35 #include "../nabo/nabo_private.h"
36 #include "../nabo/index_heap.h"
37 
38 namespace Nabo
39 {
40  // KDTree, balanced, points in nodes
41  template<typename T, typename CloudType>
42  struct KDTreeBalancedPtInNodes:public NearestNeighbourSearch<T, CloudType>
43  {
48 
49  protected:
50  struct BuildPoint
51  {
52  Vector pos;
53  size_t index;
54  BuildPoint(const Vector& pos = Vector(), const size_t index = 0): pos(pos), index(index) {}
55  };
56  typedef std::vector<BuildPoint> BuildPoints;
57  typedef typename BuildPoints::iterator BuildPointsIt;
58  typedef typename BuildPoints::const_iterator BuildPointsCstIt;
59 
60  struct CompareDim
61  {
62  size_t dim;
63  CompareDim(const size_t dim):dim(dim){}
64  bool operator() (const BuildPoint& p0, const BuildPoint& p1) { return p0.pos(dim) < p1.pos(dim); }
65  };
66 
67  struct Node
68  {
69  Vector pos;
70  int dim; // -1 == leaf, -2 == invalid
71  Index index;
72  Node(const Vector& pos = Vector(), const int dim = -2, const Index index = 0):pos(pos), dim(dim), index(index) {}
73  };
74  typedef std::vector<Node> Nodes;
75 
76  Nodes nodes;
77 
78  inline size_t childLeft(size_t pos) const { return 2*pos + 1; }
79  inline size_t childRight(size_t pos) const { return 2*pos + 2; }
80  inline size_t parent(size_t pos) const { return (pos-1)/2; }
81  size_t getTreeSize(size_t size) const;
82  IndexVector cloudIndexesFromNodesIndexes(const IndexVector& indexes) const;
83  void buildNodes(const BuildPointsIt first, const BuildPointsIt last, const size_t pos);
84  void dump(const Vector minValues, const Vector maxValues, const size_t pos) const;
85 
86  protected:
88  };
89 
90  // KDTree, balanced, points in nodes, priority queue
91  template<typename T, typename CloudType>
93  {
100 
105 
106  protected:
108  {
109  size_t index;
111 
112  SearchElement(const size_t index, const T minDist): index(index), minDist(minDist) {}
113  // invert test as std::priority_queue shows biggest element at top
114  friend bool operator<(const SearchElement& e0, const SearchElement& e1) { return e0.minDist > e1.minDist; }
115  };
116 
117  public:
119  virtual IndexVector knn(const Vector& query, const Index k, const T epsilon, const unsigned optionFlags);
120  };
121 
122  // KDTree, balanced, points in nodes, stack
123  template<typename T, typename CloudType>
125  {
132 
137 
139 
140  protected:
141  void recurseKnn(const Vector& query, const size_t n, T rd, Heap& heap, Vector& off, const T maxError, const bool allowSelfMatch);
142 
143  public:
145  virtual IndexVector knn(const Vector& query, const Index k, const T epsilon, const unsigned optionFlags);
146  };
147 
148 
149  // KDTree, balanced, points in leaves, stack
150  template<typename T, typename CloudType>
152  {
157 
162 
163  protected:
164  struct BuildPoint
165  {
166  Vector pos;
167  size_t index;
168  BuildPoint(const Vector& pos = Vector(), const size_t index = 0): pos(pos), index(index) {}
169  };
170  typedef std::vector<BuildPoint> BuildPoints;
171  typedef typename BuildPoints::iterator BuildPointsIt;
172  typedef typename BuildPoints::const_iterator BuildPointsCstIt;
173 
174  struct CompareDim
175  {
176  size_t dim;
177  CompareDim(const size_t dim):dim(dim){}
178  bool operator() (const BuildPoint& p0, const BuildPoint& p1) { return p0.pos(dim) < p1.pos(dim); }
179  };
180 
182 
183  struct Node
184  {
185  int dim; // -1 == invalid, <= -2 = index of pt
187  Node(const int dim = -1, const T cutVal = 0):
188  dim(dim), cutVal(cutVal) {}
189  };
190  typedef std::vector<Node> Nodes;
191 
192  Nodes nodes;
193 
194  inline size_t childLeft(size_t pos) const { return 2*pos + 1; }
195  inline size_t childRight(size_t pos) const { return 2*pos + 2; }
196  inline size_t parent(size_t pos) const { return (pos-1)/2; }
197  size_t getTreeSize(size_t size) const;
198  void buildNodes(const BuildPointsIt first, const BuildPointsIt last, const size_t pos, const Vector minValues, const Vector maxValues, const bool balanceVariance);
199  void recurseKnn(const Vector& query, const size_t n, T rd, Heap& heap, Vector& off, const T maxError, const bool allowSelfMatch);
200 
201  public:
202  KDTreeBalancedPtInLeavesStack(const CloudType& cloud, const bool balanceVariance);
203  virtual IndexVector knn(const Vector& query, const Index k, const T epsilon, const unsigned optionFlags);
204  };
205 
206  // KDTree, unbalanced, points in leaves, stack, implicit bounds, ANN_KD_SL_MIDPT
207  template<typename T, typename Heap, typename CloudType>
209  {
215 
220 
221  protected:
222  struct BuildPoint
223  {
224  Vector pos;
225  size_t index;
226  BuildPoint(const Vector& pos = Vector(), const size_t index = 0): pos(pos), index(index) {}
227  };
228  typedef std::vector<BuildPoint> BuildPoints;
229  typedef typename BuildPoints::iterator BuildPointsIt;
230  typedef typename BuildPoints::const_iterator BuildPointsCstIt;
231 
232  struct CompareDim
233  {
234  size_t dim;
235  CompareDim(const size_t dim):dim(dim){}
236  bool operator() (const BuildPoint& p0, const BuildPoint& p1) { return p0.pos(dim) < p1.pos(dim); }
237  };
238 
239  struct Node
240  {
241  enum
242  {
243  INVALID_CHILD = 0xffffffff,
244  INVALID_PT = 0xffffffff
245  };
246  unsigned dim;
247  unsigned rightChild;
248  union
249  {
251  unsigned ptIndex;
252  };
253 
254  Node(const int dim, const T cutVal, unsigned rightChild):
255  dim(dim), rightChild(rightChild), cutVal(cutVal) {}
256  Node(const unsigned ptIndex = INVALID_PT):
257  dim(0), rightChild(INVALID_CHILD), ptIndex(ptIndex) {}
258  };
259  typedef std::vector<Node> Nodes;
260 
261  Nodes nodes;
262 
263  unsigned buildNodes(const BuildPointsIt first, const BuildPointsIt last, const Vector minValues, const Vector maxValues);
264  void recurseKnn(const Vector& query, const unsigned n, T rd, Heap& heap, Vector& off, const T maxError, const bool allowSelfMatch);
265 
266  public:
268  virtual IndexVector knn(const Vector& query, const Index k, const T epsilon, const unsigned optionFlags);
269  virtual IndexMatrix knnM(const Matrix& query, const Index k, const T epsilon, const unsigned optionFlags);
270  };
271 
272  // KDTree, unbalanced, points in leaves, stack, explicit bounds, ANN_KD_SL_MIDPT
273  template<typename T, typename CloudType>
275  {
280 
281 
286 
287  protected:
288  struct BuildPoint
289  {
290  Vector pos;
291  size_t index;
292  BuildPoint(const Vector& pos = Vector(), const size_t index = 0): pos(pos), index(index) {}
293  };
294  typedef std::vector<BuildPoint> BuildPoints;
295  typedef typename BuildPoints::iterator BuildPointsIt;
296  typedef typename BuildPoints::const_iterator BuildPointsCstIt;
297 
298  struct CompareDim
299  {
300  size_t dim;
301  CompareDim(const size_t dim):dim(dim){}
302  bool operator() (const BuildPoint& p0, const BuildPoint& p1) { return p0.pos(dim) < p1.pos(dim); }
303  };
304 
306 
307  struct Node
308  {
309  int dim; // <= -1 = index of pt
310  unsigned rightChild;
314  Node(const int dim = -1, const T cutVal = 0, const T lowBound = 0, const T highBound = 0, unsigned rightChild = 0):
315  dim(dim), rightChild(rightChild), cutVal(cutVal), lowBound(lowBound), highBound(highBound) {}
316  };
317  typedef std::vector<Node> Nodes;
318 
319  Nodes nodes;
320 
321  unsigned buildNodes(const BuildPointsIt first, const BuildPointsIt last, const Vector minValues, const Vector maxValues);
322  void recurseKnn(const Vector& query, const size_t n, T rd, Heap& heap, const T maxError, const bool allowSelfMatch);
323 
324  public:
326  virtual IndexVector knn(const Vector& query, const Index k, const T epsilon, const unsigned optionFlags);
327  };
328 }
329 
330 #endif // __NABO_H
NearestNeighbourSearch< T, CloudType >::Vector Vector
NearestNeighbourSearch< T, CloudType >::Index Index
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
size_t childRight(size_t pos) const
KDTreeBalancedPtInNodes< T, CloudType >::Nodes Nodes
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
NearestNeighbourSearch< T, CloudType >::Index Index
BuildPoints::iterator BuildPointsIt
BuildPoint(const Vector &pos=Vector(), const size_t index=0)
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
Nearest neighbour search interface, templatized on scalar type.
Definition: nabo.h:258
KDTreeBalancedPtInNodes< T, CloudType >::Node Node
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
NearestNeighbourSearch< T, CloudType >::IndexMatrix IndexMatrix
size_t getTreeSize(size_t size) const
const CloudType & cloud
the reference to the data-point cloud, which must remain valid during the lifetime of the NearestNeig...
Definition: nabo.h:274
NearestNeighbourSearch< T, CloudType >::Index Index
size_t childRight(size_t pos) const
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
KDTreeBalancedPtInNodes< T, CloudType >::Node Node
size_t childLeft(size_t pos) const
Node(const int dim=-1, const T cutVal=0, const T lowBound=0, const T highBound=0, unsigned rightChild=0)
size_t childLeft(size_t pos) const
NearestNeighbourSearch< T, CloudType >::Vector Vector
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
NearestNeighbourSearch< T, CloudType >::Vector Vector
void buildNodes(const BuildPointsIt first, const BuildPointsIt last, const size_t pos)
Node(const int dim, const T cutVal, unsigned rightChild)
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
balanced-tree implementation of heap
Definition: index_heap.h:52
BuildPoint(const Vector &pos=Vector(), const size_t index=0)
NearestNeighbourSearch< T, CloudType >::Vector Vector
KDTreeBalancedPtInNodes< T, CloudType >::Nodes Nodes
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
KDTreeBalancedPtInNodes(const CloudType &cloud)
NearestNeighbourSearch< T, CloudType >::Index Index
Namespace for Nabo.
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
NearestNeighbourSearch< T, CloudType >::IndexVector IndexVector
NearestNeighbourSearch< T, CloudType >::Matrix Matrix
NearestNeighbourSearch< T, CloudType >::Vector Vector
unsigned long knn(const Vector &query, IndexVector &indices, Vector &dists2, const Index k=1, const T epsilon=0, const unsigned optionFlags=0, const T maxRadius=std::numeric_limits< T >::infinity()) const
Find the k nearest neighbours of query.
Definition: nabo/nabo.cpp:83
std::vector< BuildPoint > BuildPoints
NearestNeighbourSearch< T, CloudType >::Index Index
BuildPoint(const Vector &pos=Vector(), const size_t index=0)
size_t parent(size_t pos) const
void dump(const Vector minValues, const Vector maxValues, const size_t pos) const
BuildPoints::const_iterator BuildPointsCstIt
IndexVector cloudIndexesFromNodesIndexes(const IndexVector &indexes) const
Node(const int dim=-1, const T cutVal=0)
std::vector< BuildPoint > BuildPoints
Node(const Vector &pos=Vector(), const int dim=-2, const Index index=0)
BuildPoint(const Vector &pos=Vector(), const size_t index=0)
CloudType CloudType
a column-major Eigen matrix in which each column is a point; this matrix has dim rows ...
Definition: nabo.h:265
friend bool operator<(const SearchElement &e0, const SearchElement &e1)
SearchElement(const size_t index, const T minDist)
NearestNeighbourSearch< T, CloudType >::Index Index
BuildPoints::const_iterator BuildPointsCstIt
NearestNeighbourSearch< T, CloudType >::Vector Vector


libnabo
Author(s): Stéphane Magnenat
autogenerated on Mon Feb 28 2022 22:41:38