GetFEM  5.5
bgeot_kdtree.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2026 Julien Pommier
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 #ifndef BGEOT_KDTREE_H
32 #define BGEOT_KDTREE_H
33 
34 /** @file bgeot_kdtree.h
35  @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
36  @date January 2004.
37  @brief Simple implementation of a KD-tree.
38 
39  Basically, a KD-tree is a balanced N-dimensional tree.
40 */
41 #include "bgeot_small_vector.h"
42 
43 namespace bgeot {
44 
45  /* generic node for the kdtree */
46  struct kdtree_elt_base {
47  enum { PTS_PER_LEAF=8 };
48  unsigned n; /* 0 => is a tree node, != 0 => tree leaf storing n points */
49  bool isleaf() const { return (n != 0); }
50  kdtree_elt_base(unsigned n_) : n(n_) {}
51  virtual ~kdtree_elt_base() {}
52  };
53 
54  /// store a point and the associated index for the kdtree.
55  /* std::pair<size_type,base_node> is not ok since it does not
56  have a suitable overloaded swap function ...
57  */
58  struct index_node_pair {
59  size_type i;
60  base_node n;
61  index_node_pair() {}
62  index_node_pair(size_type i_, base_node n_) : i(i_), n(n_) {}
63  void swap(index_node_pair& other) { std::swap(i,other.i); n.swap(other.n);}
64  };
65 
66  /// store a set of points with associated indexes.
67  typedef std::vector<index_node_pair> kdtree_tab_type;
68 
69  /** Balanced tree over a set of points.
70 
71  Once the tree have been built, it is possible to query very
72  quickly for the list of points lying in a given box. Note that
73  this is not a dynamic structure: once you start to call
74  kdtree::points_in_box, you should not use anymore kdtree::add_point.
75 
76  Here is an example of use (which tries to find the mapping between
77  the dof of the mesh_fem and the node numbers of its mesh):
78 
79  @code
80  void dof_to_nodes(const getfem::mesh_fem &mf) {
81  const getfem::getfem_mesh &m = mf.linked_mesh();
82  bgeot::kdtree tree;
83  for (dal::bv_visitor ip(m.points().index()); !ip.finished(); ++ip) {
84  tree.add_point_with_id(m.points()[ip], ip);
85  }
86  bgeot::kdtree_tab_type t;
87  for (size_type d = 0; d < mf.nb_dof(); ++d) {
88  getfem::base_node P(mf.point_of_dof(d)), P2(P);
89  for (unsigned i=0; i < P.size(); ++i) {
90  P[i] -= 1e-12; P2[i]+= 1e-12;
91  }
92  tree.points_in_box(t, P, P2);
93  if (t.size()) {
94  assert(t.size() == 1);
95  cout << " dof " << d << " maps to mesh node " << t[0].i << "\n";
96  }
97  }
98  }
99  @endcode
100  */
101  class kdtree {
102  dim_type N; /* dimension of points */
103  std::unique_ptr<kdtree_elt_base> tree;
104  kdtree_tab_type pts;
105  public:
106  kdtree() : N(0) {}
107 
108  kdtree(const kdtree&) = delete;
109  kdtree &operator = (const kdtree&) = delete;
110 
111  /// reset the tree, remove all points
112  void clear() { clear_tree(); pts = kdtree_tab_type(); N = 0; }
113  void reserve(size_type n) { pts.reserve(n); }
114  /// insert a new point
116  size_type i = pts.size(); add_point_with_id(n,i); return i;
117  }
118  /// insert a new point, with an associated number.
120  if (pts.size() == 0)
121  N = n.size();
122  else
123  GMM_ASSERT2(N == n.size(), "invalid dimension");
124  if (tree) clear_tree();
125  pts.push_back(index_node_pair(i, n));
126  }
127  size_type nb_points() const { return pts.size(); }
128  const kdtree_tab_type &points() const { return pts; }
129  /* fills ipts with the indexes of points in the box
130  [min,max] */
131  void points_in_box(kdtree_tab_type &ipts,
132  const base_node &min,
133  const base_node &max);
134  /* assigns at ipt the index of the nearest neighbor at location
135  pos and returns the square of the distance to this point*/
136  scalar_type nearest_neighbor(index_node_pair &ipt,
137  const base_node &pos);
138  private:
139  typedef std::vector<size_type>::const_iterator ITER;
140  void clear_tree();
141  };
142 }
143 
144 #endif
Small (dim < 8) vectors.
Balanced tree over a set of points.
Definition: bgeot_kdtree.h:101
void add_point_with_id(const base_node &n, size_type i)
insert a new point, with an associated number.
Definition: bgeot_kdtree.h:119
size_type add_point(const base_node &n)
insert a new point
Definition: bgeot_kdtree.h:115
void clear()
reset the tree, remove all points
Definition: bgeot_kdtree.h:112
Basic Geometric Tools.
std::vector< index_node_pair > kdtree_tab_type
store a set of points with associated indexes.
Definition: bgeot_kdtree.h:67
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
store a point and the associated index for the kdtree.
Definition: bgeot_kdtree.h:58