MEPP2 Project
Graph_properties_cgal_point_set.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
14 #include <CGAL/boost/graph/properties.h> // for boost::vertex_point_t
15 
16 #include <CGAL/Search_traits_3.h>
17 #include <CGAL/Search_traits_adapter.h>
18 #include <CGAL/Orthogonal_k_neighbor_search.h>
19 #include <CGAL/Orthogonal_incremental_neighbor_search.h>
20 #include <CGAL/boost/iterator/counting_iterator.hpp>
21 #include <memory> // for std::unique_ptr
22 #include <utility> // for std::make_pair
23 
24 
25 namespace FEVV {
26 
27 using CGALPointSetPointMap = CGALPointSet::Point_map;
28 
29 } // namespace FEVV
30 
31 
32 namespace CGAL {
33 
34 // note:
35 // FEVV::CGALPointSet is a typedef to CGAL::Point_set_3<...> ;
36 // functions f(FEVV::CGALPointSet) must be declared in the same
37 // namespace as CGALPointSet real underlying class for the
38 // name lookup mecanism to work properly ;
39 // see https://en.cppreference.com/w/cpp/language/adl
40 
44 inline
46 get(const boost::vertex_point_t, FEVV::CGALPointSet &ps)
47 {
48  return ps.point_map();
49 }
50 // const version for filters that take a const pc as parameter
51 inline
53 get(const boost::vertex_point_t, const FEVV::CGALPointSet &ps)
54 {
55  return ps.point_map();
56 }
57 
58 // --------------- Nearest neighbors search ---------------
59 
61 {
62  // k-d tree type
65  using Traits_base = CGAL::Search_traits_3< FEVV::CGALPointSetKernel >;
66  using Traits = CGAL::Search_traits_adapter< vertex_descriptor,
68  Traits_base >;
69  using KdTree = CGAL::Kd_tree< Traits >;
70  using KdTreePtr = std::unique_ptr< KdTree >;
71  // note : CGAL::Kd_Tree can not be returned by a function because its copy
72  // constructor is private ; a compilation error occurs even in case
73  // of copy elision ; so we use a smart pointer as a workaround
74 };
75 
76 
77 // MEPP2 PointCloud concept
81 inline
84 {
85  typedef CGALPointSetNNSearch::Traits Traits;
86  typedef CGALPointSetNNSearch::KdTree KdTree;
87  typedef CGALPointSetNNSearch::KdTreePtr KdTreePtr;
88 
89  // retrieve Point map
90  auto ppmap = pc.point_map();
91 
92  // create kdtree (insert all points in the tree)
93  KdTreePtr kd_tree(
94  new KdTree(boost::counting_iterator< std::size_t >(0),
95  boost::counting_iterator< std::size_t >(pc.number_of_points()),
96  KdTree::Splitter(),
97  Traits(ppmap)));
98 
99  return kd_tree;
100 }
101 
102 
103 // MEPP2 PointCloud concept
110 inline
111 std::pair< std::vector< CGALPointSetNNSearch::vertex_descriptor >,
112  std::vector< double > >
114  const CGALPointSetNNSearch::KdTree &kd_tree,
115  unsigned int k,
116  const FEVV::CGALPointSetPoint &query,
117  const FEVV::CGALPointSet& pc)
118 {
120  typedef CGALPointSetNNSearch::Traits Traits;
121  typedef CGAL::Orthogonal_k_neighbor_search< Traits > K_neighbor_search;
122  typedef K_neighbor_search::Distance Distance;
123 
124  // retrieve Point map
125  auto ppmap = pc.point_map();
126 
127  // search K nearest neighbors
128  Distance sq_dist(ppmap); // squared distance
129  K_neighbor_search search(kd_tree, query, k, 0, true, sq_dist);
130 
131  // convert ids and distances
132  std::vector< vertex_descriptor > nn_ids;
133  std::vector< double > nn_distances;
134  for(auto it = search.begin(); it != search.end(); it++)
135  {
136  nn_ids.push_back(it->first);
137  nn_distances.push_back(sq_dist.inverse_of_transformed_distance(it->second));
138  }
139 
140  // return pair (vector_of_ids, vector_of_distances)
141  return make_pair(nn_ids, nn_distances);
142 }
143 
144 
145 // MEPP2 PointCloud concept
154 inline
155 std::pair< std::vector< CGALPointSetNNSearch::vertex_descriptor >,
156  std::vector< double > >
158  const CGALPointSetNNSearch::KdTree &kd_tree,
159  double radius,
160  const FEVV::CGALPointSetPoint &query,
161  const FEVV::CGALPointSet& pc)
162 {
164  typedef CGALPointSetNNSearch::Traits Traits;
165  typedef CGAL::Orthogonal_incremental_neighbor_search< Traits >
166  NN_incremental_search;
167  typedef NN_incremental_search::Distance Distance;
168 
169  // retrieve Point map
170  auto ppmap = pc.point_map();
171 
172  // search nearest neighbors
173  Distance sq_dist(ppmap); // squared distance
174  NN_incremental_search search(kd_tree, query, 0.0, true, sq_dist);
175 
176  // get nearest neighbors ids with distance < radius
177  std::vector< vertex_descriptor > nn_ids;
178  std::vector< double > nn_distances;
179  for(auto it = search.begin(); it != search.end(); ++it)
180  {
181  double distance = sq_dist.inverse_of_transformed_distance(it->second);
182  if(distance > radius)
183  break; // stop searching
184 
185  // add current point to list
186  nn_ids.push_back(it->first);
187  nn_distances.push_back(distance);
188  }
189 
190  // return pair (vector_of_ids, vector_of_distances)
191  return make_pair(nn_ids, nn_distances);
192 }
193 
194 } // namespace CGAL
195 
196 
197 namespace boost {
198 
199 template<>
200 struct property_traits< FEVV::CGALPointSet >
201 {
202  typedef FEVV::CGALPointSetPointMap::value_type value_type;
203  typedef FEVV::CGALPointSetPointMap::reference reference;
204 };
205 
206 } // namespace boost
CGAL::kNN_search
std::pair< std::vector< CGALPointSetNNSearch::vertex_descriptor >, std::vector< double > > kNN_search(const CGALPointSetNNSearch::KdTree &kd_tree, unsigned int k, const FEVV::CGALPointSetPoint &query, const FEVV::CGALPointSet &pc)
Search the k nearest neighbors of a geometric point.
Definition: Graph_properties_cgal_point_set.h:113
FEVV::CGALPointSet
CGAL::Point_set_3< CGALPointSetPoint > CGALPointSet
Definition: DataStructures_cgal_point_set.h:71
CGAL::create_kd_tree
CGALPointSetNNSearch::KdTreePtr create_kd_tree(const FEVV::CGALPointSet &pc)
Initialize a k-d tree with all cloud points.
Definition: Graph_properties_cgal_point_set.h:83
CGAL::CGALPointSetNNSearch::KdTreePtr
std::unique_ptr< KdTree > KdTreePtr
Definition: Graph_properties_cgal_point_set.h:70
boost
Definition: Graph_properties_aif.h:48
boost::property_traits< FEVV::CGALPointSet >::value_type
FEVV::CGALPointSetPointMap::value_type value_type
Definition: Graph_properties_cgal_point_set.h:202
FEVV::CGALPointSetPoint
CGALPointSetKernel::Point_3 CGALPointSetPoint
Definition: DataStructures_cgal_point_set.h:30
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
boost::property_traits< FEVV::CGALPointSet >::reference
FEVV::CGALPointSetPointMap::reference reference
Definition: Graph_properties_cgal_point_set.h:203
CGAL
Definition: Graph_properties_cgal_point_set.h:32
CGAL::CGALPointSetNNSearch::Traits_base
CGAL::Search_traits_3< FEVV::CGALPointSetKernel > Traits_base
Definition: Graph_properties_cgal_point_set.h:65
Graph_traits_cgal_point_set.h
CGAL::CGALPointSetNNSearch
Definition: Graph_properties_cgal_point_set.h:61
CGAL::CGALPointSetNNSearch::Traits
CGAL::Search_traits_adapter< vertex_descriptor, FEVV::CGALPointSetPointMap, Traits_base > Traits
Definition: Graph_properties_cgal_point_set.h:68
boost::graph_traits< FEVV::CGALPointSet >::vertex_descriptor
FEVV::CGALPointSet::Index vertex_descriptor
Definition: Graph_traits_cgal_point_set.h:31
msdm2::vertex_descriptor
boost::graph_traits< MeshT >::vertex_descriptor vertex_descriptor
Definition: msdm2_surfacemesh.h:33
CGAL::get
FEVV::CGALPointSetPointMap get(const boost::vertex_point_t, FEVV::CGALPointSet &ps)
Returns the points property map (aka the geometry) of the mesh.
Definition: Graph_properties_cgal_point_set.h:46
CGAL::radius_search
std::pair< std::vector< CGALPointSetNNSearch::vertex_descriptor >, std::vector< double > > radius_search(const CGALPointSetNNSearch::KdTree &kd_tree, double radius, const FEVV::CGALPointSetPoint &query, const FEVV::CGALPointSet &pc)
Search for the nearest neighbors of a geometric point in the given radius.
Definition: Graph_properties_cgal_point_set.h:157
CGAL::CGALPointSetNNSearch::KdTree
CGAL::Kd_tree< Traits > KdTree
Definition: Graph_properties_cgal_point_set.h:69
FEVV::CGALPointSetPointMap
CGALPointSet::Point_map CGALPointSetPointMap
Definition: Graph_properties_cgal_point_set.h:27
CGAL::CGALPointSetNNSearch::vertex_descriptor
boost::graph_traits< FEVV::CGALPointSet >::vertex_descriptor vertex_descriptor
Definition: Graph_properties_cgal_point_set.h:64