summaryrefslogtreecommitdiff
path: root/matching/include/bifiltration.h
blob: bdb7a4e99e667bace23da28e3a91ad69b6340d34 (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
#ifndef MATCHING_DISTANCE_BIFILTRATION_H
#define MATCHING_DISTANCE_BIFILTRATION_H

#include <string>
#include <ostream>

#include "common_util.h"
#include "box.h"
#include "simplex.h"
#include "dual_point.h"

namespace md {

    class Bifiltration {
    public:
        using Diagram = std::vector<std::pair<Real, Real>>;
        using Box = md::Box;
        using SimplexVector = std::vector<Simplex>;

        Bifiltration() = default;

        Bifiltration(const Bifiltration&) = default;

        Bifiltration(Bifiltration&&) = default;

        Bifiltration& operator=(const Bifiltration& other)& = default;

        Bifiltration& operator=(Bifiltration&& other) = default;

        Bifiltration(const std::string& fname,
                BifiltrationFormat input_format = BifiltrationFormat::rivet); // read from file


        template<class Iter>
        Bifiltration(Iter begin, Iter end)
                : simplices_(begin, end)
        {
            init();
        }

        Diagram weighted_slice_diagram(const DualPoint& line, int dim) const;

        SimplexVector simplices() const { return simplices_; }

        // translate all points by vector (a,a)
        void translate(Real a);

        // return minimal value of x- and y-coordinates
        // among all simplices
        Real minimal_coordinate() const;

        // return box that contains positions of all simplices
        Box bounding_box() const;

        void sanity_check() const;

        int maximal_dim() const { return maximal_dim_; }

        friend std::ostream& operator<<(std::ostream& os, const Bifiltration& bif);

        Real max_x() const;

        Real max_y() const;

        Real min_x() const;

        Real min_y() const;

        void add_simplex(Index _id, Point birth, int _dim, const Column& _bdry);

        void save(const std::string& filename, BifiltrationFormat format = BifiltrationFormat::rivet); // save to file

        void scale(Real lambda);

    private:
        SimplexVector simplices_;
        // axes names, for rivet bifiltration format only
        std::string parameter_1_name_ {"axis_1"};
        std::string parameter_2_name_ {"axis_2"};

        Box bounding_box_;
        int maximal_dim_ {-1};

        void init();

        void rivet_format_reader(std::ifstream&);

        void phat_like_format_reader(std::ifstream&);

        // in Rene format each simplex knows IDs of its boundary facets
        // postprocess_phat_like_format fills vector of IDs of boundary facets
        // in each simplex
        void postprocess_phat_like_format();

        // in Rivet format each simplex knows its vertices,
        // postprocess_rivet_format fills vector of IDs of boundary facets
        // in each simplex
        void postprocess_rivet_format();

    };

    std::ostream& operator<<(std::ostream& os, const Bifiltration& bif);

    class BifiltrationProxy {
    public:
        BifiltrationProxy(const Bifiltration& bif, int dim = 0);
        // return critical values of simplices that are important for current dimension (dim and dim+1)
        PointVec positions() const;
        // set current dimension
        int set_dim(int new_dim);

        // wrappers of Bifiltration
        int maximal_dim() const;
        void translate(Real a);
        Real minimal_coordinate() const;
        Box bounding_box() const;
        Real max_x() const;
        Real max_y() const;
        Real min_x() const;
        Real min_y() const;
        Diagram weighted_slice_diagram(const DualPoint& slice) const;

    private:
        int dim_ { 0 };
        mutable PointVec cached_positions_;
        Bifiltration bif_;

        void cache_positions() const;
    };
}



#endif //MATCHING_DISTANCE_BIFILTRATION_H

//// The value type of OutputIterator is Simplex_in_2D_filtration
//template<typename OutputIterator>
//void read_input(std::string filename, OutputIterator out)
//{
//    std::ifstream ifstr;
//    ifstr.open(filename.c_str());
//    long n;
//    ifstr >> n; // number of simplices is the first number in file
//
//    Index k;   // used in loop
//    for (int i = 0; i < n; i++) {
//        Simplex_in_2D_filtration next;
//        next.index = i;
//        ifstr >> next.dim >> next.pos.x >> next.pos.y;
//        if (next.dim > 0) {
//            for (int j = 0; j <= next.dim; j++) {
//                ifstr >> k;
//                next.bd.push_back(k);
//            }
//        }
//        *out++ = next;
//    }
//}