Skip to content

Commit f3ee72e

Browse files
committed
Simplify path for simple RRT
1 parent 574d789 commit f3ee72e

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

include/RRTConnect_implementation.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace rrt
2929
{
3030
Utils::Point<T> parent = pathPoints[0];
3131
finalPath.push_back(parent);
32-
int start=0;
3332
for (int i=1; i<pathPoints.size();i++)
3433
{
3534
Utils::Point<T> now = pathPoints[1];
@@ -249,7 +248,6 @@ namespace rrt
249248
template <class T>
250249
bool RRT<T>::checkPoint(Utils::Point<T> parent, Utils::Point<T> next)
251250
{
252-
253251
int x1=parent.x, x2=next.x, y1=parent.y,y2=next.y;
254252
float x=x1, count;
255253
try

include/RRT_implementation.hpp

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ namespace rrt
2727
template <class T>
2828
std::vector<Utils::Point<T> > RRT<T>::getPointsOnPath()
2929
{
30+
Utils::Point<T> parent = pathPoints[0];
31+
finalPath.push_back(parent);
32+
for (int i=1; i<pathPoints.size();i++)
33+
{
34+
Utils::Point<T> now = pathPoints[1];
35+
if(checkPoint(parent,now)!=true)
36+
{
37+
parent = pathPoints[i-1];
38+
i--;
39+
if (i!=0)
40+
finalPath.push_back(parent);
41+
}
42+
43+
}
44+
finalPath.push_back(pathPoints[pathPoints.size()-1]);
3045
return finalPath;
3146
}
3247

@@ -80,7 +95,7 @@ namespace rrt
8095
do{
8196
next= generateBiasedPoint(1);
8297
both= findClosestNode(next);
83-
}while(checkPoint(next, next)!=true);
98+
}while(checkPoint(both.first, next)!=true);
8499
//std::cout<<" : "<<next.x<<","<<next.y<<std::endl;
85100
}
86101
else
@@ -89,7 +104,7 @@ namespace rrt
89104
do{
90105
next = generatePoint();
91106
both= findClosestNode(next);
92-
}while(checkPoint(next, next)!=true);
107+
}while(checkPoint(both.first, next)!=true);
93108
//std::cout<<" : "<<next.x<<","<<next.y<<std::endl;
94109
}
95110
//std::cout<<" Growing Tree next : "<<next.x<<","<<next.y<<std::endl;
@@ -180,9 +195,85 @@ namespace rrt
180195
}
181196

182197
template <class T>
183-
bool RRT<T>::checkPoint(Utils::Point<T> first, Utils::Point<T> next)
198+
bool RRT<T>::obstacle_here(int x, int y)
184199
{
185-
return userCheck(next);
200+
for (int i=0;i<ObstaclePoints.size();i++)
201+
{
202+
Utils::Point<T> pratham;
203+
pratham.x = x; pratham.y = y;
204+
Utils::Point<T> dwitiya;
205+
dwitiya.x = ObstaclePoints[i].x; dwitiya.y = ObstaclePoints[i].y;
206+
if (dist(pratham,dwitiya)<obstacleradius)
207+
return true;
208+
}
209+
return false;
210+
}
211+
212+
213+
template <class T>
214+
bool RRT<T>::checkPoint(Utils::Point<T> parent, Utils::Point<T> next)
215+
{
216+
int x1=parent.x, x2=next.x, y1=parent.y,y2=next.y;
217+
float x=x1, count;
218+
try
219+
{
220+
// std::cout<<"\nIn try ";
221+
// std::cout<<x1<<std::endl;
222+
// std::cout<<x2<<std::endl;
223+
// std::cout<<y1<<std::endl;
224+
// std::cout<<y2<<std::endl;
225+
226+
int m=float(y2-y1)/(x2-x1);
227+
if (m==0) throw 20;
228+
int c=y2-m*x2;
229+
count = fabs(1.0/m);
230+
// while(1)
231+
if (count>1) count=1;
232+
if (count<-1) count=-1;
233+
if (x2<x1) count*=-1;
234+
// std::cout<<"\nm is "<<m<<" and count is: "<<count<<"\n";
235+
while (1)
236+
{
237+
x+=count;
238+
int y=m*x+c;
239+
if ((count>0 and x>=x2) || (count<0 and x<=x2))
240+
{
241+
// std::cout<<"Return true from try\n";
242+
return true;
243+
}
244+
else
245+
{
246+
// std::cout<<"\nm is "<<m<<" and count is: "<<count<<"\n";
247+
// std::cout<<std::endl<<"x: "<<x<<" x2: "<<x2<<std::endl;
248+
// std::cout<<"count: "<<count<<std::endl;
249+
}
250+
if (obstacle_here(x,y))
251+
{
252+
// std::cout<<"Return false from try\n";
253+
return false;
254+
}
255+
}
256+
}
257+
catch(int e)
258+
{
259+
count=1;
260+
int y=y1;
261+
if (y2<y1) count*=-1;
262+
while (1)
263+
{
264+
y+=count;
265+
if ((count>0 and y>=y2) || (count<0 and y<=y2))
266+
{
267+
// std::cout<<"Return true from catch\n";
268+
return true;
269+
}
270+
if (obstacle_here(x,y))
271+
{
272+
// std::cout<<"Return false from catch\n";
273+
return false;
274+
}
275+
}
276+
}
186277
}
187278

188279
template <class T>
@@ -215,12 +306,12 @@ namespace rrt
215306
void RRT<T>::generatePath(Utils::Point<T> first,Utils::Point<T> last)
216307
{
217308
Utils::Point<T> cur=last;
218-
finalPath.push_back(cur);
309+
pathPoints.push_back(cur);
219310
while(cur!=first || cur!=startPoint)
220311
{
221312
Utils::Point<T> parent= getParent(cur);
222313
//std::cout<<"current : "<<cur.x<<","<<cur.y<<"| parent : "<<parent.x<<","<<parent.y<<std::endl;
223-
finalPath.push_back(parent);
314+
pathPoints.push_back(parent);
224315
cur=parent;
225316
}
226317
}

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int main()
3030
test.setBiasParameter(100);
3131
test.setOrigin(origin);
3232
test.setMaxIterations(10000);
33+
test.setObstacleRadius(50);
3334
test.plan();
3435
cout<<"#################################################"<<endl;
3536
vector<Utils::Point<int> > path=test.getPointsOnPath();

0 commit comments

Comments
 (0)