summaryrefslogtreecommitdiff
path: root/geom_bottleneck/bottleneck/src/neighb_oracle.cpp
blob: 7195ef00a6e1b5c79a6d03f76896b69731b87e14 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
    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 "neighb_oracle.h"
#include "def_debug_bt.h"

namespace geom_bt {
/*static void printDebug(//bool isDebug, std::string s)*/
//{
//#ifdef DEBUG_NEIGHBOUR_ORACLE
    //if (isDebug) {
        //std::cout << s << std::endl;
    //}
//#endif
//}

//static void printDebug(//bool isDebug, std::string s, const DiagramPoint& p)
//{
//#ifdef DEBUG_NEIGHBOUR_ORACLE
    //if (isDebug) {
        //std::cout << s << p << std::endl;
    //}
//#endif
//}

//static void printDebug(//bool isDebug, std::string s, const double r)
//{
//#ifdef DEBUG_NEIGHBOUR_ORACLE
    //if (isDebug) {
        //std::cout << s << r << std::endl;
    //}
//#endif
//}

//static void printDebug(//bool isDebug, std::string s, const DiagramPointSet& dpSet)
//{
//#ifdef DEBUG_NEIGHBOUR_ORACLE
    //if (isDebug) {
        //std::cout << s << dpSet << std::endl;
    //}
//#endif
//}



// simple oracle
NeighbOracleSimple::NeighbOracleSimple() 
{
    r  = 0.0;
}

NeighbOracleSimple::NeighbOracleSimple(const DiagramPointSet& S, const double rr, const double dEps)
{
    r = rr;
    distEpsilon = dEps;
    pointSet = S;
}

void NeighbOracleSimple::rebuild(const DiagramPointSet& S, const double rr)
{
    pointSet = S;
    r = rr;
}

void NeighbOracleSimple::deletePoint(const DiagramPoint& p)
{
    pointSet.erase(p);
}

bool NeighbOracleSimple::getNeighbour(const DiagramPoint& q, DiagramPoint& result) const
{
    for(auto pit = pointSet.cbegin(); pit != pointSet.cend(); ++pit) {
        if ( distLInf(*pit, q) <= r) {
            result = *pit;
            return true;
        }
    }
    return false;
}

void NeighbOracleSimple::getAllNeighbours(const DiagramPoint& q, std::vector<DiagramPoint>& result)
{
    result.clear();
    for(const auto& point : pointSet) {
        if ( distLInf(point, q) <= r) {
            result.push_back(point);
        }
    }
    for(auto& pt : result) {
        deletePoint(pt);
    }
}

// ANN oracle
//

NeighbOracleAnn::NeighbOracleAnn(const DiagramPointSet& S, const double rr, const double dEps)
{
    assert(dEps >= 0);
    distEpsilon = dEps;
    // allocate space for query point
    // and the output of nearest neighbour search
    // this memory will be used in getNeighbour and freed in destructor
    annQueryPoint = annAllocPt(annDim);
    annIndices = new ANNidx[annK];
    annDistances = new ANNdist[annK];
    annPoints = nullptr;
    lo = annAllocPt(annDim);
    hi = annAllocPt(annDim);
    // create kd tree
    kdTree = nullptr;
    rebuild(S, rr);
}

void NeighbOracleAnn::rebuild(const DiagramPointSet& S, double rr)
{
#ifdef VERBOSE_BOTTLENECK
    std::cout << "Entered rebuild, r = " << rr << ", size  = " << S.size() << std::endl;
#endif
    r = rr;
    size_t annNumPoints = S.size();
    //printDebug(isDebug, "S = ", S);
    if (annNumPoints  > 0) {
        //originalPointSet = S;
        pointIdxLookup.clear();
        pointIdxLookup.reserve(S.size());
        allPoints.clear();
        allPoints.reserve(S.size());
        diagonalPoints.clear();
        diagonalPoints.reserve(S.size() / 2);
        for(auto pit = S.cbegin(); pit != S.cend(); ++pit) {
            allPoints.push_back(*pit);
            if (pit->type == DiagramPoint::DIAG) {
                diagonalPoints.insert(*pit);
            }
        }
        if (annPoints) {
            annDeallocPts(annPoints);
        }
        annPoints = annAllocPts(annNumPoints, annDim);
        auto annPointsPtr = *annPoints;
        size_t pointIdx = 0;
        for(auto& dataPoint : allPoints) {
            pointIdxLookup.insert( { dataPoint, pointIdx++ } );
            *annPointsPtr++ = dataPoint.getRealX();
            *annPointsPtr++ = dataPoint.getRealY();
        }
        delete kdTree;
        kdTree = new ANNkd_tree(annPoints,
                                annNumPoints,
                                annDim,
                                1,                // bucket size
                                ANN_KD_STD);
    }
#ifdef VERBOSE_BOTTLENECK
    std::cout << "Exit rebuild" << std::endl;
#endif
}

void NeighbOracleAnn::deletePoint(const DiagramPoint& p)
{
    //bool isDebug { true };
    auto findRes = pointIdxLookup.find(p);
    assert(findRes != pointIdxLookup.end());
    //printDebug(isDebug, "Deleting point ", p);
    size_t pointIdx { (*findRes).second };
    //printDebug(isDebug, "pointIdx =  ", pointIdx);
    //originalPointSet.erase(p);
    diagonalPoints.erase(p, false);
    kdTree->delete_point(pointIdx);
#ifdef DEBUG_NEIGHBOUR_ORACLE
#ifndef FOR_R_TDA
    kdTree->Print(ANNtrue, std::cout);
#endif
#endif
}

bool NeighbOracleAnn::getNeighbour(const DiagramPoint& q, DiagramPoint& result) const
{
    //bool isDebug { false };
    //printDebug(isDebug, "getNeighbour for q = ", q);
    if (0 == kdTree->getActualNumPoints() ) {
        //printDebug(isDebug, "annNumPoints = 0, not found ");
        return false;
    }
    // distance between two diagonal points
    // is  0
    if (DiagramPoint::DIAG == q.type) {
        if (!diagonalPoints.empty()) {
            result = *diagonalPoints.cbegin();
            //printDebug(isDebug, "Neighbour found in diagonal points, res =  ", result);
            return true;
        }
    }
    // if no neighbour found among diagonal points, 
    // search in ANN kd_tree
    annQueryPoint[0] = q.getRealX();
    annQueryPoint[1] = q.getRealY();
    //annIndices[0] = ANN_NULL_IDX;
    kdTree->annkSearch(annQueryPoint, annK, annIndices, annDistances, annEpsilon);
    //kdTree->annkFRSearch(annQueryPoint, r, annK, annIndices, annDistances, annEpsilon);
    //std::cout << distEpsilon << " = distEpsilon " << std::endl;
    if (annDistances[0] <= r + distEpsilon) {
    //if (annIndices[0] != ANN_NULL_IDX) {
        result = allPoints[annIndices[0]];
        //printDebug(isDebug, "Neighbour found with kd-tree, index =  ", annIndices[0]);
        //printDebug(isDebug, "result =  ", result);
        return true;
    }
    //printDebug(isDebug, "No neighbour found for r =  ", r);
    return false;
}

void NeighbOracleAnn::getAllNeighbours(const DiagramPoint& q, std::vector<DiagramPoint>& result)
{
    //bool isDebug { true };
    //printDebug(isDebug, "Entered getAllNeighbours for q = ", q);
    result.clear();
    // add diagonal points, if necessary
    if ( DiagramPoint::DIAG == q.type) { 
        for( auto& diagPt : diagonalPoints ) {
            result.push_back(diagPt);
        }
    }
    // delete diagonal points we found
    // to prevent finding them again
    for(auto& pt : result) {
        //printDebug(isDebug, "deleting DIAG point pt = ", pt);
        deletePoint(pt);
    }
    size_t diagOffset = result.size();
    // create the query rectangle
    // centered at q of radius r
    lo[0] = q.getRealX() - r;
    lo[1] = q.getRealY() - r;
    hi[0] = q.getRealX() + r;
    hi[1] = q.getRealY() + r;
    ANNorthRect annRect { annDim, lo, hi };
    std::vector<size_t> pointIndicesOut;
    // perorm range search on kd-tree
    kdTree->range_search(annRect, pointIndicesOut);
    // get actual points in result
    for(auto& ptIdx : pointIndicesOut) {
        result.push_back(allPoints[ptIdx]);
    }
    // delete all points we found
    for(auto ptIt = result.begin() + diagOffset; ptIt != result.end(); ++ptIt) {
        //printDebug(isDebug, "deleting point pt = ", *ptIt);
        deletePoint(*ptIt);
    }
}

NeighbOracleAnn::~NeighbOracleAnn()
{
    delete [] annIndices;
    delete [] annDistances;
    delete kdTree;
    annDeallocPt(annQueryPoint);
    annDeallocPt(lo);
    annDeallocPt(hi);
    if (annPoints) {
        annDeallocPts(annPoints);
    }
}
}