-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_kdtree.cpp
More file actions
executable file
·54 lines (38 loc) · 1.82 KB
/
query_kdtree.cpp
File metadata and controls
executable file
·54 lines (38 loc) · 1.82 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
#include "Point.hpp"
#include "kdtree.hpp"
#include "DotFileWriter.hpp"
#include "PCDFile.hpp"
using namespace rossb83;
/*
* quick script that queries a serialized kdtree and outputs the nearest neighbors to a file
*/
int main(int argc, char* argv[]) {
static const std::string KDTREE_FILE = "kdtreefile";
static const std::string QUERY_FILE = "queryfile";
static const std::string OUTPUT_FILE = "outputfile";
std::unordered_map<std::string,std::string> inputs;
inputs.insert({{KDTREE_FILE,"sample_kdtree.dot"},{QUERY_FILE,"query_data.csv"},{OUTPUT_FILE,"sample_query.csv"}});
for (size_t i = 1; i < argc; i++) {
std::string input = std::string(argv[i]);
int delimiter = input.find("=");
if ((argv[i][0] == '-') && (delimiter != std::string::npos)) {
inputs[input.substr(1,delimiter-1)] = input.substr(delimiter+1);
}
}
std::cout << "Reading query data input file: " << inputs[QUERY_FILE] << std::endl;
// read input file from disk
PCDFile<double> queryfile(inputs[QUERY_FILE]);
std::cout << "Deserializing kdtree file: " << inputs[KDTREE_FILE] << std::endl;
// generate kdtree from input file
DotFileReader<double> dotfile(inputs[KDTREE_FILE]);
KDTree<double> kdtree(dotfile);
// create output file
std::cout << "creating output file: " << inputs[OUTPUT_FILE] << std::endl;
std::ofstream out(inputs[OUTPUT_FILE]);
for (Point<double> p : queryfile) {
// returns tuple where 1st element is the nearest neighbor, 2nd is the euclidean distance, and 3rd is the number of nodes in the tree visited
std::tuple<Point<double>, double, int> nearestneighbor = kdtree.queryNearestNeighbor(p);
out << std::get<0>(nearestneighbor).label() << "," << std::get<1>(nearestneighbor) << std::endl;
}
return 0;
}