Robotics Library  0.7.0
LinearNearestNeighbors.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_MATH_LINEARNEARESTNEIGHBORS_H
28 #define RL_MATH_LINEARNEARESTNEIGHBORS_H
29 
30 #include <algorithm>
31 #include <vector>
32 
33 namespace rl
34 {
35  namespace math
36  {
40  template<typename MetricT, typename ContainerT = ::std::vector<typename MetricT::Value>>
42  {
43  public:
44  typedef typename ContainerT::const_iterator const_iterator;
45 
46  typedef const typename MetricT::Value& const_reference;
47 
48  typedef typename ContainerT::difference_type difference_type;
49 
50  typedef typename ContainerT::iterator iterator;
51 
52  typedef typename MetricT::Value& reference;
53 
54  typedef typename ContainerT::size_type size_type;
55 
56  typedef typename MetricT::Value value_type;
57 
58  typedef ContainerT Container;
59 
60  typedef typename MetricT::Distance Distance;
61 
62  typedef MetricT Metric;
63 
64  typedef typename MetricT::Value Value;
65 
66  typedef ::std::pair<Distance, Value> Neighbor;
67 
69  container(),
70  metric(metric)
71  {
72  }
73 
75  container(),
76  metric(::std::move(metric))
77  {
78  }
79 
80  template<typename InputIterator>
81  LinearNearestNeighbors(InputIterator first, InputIterator last, const Metric& metric) :
82  container(first, last),
83  metric(metric)
84  {
85  }
86 
87  template<typename InputIterator>
88  LinearNearestNeighbors(InputIterator first, InputIterator last, Metric&& metric = Metric()) :
89  container(first, last),
90  metric(::std::move(metric))
91  {
92  }
93 
95  {
96  }
97 
98  Value& at(const ::std::size_t& i)
99  {
100  return this->container.at(i);
101  }
102 
103  const Value& at(const ::std::size_t& i) const
104  {
105  return this->container.at(i);
106  }
107 
109  {
110  return this->container.back();
111  }
112 
113  const Value& back() const
114  {
115  return this->container.back();
116  }
117 
118  iterator begin()
119  {
120  return this->container.begin();
121  }
122 
123  const_iterator begin() const
124  {
125  return this->container.begin();
126  }
127 
128  ::std::size_t capacity() const
129  {
130  return this->container.capacity();
131  }
132 
133  const_iterator cbegin() const
134  {
135  return this->container.cbegin();
136  }
137 
138  const_iterator cend() const
139  {
140  return this->container.cend();
141  }
142 
143  void clear()
144  {
145  this->container.clear();
146  }
147 
148  bool empty() const
149  {
150  return this->container.empty();
151  }
152 
153  iterator end()
154  {
155  return this->container.end();
156  }
157 
158  const_iterator end() const
159  {
160  return this->container.end();
161  }
162 
163  void erase(const_iterator pos)
164  {
165  this->container.erase(pos);
166  }
167 
169  {
170  return this->container.front();
171  }
172 
173  const Value& front() const
174  {
175  return this->container.front();
176  }
177 
178  template<typename InputIterator>
179  void insert(InputIterator first, InputIterator last)
180  {
181  this->container.insert(this->container.end(), first, last);
182  }
183 
184  ::std::size_t max_size() const
185  {
186  return this->container.max_size();
187  }
188 
189  ::std::vector<Neighbor> nearest(const Value& query, const ::std::size_t& k, const bool& sorted = true) const
190  {
191  return this->search(query, &k, nullptr, sorted);
192  }
193 
194  Value& operator[](const ::std::size_t& i)
195  {
196  return this->container[i];
197  }
198 
199  const Value& operator[](const ::std::size_t& i) const
200  {
201  return this->container[i];
202  }
203 
204  void push(const Value& value)
205  {
206  this->container.push_back(value);
207  }
208 
209  ::std::vector<Neighbor> radius(const Value& query, const Distance& radius, const bool& sorted = true) const
210  {
211  return this->search(query, nullptr, &radius, sorted);
212  }
213 
214  void reserve(const ::std::size_t& capacity)
215  {
216  this->container.reserve(capacity);
217  }
218 
219  ::std::size_t size() const
220  {
221  return this->container.size();
222  }
223 
225  {
227  swap(this->container, other.container);
228  swap(this->metric, other.metric);
229  }
230 
232  {
233  lhs.swap(rhs);
234  }
235 
236  protected:
237 
238  private:
240  {
241  bool operator()(const Neighbor& lhs, const Neighbor& rhs) const
242  {
243  return lhs.first < rhs.first;
244  }
245  };
246 
247  ::std::vector<Neighbor> search(const Value& query, const ::std::size_t* k, const Distance* radius, const bool& sorted) const
248  {
249  ::std::vector<Neighbor> neighbors;
250  neighbors.reserve(nullptr != k ? *k : this->size());
251 
252  ::std::vector<Distance> distances(this->container.size());
253 
254 #pragma omp parallel for
255 #if defined(_OPENMP) && _OPENMP < 200805
256  for (::std::ptrdiff_t i = 0; i < this->container.size(); ++i)
257 #else
258  for (::std::size_t i = 0; i < this->container.size(); ++i)
259 #endif
260  {
261  distances[i] = this->metric(query, this->container[i]);
262  }
263 
264  for (::std::size_t i = 0; i < this->container.size(); ++i)
265  {
266  if (nullptr == k || neighbors.size() < *k || distances[i] < neighbors.front().first)
267  {
268  if (nullptr == radius || distances[i] < *radius)
269  {
270  if (nullptr != k && *k == neighbors.size())
271  {
272  ::std::pop_heap(neighbors.begin(), neighbors.end(), NeighborCompare());
273  neighbors.pop_back();
274  }
275 
276 #if defined(_MSC_VER) && _MSC_VER < 1800
277  neighbors.push_back(::std::make_pair(distances[i], this->container[i]));
278 #else
279  neighbors.emplace_back(distances[i], this->container[i]);
280 #endif
281  ::std::push_heap(neighbors.begin(), neighbors.end(), NeighborCompare());
282  }
283  }
284  }
285 
286  if (sorted)
287  {
288  ::std::sort_heap(neighbors.begin(), neighbors.end(), NeighborCompare());
289  }
290 
291  return neighbors;
292  }
293 
295 
297  };
298  }
299 }
300 
301 #endif // RL_MATH_LINEARNEARESTNEIGHBORS_H
rl::math::LinearNearestNeighbors::Neighbor
::std::pair< Distance, Value > Neighbor
Definition: LinearNearestNeighbors.h:66
rl::math::LinearNearestNeighbors::capacity
::std::size_t capacity() const
Definition: LinearNearestNeighbors.h:128
rl::math::LinearNearestNeighbors::search
::std::vector< Neighbor > search(const Value &query, const ::std::size_t *k, const Distance *radius, const bool &sorted) const
Definition: LinearNearestNeighbors.h:247
rl::math::LinearNearestNeighbors::iterator
ContainerT::iterator iterator
Definition: LinearNearestNeighbors.h:50
endif
dummy cpp endif() if(CMAKE_SIZEOF_VOID_P EQUAL 4) target_compile_definitions(math INTERFACE -DEIGEN_DONT_ALIGN_STATICALLY) endif() if(NOT CMAKE_VERSION VERSION_LESS 3.8) target_compile_features(math INTERFACE cxx_std_11) endif() target_include_directories(math INTERFACE $< BUILD_INTERFACE
Definition: CMakeLists.txt:66
rl::math::LinearNearestNeighbors::empty
bool empty() const
Definition: LinearNearestNeighbors.h:148
rl::math::LinearNearestNeighbors::cend
const_iterator cend() const
Definition: LinearNearestNeighbors.h:138
rl::math::LinearNearestNeighbors::operator[]
Value & operator[](const ::std::size_t &i)
Definition: LinearNearestNeighbors.h:194
rl::math::LinearNearestNeighbors::back
const Value & back() const
Definition: LinearNearestNeighbors.h:113
rl::math::LinearNearestNeighbors::reference
MetricT::Value & reference
Definition: LinearNearestNeighbors.h:52
rl::math::LinearNearestNeighbors::push
void push(const Value &value)
Definition: LinearNearestNeighbors.h:204
rl::math::LinearNearestNeighbors::operator[]
const Value & operator[](const ::std::size_t &i) const
Definition: LinearNearestNeighbors.h:199
rl::math::LinearNearestNeighbors::max_size
::std::size_t max_size() const
Definition: LinearNearestNeighbors.h:184
rl::math::LinearNearestNeighbors::size_type
ContainerT::size_type size_type
Definition: LinearNearestNeighbors.h:54
rl::math::LinearNearestNeighbors::LinearNearestNeighbors
LinearNearestNeighbors(const Metric &metric)
Definition: LinearNearestNeighbors.h:68
rl::math::LinearNearestNeighbors::container
Container container
Definition: LinearNearestNeighbors.h:294
rl::math::LinearNearestNeighbors::~LinearNearestNeighbors
~LinearNearestNeighbors()
Definition: LinearNearestNeighbors.h:94
rl::math::LinearNearestNeighbors::end
iterator end()
Definition: LinearNearestNeighbors.h:153
rl::math::LinearNearestNeighbors::clear
void clear()
Definition: LinearNearestNeighbors.h:143
rl::math::LinearNearestNeighbors::LinearNearestNeighbors
LinearNearestNeighbors(Metric &&metric=Metric())
Definition: LinearNearestNeighbors.h:74
rl::math::LinearNearestNeighbors::radius
::std::vector< Neighbor > radius(const Value &query, const Distance &radius, const bool &sorted=true) const
Definition: LinearNearestNeighbors.h:209
rl::math::LinearNearestNeighbors::size
::std::size_t size() const
Definition: LinearNearestNeighbors.h:219
rl::math::LinearNearestNeighbors::begin
const_iterator begin() const
Definition: LinearNearestNeighbors.h:123
rl::math::LinearNearestNeighbors::value_type
MetricT::Value value_type
Definition: LinearNearestNeighbors.h:56
rl::math::LinearNearestNeighbors::at
const Value & at(const ::std::size_t &i) const
Definition: LinearNearestNeighbors.h:103
rl::math::LinearNearestNeighbors::Metric
MetricT Metric
Definition: LinearNearestNeighbors.h:62
rl::math::LinearNearestNeighbors::const_iterator
ContainerT::const_iterator const_iterator
Definition: LinearNearestNeighbors.h:44
rl::math::LinearNearestNeighbors::erase
void erase(const_iterator pos)
Definition: LinearNearestNeighbors.h:163
rl::math::LinearNearestNeighbors::NeighborCompare
Definition: LinearNearestNeighbors.h:240
rl::math::LinearNearestNeighbors::LinearNearestNeighbors
LinearNearestNeighbors(InputIterator first, InputIterator last, Metric &&metric=Metric())
Definition: LinearNearestNeighbors.h:88
std::swap
void swap(::rl::util::rtai::thread &x, ::rl::util::rtai::thread &y)
Definition: thread.h:492
rl::math::LinearNearestNeighbors::cbegin
const_iterator cbegin() const
Definition: LinearNearestNeighbors.h:133
rl::math::LinearNearestNeighbors::reserve
void reserve(const ::std::size_t &capacity)
Definition: LinearNearestNeighbors.h:214
rl::math::LinearNearestNeighbors::begin
iterator begin()
Definition: LinearNearestNeighbors.h:118
rl::plan::Metric
Definition: Metric.h:40
rl::math::LinearNearestNeighbors
Linear nearest neighbor search.
Definition: LinearNearestNeighbors.h:42
rl::math::LinearNearestNeighbors::Distance
MetricT::Distance Distance
Definition: LinearNearestNeighbors.h:60
std
Definition: thread.h:482
rl::math::LinearNearestNeighbors::LinearNearestNeighbors
LinearNearestNeighbors(InputIterator first, InputIterator last, const Metric &metric)
Definition: LinearNearestNeighbors.h:81
rl::math::LinearNearestNeighbors::end
const_iterator end() const
Definition: LinearNearestNeighbors.h:158
rl::math::LinearNearestNeighbors::Container
ContainerT Container
Definition: LinearNearestNeighbors.h:58
rl::math::LinearNearestNeighbors::Value
MetricT::Value Value
Definition: LinearNearestNeighbors.h:64
rl::math::LinearNearestNeighbors::metric
Metric metric
Definition: LinearNearestNeighbors.h:296
rl::math::LinearNearestNeighbors::NeighborCompare::operator()
bool operator()(const Neighbor &lhs, const Neighbor &rhs) const
Definition: LinearNearestNeighbors.h:241
rl::math::LinearNearestNeighbors::front
const Value & front() const
Definition: LinearNearestNeighbors.h:173
rl::math::LinearNearestNeighbors::back
Value & back()
Definition: LinearNearestNeighbors.h:108
rl::math::LinearNearestNeighbors::front
Value & front()
Definition: LinearNearestNeighbors.h:168
rl::math::LinearNearestNeighbors::swap
void swap(LinearNearestNeighbors &other)
Definition: LinearNearestNeighbors.h:224
rl::math::LinearNearestNeighbors::nearest
::std::vector< Neighbor > nearest(const Value &query, const ::std::size_t &k, const bool &sorted=true) const
Definition: LinearNearestNeighbors.h:189
rl::math::LinearNearestNeighbors::const_reference
const MetricT::Value & const_reference
Definition: LinearNearestNeighbors.h:46
rl::math::LinearNearestNeighbors::difference_type
ContainerT::difference_type difference_type
Definition: LinearNearestNeighbors.h:48
rl::math::LinearNearestNeighbors::swap
friend void swap(LinearNearestNeighbors &lhs, LinearNearestNeighbors &rhs)
Definition: LinearNearestNeighbors.h:231
rl::math::LinearNearestNeighbors::at
Value & at(const ::std::size_t &i)
Definition: LinearNearestNeighbors.h:98
rl::math::LinearNearestNeighbors::insert
void insert(InputIterator first, InputIterator last)
Definition: LinearNearestNeighbors.h:179
rl
Robotics Library.
Definition: AnalogInput.cpp:30