summaryrefslogtreecommitdiff
path: root/src/python/include/Alpha_complex_factory.h
blob: 5d3bfb6508cce3c5981757c831d54195678e7f77 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*    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):       Vincent Rouvreau
 *
 *    Copyright (C) 2020 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef INCLUDE_ALPHA_COMPLEX_FACTORY_H_
#define INCLUDE_ALPHA_COMPLEX_FACTORY_H_

#include <gudhi/Simplex_tree.h>
#include <gudhi/Alpha_complex.h>
#include <gudhi/Alpha_complex_3d.h>
#include <gudhi/Alpha_complex_options.h>
#include <CGAL/Epeck_d.h>
#include <CGAL/Epick_d.h>

#include <boost/range/adaptor/transformed.hpp>

#include "Simplex_tree_interface.h"

#include <iostream>
#include <vector>
#include <string>
#include <memory>  // for std::unique_ptr

namespace Gudhi {

namespace alpha_complex {

template<typename CgalPointType, bool Weighted>
struct Point_cgal_to_cython;

template<typename CgalPointType>
struct Point_cgal_to_cython<CgalPointType, false> {
  std::vector<double> operator()(CgalPointType const& point) const 
  {
    std::vector<double> vd;
    vd.reserve(point.dimension());
    for (auto coord = point.cartesian_begin(); coord != point.cartesian_end(); coord++)
      vd.push_back(CGAL::to_double(*coord));
    return vd;
  }
};

template<typename CgalPointType>
struct Point_cgal_to_cython<CgalPointType, true> {
  std::vector<double> operator()(CgalPointType const& weighted_point) const 
  {
    auto point = weighted_point.point();
    std::vector<double> vd;
    vd.reserve(point.dimension());
    for (auto coord = point.cartesian_begin(); coord != point.cartesian_end(); coord++)
      vd.push_back(CGAL::to_double(*coord));
    return vd;
  }
};

template <typename CgalPointType>
std::vector<double> pt_cgal_to_cython(CgalPointType const& point) {
  std::vector<double> vd;
  vd.reserve(point.dimension());
  for (auto coord = point.cartesian_begin(); coord != point.cartesian_end(); coord++)
    vd.push_back(CGAL::to_double(*coord));
  return vd;
}

template <typename CgalPointType>
static CgalPointType pt_cython_to_cgal(std::vector<double> const& vec) {
  return CgalPointType(vec.size(), vec.begin(), vec.end());
}

class Abstract_alpha_complex {
 public:
  virtual std::vector<double> get_point(int vh) = 0;

  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                                   bool default_filtration_value) = 0;

  virtual ~Abstract_alpha_complex() = default;
};

class Exact_alpha_complex_dD final : public Abstract_alpha_complex {
 private:
  using Kernel = CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>;
  using Point = typename Kernel::Point_d;

 public:
  Exact_alpha_complex_dD(const std::vector<std::vector<double>>& points, bool exact_version)
    : exact_version_(exact_version),
      alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal<Point>)) {
  }

  virtual std::vector<double> get_point(int vh) override {
    Point const& point = alpha_complex_.get_point(vh);
    return pt_cgal_to_cython(point);
  }

  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                                   bool default_filtration_value) override {
    return alpha_complex_.create_complex(*simplex_tree, max_alpha_square, exact_version_, default_filtration_value);
  }

 private:
  bool exact_version_;
  Alpha_complex<Kernel> alpha_complex_;
};

class Inexact_alpha_complex_dD final : public Abstract_alpha_complex {
 private:
  using Kernel = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>;
  using Point = typename Kernel::Point_d;

 public:
  Inexact_alpha_complex_dD(const std::vector<std::vector<double>>& points, bool exact_version)
    : exact_version_(exact_version),
      alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal<Point>)) {
  }

  virtual std::vector<double> get_point(int vh) override {
    Point const& point = alpha_complex_.get_point(vh);
    return pt_cgal_to_cython(point);
  }
  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                                   bool default_filtration_value) override {
    return alpha_complex_.create_complex(*simplex_tree, max_alpha_square, exact_version_, default_filtration_value);
  }

 private:
  bool exact_version_;
  Alpha_complex<Kernel> alpha_complex_;
};

class Exact_weighted_alpha_complex_dD final : public Abstract_alpha_complex {
 private:
  using Kernel = CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>;
  using Point = typename Kernel::Point_d;

 public:
  Exact_weighted_alpha_complex_dD(const std::vector<std::vector<double>>& points,
                                  const std::vector<double>& weights, bool exact_version)
    : exact_version_(exact_version),
      alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal<Point>), weights) {
  }

  virtual std::vector<double> get_point(int vh) override {
    Point const& point = alpha_complex_.get_point(vh).point();
    return pt_cgal_to_cython(point);
  }

  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                                   bool default_filtration_value) override {
    return alpha_complex_.create_complex(*simplex_tree, max_alpha_square, exact_version_, default_filtration_value);
  }

 private:
  bool exact_version_;
  Alpha_complex<Kernel, true> alpha_complex_;
};

class Inexact_weighted_alpha_complex_dD final : public Abstract_alpha_complex {
 private:
  using Kernel = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>;
  using Point = typename Kernel::Point_d;

 public:
  Inexact_weighted_alpha_complex_dD(const std::vector<std::vector<double>>& points,
                                    const std::vector<double>& weights, bool exact_version)
    : exact_version_(exact_version),
      alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal<Point>), weights) {
  }

  virtual std::vector<double> get_point(int vh) override {
    Point const& point = alpha_complex_.get_point(vh).point();
    return pt_cgal_to_cython(point);
  }
  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                                   bool default_filtration_value) override {
    return alpha_complex_.create_complex(*simplex_tree, max_alpha_square, exact_version_, default_filtration_value);
  }

 private:
  bool exact_version_;
  Alpha_complex<Kernel, true> alpha_complex_;
};

template <complexity Complexity, bool Weighted = false>
class Alpha_complex_3D final : public Abstract_alpha_complex {
 private:
  using Bare_point = typename Alpha_complex_3d<Complexity, Weighted, false>::Bare_point_3;
  using Point = typename Alpha_complex_3d<Complexity, Weighted, false>::Point_3;

  static Bare_point pt_cython_to_cgal_3(std::vector<double> const& vec) {
    return Bare_point(vec[0], vec[1], vec[2]);
  }

 public:
  Alpha_complex_3D(const std::vector<std::vector<double>>& points)
    : alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal_3)) {
  }

  Alpha_complex_3D(const std::vector<std::vector<double>>& points, const std::vector<double>& weights)
    : alpha_complex_(boost::adaptors::transform(points, pt_cython_to_cgal_3), weights) {
  }

  virtual std::vector<double> get_point(int vh) override {
    Point const& point = alpha_complex_.get_point(vh);
    return Point_cgal_to_cython<Point, Weighted>()(point);
  }

  virtual bool create_simplex_tree(Simplex_tree_interface<>* simplex_tree, double max_alpha_square,
                           bool default_filtration_value) override {
    alpha_complex_.create_complex(*simplex_tree, max_alpha_square);
    return true;
  }

 private:
  Alpha_complex_3d<Complexity, Weighted, false> alpha_complex_;
};


}  // namespace alpha_complex

}  // namespace Gudhi

#endif  // INCLUDE_ALPHA_COMPLEX_FACTORY_H_