-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_kdtree.cpp
More file actions
89 lines (68 loc) · 2.59 KB
/
query_kdtree.cpp
File metadata and controls
89 lines (68 loc) · 2.59 KB
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
//============================================================================
// Name : query_kdtree.cpp
// Author : Thomas J. Meehan
// Version : 1.4
// Copyright : Copyright 2016 Thomas J. Meehan
/*
This file is part of kdTree.
kdTree 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.
kdTree 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 kdTree. If not, see <http://www.gnu.org/licenses/>.
*/
// Description : Read in a previously created tree and find the nearest
// neighbor for a series of query points
//============================================================================
#include "kdTree.h"
int main(int argc, char *argv[]) {
vector<nPoint*> queries; //store data from csv
string treeFile = "treeOut.txt";
string input = "query_data.csv";
string output = "results.txt";
if (argc > 1) {
treeFile = argv[1];
}
if (argc > 2) {
input = argv[2];
}
if (argc > 3) {
output = argv[3];
}
cout << "Reading in query data from " << input << endl;
int k = getDataFile(input, queries); //return how many dimensions (k) data is
treeNode * root = new treeNode;
if( root->readTree(treeFile, k)){//create tree from file, also test for success
cout << "Tree read in success" << endl << endl;
}else{
cout << "Error reading in tree from file, exiting\n";
exit(1);
}
double bestDistance = DBL_MAX; //max value for double
nPoint * bestPt = nullptr;
ofstream myfile(output);
myfile.precision(dbl::max_digits10); //max precision for writing to file
if (myfile.is_open()) {
for (int i = 0; i < queries.size(); i++) { //iterate through all queries and search tree to find nearest neighbor
queries.at(i)->findNear(root, bestPt, bestDistance, k);
cout << "For query " << i << " closest node was ";
bestPt->print();
cout << " with distance of " << bestDistance << endl;
myfile << bestPt->getIndex() << "," << bestDistance << endl; //write to file
bestDistance = DBL_MAX;
bestPt = nullptr;
}
cout << "Successful write out to " << output << endl;
myfile.close();
} else {
cout << "could not open file to read queries\n";
}
delete bestPt;
delete root; //delete entire tree, including associated data
queries.clear();
}