summaryrefslogtreecommitdiff
path: root/src/Coxeter_triangulation/include/gudhi/Manifold_tracing.h
blob: d61bbed7d3b370d3b896d1c5c00355580c36eef8 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*    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):       Siargey Kachanovich
 *
 *    Copyright (C) 2019 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef MANIFOLD_TRACING_H_
#define MANIFOLD_TRACING_H_

#include <gudhi/IO/output_debug_traces_to_html.h>  // for DEBUG_TRACES
#include <gudhi/Coxeter_triangulation/Query_result.h>

#include <boost/functional/hash.hpp>

#include <Eigen/Dense>

#include <queue>
#include <unordered_map>

namespace Gudhi {

namespace coxeter_triangulation {

/**
 *  \ingroup coxeter_triangulation
 */

/** \class Manifold_tracing
 *  \brief A class that assembles methods for manifold tracing algorithm.
 *
 *  \tparam Triangulation_ The type of the ambient triangulation.
 *   Needs to be a model of the concept TriangulationForManifoldTracing.
 */
template <class Triangulation_>
class Manifold_tracing {
 public:
  using Simplex_handle = typename Triangulation_::Simplex_handle;

  struct Simplex_hash {
    typedef Simplex_handle argument_type;
    typedef std::size_t result_type;
    result_type operator()(const argument_type& s) const noexcept {
      return boost::hash<typename Simplex_handle::Vertex>()(s.vertex());
    }
  };

 public:
  /** \brief Type of the output simplex map with keys of type Triangulation_::Simplex_handle
   *   and values of type Eigen::VectorXd.
   *   This type should be used for the output in the method manifold_tracing_algorithm.
   */
  typedef std::unordered_map<Simplex_handle, Eigen::VectorXd, Simplex_hash> Out_simplex_map;

  /**
   * \brief Computes the set of k-simplices that intersect
   * a boundaryless implicit manifold given by an intersection oracle, where k
   * is the codimension of the manifold.
   * The computation is based on the seed propagation --- it starts at the
   * given seed points and then propagates along the manifold.
   *
   * \tparam Point_range Range of points of type Eigen::VectorXd.
   * \tparam Intersection_oracle Intersection oracle that represents the manifold.
   *  Needs to be a model of the concept IntersectionOracle.
   *
   * \param[in] seed_points The range of points on the manifold from which
   * the computation begins.
   * \param[in] triangulation The ambient triangulation.
   * \param[in] oracle The intersection oracle for the manifold.
   * The ambient dimension needs to match the dimension of the
   * triangulation.
   * \param[out] out_simplex_map The output map, where the keys are k-simplices in
   * the input triangulation that intersect the input manifold and the mapped values
   * are the intersection points.
   */
  template <class Point_range, class Intersection_oracle>
  void manifold_tracing_algorithm(const Point_range& seed_points, const Triangulation_& triangulation,
                                  const Intersection_oracle& oracle, Out_simplex_map& out_simplex_map) {
    std::size_t cod_d = oracle.cod_d();
    std::queue<Simplex_handle> queue;

    for (const auto& p : seed_points) {
      Simplex_handle full_simplex = triangulation.locate_point(p);
      for (Simplex_handle face : full_simplex.face_range(cod_d)) {
        Query_result<Simplex_handle> qr = oracle.intersects(face, triangulation);
        if (qr.success && out_simplex_map.emplace(face, qr.intersection).second) {
#ifdef DEBUG_TRACES
          mt_seed_inserted_list.push_back(MT_inserted_info(qr, face, false));
#endif
          queue.emplace(face);
          break;
        }
      }
    }

    while (!queue.empty()) {
      Simplex_handle s = queue.front();
      queue.pop();
      for (auto cof : s.coface_range(cod_d + 1)) {
        for (auto face : cof.face_range(cod_d)) {
          Query_result<Simplex_handle> qr = oracle.intersects(face, triangulation);
          if (qr.success && out_simplex_map.emplace(face, qr.intersection).second) queue.emplace(face);
        }
      }
    }
  }

  /**
   * \brief Computes the set of k-simplices that intersect
   * the dimensional manifold given by an intersection oracle, where k
   * is the codimension of the manifold.
   * The computation is based on the seed propagation --- it starts at the
   * given seed points and then propagates along the manifold.
   *
   * \tparam Point_range Range of points of type Eigen::VectorXd.
   * \tparam Intersection_oracle Intersection oracle that represents the manifold.
   *  Needs to be a model of the concept IntersectionOracle.
   *
   * \param[in] seed_points The range of points on the manifold from which
   * the computation begins.
   * \param[in] triangulation The ambient triangulation.
   * \param[in] oracle The intersection oracle for the manifold.
   * The ambient dimension needs to match the dimension of the
   * triangulation.
   * \param[out] interior_simplex_map The output map, where the keys are k-simplices in
   * the input triangulation that intersect the relative interior of the input manifold
   * and the mapped values are the intersection points.
   * \param[out] boundary_simplex_map The output map, where the keys are k-simplices in
   * the input triangulation that intersect the boundary of the input manifold
   * and the mapped values are the intersection points.
   */
  template <class Point_range, class Intersection_oracle>
  void manifold_tracing_algorithm(const Point_range& seed_points, const Triangulation_& triangulation,
                                  const Intersection_oracle& oracle, Out_simplex_map& interior_simplex_map,
                                  Out_simplex_map& boundary_simplex_map) {
    std::size_t cod_d = oracle.cod_d();
    std::queue<Simplex_handle> queue;

    for (const auto& p : seed_points) {
      Simplex_handle full_simplex = triangulation.locate_point(p);
      for (Simplex_handle face : full_simplex.face_range(cod_d)) {
        auto qr = oracle.intersects(face, triangulation);
#ifdef DEBUG_TRACES
        mt_seed_inserted_list.push_back(MT_inserted_info(qr, face, false));
#endif
        if (qr.success) {
          if (oracle.lies_in_domain(qr.intersection, triangulation)) {
            if (interior_simplex_map.emplace(face, qr.intersection).second) queue.emplace(face);
          } else {
            for (Simplex_handle cof : face.coface_range(cod_d + 1)) {
              auto qrb = oracle.intersects_boundary(cof, triangulation);
#ifdef DEBUG_TRACES
              mt_seed_inserted_list.push_back(MT_inserted_info(qrb, cof, true));
#endif
              if (qrb.success) boundary_simplex_map.emplace(cof, qrb.intersection);
            }
          }
          // break;
        }
      }
    }

    while (!queue.empty()) {
      Simplex_handle s = queue.front();
      queue.pop();
      for (auto cof : s.coface_range(cod_d + 1)) {
        for (auto face : cof.face_range(cod_d)) {
          auto qr = oracle.intersects(face, triangulation);
#ifdef DEBUG_TRACES
          mt_inserted_list.push_back(MT_inserted_info(qr, face, false));
#endif
          if (qr.success) {
            if (oracle.lies_in_domain(qr.intersection, triangulation)) {
              if (interior_simplex_map.emplace(face, qr.intersection).second) queue.emplace(face);
            } else {
              auto qrb = oracle.intersects_boundary(cof, triangulation);
#ifdef DEBUG_TRACES
              mt_inserted_list.push_back(MT_inserted_info(qrb, cof, true));
#endif
              if (qrb.success) boundary_simplex_map.emplace(cof, qrb.intersection);
            }
          }
        }
      }
    }
  }

  /** \brief Empty constructor */
  Manifold_tracing() {}
};

/**
 * \brief Static method for Manifold_tracing<Triangulation_>::manifold_tracing_algorithm
 * that computes the set of k-simplices that intersect
 * a boundaryless implicit manifold given by an intersection oracle, where k
 * is the codimension of the manifold.
 * The computation is based on the seed propagation --- it starts at the
 * given seed points and then propagates along the manifold.
 *
 * \tparam Point_range Range of points of type Eigen::VectorXd.
 *  \tparam Triangulation_ The type of the ambient triangulation.
 *   Needs to be a model of the concept TriangulationForManifoldTracing.
 * \tparam Intersection_oracle Intersection oracle that represents the manifold.
 *  Needs to be a model of the concept IntersectionOracle.
 * \tparam Out_simplex_map Needs to be Manifold_tracing<Triangulation_>::Out_simplex_map.
 *
 * \param[in] seed_points The range of points on the manifold from which
 * the computation begins.
 * \param[in] triangulation The ambient triangulation.
 * \param[in] oracle The intersection oracle for the manifold.
 * The ambient dimension needs to match the dimension of the
 * triangulation.
 * \param[out] out_simplex_map The output map, where the keys are k-simplices in
 * the input triangulation that intersect the input manifold and the mapped values
 * are the intersection points.
 *
 * \ingroup coxeter_triangulation
 */
template <class Point_range, class Triangulation, class Intersection_oracle, class Out_simplex_map>
void manifold_tracing_algorithm(const Point_range& seed_points, const Triangulation& triangulation,
                                const Intersection_oracle& oracle, Out_simplex_map& out_simplex_map) {
  Manifold_tracing<Triangulation> mt;
  mt.manifold_tracing_algorithm(seed_points, triangulation, oracle, out_simplex_map);
}

/**
 * \brief Static method for Manifold_tracing<Triangulation_>::manifold_tracing_algorithm
 * the dimensional manifold given by an intersection oracle, where k
 * is the codimension of the manifold.
 * The computation is based on the seed propagation --- it starts at the
 * given seed points and then propagates along the manifold.
 *
 * \tparam Point_range Range of points of type Eigen::VectorXd.
 *  \tparam Triangulation_ The type of the ambient triangulation.
 *   Needs to be a model of the concept TriangulationForManifoldTracing.
 * \tparam Intersection_oracle Intersection oracle that represents the manifold.
 *  Needs to be a model of the concept IntersectionOracle.
 * \tparam Out_simplex_map Needs to be Manifold_tracing<Triangulation_>::Out_simplex_map.
 *
 * \param[in] seed_points The range of points on the manifold from which
 * the computation begins.
 * \param[in] triangulation The ambient triangulation.
 * \param[in] oracle The intersection oracle for the manifold.
 * The ambient dimension needs to match the dimension of the
 * triangulation.
 * \param[out] interior_simplex_map The output map, where the keys are k-simplices in
 * the input triangulation that intersect the relative interior of the input manifold
 * and the mapped values are the intersection points.
 * \param[out] boundary_simplex_map The output map, where the keys are k-simplices in
 * the input triangulation that intersect the boundary of the input manifold
 * and the mapped values are the intersection points.
 *
 * \ingroup coxeter_triangulation
 */
template <class Point_range, class Triangulation, class Intersection_oracle, class Out_simplex_map>
void manifold_tracing_algorithm(const Point_range& seed_points, const Triangulation& triangulation,
                                const Intersection_oracle& oracle, Out_simplex_map& interior_simplex_map,
                                Out_simplex_map& boundary_simplex_map) {
  Manifold_tracing<Triangulation> mt;
  mt.manifold_tracing_algorithm(seed_points, triangulation, oracle, interior_simplex_map, boundary_simplex_map);
}

}  // namespace coxeter_triangulation

}  // namespace Gudhi

#endif