costmap_queue.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2017, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 #include <costmap_queue/costmap_queue.h>
00035 #include <algorithm>
00036 #include <vector>
00037 
00038 namespace costmap_queue
00039 {
00040 
00041 CostmapQueue::CostmapQueue(nav_core2::Costmap& costmap, bool manhattan) :
00042   MapBasedQueue(false), costmap_(costmap), seen_(0), manhattan_(manhattan), cached_max_distance_(-1)
00043 {
00044   reset();
00045 }
00046 
00047 void CostmapQueue::reset()
00048 {
00049   seen_.setInfo(costmap_.getInfo());
00050   seen_.reset();
00051   computeCache();
00052   MapBasedQueue::reset();
00053 }
00054 
00055 void CostmapQueue::enqueueCell(unsigned int x, unsigned int y)
00056 {
00057   enqueueCell(x, y, x, y);
00058 }
00059 
00060 void CostmapQueue::enqueueCell(unsigned int cur_x, unsigned int cur_y,
00061                                unsigned int src_x, unsigned int src_y)
00062 {
00063   if (seen_(cur_x, cur_y)) return;
00064 
00065   // we compute our distance table one cell further than the inflation radius dictates so we can make the check below
00066   double distance = distanceLookup(cur_x, cur_y, src_x, src_y);
00067   CellData data(distance, cur_x, cur_y, src_x, src_y);
00068   if (validCellToQueue(data))
00069   {
00070     seen_.setValue(cur_x, cur_y, 1);
00071     enqueue(distance, data);
00072   }
00073 }
00074 
00075 CellData CostmapQueue::getNextCell()
00076 {
00077   // get the highest priority cell and pop it off the priority queue
00078   CellData current_cell = front();
00079   pop();
00080 
00081   unsigned int mx = current_cell.x_;
00082   unsigned int my = current_cell.y_;
00083   unsigned int sx = current_cell.src_x_;
00084   unsigned int sy = current_cell.src_y_;
00085 
00086   // attempt to put the neighbors of the current cell onto the queue
00087   if (mx > 0)
00088     enqueueCell(mx - 1, my, sx, sy);
00089   if (my > 0)
00090     enqueueCell(mx, my - 1, sx, sy);
00091   if (mx < costmap_.getWidth() - 1)
00092     enqueueCell(mx + 1, my, sx, sy);
00093   if (my < costmap_.getHeight() - 1)
00094     enqueueCell(mx, my + 1, sx, sy);
00095 
00096   return current_cell;
00097 }
00098 
00099 void CostmapQueue::computeCache()
00100 {
00101   int max_distance = getMaxDistance();
00102   if (max_distance == cached_max_distance_) return;
00103   cached_distances_.clear();
00104 
00105   cached_distances_.resize(max_distance + 2);
00106 
00107   for (unsigned int i = 0; i < cached_distances_.size(); ++i)
00108   {
00109     cached_distances_[i].resize(max_distance + 2);
00110     for (unsigned int j = 0; j < cached_distances_[i].size(); ++j)
00111     {
00112       if (manhattan_)
00113       {
00114         cached_distances_[i][j] = i + j;
00115       }
00116       else
00117       {
00118         cached_distances_[i][j] = hypot(i, j);
00119       }
00120     }
00121   }
00122   cached_max_distance_ = max_distance;
00123 }
00124 
00125 }  // namespace costmap_queue


costmap_queue
Author(s):
autogenerated on Wed Jun 26 2019 20:09:33