NearestNeighborsLinear.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_DATASTRUCTURES_NEAREST_NEIGHBORS_LINEAR_
38 #define OMPL_DATASTRUCTURES_NEAREST_NEIGHBORS_LINEAR_
39 
40 #include "ompl/datastructures/NearestNeighbors.h"
41 #include "ompl/util/Exception.h"
42 #include <algorithm>
43 
44 namespace ompl
45 {
46 
56  template<typename _T>
58  {
59  public:
61  {
62  }
63 
64  virtual ~NearestNeighborsLinear()
65  {
66  }
67 
68  virtual void clear()
69  {
70  data_.clear();
71  }
72 
73  virtual bool reportsSortedResults() const
74  {
75  return true;
76  }
77 
78  virtual void add(const _T &data)
79  {
80  data_.push_back(data);
81  }
82 
83  virtual void add(const std::vector<_T> &data)
84  {
85  data_.reserve(data_.size() + data.size());
86  data_.insert(data_.end(), data.begin(), data.end());
87  }
88 
89  virtual bool remove(const _T &data)
90  {
91  if (!data_.empty())
92  for (int i = data_.size() - 1 ; i >= 0 ; --i)
93  if (data_[i] == data)
94  {
95  data_.erase(data_.begin() + i);
96  return true;
97  }
98  return false;
99  }
100 
101  virtual _T nearest(const _T &data) const
102  {
103  const std::size_t sz = data_.size();
104  std::size_t pos = sz;
105  double dmin = 0.0;
106  for (std::size_t i = 0 ; i < sz ; ++i)
107  {
108  double distance = NearestNeighbors<_T>::distFun_(data_[i], data);
109  if (pos == sz || dmin > distance)
110  {
111  pos = i;
112  dmin = distance;
113  }
114  }
115  if (pos != sz)
116  return data_[pos];
117 
118  throw Exception("No elements found in nearest neighbors data structure");
119  }
120 
122  virtual void nearestK(const _T &data, std::size_t k, std::vector<_T> &nbh) const
123  {
124  nbh = data_;
125  if (nbh.size() > k)
126  {
127  std::partial_sort(nbh.begin(), nbh.begin() + k, nbh.end(),
128  ElemSort(data, NearestNeighbors<_T>::distFun_));
129  nbh.resize(k);
130  }
131  else
132  {
133  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
134  }
135  }
136 
138  virtual void nearestR(const _T &data, double radius, std::vector<_T> &nbh) const
139  {
140  nbh.clear();
141  for (std::size_t i = 0 ; i < data_.size() ; ++i)
142  if (NearestNeighbors<_T>::distFun_(data_[i], data) <= radius)
143  nbh.push_back(data_[i]);
144  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
145  }
146 
147  virtual std::size_t size() const
148  {
149  return data_.size();
150  }
151 
152  virtual void list(std::vector<_T> &data) const
153  {
154  data = data_;
155  }
156 
157  protected:
158 
160  std::vector<_T> data_;
161 
162  private:
163 
164  struct ElemSort
165  {
166  ElemSort(const _T &e, const typename NearestNeighbors<_T>::DistanceFunction &df) : e_(e), df_(df)
167  {
168  }
169 
170  bool operator()(const _T &a, const _T &b) const
171  {
172  return df_(a, e_) < df_(b, e_);
173  }
174 
175  const _T &e_;
176  const typename NearestNeighbors<_T>::DistanceFunction &df_;
177  };
178 
179  };
180 
181 
182 }
183 
184 #endif
boost::function< double(const _T &, const _T &)> DistanceFunction
The definition of a distance function.
virtual void nearestR(const _T &data, double radius, std::vector< _T > &nbh) const
Return the nearest neighbors within distance radius in sorted order.
Main namespace. Contains everything in this library.
Definition: Cost.h:42
virtual void nearestK(const _T &data, std::size_t k, std::vector< _T > &nbh) const
Return the k nearest neighbors in sorted order.
virtual void add(const std::vector< _T > &data)
Add a vector of points.
virtual void list(std::vector< _T > &data) const
Get all the elements in the datastructure.
A nearest neighbors datastructure that uses linear search.
DistanceFunction distFun_
The used distance function.
Abstract representation of a container that can perform nearest neighbors queries.
The exception type for ompl.
Definition: Exception.h:47
virtual std::size_t size() const
Get the number of elements in the datastructure.
virtual bool reportsSortedResults() const
Return true if the solutions reported by this data structure are sorted, when calling nearestK / near...
virtual void add(const _T &data)
Add an element to the datastructure.
virtual void clear()
Clear the datastructure.
std::vector< _T > data_
The data elements stored in this structure.
virtual _T nearest(const _T &data) const
Get the nearest neighbor of a point.