libnabo  1.1.2
index_heap.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 __INDEX_HEAP_H
33 #define __INDEX_HEAP_H
34 
35 #include "nabo.h"
36 #include <vector>
37 #include <algorithm>
38 #include <limits>
39 #include <iostream>
40 
46 namespace Nabo
47 {
49 
51  template<typename IT, typename VT>
52  struct IndexHeapSTL
53  {
55  typedef IT Index;
57  typedef VT Value;
58 
60  struct Entry
61  {
62  IT index;
63  VT value;
64 
66  Entry(const IT index, const VT value): index(index), value(value) {}
68  friend bool operator<(const Entry& e0, const Entry& e1) { return e0.value < e1.value; }
69  };
71  typedef std::vector<Entry> Entries;
73  typedef typename Eigen::Matrix<Index, Eigen::Dynamic, 1> IndexVector;
75  typedef typename Eigen::Matrix<Value, Eigen::Dynamic, 1> ValueVector;
76 
80  const size_t nbNeighbours;
81 
82 
84 
85  IndexHeapSTL(const size_t size):
86  data(1, Entry(invalidIndex<IT>(), invalidValue<VT>())),
87  nbNeighbours(size)
88  {
89  data.reserve(size);
90  }
91 
93  inline void reset()
94  {
95  data.clear();
96  data.push_back(Entry(invalidIndex<IT>(), invalidValue<VT>()));
97  }
98 
100 
101  inline const VT& headValue() const { return data.front().value; }
102 
104 
106  inline void replaceHead(const Index index, const Value value)
107  {
108 
109  if (data.size() == nbNeighbours)
110  { // we have enough neighbours to discard largest
111  pop_heap(data.begin(), data.end());
112  data.back() = Entry(index, value);
113  }
114  else
115  { // missing neighbours
116  data.push_back(Entry(index, value));
117  }
118  // ensure heap
119  push_heap(data.begin(), data.end());
120  }
121 
123  inline void sort()
124  {
125  sort_heap (data.begin(), data.end());
126  }
127 
129 
131  template<typename DI, typename DV>
132  inline void getData(const Eigen::MatrixBase<DI>& indices, const Eigen::MatrixBase<DV> & values) const
133  {
134  // note: we must implement this hack because of problem with reference to temporary
135  // C++0x will solve this with rvalue
136  // see: http://eigen.tuxfamily.org/dox-devel/TopicFunctionTakingEigenTypes.html
137  // for more informations
138  size_t i = 0;
139  for (; i < data.size(); ++i)
140  {
141  const_cast<Eigen::MatrixBase<DI>&>(indices).coeffRef(i) = data[i].index;
142  const_cast<Eigen::MatrixBase<DV>&>(values).coeffRef(i) = data[i].value;
143  }
144  for (; i < nbNeighbours; ++i)
145  {
146  const_cast<Eigen::MatrixBase<DI>&>(indices).coeffRef(i) = invalidIndex<IT>();
147  const_cast<Eigen::MatrixBase<DV>&>(values).coeffRef(i) = invalidValue<VT>();
148  }
149  }
150 
151 #if 0
153 
154  inline IndexVector getIndexes() const
155  {
156  IndexVector indexes(data.capacity());
157  size_t i = 0;
158  for (; i < data.size(); ++i)
159  indexes.coeffRef(i) = data[i].index;
160  for (; i < data.capacity(); ++i)
161  indexes.coeffRef(i) = 0;
162  return indexes;
163  }
164 #endif
165  };
166 
167 #if 0
169 
171  template<typename IT, typename VT>
172  struct IndexHeapBruteForceVector
173  {
175  typedef IT Index;
177  typedef VT Value;
178 
180  struct Entry
181  {
182  IT index;
183  VT value;
184 
186  Entry(const IT index, const VT value): index(index), value(value) {}
188  friend bool operator<(const Entry& e0, const Entry& e1) { return e0.value < e1.value; }
189  };
191  typedef std::vector<Entry> Entries;
193  typedef typename Eigen::Matrix<Index, Eigen::Dynamic, 1> IndexVector;
194 
196  Entries data;
198  const VT& headValueRef;
200  const size_t sizeMinusOne;
201 
203 
204  IndexHeapBruteForceVector(const size_t size):
205  data(size, Entry(0, std::numeric_limits<VT>::infinity())),
206  headValueRef((data.end() - 1)->value),
207  sizeMinusOne(data.size() - 1)
208  {
209  }
210 
212  inline void reset()
213  {
214  for (typename Entries::iterator it(data.begin()); it != data.end(); ++it)
215  {
216  it->value = std::numeric_limits<VT>::infinity();
217  it->index = 0;
218  }
219  }
220 
222 
223  inline const VT& headValue() const { return data[0].value; }
224 
226 
228  inline void replaceHead(const Index index, const Value value)
229  {
230  size_t i = 0;
231  for (; i < sizeMinusOne; ++i)
232  {
233  if (data[i + 1].value > value)
234  data[i] = data[i + 1];
235  else
236  break;
237  }
238  data[i].value = value;
239  data[i].index = index;
240  }
241 
243  inline void sort()
244  {
245  // no need to sort as data are already sorted
246  }
247 
249 
250  inline IndexVector getIndexes() const
251  {
252  IndexVector indexes(data.size());
253  for (size_t i = 0; i < data.size(); ++i)
254  indexes.coeffRef(i) = data[sizeMinusOne-i].index;
255  return indexes;
256  }
257  };
258 #endif
259 
261 
263  template<typename IT, typename VT>
265  {
267  typedef IT Index;
269  typedef VT Value;
270 
272  struct Entry
273  {
274  IT index;
275  VT value;
276 
278  Entry(const IT index, const VT value): index(index), value(value) {}
280  friend bool operator<(const Entry& e0, const Entry& e1) { return e0.value < e1.value; }
281  };
283  typedef std::vector<Entry> Entries;
285  typedef typename Eigen::Matrix<Index, Eigen::Dynamic, 1> IndexVector;
287  typedef typename Eigen::Matrix<Value, Eigen::Dynamic, 1> ValueVector;
288 
292  const VT& headValueRef;
294  const size_t sizeMinusOne;
295 
297 
298  IndexHeapBruteForceVector(const size_t size):
299  data(size, Entry(invalidIndex<IT>(), invalidValue<VT>())),
300  headValueRef((data.end() - 1)->value),
301  sizeMinusOne(data.size() - 1)
302  {
303  }
304 
306  inline void reset()
307  {
308  for (typename Entries::iterator it(data.begin()); it != data.end(); ++it)
309  {
310  it->value = invalidValue<VT>();
311  it->index = invalidIndex<IT>();
312  }
313  }
314 
316 
317  inline const VT& headValue() const { return headValueRef; }
318 
320 
322  inline void replaceHead(const Index index, const Value value)
323  {
324  size_t i;
325  for (i = sizeMinusOne; i > 0; --i)
326  {
327  if (data[i-1].value > value)
328  data[i] = data[i-1];
329  else
330  break;
331  }
332  data[i].value = value;
333  data[i].index = index;
334  }
335 
337  inline void sort()
338  {
339  // no need to sort as data are already sorted
340  }
341 
343 
345  template<typename DI, typename DV>
346  inline void getData(const Eigen::MatrixBase<DI>& indices, const Eigen::MatrixBase<DV> & values) const
347  {
348  // note: we must implement this hack because of problem with reference to temporary
349  // C++0x will solve this with rvalue
350  // see: http://eigen.tuxfamily.org/dox-devel/TopicFunctionTakingEigenTypes.html
351  // for more informations
352  for (size_t i = 0; i < data.size(); ++i)
353  {
354  const_cast<Eigen::MatrixBase<DI>&>(indices).coeffRef(i) = data[i].index;
355  const_cast<Eigen::MatrixBase<DV>&>(values).coeffRef(i) = data[i].value;
356  }
357  }
358 #if 0
360 
361  inline IndexVector getIndexes() const
362  {
363  IndexVector indexes(data.size());
364  for (size_t i = 0; i < data.size(); ++i)
365  indexes.coeffRef(i) = data[i].index;
366  return indexes;
367  }
368 #endif
369  };
370 }
371 
372 #endif // __INDEX_HEAP_H
public interface
Namespace for Nabo.
Definition: brute_force_cpu.cpp:41
an entry of the heap vector
Definition: index_heap.h:273
Entry(const IT index, const VT value)
create a new entry
Definition: index_heap.h:278
IT index
index of point
Definition: index_heap.h:274
VT value
distance for this point
Definition: index_heap.h:275
friend bool operator<(const Entry &e0, const Entry &e1)
return true if e0 is smaller than e1, false otherwise
Definition: index_heap.h:280
brute-force implementation of heap
Definition: index_heap.h:265
const VT & headValueRef
reference to the largest value in the tree, to optimise access speed
Definition: index_heap.h:292
IndexHeapBruteForceVector(const size_t size)
Constructor.
Definition: index_heap.h:298
std::vector< Entry > Entries
vector of entry, type for the storage of the tree
Definition: index_heap.h:283
void getData(const Eigen::MatrixBase< DI > &indices, const Eigen::MatrixBase< DV > &values) const
get the data from the heap
Definition: index_heap.h:346
Entries data
storage for the tree
Definition: index_heap.h:290
Eigen::Matrix< Index, Eigen::Dynamic, 1 > IndexVector
vector of indices
Definition: index_heap.h:285
const VT & headValue() const
get the largest value of the heap
Definition: index_heap.h:317
VT Value
type of a value
Definition: index_heap.h:269
const size_t sizeMinusOne
pre-competed size minus one, to optimise access speed
Definition: index_heap.h:294
IT Index
type of an index
Definition: index_heap.h:267
Eigen::Matrix< Value, Eigen::Dynamic, 1 > ValueVector
vector of values
Definition: index_heap.h:287
void sort()
sort the entries, from the smallest to the largest
Definition: index_heap.h:337
void reset()
reset to the empty heap
Definition: index_heap.h:306
void replaceHead(const Index index, const Value value)
replace the largest value of the heap
Definition: index_heap.h:322
an entry of the heap tree
Definition: index_heap.h:61
IT index
index of point
Definition: index_heap.h:62
friend bool operator<(const Entry &e0, const Entry &e1)
return true if e0 is of lower value than e1, false otherwise
Definition: index_heap.h:68
Entry(const IT index, const VT value)
create a new entry
Definition: index_heap.h:66
VT value
distance for this point
Definition: index_heap.h:63
balanced-tree implementation of heap
Definition: index_heap.h:53
Eigen::Matrix< Index, Eigen::Dynamic, 1 > IndexVector
vector of indices
Definition: index_heap.h:73
void getData(const Eigen::MatrixBase< DI > &indices, const Eigen::MatrixBase< DV > &values) const
get the data from the heap
Definition: index_heap.h:132
std::vector< Entry > Entries
vector of entry, type for the storage of the tree
Definition: index_heap.h:71
void replaceHead(const Index index, const Value value)
put value into heap, replace the largest value if full
Definition: index_heap.h:106
VT Value
type of a value
Definition: index_heap.h:57
void reset()
reset to the empty heap
Definition: index_heap.h:93
IT Index
type of an index
Definition: index_heap.h:55
Entries data
storage for the tree
Definition: index_heap.h:78
Eigen::Matrix< Value, Eigen::Dynamic, 1 > ValueVector
vector of values
Definition: index_heap.h:75
void sort()
sort the entries, from the smallest to the largest
Definition: index_heap.h:123
const VT & headValue() const
get the largest value of the heap
Definition: index_heap.h:101
const size_t nbNeighbours
number of neighbours requested
Definition: index_heap.h:80
IndexHeapSTL(const size_t size)
Constructor.
Definition: index_heap.h:85