summaryrefslogtreecommitdiff
path: root/geom_bottleneck/bottleneck/src/basic_defs.cpp
blob: 76e6cc5355e56087ac5e22f4478ef08eb3bc8f6b (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
/*
    Copyrigth 2015, D. Morozov, M. Kerber, A. Nigmetov

    This file is part of GeomBottleneck.

    GeomBottleneck is free software: you can redistribute it and/or modify
    it under the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    GeomBottleneck 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
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with GeomBottleneck.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <algorithm>
#include <cfloat>
#include "def_debug_bt.h"
#include "basic_defs_bt.h"

namespace geom_bt {

// Point

bool Point::operator==(const Point& other) const
{
    return ((this->x == other.x) and (this->y == other.y));
}

bool Point::operator!=(const Point& other) const
{
    return !(*this == other);
}

#ifndef FOR_R_TDA
std::ostream& operator<<(std::ostream& output, const Point p)
{
    output << "(" << p.x << ", " << p.y << ")";
    return output;
}

std::ostream& operator<<(std::ostream& output, const PointSet& ps)
{
    output << "{ ";
    for(auto& p : ps) {
        output << p << ", ";
    }
    output << "\b\b }";
    return output;
}
#endif

double sqrDist(const Point& a, const Point& b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

double dist(const Point& a, const Point& b)
{
    return sqrt(sqrDist(a, b));
}

// DiagramPoint

// compute l-inf distance between two diagram points
double distLInf(const DiagramPoint& a, const DiagramPoint& b)
{
    if ( DiagramPoint::DIAG == a.type &&
         DiagramPoint::DIAG == b.type ) {
        // distance between points on the diagonal is 0
        return 0.0;
    } 
    // otherwise distance is a usual l-inf distance
    return std::max(fabs(a.getRealX() - b.getRealX()), fabs(a.getRealY() - b.getRealY()));
}

bool DiagramPoint::operator==(const DiagramPoint& other) const
{
    assert(this->id >= MinValidId);
    assert(other.id >= MinValidId);
    bool areEqual{ this->id == other.id };
    assert(!areEqual or  ((this->x == other.x) and (this->y == other.y) and (this->type == other.type)));
    return areEqual;
}

bool DiagramPoint::operator!=(const DiagramPoint& other) const
{
    return !(*this == other);
}

#ifndef FOR_R_TDA
std::ostream& operator<<(std::ostream& output, const DiagramPoint p)
{
    if ( p.type == DiagramPoint::DIAG ) {
        output << "(" << p.x << ", " << p.y << ", " <<  0.5 * (p.x + p.y) << ", "  << p.id << " DIAG )";
    } else {
        output << "(" << p.x << ", " << p.y << ", " << p.id << " NORMAL)";
    }
    return output;
}

std::ostream& operator<<(std::ostream& output, const DiagramPointSet& ps)
{
    output << "{ ";
    for(auto pit = ps.cbegin(); pit != ps.cend(); ++pit) {
        output << *pit << ", ";
    }
    output << "\b\b }";
    return output;
}
#endif

DiagramPoint::DiagramPoint(double xx, double yy, Type ttype, IdType uid) : 
    x(xx),
    y(yy),
    type(ttype),
    id(uid)
{
    if ( yy == xx and ttype != DiagramPoint::DIAG)
        throw std::runtime_error("Point on the main diagonal must have DIAG type");

}

void DiagramPointSet::insert(const DiagramPoint p)
{
    points.insert(p);
    if (p.id > maxId) {
        maxId = p.id + 1;
    }
}

// erase should be called only for the element of the set
void DiagramPointSet::erase(const DiagramPoint& p, bool doCheck)
{
    auto it = points.find(p);
    if (it != points.end()) {
        points.erase(it);
    } else {
        assert(!doCheck);
    }
}

void DiagramPointSet::reserve(const size_t newSize)
{
    points.reserve(newSize);
}


void DiagramPointSet::erase(const std::unordered_set<DiagramPoint, DiagramPointHash>::const_iterator it)
{
    points.erase(it);
}

void DiagramPointSet::clear()
{
    points.clear();
}

size_t DiagramPointSet::size() const
{
    return points.size();
}

bool DiagramPointSet::empty() const
{
    return points.empty();
}

bool DiagramPointSet::hasElement(const DiagramPoint& p) const
{
    return points.find(p) != points.end();
}


void DiagramPointSet::removeDiagonalPoints() 
{
    if (isLinked) {
        auto ptIter = points.begin();
        while(ptIter != points.end()) {
            if (ptIter->isDiagonal()) {
                ptIter = points.erase(ptIter);
            } else {
                ptIter++;
            }
        }
        isLinked = false;
    }
}


// preprocess diagrams A and B by adding projections onto diagonal of points of
// A to B and vice versa. NB: ids of points will be changed!
void addProjections(DiagramPointSet& A, DiagramPointSet& B)
{

    IdType uniqueId {MinValidId + 1};
    DiagramPointSet newA, newB;
    
    // copy normal points from A to newA
    // add projections to newB
    for(auto& pA : A) {
        if (pA.isNormal()) {
            DiagramPoint dpA {pA.getRealX(), pA.getRealY(), DiagramPoint::NORMAL, uniqueId++};
            DiagramPoint dpB {0.5*(pA.getRealX() +pA.getRealY()), 0.5 *(pA.getRealX() +pA.getRealY()), DiagramPoint::DIAG, uniqueId++};
            newA.insert(dpA);
            newB.insert(dpB);
        }
    }

    for(auto& pB : B) {
        if (pB.isNormal()) {
            DiagramPoint dpB {pB.getRealX(), pB.getRealY(), DiagramPoint::NORMAL, uniqueId++};
            DiagramPoint dpA {0.5*(pB.getRealX() +pB.getRealY()), 0.5 *(pB.getRealX() +pB.getRealY()), DiagramPoint::DIAG, uniqueId++};
            newB.insert(dpB);
            newA.insert(dpA);
        }
    }
   
    A = newA;
    B = newB;
    A.isLinked = true;
    B.isLinked = true;
}
}