-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverageAlgorithm.cpp
502 lines (433 loc) · 12.9 KB
/
coverageAlgorithm.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "coverageAlgorithm.h"
//Takes convex hull of input Point_2 and returns a vector<Point_2>
Points PathPlanner::getHull(Points input){
Points result;
CGAL::convex_hull_2( input.begin(), input.end(), std::back_inserter(result) );
this->BPs = result;
cout<<"Hull Result\n";
printPoints(result);
return result;
}
//Get the BPs from a file called WP.txt
Points PathPlanner::setBPs(){
Points wps;
ifstream inputFile ("BP.txt");
while(!inputFile.eof()) { //reads input
// stringstream ss;
// string s;
// long double xval, yval;
// string trash;
// getline(inputFile , s);
// ss << s;
// if (s[s.size()-1] != ';') {
// //The format of the text file is latitude, longitude
// ss >> yval >> xval;
// cout<<std::fixed;
// cout<<xval<<" , "<<yval<<endl;
// wps.push_back(Point_2(xval,yval));
// }
long double xval, yval;
inputFile>>yval>>xval;
std::cout.precision(20);
//cout<<xval<<" , "<<yval<<endl;
wps.push_back(Point_2(xval,yval));
}
return wps;
}
//Sill function to make a polygon type.There are more intricate checks and functions that can be used with the ploygon
//class but for now it is simply an intermediate or unecessary step.
Polygon_2 PathPlanner::makePoly(Points points){
Polygon_2 poly;
for (int i =0 ; i < points.size(); i++){
poly.push_back(points[i]);
}
return poly;
}
//Euclidean distance calculation
long double PathPlanner::getDistance(Point_2 p1, Point_2 p2){
long double dx = p1.x()-p2.x();
long double dy = p1.y()-p2.y();
long double dist = sqrt(dx*dx + dy*dy);
return dist;
}
//Returns the angle between points in degrees
long double PathPlanner::getAnglebetweenPoints(Point_2 point, Point_2 centroid) {
long double dx = centroid.x()-point.x();
long double dy = centroid.y()-point.y();
dx = abs(dx);
dy = abs(dy);
long double angle = atan(dy/dx)*(180/M_PI);
cout<<point<<" deg: "<<angle<<'\n';
return angle;
}
//Ohhh gosh. uhmm. gee what does this do?
Point_2 PathPlanner::getCentroid(Polygon_2 poly){
long double totalX =0;
long double totalY =0;
for (int i =0 ; i < poly.size(); i ++){
totalX += poly[i].x();
totalY += poly[i].y();
}
Point_2 centroid( (totalX/poly.size()) , (totalY/poly.size()) );
return centroid;
}
void PathPlanner::printPoly(Polygon_2 poly){
for ( int i =0 ; i < poly.size(); i++){
cout<<std::fixed;
cout<<"Point: "<<i<<" "<<poly[i].x()<<" , "<<poly[i].y()<<'\n';
}
}
void PathPlanner::printPoints(Points points){
for ( int i =0 ; i < points.size(); i++){
cout<<"Point: "<<i<<" "<<points[i].x()<<" , "<<points[i].y()<<'\n';
}
}
//Writes to file "Path.txt" creating a list of points
Points PathPlanner::setPath(vector<Polygon_2> allPaths){
Points inPath;
for(int i =0 ; i < allPaths.size(); i++){
for(int j =0 ; j< allPaths[i].size(); j++){
inPath.push_back(allPaths[i][j]);
}
}
this->path = inPath;
return inPath;
}
//Write to file "Path.txt" using vector<Point_2>
Points PathPlanner::setPath(Points inPath){
this->path = inPath;
return inPath;
}
void PathPlanner::writePath(){
ofstream output("Path.txt");
if(output.is_open()){
for (int i =0 ; i <path.size(); i ++){
output.precision(10);
output<<path[i].y()/SCALEUP<<" , "<<path[i].x()/SCALEUP<<'\n';
}
output.close();
}
}
void PathPlanner::writeMetadata(){
ofstream output("Metadata.txt");
if(output.is_open()){
output.precision(10);
output<<this->centroid.y()/SCALEUP<<" , "<<this->centroid.x()/SCALEUP<<'\n';
for (int i =0 ; i <BPs.size(); i ++){
//output<<std::fixed;
output<<BPs[i].y()/SCALEUP<<" , "<<BPs[i].x()/SCALEUP<<'\n';
}
output.close();
}
}
bool PathPlanner::enscribedPoly(Polygon_2 poly){
}
Polygon_2 PathPlanner::convertPoints2Poly(Points in){
Polygon_2 poly;
for (int i = 0; i < in.size(); ++i)
{
poly.push_back(in[i]);
}
return poly;
}
vector<Polygon_2> PathPlanner::linearShrink(){
// Points dist2center; //really lazy hack to store xcomp and ycomp in the point class
// for(int i =0 ; i < BPs.size(); i++){
// long double distance = getDistance(this->centroid,BPs[i]);
// cout<<"Dist: "<<distance<<'\n';
// cout<<"distBetweenPoints "<< this->distBetweenPoints<<'\n';
// //BUG - dividing by any decimal severly skews the results
// long double increment = distance - distBetweenPoints;
// cout<<"increment: "<<increment<<'\n';
// long double angle = getAnglebetweenPoints(BPs[i], this->centroid);
// cout<<"angle: "<<angle<<'\n';
// long double xcomp = cos(angle*M_PI/180.0)*increment;
// cout<<"xcomp: "<<xcomp<<'\n';
// long double ycomp = sin(angle*M_PI/180.0)*increment;
// cout<<"ycomp: "<<ycomp<<'\n';
// dist2center.push_back(Point_2(xcomp,ycomp));
// }
// printPoints(dist2center);
vector<Polygon_2> allTraces;
Polygon_2 polyIterator = convertPoints2Poly(this->BPs);
bool centroidFlag = true;
while(centroidFlag){
Polygon_2 holder;
for (int i = 0; i < polyIterator.size(); ++i)
{
// long double x,y;
// if(polyIterator[i].x() <centroid.x() ){
// x = polyIterator[i].x()+dist2center[i].x();
// y = polyIterator[i].y()+dist2center[i].y();
// }
// else{
// x = polyIterator[i].x()-dist2center[i].x();
// y = polyIterator[i].y()-dist2center[i].y();
// }
cout<<"centroid: "<<this->centroid<<"\tpoint: "<<polyIterator[i]<<endl;
long double distance = getDistance(this->centroid,polyIterator[i]);
cout<<"Dist: "<<distance<<'\n';
cout<<"distBetweenPoints "<< this->distBetweenPoints<<'\n';
long double increment = distance - this->distBetweenPoints;
cout<<"increment: "<<increment<<'\n';
long double angle = getAnglebetweenPoints(polyIterator[i], this->centroid);
cout<<"angle: "<<angle<<'\n';
long double xcomp = cos(angle*M_PI/180.0)*increment;
cout<<"xcomp: "<<xcomp<<'\n';
long double ycomp = sin(angle*M_PI/180.0)*increment;
cout<<"ycomp: "<<ycomp<<'\n';
long double x, y;
x =this->centroid.x();
y =this->centroid.y();
//top right quadrant from centroid
if((polyIterator[i].x()>centroid.x()) && (polyIterator[i].y()>centroid.y()) ){
Point_2 newPoint(x+xcomp,y+ycomp);
if(getDistance(newPoint, getCentroid() ) > this->distBetweenPoints){
holder.push_back(newPoint);
}
else{
cout<<"Point too close to center"<<endl;
}
}
//top left quadrant from centroid
else if((polyIterator[i].x()<centroid.x()) &&(polyIterator[i].y()>centroid.y())){
Point_2 newPoint(x-xcomp,y+ycomp);
if(getDistance(newPoint, getCentroid() ) > this->distBetweenPoints){
holder.push_back(newPoint);
}
else{
cout<<"Point too close to center"<<endl;
}
}
//bottom right quadrant of centroid
else if((polyIterator[i].x()>centroid.x()) && polyIterator[i].y()<centroid.y()){
Point_2 newPoint(x+xcomp,y-ycomp);
if(getDistance(newPoint, getCentroid() ) > this->distBetweenPoints){
holder.push_back(newPoint);
}
else{
cout<<"Point too close to center"<<endl;
}
}
//Bottom left quadrant of centriod
else{
Point_2 newPoint(x-xcomp,y-ycomp);
if(getDistance(newPoint, getCentroid() ) > this->distBetweenPoints){
holder.push_back(newPoint);
}
else{
cout<<"Point too close to center"<<endl;
}
}
cout<<xcomp<<" , "<<ycomp<<'\n';
}
if(holder.size() == 0){
centroidFlag = false;
holder.push_back(getCentroid());
allTraces.push_back(holder);
}
else{
allTraces.push_back(holder);
polyIterator = holder;
}
}
return allTraces;
}
//Puny constructor. It should probably load from some initialization file.
PathPlanner::PathPlanner(){
}
//Constructs the grid based on the polygon's bounding box and generates a random point if inside polygon
Points PathPlanner::getRandGridPoints(){
//Grid
CGAL::Bbox_2 box = this->BPpoly.bbox();
long double xmini = box.xmin();
long double ymini = box.ymin();
long double xmaxi = box.xmax();
long double ymaxi = box.ymax();
cout<<xmini<<" "<<xmaxi<<" "<<ymini<<" "<<ymaxi<<'\n';
Point_2 iterator;
vector<Grid> grids;
Points randGridPoints;
for(long double i =ymini; i<ymaxi; i += this->gridSideLen ){
for(long double j = xmini; j<xmaxi; j+= this->gridSideLen ){
grids.push_back( Grid( Point_2(j,i) , this->gridSideLen) );
cout<<"row: "<<i<<" column: "<<j<<'\n';
}
}
for (int i =0 ; i <grids.size();i++){
if(grids[i].is_bounded(BPpoly)){
randGridPoints.push_back(grids[i].genRandPoint());
grids[i].printGrid();
}
}
return randGridPoints;
}
//Rapidly Exploring Random Tree Search
Points PathPlanner::RRT(){
try{
srand(time(NULL));
Points randPoints = getRandGridPoints();
printPoints(randPoints);
randPoints = this->TSP(randPoints);
setPath(randPoints);
return randPoints;
}
catch(const bad_alloc&){
cout<<"Something really bad happened!";
//return NULL;
}
}
list<Point_2> PathPlanner::vector2List(Points vPoints){
list<Point_2> buffer;
copy(vPoints.begin(), vPoints.end(), back_inserter(buffer));
return buffer;
}
//Uses a greedy nearest neighbor algorithm
Points PathPlanner::TSP(Points unorderedPoints){
list<Point_2> pList =vector2List(unorderedPoints);
Points randPath;
Point_2 firstPoint = unorderedPoints[0];
CGAL::Point_set_2<K> pSet;
pSet.insert(pList.begin(),pList.end());
Vertex_handle handler = pSet.nearest_neighbor(firstPoint);
//pSet.remove(firstPoint);
randPath.push_back(firstPoint);
while(pSet.number_of_vertices()!= 0){
Point_2 iterator = handler->point();
pSet.remove(handler);
randPath.push_back(iterator);
handler = pSet.nearest_neighbor(iterator);
}
return randPath;
}
//Silly, arrogant sequence {1,1,2,2,3,3,...,n,n}*distBetweenPoints
vector<int> PathPlanner::genSpiralSequence(int seqSize){
vector<int> sequence;
for(int i =1; i < seqSize ; i++){
sequence.push_back(i*this->distBetweenPoints);
sequence.push_back(i*this->distBetweenPoints);
}
return sequence;
}
//Generates a square spiral path using a silly number sequence {1,1,2,2,3,3,4,4,...,n,n}*distBetweenPoints
Points PathPlanner::getSpiralPath(){
//It is unlikely that the user will request more that 4000 points
vector<int> seq = genSpiralSequence(2000);
Points spiral;
Point_2 buffer= this->centroid;
spiral.push_back(buffer);
for(int i =0; i < INT_MAX; i++){
//Establishes the order of spiral: up, left, down, right, up....
int hashcode = i % 4;
switch(hashcode){
case 0:
buffer =Point_2( buffer.x(),buffer.y()+seq[i]);
break;
case 1:
buffer = Point_2(buffer.x()-seq[i],buffer.y());
break;
case 2:
buffer = Point_2(buffer.x(), buffer.y()-seq[i]);
break;
case 3:
buffer = Point_2(buffer.x()+seq[i],buffer.y());
break;
}
if(CGAL::ON_UNBOUNDED_SIDE!= BPpoly.bounded_side(buffer)){
spiral.push_back(buffer);
}
else{
return spiral;
}
}
}
void PathPlanner::calcPathLength(){
long double totalDist = 0;
for(int i = 0; i < (path.size()-2); ++i){
totalDist += getDistance(path[i],path[i+1]);
}
this->pathLength = totalDist;
}
//Initialize critical data members
void PathPlanner::initialize(){
Points points, result;
points = this->setBPs();
result = this->getHull(points);
this->BPs = result;
this->BPpoly = makePoly(result);
Polygon_2 test = this->makePoly(result);
this->setCentroid(this->getCentroid(test));
cout<<"\nCentroid: "<<this->centroid.x()<<" , "<<this->centroid.y()<<endl;
}
//*********************************************************************************
// Interface Section
//In a perfect world these would have some kind of error checking. I like to live dangerously....
int PathPlanner::runTrace(){
vector<Polygon_2> all = this->linearShrink();
this->setPath(all);
calcPathLength();
writePath();
writeMetadata();
cout<<"Estimated path length: "<<this->pathLength<<"\n";
return 0;
}
int PathPlanner::runSquareSpiral(){
Points spiralpath = this->getSpiralPath();
this->setPath(spiralpath);
calcPathLength();
writePath();
writeMetadata();
cout<<"Estimated path length: "<<this->pathLength<<"\n";
return 0;
}
int PathPlanner::runRRT(){
this->RRT();
calcPathLength();
writePath();
writeMetadata();
cout<<"Estimated path length: "<<this->pathLength<<"\n";
return 0;
}
void PathPlanner::mainMenu(){
cout<<"\n\nWelcome to Eric's Caffeine Induced Path Planner!\n";
bool choiceFlag= true;
string choice;
while(choiceFlag){
cout<<"Please select from the following menu options.\n\n";
cout<<"1: Run Inner Trace Algorithm\n";
cout<<"2: Run a silly Square Spiral\n";
cout<<"3: Run a Rapidly Exploring Random Tree Search Algorithm\n";
cin>>choice;
if(isdigit(choice[0])){
//Really dirty hack to get the int value. Im sorry :(
int option= choice[0] - '0';
switch(option){
case 1:
runTrace();
break;
case 2:
runSquareSpiral();
break;
case 3:
runRRT();
break;
}
}
else if(choice[0] == 'q' || choice[0] =='Q'){
cout<<"\n\nThanks for using Eric's Caffeine Induced Path Planner!\n";
cout<<"Bye\n";
break;
}
else{
cout<<"Please enter a number listed on the options menu!\n";
}
}
}
int main()
{
// Points points, result;
PathPlanner planner;
planner.initialize();
planner.mainMenu();
return 0;
}