@@ -20,10 +20,10 @@ class Node{
2020public:
2121 int x;
2222 int y;
23- float cost ;
23+ float sum_cost ;
2424 Node* p_node;
2525
26- Node (int x_, int y_, float cost_ , Node* p_node_=NULL ):x(x_), y(y_),cost(cost_ ), p_node(p_node_){};
26+ Node (int x_, int y_, float sum_cost_= 0 , Node* p_node_=NULL ):x(x_), y(y_), sum_cost(sum_cost_ ), p_node(p_node_){};
2727};
2828
2929
@@ -91,8 +91,8 @@ bool verify_node(Node* node,
9191}
9292
9393
94- float calc_heristic (Node n1, Node n2, float w=1.0 ){
95- return w * std::sqrt (std::pow (n1. x -n2. x , 2 )+std::pow (n1. y -n2. y , 2 ));
94+ float calc_heristic (Node* n1, Node* n2, float w=1.0 ){
95+ return w * std::sqrt (std::pow (n1-> x -n2-> x , 2 )+std::pow (n1-> y -n2-> y , 2 ));
9696}
9797
9898std::vector<Node> get_motion_model (){
@@ -160,7 +160,10 @@ void a_star_planning(float sx, float sy,
160160 cv::Scalar (0 , 0 , 255 ), -1 );
161161
162162 std::vector<std::vector<int > > visit_map (xwidth, vector<int >(ywidth, 0 ));
163+
164+ std::vector<std::vector<float > > path_cost (xwidth, vector<float >(ywidth, std::numeric_limits<float >::max ()));
163165
166+ path_cost[nstart->x ][nstart->y ] = 0 ;
164167
165168 std::vector<std::vector<int > > obmap = calc_obstacle_map (
166169 ox, oy,
@@ -170,7 +173,7 @@ void a_star_planning(float sx, float sy,
170173 bg, img_reso);
171174
172175 // NOTE: d_ary_heap should be a better choice here
173- auto cmp = [](const Node* left, const Node* right){return left->cost > right->cost ;};
176+ auto cmp = [](const Node* left, const Node* right){return left->sum_cost > right->sum_cost ;};
174177 std::priority_queue<Node*, std::vector<Node*>, decltype (cmp)> pq (cmp);
175178
176179 pq.push (nstart);
@@ -189,7 +192,7 @@ void a_star_planning(float sx, float sy,
189192 }
190193
191194 if (node->x == ngoal->x && node->y ==ngoal->y ){
192- ngoal->cost = node->cost ;
195+ ngoal->sum_cost = node->sum_cost ;
193196 ngoal->p_node = node;
194197 break ;
195198 }
@@ -198,7 +201,7 @@ void a_star_planning(float sx, float sy,
198201 Node * new_node = new Node (
199202 node->x + motion[i].x ,
200203 node->y + motion[i].y ,
201- node->cost + motion[i].cost ,
204+ path_cost[ node->x ][node-> y ] + motion[i].sum_cost + calc_heristic (ngoal, node) ,
202205 node);
203206
204207 if (!verify_node (new_node, obmap, min_ox, max_ox, min_oy, max_oy)){
@@ -216,22 +219,25 @@ void a_star_planning(float sx, float sy,
216219 cv::Point ((new_node->x +1 )*img_reso, (new_node->y +1 )*img_reso),
217220 cv::Scalar (0 , 255 , 0 ));
218221
219- // std::string int_count = std::to_string(count);
220- // cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
222+ std::string int_count = std::to_string (count);
223+ cv::imwrite (" ./pngs/" +std::string (5 -int_count.length (), ' 0' ).append (int_count)+" .png" , bg);
221224 count++;
222225 cv::imshow (" astar" , bg);
223226 cv::waitKey (5 );
224227
225- pq.push (new_node);
228+ if (path_cost[node->x ][node->y ]+motion[i].sum_cost < path_cost[new_node->x ][new_node->y ]){
229+ path_cost[new_node->x ][new_node->y ]=path_cost[node->x ][node->y ]+motion[i].sum_cost ;
230+ pq.push (new_node);
231+ }
226232 }
227233 }
228234
229235 calc_final_path (ngoal, reso, bg, img_reso);
230236 delete ngoal;
231237 delete nstart;
232238
233- // std::string int_count = std::to_string(count);
234- // cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
239+ std::string int_count = std::to_string (count);
240+ cv::imwrite (" ./pngs/" +std::string (5 -int_count.length (), ' 0' ).append (int_count)+" .png" , bg);
235241 cv::imshow (" astar" , bg);
236242 cv::waitKey (5 );
237243};
0 commit comments