Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
line_iterator.hpp
1 // Copyright (c) 2012, Willow Garage, Inc.
2 // All rights reserved.
3 //
4 // Software License Agreement (BSD License 2.0)
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of the Willow Garage, Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 #ifndef NAV2_UTIL__LINE_ITERATOR_HPP_
34 #define NAV2_UTIL__LINE_ITERATOR_HPP_
35 
36 #include <stdlib.h>
37 
38 namespace nav2_util
39 {
40 
41 
47 {
48 public:
56  LineIterator(int x0, int y0, int x1, int y1)
57  : x0_(x0),
58  y0_(y0),
59  x1_(x1),
60  y1_(y1),
61  x_(x0), // X and Y start of at first endpoint.
62  y_(y0),
63  deltax_(abs(x1 - x0)),
64  deltay_(abs(y1 - y0)),
65  curpixel_(0)
66  {
67  if (x1_ >= x0_) { // The x-values are increasing
68  xinc1_ = 1;
69  xinc2_ = 1;
70  } else { // The x-values are decreasing
71  xinc1_ = -1;
72  xinc2_ = -1;
73  }
74 
75  if (y1_ >= y0_) { // The y-values are increasing
76  yinc1_ = 1;
77  yinc2_ = 1;
78  } else { // The y-values are decreasing
79  yinc1_ = -1;
80  yinc2_ = -1;
81  }
82 
83  if (deltax_ >= deltay_) { // There is at least one x-value for every y-value
84  xinc1_ = 0; // Don't change the x when numerator >= denominator
85  yinc2_ = 0; // Don't change the y for every iteration
86  den_ = deltax_;
87  num_ = deltax_ / 2;
88  numadd_ = deltay_;
89  numpixels_ = deltax_; // There are more x-values than y-values
90  } else { // There is at least one y-value for every x-value
91  xinc2_ = 0; // Don't change the x for every iteration
92  yinc1_ = 0; // Don't change the y when numerator >= denominator
93  den_ = deltay_;
94  num_ = deltay_ / 2;
95  numadd_ = deltax_;
96  numpixels_ = deltay_; // There are more y-values than x-values
97  }
98  }
99 
104  bool isValid() const
105  {
106  return curpixel_ <= numpixels_;
107  }
108 
112  void advance()
113  {
114  num_ += numadd_; // Increase the numerator by the top of the fraction
115  if (num_ >= den_) { // Check if numerator >= denominator
116  num_ -= den_; // Calculate the new numerator value
117  x_ += xinc1_; // Change the x as appropriate
118  y_ += yinc1_; // Change the y as appropriate
119  }
120  x_ += xinc2_; // Change the x as appropriate
121  y_ += yinc2_; // Change the y as appropriate
122 
123  curpixel_++;
124  }
125 
130  int getX() const
131  {
132  return x_;
133  }
134 
139  int getY() const
140  {
141  return y_;
142  }
143 
148  int getX0() const
149  {
150  return x0_;
151  }
152 
157  int getY0() const
158  {
159  return y0_;
160  }
161 
166  int getX1() const
167  {
168  return x1_;
169  }
170 
175  int getY1() const
176  {
177  return y1_;
178  }
179 
180 private:
181  int x0_;
182  int y0_;
183  int x1_;
184  int y1_;
185 
186  int x_;
187  int y_;
188 
189  int deltax_;
190  int deltay_;
191 
192  int curpixel_;
193 
194  int xinc1_, xinc2_, yinc1_, yinc2_;
195  int den_, num_, numadd_, numpixels_;
196 };
197 
198 } // end namespace nav2_util
199 
200 #endif // NAV2_UTIL__LINE_ITERATOR_HPP_
An iterator implementing Bresenham Ray-Tracing.
bool isValid() const
If the iterator is valid.
int getX() const
Get current X value.
int getX1() const
Get terminal X value.
void advance()
Advance iteration along the line.
int getY0() const
Get initial Y value.
int getY() const
Get current Y value.
LineIterator(int x0, int y0, int x1, int y1)
A constructor for LineIterator.
int getY1() const
Get terminal Y value.
int getX0() const
Get initial X value.