circle_outline.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, 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 
00035 #include <nav_grid_iterators/circle_outline.h>
00036 #include <nav_grid/coordinate_conversion.h>
00037 
00038 namespace nav_grid_iterators
00039 {
00040 CircleOutline::CircleOutline(const nav_grid::NavGridInfo* info, double center_x, double center_y, double radius)
00041   : CircleOutline(info, center_x, center_y, static_cast<unsigned int>(ceil(radius / info->resolution)))
00042 {
00043 }
00044 
00045 CircleOutline::CircleOutline(const nav_grid::NavGridInfo* info, double center_x, double center_y, unsigned int radius)
00046   : BaseIterator(info), distance_(radius), init_(false)
00047 {
00048   signed_width_ = static_cast<int>(info->width);
00049   signed_height_ = static_cast<int>(info->height);
00050 
00051   // Calculate and save the center coordinates
00052   worldToGrid(*info_, center_x, center_y, center_index_x_, center_index_y_);
00053 
00054   point_x_ = distance_;
00055   point_y_ = 0;
00056 
00057   if (!isValidIndex(center_index_x_ + point_x_, center_index_y_ + point_y_))
00058   {
00059     increment();
00060     init_ = !isValidIndex(center_index_x_ + point_x_, center_index_y_ + point_y_);
00061   }
00062   index_.x = center_index_x_ + point_x_;
00063   index_.y = center_index_y_ + point_y_;
00064   start_index_ = index_;
00065 }
00066 
00067 CircleOutline::CircleOutline(const nav_grid::NavGridInfo* info, const nav_grid::Index& index,
00068                              int center_index_x, int center_index_y, unsigned int distance,
00069                              bool init, const nav_grid::Index& start_index)
00070   : BaseIterator(info, index),
00071     center_index_x_(center_index_x), center_index_y_(center_index_y), distance_(distance), init_(init),
00072     start_index_(start_index)
00073 {
00074   signed_width_ = static_cast<int>(info->width);
00075   signed_height_ = static_cast<int>(info->height);
00076   point_x_ = distance_;
00077   point_y_ = 0;
00078 }
00079 
00080 CircleOutline CircleOutline::begin() const
00081 {
00082   return CircleOutline(info_, start_index_, center_index_x_, center_index_y_,
00083                        distance_, false, start_index_);
00084 }
00085 
00086 CircleOutline CircleOutline::end() const
00087 {
00088   return CircleOutline(info_, start_index_, center_index_x_, center_index_y_,
00089                        distance_, true, start_index_);
00090 }
00091 
00092 void CircleOutline::increment()
00093 {
00094   init_ = true;
00095   while (true)
00096   {
00097     int nx = -signum(point_y_);
00098     int ny = signum(point_x_);
00099     if (nx != 0 && isOnOutline(point_x_ + nx, point_y_))
00100     {
00101       point_x_ += nx;
00102     }
00103     else if (ny != 0 && isOnOutline(point_x_, point_y_ + ny))
00104     {
00105       point_y_ += ny;
00106     }
00107     else
00108     {
00109       point_x_ += nx;
00110       point_y_ += ny;
00111     }
00112 
00113     if (isValidIndex(center_index_x_ + point_x_, center_index_y_ + point_y_))
00114     {
00115       break;
00116     }
00117     if (point_x_ == static_cast<int>(distance_) && point_y_ == 0)
00118     {
00119       index_ = start_index_;
00120       return;
00121     }
00122   }
00123   index_.x = center_index_x_ + point_x_;
00124   index_.y = center_index_y_ + point_y_;
00125 }
00126 
00127 bool CircleOutline::fieldsEqual(const CircleOutline& other)
00128 {
00129   return center_index_x_ == other.center_index_x_ && center_index_y_ == other.center_index_y_ &&
00130          distance_ == other.distance_ && init_ == other.init_;
00131 }
00132 
00133 bool CircleOutline::isValidIndex(int x, int y) const
00134 {
00135   return x >= 0 && y >= 0 && x < signed_width_ && y < signed_height_;
00136 }
00137 
00138 bool CircleOutline::isOnOutline(int dx, int dy) const
00139 {
00140   return static_cast<unsigned int>(hypot(dx, dy)) == distance_;
00141 }
00142 
00143 }  // namespace nav_grid_iterators


nav_grid_iterators
Author(s):
autogenerated on Wed Jun 26 2019 20:09:45