K_nearest_builder.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): David Salinas
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef UTILS_K_NEAREST_BUILDER_H_
12 #define UTILS_K_NEAREST_BUILDER_H_
13 
14 #include <CGAL/Euclidean_distance.h>
15 #include <CGAL/Orthogonal_k_neighbor_search.h>
16 #include <CGAL/Search_traits_d.h>
17 #include <CGAL/Search_traits_adapter.h>
18 #include <CGAL/property_map.h>
19 
20 #include <unordered_map>
21 #include <list>
22 #include <utility>
23 
24 #include "utils/UI_utils.h"
25 #include "model/Complex_typedefs.h"
26 
27 template<typename SkBlComplex> class K_nearest_builder {
28  private:
29  typedef Geometry_trait Kernel;
30  typedef Point Point_d;
31  typedef std::pair<Point_d, unsigned> Point_d_with_id;
32  typedef CGAL::Search_traits_d<Kernel> Traits_base;
33  typedef CGAL::Search_traits_adapter<Point_d_with_id, CGAL::First_of_pair_property_map<Point_d_with_id>,
34  Traits_base> Traits;
35  typedef CGAL::Orthogonal_k_neighbor_search<Traits> Neighbor_search;
36  typedef Neighbor_search::Tree Tree;
37  typedef Neighbor_search::Distance Distance;
38 
39  SkBlComplex& complex_;
40 
41  public:
46  K_nearest_builder(SkBlComplex& complex, unsigned k) : complex_(complex) {
47  complex.keep_only_vertices();
48  compute_edges(k);
49  }
50 
51  private:
52  double squared_eucl_distance(const Point& p1, const Point& p2) const {
53  return Geometry_trait::Squared_distance_d()(p1, p2);
54  }
55 
56  void compute_edges(unsigned k) {
57  std::list<Point_d_with_id> points_with_id;
58  for (auto v : complex_.vertex_range()) {
59  Point_d_with_id point_v(complex_.point(v), v.vertex);
60  points_with_id.push_back(point_v);
61  }
62 
63  Tree tree(points_with_id.begin(), points_with_id.end());
64 
65  typedef typename SkBlComplex::Vertex_handle Vertex_handle;
66  for (auto p : complex_.vertex_range()) {
67  Neighbor_search search(tree, complex_.point(p), k + 1);
68  for (auto it = ++search.begin(); it != search.end(); ++it) {
69  Vertex_handle q(std::get<1>(it->first));
70  if (p != q && complex_.contains_vertex(p) && complex_.contains_vertex(q))
71  complex_.add_edge_without_blockers(p, q);
72  }
73  }
74  }
75 };
76 
77 #endif // UTILS_K_NEAREST_BUILDER_H_
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15
GUDHIdev  Version 3.5.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 16 2022 14:01:50 for GUDHIdev by Doxygen 1.9.1