polygon_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/polygon_outline.h>
00036 #include <nav_grid/coordinate_conversion.h>
00037 #include <nav_2d_utils/polygons.h>
00038 
00039 namespace nav_grid_iterators
00040 {
00041 PolygonOutline::PolygonOutline(const nav_grid::NavGridInfo* info, nav_2d_msgs::Polygon2D polygon, bool bresenham)
00042   : BaseIterator(info), polygon_(polygon), start_index_(0, 0), bresenham_(bresenham), side_index_(0)
00043 {
00044   if (polygon.points.size() == 0)
00045   {
00046     internal_iterator_.reset(new Line(info_, 0.0, 0.0, 0.0, 0.0, false, bresenham_));
00047     index_ = **internal_iterator_;
00048     start_index_ = index_;
00049     return;
00050   }
00051   loadSide();
00052   index_ = **internal_iterator_;
00053   start_index_ = index_;
00054 }
00055 
00056 PolygonOutline::PolygonOutline(const PolygonOutline& other)
00057   : PolygonOutline(other.info_, other.index_, other.polygon_, other.bresenham_, other.side_index_)
00058 {
00059 }
00060 
00061 PolygonOutline::PolygonOutline(const nav_grid::NavGridInfo* info, const nav_grid::Index& index,
00062                                nav_2d_msgs::Polygon2D polygon, bool bresenham, unsigned int side_index)
00063   : BaseIterator(info, index), polygon_(polygon), start_index_(index), bresenham_(bresenham), side_index_(side_index)
00064 {
00065   loadSide();
00066 }
00067 
00068 PolygonOutline& PolygonOutline::operator=(const PolygonOutline& other)
00069 {
00070   info_ = other.info_;
00071   index_ = other.index_;
00072   polygon_ = other.polygon_;
00073   bresenham_ = other.bresenham_;
00074   side_index_ = other.side_index_;
00075   loadSide();
00076   return *this;
00077 }
00078 
00079 void PolygonOutline::loadSide()
00080 {
00081   while (side_index_ < polygon_.points.size())
00082   {
00083     // The next index loops around to the first index
00084     unsigned int next_index = side_index_ + 1;
00085     if (next_index == polygon_.points.size())
00086     {
00087       next_index = 0;
00088     }
00089 
00090     internal_iterator_.reset(new Line(info_,
00091                                       polygon_.points[side_index_].x, polygon_.points[side_index_].y,
00092                                       polygon_.points[next_index].x, polygon_.points[next_index].y,
00093                                       false, bresenham_));
00094 
00095     if (*internal_iterator_ != internal_iterator_->end())
00096       break;
00097     ++side_index_;
00098   }
00099 }
00100 
00101 PolygonOutline PolygonOutline::begin() const
00102 {
00103   return PolygonOutline(info_, start_index_, polygon_, bresenham_, 0);
00104 }
00105 
00106 PolygonOutline PolygonOutline::end() const
00107 {
00108   // Since the polygon outline loops around to the original index when it is complete
00109   // the end iterator is represented by having the current side to be the invalid
00110   return PolygonOutline(info_, start_index_, polygon_, bresenham_, polygon_.points.size());
00111 }
00112 
00113 void PolygonOutline::increment()
00114 {
00115   ++(*internal_iterator_);
00116   if (*internal_iterator_ == internal_iterator_->end())
00117   {
00118     ++side_index_;
00119     if (side_index_ == polygon_.points.size())
00120     {
00121       index_ = start_index_;
00122       return;
00123     }
00124     loadSide();
00125   }
00126   index_ = **internal_iterator_;
00127 }
00128 
00129 bool PolygonOutline::fieldsEqual(const PolygonOutline& other)
00130 {
00131   return side_index_ == other.side_index_ && nav_2d_utils::equals(polygon_, other.polygon_) &&
00132          bresenham_ == other.bresenham_;
00133 }
00134 
00135 }  // namespace nav_grid_iterators


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