@@ -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 }
0 commit comments