All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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(void)
65  {
66  }
67 
68  virtual void clear(void)
69  {
70  data_.clear();
71  }
72 
73  virtual void add(const _T &data)
74  {
75  data_.push_back(data);
76  }
77 
78  virtual void add(const std::vector<_T> &data)
79  {
80  data_.reserve(data_.size() + data.size());
81  data_.insert(data_.end(), data.begin(), data.end());
82  }
83 
84  virtual bool remove(const _T &data)
85  {
86  if (!data_.empty())
87  for (int i = data_.size() - 1 ; i >= 0 ; --i)
88  if (data_[i] == data)
89  {
90  data_.erase(data_.begin() + i);
91  return true;
92  }
93  return false;
94  }
95 
96  virtual _T nearest(const _T &data) const
97  {
98  const std::size_t sz = data_.size();
99  std::size_t pos = sz;
100  double dmin = 0.0;
101  for (std::size_t i = 0 ; i < sz ; ++i)
102  {
103  double distance = NearestNeighbors<_T>::distFun_(data_[i], data);
104  if (pos == sz || dmin > distance)
105  {
106  pos = i;
107  dmin = distance;
108  }
109  }
110  if (pos != sz)
111  return data_[pos];
112 
113  throw Exception("No elements found in nearest neighbors data structure");
114  }
115 
117  virtual void nearestK(const _T &data, std::size_t k, std::vector<_T> &nbh) const
118  {
119  nbh = data_;
120  if (nbh.size() > k)
121  {
122  std::partial_sort(nbh.begin(), nbh.begin() + k, nbh.end(),
123  ElemSort(data, NearestNeighbors<_T>::distFun_));
124  nbh.resize(k);
125  }
126  else
127  {
128  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
129  }
130  }
131 
133  virtual void nearestR(const _T &data, double radius, std::vector<_T> &nbh) const
134  {
135  nbh.clear();
136  for (std::size_t i = 0 ; i < data_.size() ; ++i)
137  if (NearestNeighbors<_T>::distFun_(data_[i], data) <= radius)
138  nbh.push_back(data_[i]);
139  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
140  }
141 
142  virtual std::size_t size(void) const
143  {
144  return data_.size();
145  }
146 
147  virtual void list(std::vector<_T> &data) const
148  {
149  data = data_;
150  }
151 
152  protected:
153 
155  std::vector<_T> data_;
156 
157  private:
158 
159  struct ElemSort
160  {
161  ElemSort(const _T &e, const typename NearestNeighbors<_T>::DistanceFunction &df) : e_(e), df_(df)
162  {
163  }
164 
165  bool operator()(const _T &a, const _T &b) const
166  {
167  return df_(a, e_) < df_(b, e_);
168  }
169 
170  const _T &e_;
171  const typename NearestNeighbors<_T>::DistanceFunction &df_;
172  };
173 
174  };
175 
176 
177 }
178 
179 #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.
virtual void clear(void)
Clear the datastructure.
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(void) const
Get the number of elements in the datastructure.
virtual void add(const _T &data)
Add an element to 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.