Kd_tree_search.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Clement Jamin
6  *
7  * Copyright (C) 2016 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef KD_TREE_SEARCH_H_
24 #define KD_TREE_SEARCH_H_
25 
26 #include <CGAL/Orthogonal_k_neighbor_search.h>
27 #include <CGAL/Orthogonal_incremental_neighbor_search.h>
28 #include <CGAL/Search_traits.h>
29 #include <CGAL/Search_traits_adapter.h>
30 #include <CGAL/Fuzzy_sphere.h>
31 #include <CGAL/property_map.h>
32 
33 #include <boost/property_map/property_map.hpp>
34 #include <boost/iterator/counting_iterator.hpp>
35 
36 #include <cstddef>
37 #include <vector>
38 
39 namespace Gudhi {
40 namespace spatial_searching {
41 
42 
68 template <typename Search_traits, typename Point_range>
70  typedef boost::iterator_property_map<
71  typename Point_range::const_iterator,
72  CGAL::Identity_property_map<std::ptrdiff_t> > Point_property_map;
73 
74  public:
76  typedef Search_traits Traits;
78  typedef typename Traits::FT FT;
80  typedef typename Point_range::value_type Point;
81 
82  typedef CGAL::Search_traits<
83  FT, Point,
84  typename Traits::Cartesian_const_iterator_d,
85  typename Traits::Construct_cartesian_const_iterator_d> Traits_base;
86 
87  typedef CGAL::Search_traits_adapter<
88  std::ptrdiff_t,
89  Point_property_map,
90  Traits_base> STraits;
91  typedef CGAL::Distance_adapter<
92  std::ptrdiff_t,
93  Point_property_map,
94  CGAL::Euclidean_distance<Traits_base> > Orthogonal_distance;
95 
96  typedef CGAL::Orthogonal_k_neighbor_search<STraits> K_neighbor_search;
97  typedef typename K_neighbor_search::Tree Tree;
98  typedef typename K_neighbor_search::Distance Distance;
102  typedef K_neighbor_search KNS_range;
103 
104  typedef CGAL::Orthogonal_incremental_neighbor_search<
105  STraits, Distance, CGAL::Sliding_midpoint<STraits>, Tree>
106  Incremental_neighbor_search;
110  typedef Incremental_neighbor_search INS_range;
111 
112  typedef CGAL::Fuzzy_sphere<STraits> Fuzzy_sphere;
116  Kd_tree_search(Point_range const& points)
117  : m_points(points),
118  m_tree(boost::counting_iterator<std::ptrdiff_t>(0),
119  boost::counting_iterator<std::ptrdiff_t>(points.size()),
120  typename Tree::Splitter(),
121  STraits(std::begin(points))) {
122  // Build the tree now (we don't want to wait for the first query)
123  m_tree.build();
124  }
125 
131  template <typename Point_indices_range>
133  Point_range const& points,
134  Point_indices_range const& only_these_points)
135  : m_points(points),
136  m_tree(
137  only_these_points.begin(), only_these_points.end(),
138  typename Tree::Splitter(),
139  STraits(std::begin(points))) {
140  // Build the tree now (we don't want to wait for the first query)
141  m_tree.build();
142  }
143 
150  Point_range const& points,
151  std::size_t begin_idx, std::size_t past_the_end_idx)
152  : m_points(points),
153  m_tree(
154  boost::counting_iterator<std::ptrdiff_t>(begin_idx),
155  boost::counting_iterator<std::ptrdiff_t>(past_the_end_idx),
156  typename Tree::Splitter(),
157  STraits(std::begin(points))) {
158  // Build the tree now (we don't want to wait for the first query)
159  m_tree.build();
160  }
161 
162  // Be careful, this function invalidates the tree,
163  // which will be recomputed at the next query
164  void insert(std::ptrdiff_t point_idx) {
165  m_tree.insert(point_idx);
166  }
167 
175  Point const& p,
176  unsigned int k,
177  bool sorted = true,
178  FT eps = FT(0)) const {
179  // Initialize the search structure, and search all N points
180  // Note that we need to pass the Distance explicitly since it needs to
181  // know the property map
182  K_neighbor_search search(
183  m_tree,
184  p,
185  k,
186  eps,
187  true,
188  Orthogonal_distance(std::begin(m_points)), sorted);
189 
190  return search;
191  }
192 
200  INS_range incremental_nearest_neighbors(Point const& p, FT eps = FT(0)) const {
201  // Initialize the search structure, and search all N points
202  // Note that we need to pass the Distance explicitly since it needs to
203  // know the property map
204  Incremental_neighbor_search search(
205  m_tree,
206  p,
207  eps,
208  true,
209  Orthogonal_distance(std::begin(m_points)) );
210 
211  return search;
212  }
213 
221  Point const& p,
222  unsigned int k,
223  bool sorted = true,
224  FT eps = FT(0)) const {
225  // Initialize the search structure, and search all N points
226  // Note that we need to pass the Distance explicitly since it needs to
227  // know the property map
228  K_neighbor_search search(
229  m_tree,
230  p,
231  k,
232  eps,
233  false,
234  Orthogonal_distance(std::begin(m_points)), sorted);
235 
236  return search;
237  }
238 
246  INS_range incremental_furthest_neighbors(Point const& p, FT eps = FT(0)) const {
247  // Initialize the search structure, and search all N points
248  // Note that we need to pass the Distance explicitly since it needs to
249  // know the property map
250  Incremental_neighbor_search search(
251  m_tree,
252  p,
253  eps,
254  false,
255  Orthogonal_distance(std::begin(m_points)) );
256 
257  return search;
258  }
259 
266  template <typename OutputIterator>
267  void all_near_neighbors(Point const& p,
268  FT radius,
269  OutputIterator it,
270  FT eps = FT(0)) const {
271  m_tree.search(it, Fuzzy_sphere(p, radius, eps, m_tree.traits()));
272  }
273 
274  int tree_depth() const {
275  return m_tree.root()->depth();
276  }
277 
278  private:
279  Point_range const& m_points;
280  Tree m_tree;
281 };
282 
283 } // namespace spatial_searching
284 } // namespace Gudhi
285 
286 #endif // KD_TREE_SEARCH_H_
Spatial tree data structure to perform (approximate) nearest and furthest neighbor search...
Definition: Kd_tree_search.h:69
Kd_tree_search(Point_range const &points, std::size_t begin_idx, std::size_t past_the_end_idx)
Constructor.
Definition: Kd_tree_search.h:149
Kd_tree_search(Point_range const &points, Point_indices_range const &only_these_points)
Constructor.
Definition: Kd_tree_search.h:132
Incremental_neighbor_search INS_range
The range returned by an incremental nearest or furthest neighbor search. Its value type is std::pair...
Definition: Kd_tree_search.h:110
K_neighbor_search KNS_range
The range returned by a k-nearest or k-furthest neighbor search. Its value type is std::pair<std::siz...
Definition: Kd_tree_search.h:102
INS_range incremental_furthest_neighbors(Point const &p, FT eps=FT(0)) const
Search incrementally for the furthest neighbors from a query point.
Definition: Kd_tree_search.h:246
KNS_range k_nearest_neighbors(Point const &p, unsigned int k, bool sorted=true, FT eps=FT(0)) const
Search for the k-nearest neighbors from a query point.
Definition: Kd_tree_search.h:174
Kd_tree_search(Point_range const &points)
Constructor.
Definition: Kd_tree_search.h:116
Definition: SimplicialComplexForAlpha.h:26
INS_range incremental_nearest_neighbors(Point const &p, FT eps=FT(0)) const
Search incrementally for the nearest neighbors from a query point.
Definition: Kd_tree_search.h:200
KNS_range k_furthest_neighbors(Point const &p, unsigned int k, bool sorted=true, FT eps=FT(0)) const
Search for the k-furthest points from a query point.
Definition: Kd_tree_search.h:220
Traits::FT FT
Number type used for distances.
Definition: Kd_tree_search.h:78
void all_near_neighbors(Point const &p, FT radius, OutputIterator it, FT eps=FT(0)) const
Search for all the neighbors in a ball.
Definition: Kd_tree_search.h:267
Point_range::value_type Point
The point type.
Definition: Kd_tree_search.h:80
Search_traits Traits
The Traits.
Definition: Kd_tree_search.h:76
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:54 for GUDHI by Doxygen 1.8.13