DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_StaticSearchTree.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived 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 FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #ifndef DTK_STATICSEARCHTREE_HPP
42 #define DTK_STATICSEARCHTREE_HPP
43 
44 #include <Teuchos_Array.hpp>
45 #include <Teuchos_ArrayView.hpp>
46 #include <Teuchos_RCP.hpp>
47 
48 #include <DTK_nanoflann.hpp>
49 
50 namespace DataTransferKit
51 {
52 //---------------------------------------------------------------------------//
53 // Non-templated static search tree base class.
54 //---------------------------------------------------------------------------//
55 class StaticSearchTree
56 {
57  public:
58  // Default constructor.
59  StaticSearchTree() { /* ... */}
60 
61  // Destructor.
62  virtual ~StaticSearchTree() { /* ... */}
63 
64  // Perform an n-nearest neighbor search.
65  virtual Teuchos::Array<unsigned>
66  nnSearch( const Teuchos::ArrayView<const double> &point,
67  const unsigned num_neighbors ) const = 0;
68 
69  // Perform a nearest neighbor search within a specified radius.
70  virtual Teuchos::Array<unsigned>
71  radiusSearch( const Teuchos::ArrayView<const double> &point,
72  const double radius ) const = 0;
73 };
74 
75 //---------------------------------------------------------------------------//
76 // Point cloud structure.
77 //---------------------------------------------------------------------------//
78 template <int DIM>
79 class PointCloud
80 {
81  public:
83  PointCloud() { /* ... */}
84 
86  PointCloud( const Teuchos::ArrayView<const double> &points )
87  : d_points( points )
88  { /* ... */
89  }
90 
92  ~PointCloud() { /* ... */}
93 
95  inline std::size_t kdtree_get_point_count() const
96  {
97  return d_points.size() / DIM;
98  }
99 
100  // Distance between points.
101  double kdtree_distance( const double *p1, const std::size_t idx_p2,
102  std::size_t size ) const;
103 
104  // Get the point coordinate at the given dimension.
105  double kdtree_get_pt( const std::size_t idx, int dim ) const;
106 
108  template <class BBOX>
109  bool kdtree_get_bbox( BBOX & /*bb*/ ) const
110  {
111  return false;
112  }
113 
114  private:
115  // PointCloud points.
116  Teuchos::ArrayView<const double> d_points;
117 };
118 
119 //---------------------------------------------------------------------------//
124 //---------------------------------------------------------------------------//
125 template <int DIM>
126 class NanoflannTree : public StaticSearchTree
127 {
128  public:
132  DIM, unsigned>
134 
135  public:
136  // Default constructor.
137  NanoflannTree( const Teuchos::ArrayView<const double> &points,
138  const unsigned max_leaf_size );
139 
140  // Destructor.
141  ~NanoflannTree() { /* ... */}
142 
143  // Perform an n-nearest neighbor search.
144  Teuchos::Array<unsigned>
145  nnSearch( const Teuchos::ArrayView<const double> &point,
146  const unsigned num_neighbors ) const;
147 
148  // Perform a nearest neighbor search within a specified radius.
149  Teuchos::Array<unsigned>
150  radiusSearch( const Teuchos::ArrayView<const double> &point,
151  const double radius ) const;
152 
153  private:
154  // PointCloud.
155  PointCloud<DIM> d_cloud;
156 
157  // kD-tree.
158  Teuchos::RCP<TreeType> d_tree;
159 };
160 
161 //---------------------------------------------------------------------------//
162 
163 } // end namespace DataTransferKit
164 
165 //---------------------------------------------------------------------------//
166 // Template includes.
167 //---------------------------------------------------------------------------//
168 
170 
171 //---------------------------------------------------------------------------//
172 
173 #endif // end DTK_STATICSEARCHTREE_HPP
174 
175 //---------------------------------------------------------------------------//
176 // end DTK_StaticSearchTree.hpp
177 //---------------------------------------------------------------------------//
Spatial searching for point clouds.
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloud< DIM > >, PointCloud< DIM >, DIM, unsigned > TreeType
Tree typedef.
Spatial searching for point clouds.
DTK_BasicEntitySet.cpp.