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