NearestNeighbors.h
Go to the documentation of this file.
1 #ifndef __GfxTL_NEARESTNEIGHBORS_HEADER__
2 #define __GfxTL_NEARESTNEIGHBORS_HEADER__
3 #include <limits>
4 
5 #include <GfxTL/LimitedHeap.h>
6 
7 namespace GfxTL
8 {
9  template <class Scalar, class HandleT = size_t>
11  {
12  typedef Scalar ScalarType;
13  typedef HandleT HandleType;
14 
16  {
17  }
18 
20  {
21  }
22 
23  bool
24  operator<(const NearestNeighbor& nn) const
25  {
26  return sqrDist < nn.sqrDist;
27  }
28 
29  bool
30  operator>(const NearestNeighbor& nn) const
31  {
32  return sqrDist > nn.sqrDist;
33  }
34 
35  bool
36  operator<=(const NearestNeighbor& nn) const
37  {
38  return sqrDist <= nn.sqrDist;
39  }
40 
41  bool
42  operator>=(const NearestNeighbor& nn) const
43  {
44  return sqrDist >= nn.sqrDist;
45  }
46 
47  operator HandleType&()
48  {
49  return index;
50  }
51 
52  operator const HandleType&() const
53  {
54  return index;
55  }
56 
59  };
60 
61  template <class NN, class NNs>
62  void
63  SortedNearestNeighborInsert(const NN& nn, unsigned int k, NNs* n)
64  {
65  /*typename NNs::iterator low = n->begin(), high = n->end() - 1,
66  index = low, mid;
67  if(n->size() > 0)
68  {
69  while(low <= high)
70  {
71  mid = low + ((high - low) >> 1);
72  if(*mid <= nn)
73  {
74  low = mid + 1;
75  index = low;
76  }
77  else
78  {
79  index = mid;
80  high = mid - 1;
81  }
82  }
83  }
84  n->insert(index, nn);*/
85  intptr_t low = 0, high = n->size() - 1, index = 0, mid;
86  if (n->size() > 0)
87  {
88  while (low <= high)
89  {
90  mid = (low + high) >> 1;
91  if ((*n)[mid] <= nn)
92  {
93  low = mid + 1;
94  index = low;
95  }
96  else
97  {
98  index = mid;
99  high = mid - 1;
100  }
101  }
102  }
103  n->insert(n->begin() + index, nn);
104  if (n->size() > k)
105  {
106  if ((*n)[k] > (*n)[k - 1])
107  {
108  n->resize(k);
109  }
110  }
111  /*
112  // remove all that are too far away
113  typename NNs::iterator begin = n->begin() +
114  Math< typename NNs::size_type >::Min(n->size() - 1, k - 1),
115  nEnd = n->end();
116  while(begin + 1 != nEnd && begin->sqrDist == (begin + 1)->sqrDist)
117  ++begin;
118  if(begin + 1 != nEnd)
119  n->erase(begin + 1, nEnd);*/
120  }
121 
122  template <class NNs, class ScalarType>
123  void
124  FindFurthestNearestNeighbor(const NNs& n, size_t size, size_t* worst, ScalarType* worstDist)
125  {
126  size_t j = 0;
127  *worst = j;
128  *worstDist = n[j].sqrDist;
129  ++j;
130  for (; j < size; ++j)
131  {
132  if (n[j].sqrDist > *worstDist)
133  {
134  *worstDist = n[j].sqrDist;
135  *worst = j;
136  }
137  }
138  }
139 
140  template <class Point, class Points>
141  void
143  unsigned int k,
144  const Point& p,
145  const Points& points,
146  size_t size,
148  {
149  typedef Point PointType;
150  typedef typename Point::ScalarType ScalarType;
152  typedef typename Points::size_type size_type;
153  n->reserve(k);
154  size_type worst;
155  ScalarType worstDist = 0;
156  size_type i = 0;
157  for (; i < size && i < k; ++i)
158  {
159  PointType diff = points[i] - p;
160  n->push_back(NN(i, diff * diff));
161  if (n->back().sqrDist > worstDist)
162  {
163  worstDist = n->back().sqrDist;
164  worst = n->size() - 1;
165  }
166  }
167 
168  for (i = k; i < size; ++i)
169  {
170  PointType diff = points[i] - p;
171  ScalarType dist = diff * diff;
172  if (dist < worstDist)
173  {
174  (*n)[worst] = NN(i, dist);
175  FindFurthestNearestNeighbor(*n, n->size(), &worst, &worstDist);
176  }
177  }
178  }
179 
180  template <class Point, class Points, template <class> class ContainerT, class NN>
181  void
183  const Point& p,
184  const Points& points,
185  size_t size,
186  LimitedHeap<NN, std::less<NN>, ContainerT>* neighbors)
187  {
188  neighbors->clear();
189  neighbors->Limit(k);
190  for (size_t i = 0; i < size; ++i)
191  {
192  NN nn(p.SqrDistance(points[i]), i);
193  neighbors->PushHeap(nn);
194  }
195  }
196 
197  template <class Point, class Points, template <class> class ContainerT, class NN>
198  void
200  const Point& p,
201  const Points& points,
202  size_t size,
203  LimitedHeap<NN, std::less<NN>, ContainerT>* neighbors)
204  {
205  neighbors->clear();
206  neighbors->Limit(k);
207  for (size_t i = 0; i < size; ++i)
208  {
209  NN nn(p.L1Distance(points[i]), i);
210  neighbors->PushHeap(nn);
211  }
212  }
213 }; // namespace GfxTL
214 
215 #endif
GfxTL::NearestNeighbor::HandleType
HandleT HandleType
Definition: NearestNeighbors.h:13
visionx::armem::pointcloud::PointType
PointType
Definition: constants.h:78
GfxTL::NearestNeighbor::ScalarType
Scalar ScalarType
Definition: NearestNeighbors.h:12
index
uint8_t index
Definition: EtherCATFrame.h:59
visionx::Points
std::vector< Point > Points
Definition: ObjectShapeClassification.h:71
GfxTL::NearestNeighbor::NearestNeighbor
NearestNeighbor(HandleType i, ScalarType d)
Definition: NearestNeighbors.h:19
GfxTL::NearestNeighbor::operator>
bool operator>(const NearestNeighbor &nn) const
Definition: NearestNeighbors.h:30
LimitedHeap.h
Point::ScalarType
float ScalarType
Definition: PointCloud.h:28
GfxTL::NearestNeighbor::operator>=
bool operator>=(const NearestNeighbor &nn) const
Definition: NearestNeighbors.h:42
GfxTL::BruteForceNearestNeighbors
void BruteForceNearestNeighbors(unsigned int k, const Point &p, const Points &points, size_t size, LimitedHeap< NN, std::less< NN >, ContainerT > *neighbors)
Definition: NearestNeighbors.h:182
GfxTL::NN
Definition: NearestNeighbor.h:8
GfxTL::NearestNeighbor::sqrDist
ScalarType sqrDist
Definition: NearestNeighbors.h:58
GfxTL::SortedNearestNeighborInsert
void SortedNearestNeighborInsert(const NN &nn, unsigned int k, NNs *n)
Definition: NearestNeighbors.h:63
GfxTL::NearestNeighbor::operator<
bool operator<(const NearestNeighbor &nn) const
Definition: NearestNeighbors.h:24
GfxTL::BruteForceL1NearestNeighbors
void BruteForceL1NearestNeighbors(unsigned int k, const Point &p, const Points &points, size_t size, LimitedHeap< NN, std::less< NN >, ContainerT > *neighbors)
Definition: NearestNeighbors.h:199
Point
Definition: PointCloud.h:21
GfxTL
Definition: AABox.h:9
GfxTL::FindFurthestNearestNeighbor
void FindFurthestNearestNeighbor(const NNs &n, size_t size, size_t *worst, ScalarType *worstDist)
Definition: NearestNeighbors.h:124
GfxTL::NearestNeighbor::operator<=
bool operator<=(const NearestNeighbor &nn) const
Definition: NearestNeighbors.h:36
GfxTL::NearestNeighbor
Definition: NearestNeighbors.h:10
GfxTL::NearestNeighbors
void NearestNeighbors(unsigned int k, const Point &p, const Points &points, size_t size, std::vector< NearestNeighbor< typename Points::size_type, typename Point::ScalarType >> *n)
Definition: NearestNeighbors.h:142
Scalar
float Scalar
Definition: basic.h:15
GfxTL::LimitedHeap
Definition: LimitedHeap.h:15
GfxTL::NearestNeighbor::NearestNeighbor
NearestNeighbor()
Definition: NearestNeighbors.h:15
GfxTL::NearestNeighbor::index
HandleType index
Definition: NearestNeighbors.h:57