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 
116  virtual void nearestK(const _T &data, std::size_t k, std::vector<_T> &nbh) const
117  {
118  nbh = data_;
119  if (nbh.size() > k)
120  {
121  std::partial_sort(nbh.begin(), nbh.begin() + k, nbh.end(),
122  ElemSort(data, NearestNeighbors<_T>::distFun_));
123  nbh.resize(k);
124  }
125  else
126  {
127  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
128  }
129  }
130 
131  virtual void nearestR(const _T &data, double radius, std::vector<_T> &nbh) const
132  {
133  nbh.clear();
134  for (std::size_t i = 0 ; i < data_.size() ; ++i)
135  if (NearestNeighbors<_T>::distFun_(data_[i], data) <= radius)
136  nbh.push_back(data_[i]);
137  std::sort(nbh.begin(), nbh.end(), ElemSort(data, NearestNeighbors<_T>::distFun_));
138  }
139 
140  virtual std::size_t size(void) const
141  {
142  return data_.size();
143  }
144 
145  virtual void list(std::vector<_T> &data) const
146  {
147  data = data_;
148  }
149 
150  protected:
151 
153  std::vector<_T> data_;
154 
155  private:
156 
157  struct ElemSort
158  {
159  ElemSort(const _T &e, const typename NearestNeighbors<_T>::DistanceFunction &df) : e_(e), df_(df)
160  {
161  }
162 
163  bool operator()(const _T &a, const _T &b) const
164  {
165  return df_(a, e_) < df_(b, e_);
166  }
167 
168  const _T &e_;
169  const typename NearestNeighbors<_T>::DistanceFunction &df_;
170  };
171 
172  };
173 
174 
175 }
176 
177 #endif