Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
line_iterator.hpp
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef DWB_CRITICS__LINE_ITERATOR_HPP_
30 #define DWB_CRITICS__LINE_ITERATOR_HPP_
31 
32 #include <stdlib.h>
33 
34 namespace dwb_critics
35 {
36 
39 {
40 public:
41  LineIterator(int x0, int y0, int x1, int y1)
42  : x0_(x0),
43  y0_(y0),
44  x1_(x1),
45  y1_(y1),
46  x_(x0), // X and Y start of at first endpoint.
47  y_(y0),
48  deltax_(abs(x1 - x0)),
49  deltay_(abs(y1 - y0)),
50  curpixel_(0)
51  {
52  if (x1_ >= x0_) { // The x-values are increasing
53  xinc1_ = 1;
54  xinc2_ = 1;
55  } else { // The x-values are decreasing
56  xinc1_ = -1;
57  xinc2_ = -1;
58  }
59 
60  if (y1_ >= y0_) { // The y-values are increasing
61  yinc1_ = 1;
62  yinc2_ = 1;
63  } else { // The y-values are decreasing
64  yinc1_ = -1;
65  yinc2_ = -1;
66  }
67 
68  if (deltax_ >= deltay_) { // There is at least one x-value for every y-value
69  xinc1_ = 0; // Don't change the x when numerator >= denominator
70  yinc2_ = 0; // Don't change the y for every iteration
71  den_ = deltax_;
72  num_ = deltax_ / 2;
73  numadd_ = deltay_;
74  numpixels_ = deltax_; // There are more x-values than y-values
75  } else { // There is at least one y-value for every x-value
76  xinc2_ = 0; // Don't change the x for every iteration
77  yinc1_ = 0; // Don't change the y when numerator >= denominator
78  den_ = deltay_;
79  num_ = deltay_ / 2;
80  numadd_ = deltax_;
81  numpixels_ = deltay_; // There are more y-values than x-values
82  }
83  }
84 
85  bool isValid() const
86  {
87  return curpixel_ <= numpixels_;
88  }
89 
90  void advance()
91  {
92  num_ += numadd_; // Increase the numerator by the top of the fraction
93  if (num_ >= den_) { // Check if numerator >= denominator
94  num_ -= den_; // Calculate the new numerator value
95  x_ += xinc1_; // Change the x as appropriate
96  y_ += yinc1_; // Change the y as appropriate
97  }
98  x_ += xinc2_; // Change the x as appropriate
99  y_ += yinc2_; // Change the y as appropriate
100 
101  curpixel_++;
102  }
103 
104  int getX() const
105  {
106  return x_;
107  }
108  int getY() const
109  {
110  return y_;
111  }
112 
113  int getX0() const
114  {
115  return x0_;
116  }
117  int getY0() const
118  {
119  return y0_;
120  }
121 
122  int getX1() const
123  {
124  return x1_;
125  }
126  int getY1() const
127  {
128  return y1_;
129  }
130 
131 private:
132  int x0_;
133  int y0_;
134  int x1_;
135  int y1_;
136 
137  int x_;
138  int y_;
139 
140  int deltax_;
141  int deltay_;
142 
143  int curpixel_;
144 
145  int xinc1_, xinc2_, yinc1_, yinc2_;
146  int den_, num_, numadd_, numpixels_;
147 };
148 
149 } // end namespace dwb_critics
150 
151 #endif // DWB_CRITICS__LINE_ITERATOR_HPP_