DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_StaticSearchTree_impl.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_IMPL_HPP
42 #define DTK_STATICSEARCHTREE_IMPL_HPP
43 
44 #include <limits>
45 
46 #include "DTK_DBC.hpp"
47 
48 #include <Teuchos_as.hpp>
49 
50 namespace DataTransferKit
51 {
52 //---------------------------------------------------------------------------//
56 template <>
57 inline double
58 PointCloud<1>::kdtree_distance( const double *p1, const std::size_t idx_p2,
59  std::size_t DTK_REMEMBER( size ) ) const
60 {
61  DTK_REQUIRE( 1 == size );
62  DTK_REQUIRE( idx_p2 < Teuchos::as<std::size_t>( d_points.size() ) );
63  const double d0 = p1[0] - d_points[idx_p2];
64  return d0 * d0;
65 }
66 
67 //---------------------------------------------------------------------------//
71 template <>
72 inline double
73 PointCloud<2>::kdtree_distance( const double *p1, const std::size_t idx_p2,
74  std::size_t DTK_REMEMBER( size ) ) const
75 {
76  DTK_REQUIRE( 2 == size );
77  DTK_REQUIRE( 2 * idx_p2 + 1 < Teuchos::as<std::size_t>( d_points.size() ) );
78  const double d0 = p1[0] - d_points[2 * idx_p2];
79  const double d1 = p1[1] - d_points[2 * idx_p2 + 1];
80  return d0 * d0 + d1 * d1;
81 }
82 
83 //---------------------------------------------------------------------------//
87 template <>
88 inline double
89 PointCloud<3>::kdtree_distance( const double *p1, const std::size_t idx_p2,
90  std::size_t DTK_REMEMBER( size ) ) const
91 {
92  DTK_REQUIRE( 3 == size );
93  DTK_REQUIRE( 3 * idx_p2 + 2 < Teuchos::as<std::size_t>( d_points.size() ) );
94  const double d0 = p1[0] - d_points[3 * idx_p2];
95  const double d1 = p1[1] - d_points[3 * idx_p2 + 1];
96  const double d2 = p1[2] - d_points[3 * idx_p2 + 2];
97  return d0 * d0 + d1 * d1 + d2 * d2;
98 }
99 
100 //---------------------------------------------------------------------------//
104 template <>
105 inline double PointCloud<1>::kdtree_get_pt( const std::size_t idx,
106  int DTK_REMEMBER( dim ) ) const
107 {
108  DTK_REQUIRE( dim < 1 );
109  DTK_REQUIRE( idx < Teuchos::as<std::size_t>( d_points.size() ) );
110  return d_points[idx];
111 }
112 
113 //---------------------------------------------------------------------------//
117 template <>
118 inline double PointCloud<2>::kdtree_get_pt( const std::size_t idx,
119  int dim ) const
120 {
121  DTK_REQUIRE( dim < 2 );
122  DTK_REQUIRE( 2 * idx + dim < Teuchos::as<std::size_t>( d_points.size() ) );
123  return d_points[2 * idx + dim];
124 }
125 
126 //---------------------------------------------------------------------------//
130 template <>
131 inline double PointCloud<3>::kdtree_get_pt( const std::size_t idx,
132  int dim ) const
133 {
134  DTK_REQUIRE( dim < 3 );
135  DTK_REQUIRE( 3 * idx + dim < Teuchos::as<std::size_t>( d_points.size() ) );
136  return d_points[3 * idx + dim];
137 }
138 
139 //---------------------------------------------------------------------------//
140 // NanoflannTree Implementation.
141 //---------------------------------------------------------------------------//
145 template <int DIM>
147  const Teuchos::ArrayView<const double> &points,
148  const unsigned max_leaf_size )
149 {
150  DTK_CHECK( 0 == points.size() % DIM );
151 
152  d_cloud = PointCloud<DIM>( points );
153  d_tree = Teuchos::rcp( new TreeType(
154  DIM, d_cloud,
155  nanoflann::KDTreeSingleIndexAdaptorParams( max_leaf_size ) ) );
156  d_tree->buildIndex();
157 }
158 
159 //---------------------------------------------------------------------------//
163 template <int DIM>
164 Teuchos::Array<unsigned>
165 NanoflannTree<DIM>::nnSearch( const Teuchos::ArrayView<const double> &point,
166  const unsigned num_neighbors ) const
167 {
168  DTK_REQUIRE( DIM == point.size() );
169  Teuchos::Array<unsigned> neighbors( num_neighbors );
170  Teuchos::Array<double> neighbor_dists( num_neighbors );
171  d_tree->knnSearch( point.getRawPtr(), num_neighbors, neighbors.getRawPtr(),
172  neighbor_dists.getRawPtr() );
173  return neighbors;
174 }
175 
176 //---------------------------------------------------------------------------//
180 template <int DIM>
181 Teuchos::Array<unsigned>
182 NanoflannTree<DIM>::radiusSearch( const Teuchos::ArrayView<const double> &point,
183  const double radius ) const
184 {
185  DTK_REQUIRE( DIM == point.size() );
186  Teuchos::Array<std::pair<unsigned, double>> neighbor_pairs;
188  double l2_radius = radius * radius;
189  d_tree->radiusSearch( point.getRawPtr(), l2_radius, neighbor_pairs,
190  params );
191 
192  Teuchos::Array<std::pair<unsigned, double>>::const_iterator pair_it;
193  Teuchos::Array<unsigned> neighbors( neighbor_pairs.size() );
194  Teuchos::Array<unsigned>::iterator id_it;
195  for ( id_it = neighbors.begin(), pair_it = neighbor_pairs.begin();
196  id_it != neighbors.end(); ++id_it, ++pair_it )
197  {
198  *id_it = pair_it->first;
199  }
200 
201  return neighbors;
202 }
203 
204 //---------------------------------------------------------------------------//
205 
206 } // end namespace DataTransferKit
207 
208 //---------------------------------------------------------------------------//
209 
210 #endif // end DTK_STATICSEARCHTREE_IMPL_HPP
211 
212 //---------------------------------------------------------------------------//
213 // end DTK_StaticSearchTree_impl.hpp
214 //---------------------------------------------------------------------------//
Teuchos::Array< unsigned > nnSearch(const Teuchos::ArrayView< const double > &point, const unsigned num_neighbors) const
Perform an n-nearest neighbor search.
Teuchos::Array< unsigned > radiusSearch(const Teuchos::ArrayView< const double > &point, const double radius) const
Perform a nearest neighbor search within a specified radius.
Assertions and Design-by-Contract for error handling.
NanoflannTree(const Teuchos::ArrayView< const double > &points, const unsigned max_leaf_size)
Constructor.
DTK_BasicEntitySet.cpp.