dijkstra.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2008, 2013, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Eitan Marder-Eppstein
00036  *         David V. Lu!!
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     // priority buffers
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 // Set/Reset map size
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 // main propagation function
00066 // Dijkstra method, breadth-first
00067 // runs for a specified number of cycles,
00068 //   or until it runs out of cells to update,
00069 //   or until the Start cell is found (atStart = true)
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     // priority buffers
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     // set goal
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;            // max priority block size
00117     int nc = 0;            // number of cells put into priority blocks
00118     int cycle = 0;        // which cycle we're on
00119 
00120     // set up start cell
00121     int startCell = toIndex(end_x, end_y);
00122 
00123     for (; cycle < cycles; cycle++) // go for this many cycles, unless interrupted
00124             {
00125         // 
00126         if (currentEnd_ == 0 && nextEnd_ == 0) // priority blocks empty
00127             return false;
00128 
00129         // stats
00130         nc += currentEnd_;
00131         if (currentEnd_ > nwv)
00132             nwv = currentEnd_;
00133 
00134         // reset pending_ flags on current priority buffer
00135         int *pb = currentBuffer_;
00136         int i = currentEnd_;
00137         while (i-- > 0)
00138             pending_[*(pb++)] = false;
00139 
00140         // process current priority buffer
00141         pb = currentBuffer_;
00142         i = currentEnd_;
00143         while (i-- > 0)
00144             updateCell(costs, potential, *pb++);
00145 
00146         // swap priority blocks currentBuffer_ <=> nextBuffer_
00147         currentEnd_ = nextEnd_;
00148         nextEnd_ = 0;
00149         pb = currentBuffer_;        // swap buffers
00150         currentBuffer_ = nextBuffer_;
00151         nextBuffer_ = pb;
00152 
00153         // see if we're done with this priority level
00154         if (currentEnd_ == 0) {
00155             threshold_ += priorityIncrement_;    // increment priority threshold
00156             currentEnd_ = overEnd_;    // set current to overflow block
00157             overEnd_ = 0;
00158             pb = currentBuffer_;        // swap buffers
00159             currentBuffer_ = overBuffer_;
00160             overBuffer_ = pb;
00161         }
00162 
00163         // check if we've hit the Start cell
00164         if (potential[startCell] < POT_HIGH)
00165             break;
00166     }
00167     //ROS_INFO("CYCLES %d/%d ", cycle, cycles);
00168     if (cycle < cycles)
00169         return true; // finished up here
00170     else
00171         return false;
00172 }
00173 
00174 //
00175 // Critical function: calculate updated potential value of a cell,
00176 //   given its neighbors' values
00177 // Planar-wave update calculation from two lowest neighbors in a 4-grid
00178 // Quadratic approximation to the interpolated value
00179 // No checking of bounds here, this function should be fast
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     // do planar wave update
00188     float c = getCost(costs, n);
00189     if (c >= lethal_cost_)    // don't propagate into obstacles
00190         return;
00191 
00192     float pot = p_calc_->calculatePotential(potential, c, n);
00193 
00194     // now add affected neighbors to priority blocks
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         //ROS_INFO("UPDATE %d %d %d %f", n, n%nx, n/nx, potential[n]);
00202         if (pot < threshold_)    // low-cost buffer block
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            // overflow block
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 } //end namespace global_planner


global_planner
Author(s): David Lu!!
autogenerated on Thu Aug 27 2015 14:07:53