summaryrefslogtreecommitdiff
path: root/matching/include/bifiltration.h
blob: c9b64f1e716ac16a3fa7e7050f98bbe0404cc338 (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
#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();
        }

        DiagramKeeper 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 rene_format_reader(std::ifstream&);

        // in Rene format each simplex knows IDs of its boundary facets
        // postprocess_rene_format fills vector of IDs of boundary facets
        // in each simplex
        void postprocess_rene_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);
}

#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;
//    }
//}