RINGMesh  Version 5.0.0
A programming library for geological model meshes
nn_search.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #pragma once
37 
38 #include <ringmesh/basic/common.h>
39 
40 #include <ringmesh/basic/pimpl.h>
41 
47 namespace RINGMesh
48 {
49  template < index_t DIMENSION >
50  class NNSearch
51  {
54 
55  public:
56  explicit NNSearch( const std::vector< vecn< DIMENSION > >& vertices,
57  bool copy = true );
58 
59  ~NNSearch() = default;
60 
70  std::tuple< index_t, std::vector< index_t > >
71  get_colocated_index_mapping( double epsilon ) const;
82  std::tuple< index_t,
83  std::vector< index_t >,
84  std::vector< vecn< DIMENSION > > >
86  double epsilon ) const;
92  index_t get_closest_neighbor( const vecn< DIMENSION >& v ) const
93  {
94  index_t nb_neighbors{ 1 };
95  return get_neighbors( v, nb_neighbors ).front();
96  }
97 
105  std::vector< index_t > get_neighbors(
106  const vecn< DIMENSION >& v, double threshold_distance ) const;
107 
116  template < typename TEST >
117  std::vector< index_t > get_neighbors(
118  const vecn< DIMENSION >& v, const TEST& test ) const
119  {
120  std::vector< index_t > result;
121  auto nb_points = this->nb_points();
122  if( nb_points != 0 )
123  {
124  index_t nb_neighbors{ std::min( index_t( 5 ), nb_points ) };
125  index_t cur_neighbor{ 0 };
126  index_t prev_neighbor{ 0 };
127  do
128  {
129  prev_neighbor = cur_neighbor;
130  cur_neighbor += nb_neighbors;
131  result.reserve( cur_neighbor );
132  auto neighbors = get_neighbors( v, cur_neighbor );
133  nb_neighbors = static_cast< index_t >( neighbors.size() );
134  for( auto i : range( prev_neighbor, cur_neighbor ) )
135  {
136  if( test( neighbors[i] ) )
137  {
138  break;
139  }
140  result.push_back( neighbors[i] );
141  }
142  } while( result.size() == cur_neighbor
143  && result.size() < nb_points );
144  }
145  return result;
146  }
147 
156  std::vector< index_t > get_neighbors(
157  const vecn< DIMENSION >& v, index_t nb_neighbors ) const;
158 
159  vecn< DIMENSION > point( index_t v ) const;
160 
161  index_t nb_points() const;
162 
163  private:
164  IMPLEMENTATION_MEMBER( impl_ );
165  };
167 
168 } // namespace RINGMesh
index_t get_closest_neighbor(const vecn< DIMENSION > &v) const
Definition: nn_search.h:92
GEO::vecng< DIMENSION, double > vecn
Definition: types.h:74
ALIAS_2D_AND_3D(Box)
std::vector< index_t > get_neighbors(const vecn< DIMENSION > &v, const TEST &test) const
Definition: nn_search.h:117
ringmesh_template_assert_2d_or_3d(DIMENSION)
index_t nb_points() const
Definition: nn_search.cpp:148
NNSearch(const std::vector< vecn< DIMENSION > > &vertices, bool copy=true)
Definition: nn_search.cpp:135
vecn< DIMENSION > point(index_t v) const
Definition: nn_search.cpp:142
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
IMPLEMENTATION_MEMBER(impl_)
std::vector< index_t > get_neighbors(const vecn< DIMENSION > &v, double threshold_distance) const
Definition: nn_search.cpp:205
std::tuple< index_t, std::vector< index_t > > get_colocated_index_mapping(double epsilon) const
Gets the index_map that link all the duplicated points to their first occurancy.
Definition: nn_search.cpp:155
ringmesh_disable_copy_and_move(NNSearch)
std::tuple< index_t, std::vector< index_t >, std::vector< vecn< DIMENSION > > > get_colocated_index_mapping_and_unique_points(double epsilon) const
Gets the index_map that link all the points to a no duplicated list of index in the list of unique_po...
Definition: nn_search.cpp:177