MEB_filtration.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): Marc Glisse
4 *
5 * Copyright (C) 2023 Inria
6 *
7 * Modification(s):
8 * - 2024/10 Vincent Rouvreau: Add Output_squared_values argument to enable/disable squared radii computation
9 * - YYYY/MM Author: Description of the modification
10 */
11
12#ifndef MEB_FILTRATION_H_
13#define MEB_FILTRATION_H_
14
15#include <CGAL/NT_converter.h>
16
17#include <vector>
18#include <utility> // for std::pair
19#include <cmath> // for std::sqrt
20
21namespace Gudhi::cech_complex {
22
45template<bool Output_squared_values = true, typename Kernel, typename SimplicialComplexForMEB, typename PointRange>
46void assign_MEB_filtration(Kernel&&k, SimplicialComplexForMEB& complex, PointRange const& points, bool exact = false) {
47 using Point_d = typename Kernel::Point_d;
48 using FT = typename Kernel::FT;
49 using Sphere = std::pair<Point_d, FT>;
50
52 using Simplex_handle = typename SimplicialComplexForMEB::Simplex_handle;
54
55 // For users to be able to define their own sqrt function on their desired Filtration_value type
56 using std::sqrt;
57
58 std::vector<Sphere> cache_;
59 std::vector<Point_d> pts;
60 CGAL::NT_converter<FT, Filtration_value> cvt;
61
62 // This block is only needed to get ambient_dim
63 if(std::begin(points) == std::end(points)) {
64 // assert(complex.is_empty());
65 return;
66 }
67 int ambient_dim = k.point_dimension_d_object()(*std::begin(points));
68
69 auto fun = [&](Simplex_handle sh, int dim){
70 using std::max;
71 if (dim == 0) complex.assign_filtration(sh, 0);
72 else if (dim == 1) {
73 // For a Simplex_tree, this would be a bit faster, but that's probably negligible
74 // Vertex_handle u = sh->first; Vertex_handle v = self_siblings(sh)->parent();
75 auto verts = complex.simplex_vertex_range(sh);
76 auto vert_it = verts.begin();
77 Vertex_handle u = *vert_it;
78 Vertex_handle v = *++vert_it;
79 auto&& pu = points[u];
80 Point_d m = k.midpoint_d_object()(pu, points[v]);
81 FT r = k.squared_distance_d_object()(m, pu);
82 if (exact) CGAL::exact(r);
83 complex.assign_key(sh, cache_.size());
84 Filtration_value filt{max(cvt(r), Filtration_value(0))};
85 if constexpr (!Output_squared_values)
86 filt = sqrt(filt);
87 complex.assign_filtration(sh, filt);
88 cache_.emplace_back(std::move(m), std::move(r));
89 } else if (dim > ambient_dim) {
90 // The sphere is always defined by at most d+1 points
91 Filtration_value maxf = 0; // max filtration of the faces
92 for (auto face : complex.boundary_simplex_range(sh)) {
93 maxf = max(maxf, complex.filtration(face));
94 }
95 complex.assign_filtration(sh, maxf);
96 } else {
97 Filtration_value maxf = 0; // max filtration of the faces
98 bool found = false;
99 for (auto face_opposite_vertex : complex.boundary_opposite_vertex_simplex_range(sh)) {
100 maxf = max(maxf, complex.filtration(face_opposite_vertex.first));
101 if (!found) {
102 auto key = complex.key(face_opposite_vertex.first);
103 Sphere const& sph = cache_[key];
104 if (k.squared_distance_d_object()(sph.first, points[face_opposite_vertex.second]) > sph.second) continue;
105 found = true;
106 complex.assign_key(sh, key);
107 // With exact computations, we could stop here
108 // complex.assign_filtration(sh, complex.filtration(face_opposite_vertex.first)); return;
109 // but because of possible rounding errors, we continue with the equivalent of make_filtration_non_decreasing
110 }
111 }
112 if (!found) {
113 // None of the faces are good enough, MEB must be the circumsphere.
114 pts.clear();
115 for (auto vertex : complex.simplex_vertex_range(sh))
116 pts.push_back(points[vertex]);
117 Point_d c = k.construct_circumcenter_d_object()(pts.begin(), pts.end());
118 FT r = k.squared_distance_d_object()(c, pts.front());
119 if (exact) CGAL::exact(r);
120 // For Epick_d, if the circumcenter computation is too unstable, we could compute
121 // int d2 = dim * dim;
122 // Filtration_value max_sanity = maxf * d2 / (d2 - 1);
123 // and use min(max_sanity, ...), which would limit how bad numerical errors can be.
124 Filtration_value filt{cvt(r)};
125 if constexpr (!Output_squared_values)
126 filt = sqrt(max(filt, Filtration_value(0)));
127 // maxf = filt except for rounding errors
128 maxf = max(maxf, filt);
129 complex.assign_key(sh, cache_.size());
130 // We could check if the simplex is maximal and avoiding adding it to the cache in that case.
131 cache_.emplace_back(std::move(c), std::move(r));
132 }
133 complex.assign_filtration(sh, maxf);
134 }
135 };
136 complex.for_each_simplex(fun);
137
138 // We could avoid computing maxf, but when !exact rounding errors may cause
139 // the filtration values to be non-monotonous, so we would need to call
140 // if (!exact) complex.make_filtration_non_decreasing();
141 // which is way more costly than computing maxf. The exact case is already so
142 // costly that it isn't worth maintaining code without maxf just for it.
143 // Cech_complex has "free" access to the max of the faces, because
144 // expansion_with_blockers computes it before the callback.
145
146 // TODO: use a map if complex does not provide key?
147}
148} // namespace Gudhi::cech_complex
149
150#endif // MEB_FILTRATION_H_
void assign_MEB_filtration(Kernel &&k, SimplicialComplexForMEB &complex, PointRange const &points, bool exact=false)
Given a simplicial complex and an embedding of its vertices, this assigns to each simplex a filtratio...
Definition: MEB_filtration.h:46
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
Definition: SimplicialComplexForMEB.h:22
unspecified Simplex_handle
Handle for a simplex.
Definition: SimplicialComplexForMEB.h:24
Simplex_key key(Simplex_handle simplex)
Returns the key assigned to the 'simplex' with assign_key().
Filtration_value filtration(Simplex_handle simplex)
Returns the filtration value to the 'simplex'.
Boundary_opposite_vertex_simplex_range boundary_opposite_vertex_simplex_range(Simplex_handle simplex)
Returns a range of the pairs (simplex, opposite vertex) of the boundary of the 'simplex'.
unspecified Filtration_value
Type of filtration values.
Definition: SimplicialComplexForMEB.h:29
void assign_key(Simplex_handle simplex, Simplex_key key)
Assigns this 'key' to the 'simplex'.
Simplex_vertex_range simplex_vertex_range(Simplex_handle simplex)
Returns a range over vertices (as Vertex_handle) of a given simplex.
unspecified Vertex_handle
Handle for a vertex. Must be a non-negative integer, it is also used as an index into the input list ...
Definition: SimplicialComplexForMEB.h:27
int assign_filtration(Simplex_handle simplex, Filtration_value filtration)
Assigns this 'filtration' value to the 'simplex'.
void for_each_simplex(auto callback)
Calls callback(simplex, dim) for every simplex of the complex, with the guarantee that faces are visi...
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15