summaryrefslogtreecommitdiff
path: root/src/Bipartite_graph_matching/include/gudhi/Grid_cell.h
blob: e9cf08b9499679b38e2f8e49fcf736c8c571cb56 (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
/*    This file is part of the Gudhi Library. The Gudhi library
 *    (Geometric Understanding in Higher Dimensions) is a generic C++
 *    library for computational topology.
 *
 *    Author(s):       Francois Godi
 *
 *    Copyright (C) 2015  INRIA Sophia-Antipolis (France)
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SRC_BOTTLENECK_INCLUDE_GUDHI_GRID_CELL_H_
#define SRC_BOTTLENECK_INCLUDE_GUDHI_GRID_CELL_H_

#include <list>
#include <set>
#include <map>

#include "Persistence_diagrams_graph.h"

namespace Gudhi {

namespace bipartite_graph_matching {

/** \internal \brief TODO
 *
 * \ingroup bottleneck_distance
 */
class Grid_cell {
public:
    Grid_cell(double r);
    void add(int v_point_index);
    void remove(int v_point_index);
    bool contains(int v_point_index) const;
    int pull_center();
    int pull_xi(int u_point_index);
    int pull_xd(int u_point_index);
    int pull_yi(int u_point_index);
    int pull_yd(int u_point_index);
    int pull_xi_yi(int u_point_index);
    int pull_xi_yd(int u_point_index);
    int pull_xd_yi(int u_point_index);
    int pull_xd_yd(int u_point_index);

private:
    double r;
    std::set<int, G::Compare_x> xi_order;
    std::set<int, G::Compare_y> yi_order;
    struct Hidden_points_tree{int point; std::list<Hidden_points_tree> hidden;};
    typedef std::map<int, std::list<Hidden_points_tree>, G::Compare_x> Corner_tree;
    Corner_tree xi_yi_order;
    Corner_tree xi_yd_order;
    Corner_tree xd_yi_order;
    Corner_tree xd_yd_order;
    void remove_aux(Corner_tree t, int v_point_index);
    void build_xi_yi();
    void build_xi_yd();
    void build_xd_yi();
    void build_xd_yd();
};


inline Grid_cell::Grid_cell(double r)
    : r(r), xi_order(G::Compare_x(r)), yi_order(G::Compare_y(r)), xi_yi_order(G::Compare_x(r)),
      xi_yd_order(G::Compare_x(r)), xd_yi_order(G::Compare_x(r)), xd_yd_order(G::Compare_x(r)) {}

inline void Grid_cell::add(int v_point_index){
    xi_order.emplace(v_point_index);
}

inline bool Grid_cell::contains(int v_point_index) const{
    return (xi_order.count(v_point_index) > 0);
}

inline void Grid_cell::remove_aux(Corner_tree t, int v_point_index){
    if(t.empty())
        return;
    std::list<Hidden_points_tree> hidden_points = t.at(v_point_index);
    t.erase(v_point_index);
    for(auto it = hidden_points.begin(); it != hidden_points.end(); ++it)
        t.emplace(it->point,it->hidden);

}

inline void Grid_cell::remove(int v_point_index){
    xi_order.erase(v_point_index);
    yi_order.erase(v_point_index);
    remove_aux(xi_yi_order,v_point_index);
    remove_aux(xi_yd_order,v_point_index);
    remove_aux(xd_yi_order,v_point_index);
    remove_aux(xd_yd_order,v_point_index);
}

//factorization needed \/ \/ \/

inline int Grid_cell::pull_center(){
    if(xi_order.empty())
        return null_point_index();
    int v_point_index = *xi_order.begin();
    remove(v_point_index);
    return v_point_index;
}

inline int Grid_cell::pull_xi(int u_point_index){
    if(xi_order.empty())
        return null_point_index();
    int v_point_index = *xi_order.begin(); //!
    if(G::distance(u_point_index,v_point_index)<=r){
        remove(v_point_index);
        return v_point_index;
    }
    return null_point_index();
}

inline int Grid_cell::pull_xd(int u_point_index){
    if(xi_order.empty())
        return null_point_index();
    int v_point_index = *xi_order.rbegin(); //!
    if(G::distance(u_point_index,v_point_index)<=r){
        remove(v_point_index);
        return v_point_index;
    }
    return null_point_index();
}

inline int Grid_cell::pull_yi(int u_point_index){
    if(xi_order.empty())
        return null_point_index();
    if(yi_order.empty())
        for(auto it = xi_order.begin(); it!= xi_order.end(); ++it)
            yi_order.emplace(*it);
    int v_point_index = *yi_order.begin(); //!
    if(G::distance(u_point_index,v_point_index)<=r){
        remove(v_point_index);
        return v_point_index;
    }
    return null_point_index();
}

inline int Grid_cell::pull_yd(int u_point_index){
    if(xi_order.empty())
        return null_point_index();
    if(yi_order.empty())
        for(auto it = xi_order.begin(); it!= xi_order.end(); ++it)
            yi_order.emplace(*it);
    int v_point_index = *yi_order.rbegin(); //!
    if(G::distance(u_point_index,v_point_index)<=r){
        remove(v_point_index);
        return v_point_index;
    }
    return null_point_index();
}

inline int Grid_cell::pull_xi_yi(int u_point_index){
    if(xi_order.empty())
        return null_point_index();
    if(xi_yi_order.empty())
        build_xi_yi();
    auto it = xi_yi_order.upper_bound(u_point_index+G::size());
    if(it==xi_yi_order.cbegin()) //!
        return null_point_index();
    it--; //!
    int v_point_index = it->first;
    for(auto it2=it->second.begin();it2!=it->second.end();it2++)
        xi_yi_order.emplace(it2->point,it2->hidden);
    if(G::distance(u_point_index,v_point_index)<=r){
        remove(v_point_index);
        return v_point_index;
    }
    return null_point_index();
}

}  // namespace bipartite_graph_matching

}  // namespace Gudhi

#endif  // SRC_BOTTLENECK_INCLUDE_GUDHI_GRID_CELL_H_