forked from zhzhang0225/sampling_based_planners
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprm_graph.cpp
More file actions
82 lines (70 loc) · 1.99 KB
/
Copy pathprm_graph.cpp
File metadata and controls
82 lines (70 loc) · 1.99 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
#include "prm_graph.h"
PRMGraph::PRMGraph(int numofDOFs)
: num_dof_(numofDOFs),
node_id_(0) { }
int PRMGraph::getCurrentNodeID()
{
return this->node_id_;
}
void PRMGraph::addVertex(vector<double>& vertex)
{
// add one row
(this->vertices_).push_back(vector<double>());
// fill in the new row
vertices_[this->node_id_] = vertex;
// add place-holder
(this->edges_).push_back(vector<int>());
(this->node_id_)++;
}
vector<double> PRMGraph::getNodeConfig(int node_id)
{
return (this->vertices_)[node_id];
}
vector<int> PRMGraph::getNeighborsID(int node_id)
{
return (this->edges_)[node_id];
}
void PRMGraph::addEdge(int vertex1_id, int vertex2_id)
{
(this->edges_)[vertex1_id].push_back(vertex2_id);
(this->edges_)[vertex2_id].push_back(vertex1_id);
//std::cout << (this->edges_)[vertex1_id].size() << " " << (this->edges_)[vertex2_id].size() << std::endl;
}
vector<int> PRMGraph::findKNN(vector<double>& new_vertex, int k)
{
vector<pair<int, double>> dist;
vector<int> knn_id;
// perform linear search to get K nearest neighbors
// calculate distance for all vertices
for (int i=0; i<(this->vertices_).size(); i++) {
auto curr_dist = make_pair(i, this->calculateDistance(new_vertex, (this->vertices_)[i]));
dist.push_back(curr_dist);
}
// sort the distance
sort(dist.begin(), dist.end(),
[](const pair<int, double>& p1, const pair<int, double>& p2)
{
return (p1.second < p2.second);
});
// find id of k nearest neighbors
for (int i=0; i<dist.size(); i++) {
if (dist[i].second > 0.1) knn_id.push_back(dist[i].first);
if (knn_id.size() >= k) break;
}
return knn_id;
}
int PRMGraph::getNearestVertex(vector<double>& new_vertex)
{
double min_dist = DBL_MAX;
int min_index = -1;
double curr_dist = 0.0;
// perform linear search to get the nearest neighbor
for (int i=0; i<(this->vertices_).size(); i++) {
curr_dist = this->calculateDistance(new_vertex, (this->vertices_)[i]);
if (curr_dist < min_dist) {
min_dist = curr_dist;
min_index = i;
}
}
return min_index;
}