Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
contour.h
Go to the documentation of this file.
1#ifndef CONTOUR_H
2#define CONTOUR_H
3
4#include <vector>
5#include <array>
6#include <set>
7#include <map>
8#include <stack>
9#include <optional>
10#include <memory>
11#include "functions.h"
12
13using Point = std::pair<double, double>;
14using Segment = std::pair<Point, Point>;
15using Index = std::pair<int, int>;
16using Path = std::vector<Point>;
17using ScalarField2D = std::function<double(double, double)>;
18
19inline const std::vector<std::set<Index>> MASK_LOOKUP {
20 {},
21 {{3, 0}},
22 {{0, 1}},
23 {{3, 1}},
24 {{1, 2}},
25 {{3, 2}, {0, 1}},
26 {{0, 2}},
27 {{3, 2}},
28 {{2, 3}},
29 {{0, 2}},
30 {{0, 3}, {1, 2}},
31 {{1, 2}},
32 {{1, 3}},
33 {{0, 1}},
34 {{3, 0}},
35 {}
36};
37
38enum class CellStatus {
42};
43
44struct Cell {
45 static inline const std::vector<Index> EDGES {{0, 1}, {1, 2}, {2, 3}, {3, 0}};
46
47 std::array<Index, 4> vertices;
48 std::array<double, 4> values;
49 std::size_t depth;
51 std::optional<std::array<std::unique_ptr<Cell>, 4>> children;
52};
53
54struct MSGraph {
55 std::map<Index, Point> vertices;
56 std::map<Index, std::vector<Index>> adjacency;
57};
58
60public:
61 MarchingSquaresExtractor(const ScalarField2D& f, std::array<double, 4> bounds, size_t max_depth = 7);
62
63 std::set<Path> find_iso_contour(double lvl);
64
65private:
66 std::unique_ptr<Cell> build(int i, int j, int depth, int force_refine = 2);
67
68 Index point_to_idx(Point xy) const;
69 Point idx_to_point(Index ij) const;
70 std::array<Index, 4> cell_vertices(Index ij, std::size_t depth) const;
71 double vertex_value(Index ij);
72 Cell* locate_leaf(Point xy) const;
73 Point edge_point(Cell* cell, int edge);
74 std::set<std::pair<int, int>> get_crossed_edges(Cell* cell);
75 bool point_on_boundary(Point xy, double tol = 1e-8) const;
76
77 void set_iso_value(double lvl);
78 void update_active_cells(Cell* cell);
79 std::vector<Segment> compute_segments();
80 MSGraph build_adjacency(std::vector<Segment> segments) const;
81 std::set<Path> extract_paths(MSGraph graph) const;
82 std::set<Point> detect_dangling_ends(std::set<Path> paths) const;
83 void refine_leaf(Cell *leaf);
84 std::set<Path> correct_topology(std::set<Path> raw_paths);
85
86private:
87 double xmin, xmax, ymin, ymax;
88 std::size_t max_depth;
89 int N;
90 std::map<Index, double> vertices;
91 std::set<Cell*> active_cells;
92 std::unique_ptr<Cell> root;
94 double k;
95};
96
97namespace std {
98 template <>
99 struct hash<std::pair<int, int>> {
100 std::size_t operator()(const Index& p) const noexcept {
101 std::size_t h = static_cast<std::size_t>(p.first);
102 h ^= (static_cast<std::size_t>(p.second) + 0x9e3779b97f4a7c15ULL + (h<<6) + (h>>2));
103 return h;
104 }
105 };
106}
107
108#endif // CONTOUR_H
std::set< Path > find_iso_contour(double lvl)
Definition contour.cpp:14
std::pair< int, int > Index
Definition contour.h:15
std::pair< Point, Point > Segment
Definition contour.h:14
const std::vector< std::set< Index > > MASK_LOOKUP
Definition contour.h:19
std::vector< Point > Path
Definition contour.h:16
std::function< double(double, double)> ScalarField2D
Definition contour.h:17
std::pair< double, double > Point
Definition contour.h:13
CellStatus
Definition contour.h:38
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
double f(double x)
Wilson special function f depending on x.
Definition contour.h:44
std::array< Index, 4 > vertices
Definition contour.h:47
std::size_t depth
Definition contour.h:49
std::array< double, 4 > values
Definition contour.h:48
CellStatus status
Definition contour.h:50
static const std::vector< Index > EDGES
Definition contour.h:45
std::optional< std::array< std::unique_ptr< Cell >, 4 > > children
Definition contour.h:51
std::map< Index, Point > vertices
Definition contour.h:55
std::map< Index, std::vector< Index > > adjacency
Definition contour.h:56