Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include<global_planner/dijkstra.h>
00039 #include <algorithm>
00040 namespace global_planner {
00041
00042 DijkstraExpansion::DijkstraExpansion(PotentialCalculator* p_calc, int nx, int ny) :
00043 Expander(p_calc, nx, ny), pending_(NULL), precise_(false) {
00044
00045 buffer1_ = new int[PRIORITYBUFSIZE];
00046 buffer2_ = new int[PRIORITYBUFSIZE];
00047 buffer3_ = new int[PRIORITYBUFSIZE];
00048
00049 priorityIncrement_ = 2 * neutral_cost_;
00050 }
00051
00052
00053
00054
00055 void DijkstraExpansion::setSize(int xs, int ys) {
00056 Expander::setSize(xs, ys);
00057 if (pending_)
00058 delete[] pending_;
00059
00060 pending_ = new bool[ns_];
00061 memset(pending_, 0, ns_ * sizeof(bool));
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 bool DijkstraExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y,
00073 int cycles, float* potential) {
00074 cells_visited_ = 0;
00075
00076 threshold_ = lethal_cost_;
00077 currentBuffer_ = buffer1_;
00078 currentEnd_ = 0;
00079 nextBuffer_ = buffer2_;
00080 nextEnd_ = 0;
00081 overBuffer_ = buffer3_;
00082 overEnd_ = 0;
00083 memset(pending_, 0, ns_ * sizeof(bool));
00084 std::fill(potential, potential + ns_, POT_HIGH);
00085
00086
00087 int k = toIndex(start_x, start_y);
00088
00089 if(precise_)
00090 {
00091 double dx = start_x - (int)start_x, dy = start_y - (int)start_y;
00092 dx = floorf(dx * 100 + 0.5) / 100;
00093 dy = floorf(dy * 100 + 0.5) / 100;
00094 potential[k] = neutral_cost_ * 2 * dx * dy;
00095 potential[k+1] = neutral_cost_ * 2 * (1-dx)*dy;
00096 potential[k+nx_] = neutral_cost_*2*dx*(1-dy);
00097 potential[k+nx_+1] = neutral_cost_*2*(1-dx)*(1-dy);
00098
00099 push_cur(k+2);
00100 push_cur(k-1);
00101 push_cur(k+nx_-1);
00102 push_cur(k+nx_+2);
00103
00104 push_cur(k-nx_);
00105 push_cur(k-nx_+1);
00106 push_cur(k+nx_*2);
00107 push_cur(k+nx_*2+1);
00108 }else{
00109 potential[k] = 0;
00110 push_cur(k+1);
00111 push_cur(k-1);
00112 push_cur(k-nx_);
00113 push_cur(k+nx_);
00114 }
00115
00116 int nwv = 0;
00117 int nc = 0;
00118 int cycle = 0;
00119
00120
00121 int startCell = toIndex(end_x, end_y);
00122
00123 for (; cycle < cycles; cycle++)
00124 {
00125
00126 if (currentEnd_ == 0 && nextEnd_ == 0)
00127 return false;
00128
00129
00130 nc += currentEnd_;
00131 if (currentEnd_ > nwv)
00132 nwv = currentEnd_;
00133
00134
00135 int *pb = currentBuffer_;
00136 int i = currentEnd_;
00137 while (i-- > 0)
00138 pending_[*(pb++)] = false;
00139
00140
00141 pb = currentBuffer_;
00142 i = currentEnd_;
00143 while (i-- > 0)
00144 updateCell(costs, potential, *pb++);
00145
00146
00147 currentEnd_ = nextEnd_;
00148 nextEnd_ = 0;
00149 pb = currentBuffer_;
00150 currentBuffer_ = nextBuffer_;
00151 nextBuffer_ = pb;
00152
00153
00154 if (currentEnd_ == 0) {
00155 threshold_ += priorityIncrement_;
00156 currentEnd_ = overEnd_;
00157 overEnd_ = 0;
00158 pb = currentBuffer_;
00159 currentBuffer_ = overBuffer_;
00160 overBuffer_ = pb;
00161 }
00162
00163
00164 if (potential[startCell] < POT_HIGH)
00165 break;
00166 }
00167
00168 if (cycle < cycles)
00169 return true;
00170 else
00171 return false;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 #define INVSQRT2 0.707106781
00183
00184 inline void DijkstraExpansion::updateCell(unsigned char* costs, float* potential, int n) {
00185 cells_visited_++;
00186
00187
00188 float c = getCost(costs, n);
00189 if (c >= lethal_cost_)
00190 return;
00191
00192 float pot = p_calc_->calculatePotential(potential, c, n);
00193
00194
00195 if (pot < potential[n]) {
00196 float le = INVSQRT2 * (float)getCost(costs, n - 1);
00197 float re = INVSQRT2 * (float)getCost(costs, n + 1);
00198 float ue = INVSQRT2 * (float)getCost(costs, n - nx_);
00199 float de = INVSQRT2 * (float)getCost(costs, n + nx_);
00200 potential[n] = pot;
00201
00202 if (pot < threshold_)
00203 {
00204 if (potential[n - 1] > pot + le)
00205 push_next(n-1);
00206 if (potential[n + 1] > pot + re)
00207 push_next(n+1);
00208 if (potential[n - nx_] > pot + ue)
00209 push_next(n-nx_);
00210 if (potential[n + nx_] > pot + de)
00211 push_next(n+nx_);
00212 } else
00213 {
00214 if (potential[n - 1] > pot + le)
00215 push_over(n-1);
00216 if (potential[n + 1] > pot + re)
00217 push_over(n+1);
00218 if (potential[n - nx_] > pot + ue)
00219 push_over(n-nx_);
00220 if (potential[n + nx_] > pot + de)
00221 push_over(n+nx_);
00222 }
00223 }
00224 }
00225
00226 }