-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
296 lines (252 loc) · 8.49 KB
/
main.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
//
// Created by Nikhil Italiya on 20-12-2019.
//
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <random>
#include <fstream>
#include <time.h>
using namespace std;
//node for storing values and parent information
struct theNode {
double x = 0;
double y = 0;
double z = 0;
int parent = 0;
};
//creates node with given set of xyz values and the name of parent node as previous node
theNode* createNode(double x, double y, double z, int parent) {
auto* temp = new theNode;
temp->x = x;
temp->y = y;
temp->z = z;
temp->parent = parent;
return temp;
}
//to find the nearest node
std::pair <int, theNode> FindNearest(double xrand, double yrand, double zrand, std::vector<theNode> tree) {
std::vector<double > disvec;
//loop for creating a vector of distance from the random point to all the nodes
for (auto temp : tree) {
double distance = ((xrand - temp.x) * (xrand - temp.x)) + ((yrand - temp.y) * (yrand - temp.y)) +
((zrand - temp.z) * (zrand - temp.z));
double dd = sqrt(distance);
disvec.push_back(dd);
}
//finds the mimimum value
__gnu_cxx::__normal_iterator<double*, vector<double>> leastIterator = std::min_element(disvec.begin(), disvec.end());
auto nearest = (double)*leastIterator;
// a copy of the nearest node
int parentindex = 0;
theNode tempNearNode;
// loop to get the index of the parent node
for (int i = 0; i < disvec.size(); ++i) {
if (disvec.at(i) == nearest) {
tempNearNode = tree.at(i);
parentindex = i;
break;
}
}
return std::make_pair(parentindex, tempNearNode);
}
// to print the tree
void printTree(std::vector<theNode> tree1, int j) {
double xvalue[j];
double yvalue[j];
double zvalue[j];
int pvalue[j];
//print the tree
for (int k = 0; k < tree1.size(); ++k) {
xvalue[k] = tree1.at(k).x;
yvalue[k] = tree1.at(k).y;
zvalue[k] = tree1.at(k).z;
pvalue[k] = tree1.at(k).parent;
}
cout << "arrx = [";
for (int l = 0; l < tree1.size(); ++l) {
if (l == tree1.size() - 1) {
cout << xvalue[l];
}
else {
cout << xvalue[l] << ", " << endl;
}
}
cout << "]" << endl;
cout << "arry = [";
for (int l = 0; l < tree1.size(); ++l) {
if (l == tree1.size() - 1) {
cout << yvalue[l];
}
else {
cout << yvalue[l] << "," << endl;
}
}
cout << "]" << endl;
cout << "arrz = [";
for (int l = 0; l < tree1.size(); ++l) {
if (l == tree1.size() - 1) {
cout << zvalue[l];
}
else {
cout << zvalue[l] << "," << endl;
}
}
cout << "]" << endl;
}
//Euclidean distance
double matric(double nx, double ny, double nz, double x2, double y2, double z2) {
double dis2Obs2 = ((nx - x2) * (nx - x2)) + ((ny - y2) * (ny - y2)) + ((nz - z2) * (nz - z2));
double d2O2 = sqrt(dis2Obs2);
return d2O2;
}
//Collision check
bool checkCollision(double nx, double ny, double nz, double x2, double y2, double z2, double radius) {
double distance = matric(nx, ny, nz, x2, y2, z2);
return distance <= radius;
}
// csv file for the path
void writeCSV(std::vector<theNode> tree1, int j, int number) {
double xvalue[j];
double yvalue[j];
double zvalue[j];
int pvalue[j];
//path points
for (int k = 0; k < tree1.size(); ++k) {
xvalue[k] = tree1.at(k).x;
yvalue[k] = tree1.at(k).y;
zvalue[k] = tree1.at(k).z;
pvalue[k] = tree1.at(k).parent;
}
//create a csv file
std::ofstream myFile("path" + to_string(number) + ".csv");
for (int l = 0; l < tree1.size(); ++l) {
myFile << xvalue[l] << ", " << yvalue[l] << ", " << zvalue[l];
myFile << "\n";
}
myFile.close();
}
// The algorithm itself
void rrtAlgorithm(double xstart, double ystart, double zstart, double gx, double gy, double gz, double goalRadius, double ax1, double ay1, double az1, double obsRadius, double dmax, int iterations) {
//** define the environment
int xmin = 0;
int ymin = 0;
int zmin = 0;
int xmax = 10;
int ymax = 10;
int zmax = 10;
//a vector containing all the nodes with its parent values
std::vector<theNode> tree;
std::vector<theNode> obstacle;
std::vector<theNode> goal;
theNode node1 = *createNode(xstart, ystart, zstart, 0);
tree.push_back(node1);
//random coordinates generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> positionX(xmin, xmax);
std::uniform_real_distribution<double> positionY(ymin, ymax);
std::uniform_real_distribution<double> positionZ(zmin, zmax);
gen.seed(time(NULL));
int j = 0;
//creating and adding new point to the tree
while (true) {
double xrand = positionX(gen);
double yrand = positionY(gen);
double zrand = positionZ(gen);
//finding the minimum distance from the random node(distance vector)
int parentindex = 0;
theNode tempNearNode;
// Bias
if (j % 10 != 0) {
parentindex = FindNearest(xrand, yrand, zrand, tree).first;
tempNearNode = FindNearest(xrand, yrand, zrand, tree).second;
}
else {
parentindex = FindNearest(gx, gy, gz, tree).first;
tempNearNode = FindNearest(gx, gy, gz, tree).second;
}
//calculation for adding a point to certain distance to the direction of random point
double x0 = tempNearNode.x;
double y0 = tempNearNode.y;
double z0 = tempNearNode.z;
double x1 = xrand;
double y1 = yrand;
double z1 = zrand;
//adding the point
double nx;
double ny;
double nz;
double d = matric(x1, y1, z1, x0, y0, z0);
double t = dmax / d;
if (d > dmax) {
nx = ((1 - t) * x0) + t * x1;
ny = ((1 - t) * y0) + t * y1;
nz = ((1 - t) * z0) + t * z1;
}
else {
nx = x1;
ny = y1;
nz = z1;
}
theNode newNode = *createNode(nx, ny, nz, parentindex);
//Obstacle Check
bool inCollision = checkCollision(nx, ny, nz, ax1, ay1, az1, obsRadius);
if (!inCollision) {
tree.push_back(newNode);
}
if (inCollision) {
obstacle.push_back(newNode);
}
// Goal checking
if (checkCollision(nx, ny, nz, gx, gy, gz, goalRadius)) {
goal.push_back(newNode);
theNode lastNode = *createNode(gx, gy, gz, parentindex - 1);
tree.push_back(lastNode);
break; // for only one solution, comment out this command
}
if (j == iterations) {
cout << "iteration limit reached" << endl;
break; //for more than one solution, comment out command
}
j++;
}
printTree(obstacle, obstacle.size());
cout << j << " "<< goal.size()<< endl ;
vector <vector <theNode>> resltvec;
//backtracking
for (int i = 0; i < goal.size(); ++i) {
vector<theNode> resultpoints;
theNode resultNode = goal.at(i); // here there will be a goal node
int position = resultNode.parent;
while (position > 0) {
theNode temp;
temp.x = resultNode.x;
temp.y = resultNode.y;
temp.z = resultNode.z;
resultpoints.push_back(temp);
position = resultNode.parent;
resultNode = tree.at(position);
if (position == 0) {
resultpoints.push_back(node1);
}
}
// the resultpoints vector will be in reverse order
std::reverse(resultpoints.begin(), resultpoints.end());
resultpoints.push_back(*createNode(gx, gy, gz, 0));
resltvec.push_back(resultpoints);
// print the path
printTree(resultpoints, resultpoints.size());
writeCSV(resultpoints, resultpoints.size(), i);
}
cout << "Total " << resltvec.size() << " paths found" << endl;
}
int main() {
// first 3 values for start point
// second 3 values for target point and then goal region value
// next 3 values are for obstacle position and then its size in radius
// the last two values are the step size and the numbers of iterations to perform respectively.
rrtAlgorithm(0, 0, 0, 8, 8, 8, 0.3, 5, 5, 5, 0.5, 0.5, 5000);
return 0;
}