summaryrefslogtreecommitdiff
path: root/src/Bottleneck_distance/include/gudhi/Neighbors_finder.h
blob: c65e60828e140b1e1a7b9598cc11fc06c8040215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author:       Francois Godi
 *
 *    Copyright (C) 2015 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef NEIGHBORS_FINDER_H_
#define NEIGHBORS_FINDER_H_

// Inclusion order is important for CGAL patch
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits.h>

#include <gudhi/Persistence_graph.h>
#include <gudhi/Internal_point.h>

#include <unordered_set>
#include <vector>
#include <algorithm>  // for std::max
#include <cmath>  // for std::abs

namespace Gudhi {

namespace persistence_diagram {

/** \internal \brief Variant of CGAL::Fuzzy_iso_box to ensure that the box ic closed. It isn't clear how necessary that is.
 */
struct Square_query {
  typedef CGAL::Dimension_tag<2> D;
  typedef Internal_point Point_d;
  typedef double FT;
  bool contains(Point_d p) const {
    return (std::max)(std::abs(p.x()-c.x()), std::abs(p.y()-c.y())) <= size;
  }
  bool inner_range_intersects(CGAL::Kd_tree_rectangle<FT, D> const&r) const {
    return
      r.max_coord(0) >= c.x() - size &&
      r.min_coord(0) <= c.x() + size &&
      r.max_coord(1) >= c.y() - size &&
      r.min_coord(1) <= c.y() + size;
  }
  bool outer_range_contains(CGAL::Kd_tree_rectangle<FT, D> const&r) const {
    return
      r.min_coord(0) >= c.x() - size &&
      r.max_coord(0) <= c.x() + size &&
      r.min_coord(1) >= c.y() - size &&
      r.max_coord(1) <= c.y() + size;
  }
  Point_d c;
  FT size;
};

/** \internal \brief data structure used to find any point (including projections) in V near to a query point from U
 * (which can be a projection).
 *
 * V points have to be added manually using their index and before the first pull. A neighbor pulled is automatically
 * removed.
 *
 * \ingroup bottleneck_distance
 */
class Neighbors_finder {
  typedef CGAL::Dimension_tag<2> D;
  typedef CGAL::Search_traits<double, Internal_point, const double*, Construct_coord_iterator, D> Traits;
  typedef CGAL::Kd_tree<Traits> Kd_tree;

 public:
  /** \internal \brief Constructor taking the near distance definition as parameter. */
  Neighbors_finder(const Persistence_graph& g, double r);
  /** \internal \brief A point added will be possibly pulled. */
  void add(int v_point_index);
  /** \internal \brief Returns and remove a V point near to the U point given as parameter, null_point_index() if
   * there isn't such a point. */
  int pull_near(int u_point_index);
  /** \internal \brief Returns and remove all the V points near to the U point given as parameter. */
  std::vector<int> pull_all_near(int u_point_index);

 private:
  const Persistence_graph& g;
  const double r;
  Kd_tree kd_t;
  std::unordered_set<int> projections_f;
};

/** \internal \brief data structure used to find any point (including projections) in V near to a query point from U
 * (which can be a projection) in a layered graph layer given as parmeter.
 *
 * V points have to be added manually using their index and before the first pull. A neighbor pulled is automatically
 * removed.
 *
 * \ingroup bottleneck_distance
 */
class Layered_neighbors_finder {
 public:
  /** \internal \brief Constructor taking the near distance definition as parameter. */
  Layered_neighbors_finder(const Persistence_graph& g, double r);
  /** \internal \brief A point added will be possibly pulled. */
  void add(int v_point_index, int vlayer);
  /** \internal \brief Returns and remove a V point near to the U point given as parameter, null_point_index() if
   * there isn't such a point. */
  int pull_near(int u_point_index, int vlayer);
  /** \internal \brief Returns the number of layers. */
  int vlayers_number() const;

 private:
  const Persistence_graph& g;
  const double r;
  std::vector<std::unique_ptr<Neighbors_finder>> neighbors_finder;
};

inline Neighbors_finder::Neighbors_finder(const Persistence_graph& g, double r) :
    g(g), r(r), kd_t(), projections_f() { }

inline void Neighbors_finder::add(int v_point_index) {
  if (g.on_the_v_diagonal(v_point_index))
    projections_f.emplace(v_point_index);
  else
    kd_t.insert(g.get_v_point(v_point_index));
}

inline int Neighbors_finder::pull_near(int u_point_index) {
  int tmp;
  int c = g.corresponding_point_in_v(u_point_index);
  if (g.on_the_u_diagonal(u_point_index) && !projections_f.empty()) {
    // Any pair of projection is at distance 0
    tmp = *projections_f.cbegin();
    projections_f.erase(tmp);
  } else if (projections_f.count(c) && (g.distance(u_point_index, c) <= r)) {
    // Is the query point near to its projection ?
    tmp = c;
    projections_f.erase(tmp);
  } else {
    // Is the query point near to a V point in the plane ?
    Internal_point u_point = g.get_u_point(u_point_index);
    auto neighbor = kd_t.search_any_point(Square_query{u_point, r});
    if (!neighbor)
      return null_point_index();
    tmp = neighbor->point_index;
    auto point = g.get_v_point(tmp);
    int idx = point.point_index;
    kd_t.remove(point, [idx](Internal_point const&p){return p.point_index == idx;});
  }
  return tmp;
}

inline std::vector<int> Neighbors_finder::pull_all_near(int u_point_index) {
  std::vector<int> all_pull;
  int last_pull = pull_near(u_point_index);
  while (last_pull != null_point_index()) {
    all_pull.emplace_back(last_pull);
    last_pull = pull_near(u_point_index);
  }
  return all_pull;
}

inline Layered_neighbors_finder::Layered_neighbors_finder(const Persistence_graph& g, double r) :
    g(g), r(r), neighbors_finder() { }

inline void Layered_neighbors_finder::add(int v_point_index, int vlayer) {
  for (int l = neighbors_finder.size(); l <= vlayer; l++)
    neighbors_finder.emplace_back(std::unique_ptr<Neighbors_finder>(new Neighbors_finder(g, r)));
  neighbors_finder.at(vlayer)->add(v_point_index);
}

inline int Layered_neighbors_finder::pull_near(int u_point_index, int vlayer) {
  if (static_cast<int> (neighbors_finder.size()) <= vlayer)
    return null_point_index();
  return neighbors_finder.at(vlayer)->pull_near(u_point_index);
}

inline int Layered_neighbors_finder::vlayers_number() const {
  return static_cast<int> (neighbors_finder.size());
}

}  // namespace persistence_diagram

}  // namespace Gudhi

#endif  // NEIGHBORS_FINDER_H_