astar.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  * David V. Lu!!
37  *********************************************************************/
38 #include<global_planner/astar.h>
40 
41 namespace global_planner {
42 
44  Expander(p_calc, xs, ys) {
45 }
46 
47 bool AStarExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y,
48  int cycles, float* potential) {
49  queue_.clear();
50  int start_i = toIndex(start_x, start_y);
51  queue_.push_back(Index(start_i, 0));
52 
53  std::fill(potential, potential + ns_, POT_HIGH);
54  potential[start_i] = 0;
55 
56  int goal_i = toIndex(end_x, end_y);
57  int cycle = 0;
58 
59  while (queue_.size() > 0 && cycle < cycles) {
60  Index top = queue_[0];
61  std::pop_heap(queue_.begin(), queue_.end(), greater1());
62  queue_.pop_back();
63 
64  int i = top.i;
65  if (i == goal_i)
66  return true;
67 
68  add(costs, potential, potential[i], i + 1, end_x, end_y);
69  add(costs, potential, potential[i], i - 1, end_x, end_y);
70  add(costs, potential, potential[i], i + nx_, end_x, end_y);
71  add(costs, potential, potential[i], i - nx_, end_x, end_y);
72 
73  cycle++;
74  }
75 
76  return false;
77 }
78 
79 void AStarExpansion::add(unsigned char* costs, float* potential, float prev_potential, int next_i, int end_x,
80  int end_y) {
81  if (next_i < 0 || next_i >= ns_)
82  return;
83 
84  if (potential[next_i] < POT_HIGH)
85  return;
86 
87  if(costs[next_i]>=lethal_cost_ && !(unknown_ && costs[next_i]==costmap_2d::NO_INFORMATION))
88  return;
89 
90  potential[next_i] = p_calc_->calculatePotential(potential, costs[next_i] + neutral_cost_, next_i, prev_potential);
91  int x = next_i % nx_, y = next_i / nx_;
92  float distance = abs(end_x - x) + abs(end_y - y);
93 
94  queue_.push_back(Index(next_i, potential[next_i] + distance * neutral_cost_));
95  std::push_heap(queue_.begin(), queue_.end(), greater1());
96 }
97 
98 } //end namespace global_planner
AStarExpansion(PotentialCalculator *p_calc, int nx, int ny)
Definition: astar.cpp:43
virtual float calculatePotential(float *potential, unsigned char cost, int n, float prev_potential=-1)
std::vector< Index > queue_
Definition: astar.h:70
TFSIMD_FORCE_INLINE const tfScalar & y() const
unsigned char lethal_cost_
Definition: expander.h:98
#define POT_HIGH
Definition: planner_core.h:40
int toIndex(int x, int y)
Definition: expander.h:92
unsigned char neutral_cost_
Definition: expander.h:98
double distance(double x0, double y0, double x1, double y1)
TFSIMD_FORCE_INLINE const tfScalar & x() const
PotentialCalculator * p_calc_
Definition: expander.h:101
static const unsigned char NO_INFORMATION
void add(unsigned char *costs, float *potential, float prev_potential, int next_i, int end_x, int end_y)
Definition: astar.cpp:79
bool calculatePotentials(unsigned char *costs, double start_x, double start_y, double end_x, double end_y, int cycles, float *potential)
Definition: astar.cpp:47


global_planner
Author(s): David Lu!!
autogenerated on Sun Mar 3 2019 03:44:42