summaryrefslogtreecommitdiff
path: root/src/GudhUI/utils/Rips_builder.h
blob: aba1a8e479486d55a6620570e9157b10303a2be4 (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
/*    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(s):       David Salinas
 *
 *    Copyright (C) 2014 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef UTILS_RIPS_BUILDER_H_
#define UTILS_RIPS_BUILDER_H_

#include <boost/iterator/iterator_facade.hpp>

#include <CGAL/Euclidean_distance.h>
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <CGAL/Search_traits_d.h>

#include "utils/UI_utils.h"
#include "model/Complex_typedefs.h"

template<typename SkBlComplex> class Rips_builder {
 private:
  SkBlComplex& complex_;

 public:
  /**
   * @brief Modify complex to be the Rips complex
   * of its points with offset alpha.
   */
  Rips_builder(SkBlComplex& complex, double alpha) : complex_(complex) {
    complex.keep_only_vertices();
    if (alpha <= 0) return;
    compute_edges(alpha);
  }

 private:
  double squared_eucl_distance(const Point& p1, const Point& p2) const {
    return Geometry_trait::Squared_distance_d()(p1, p2);
  }

  void compute_edges(double alpha) {
    auto vertices = complex_.vertex_range();
    for (auto p = vertices.begin(); p != vertices.end(); ++p) {
      std::cout << *p << " ";
      std::cout.flush();
      for (auto q = p; ++q != vertices.end(); /**/)
        if (squared_eucl_distance(complex_.point(*p), complex_.point(*q)) < 4 * alpha * alpha)
          complex_.add_edge_without_blockers(*p, *q);
    }
    std::cout << std::endl;
  }
};

#endif  // UTILS_RIPS_BUILDER_H_