polygon_outline.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 #include <nav_2d_utils/polygons.h>
38 
39 namespace nav_grid_iterators
40 {
41 PolygonOutline::PolygonOutline(const nav_grid::NavGridInfo* info, nav_2d_msgs::Polygon2D polygon, bool bresenham)
42  : BaseIterator(info), polygon_(polygon), start_index_(0, 0), bresenham_(bresenham), side_index_(0)
43 {
44  if (polygon.points.size() == 0)
45  {
46  internal_iterator_.reset(new Line(info_, 0.0, 0.0, 0.0, 0.0, false, bresenham_));
49  return;
50  }
51  loadSide();
54 }
55 
57  : PolygonOutline(other.info_, other.index_, other.polygon_, other.bresenham_, other.side_index_)
58 {
59 }
60 
62  nav_2d_msgs::Polygon2D polygon, bool bresenham, unsigned int side_index)
63  : BaseIterator(info, index), polygon_(polygon), start_index_(index), bresenham_(bresenham), side_index_(side_index)
64 {
65  loadSide();
66 }
67 
69 {
70  info_ = other.info_;
71  index_ = other.index_;
72  polygon_ = other.polygon_;
73  bresenham_ = other.bresenham_;
74  side_index_ = other.side_index_;
75  loadSide();
76  return *this;
77 }
78 
80 {
81  while (side_index_ < polygon_.points.size())
82  {
83  // The next index loops around to the first index
84  unsigned int next_index = side_index_ + 1;
85  if (next_index == polygon_.points.size())
86  {
87  next_index = 0;
88  }
89 
90  internal_iterator_.reset(new Line(info_,
91  polygon_.points[side_index_].x, polygon_.points[side_index_].y,
92  polygon_.points[next_index].x, polygon_.points[next_index].y,
93  false, bresenham_));
94 
96  break;
97  ++side_index_;
98  }
99 }
100 
102 {
104 }
105 
107 {
108  // Since the polygon outline loops around to the original index when it is complete
109  // the end iterator is represented by having the current side to be the invalid
110  return PolygonOutline(info_, start_index_, polygon_, bresenham_, polygon_.points.size());
111 }
112 
114 {
115  ++(*internal_iterator_);
116  if (*internal_iterator_ == internal_iterator_->end())
117  {
118  ++side_index_;
119  if (side_index_ == polygon_.points.size())
120  {
122  return;
123  }
124  loadSide();
125  }
127 }
128 
130 {
131  return side_index_ == other.side_index_ && nav_2d_utils::equals(polygon_, other.polygon_) &&
132  bresenham_ == other.bresenham_;
133 }
134 
135 } // namespace nav_grid_iterators
std::unique_ptr< Line > internal_iterator_
bool equals(const nav_2d_msgs::Polygon2D &polygon0, const nav_2d_msgs::Polygon2D &polygon1)
void loadSide()
Given a new side index, loads the internal iterator. If there are no valid values in the internal ite...
Iterates over all of the valid indexes on the outline of a polygon.
bool fieldsEqual(const PolygonOutline &other) override
Additional check for whether fields of derived iterators are equal.
PolygonOutline(const nav_grid::NavGridInfo *info, nav_2d_msgs::Polygon2D polygon, bool bresenham=true)
Public Constructor.
Iterates over all of the valid indexes of a line.
Definition: line.h:48
nav_2d_msgs::Polygon2D polygon_
void increment() override
Increase the iterator to the next element.
PolygonOutline begin() const override
Helper function for range-style iteration Equivalent to the above constructor.
PolygonOutline & operator=(const PolygonOutline &other)
Assignment Operator Required to ensure unique_ptr is set properly.
PolygonOutline end() const override
Helper function for range-style iteration.


nav_grid_iterators
Author(s):
autogenerated on Sun Jan 10 2021 04:08:42