line_iterator.h
Go to the documentation of this file.
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 LINE_ITERATOR_H
30 #define LINE_ITERATOR_H
31 
32 #include <stdlib.h>
33 
34 namespace base_local_planner
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  {
54  xinc1_ = 1;
55  xinc2_ = 1;
56  }
57  else // The x-values are decreasing
58  {
59  xinc1_ = -1;
60  xinc2_ = -1;
61  }
62 
63  if( y1_ >= y0_) // The y-values are increasing
64  {
65  yinc1_ = 1;
66  yinc2_ = 1;
67  }
68  else // The y-values are decreasing
69  {
70  yinc1_ = -1;
71  yinc2_ = -1;
72  }
73 
74  if( deltax_ >= deltay_ ) // There is at least one x-value for every y-value
75  {
76  xinc1_ = 0; // Don't change the x when numerator >= denominator
77  yinc2_ = 0; // Don't change the y for every iteration
78  den_ = deltax_;
79  num_ = deltax_ / 2;
80  numadd_ = deltay_;
81  numpixels_ = deltax_; // There are more x-values than y-values
82  }
83  else // There is at least one y-value for every x-value
84  {
85  xinc2_ = 0; // Don't change the x for every iteration
86  yinc1_ = 0; // Don't change the y when numerator >= denominator
87  den_ = deltay_;
88  num_ = deltay_ / 2;
89  numadd_ = deltax_;
90  numpixels_ = deltay_; // There are more y-values than x-values
91  }
92  }
93 
94  bool isValid() const
95  {
96  return curpixel_ <= numpixels_;
97  }
98 
99  void advance()
100  {
101  num_ += numadd_; // Increase the numerator by the top of the fraction
102  if( num_ >= den_ ) // Check if numerator >= denominator
103  {
104  num_ -= den_; // Calculate the new numerator value
105  x_ += xinc1_; // Change the x as appropriate
106  y_ += yinc1_; // Change the y as appropriate
107  }
108  x_ += xinc2_; // Change the x as appropriate
109  y_ += yinc2_; // Change the y as appropriate
110 
111  curpixel_++;
112  }
113 
114  int getX() const { return x_; }
115  int getY() const { return y_; }
116 
117  int getX0() const { return x0_; }
118  int getY0() const { return y0_; }
119 
120  int getX1() const { return x1_; }
121  int getY1() const { return y1_; }
122 
123 private:
124  int x0_;
125  int y0_;
126  int x1_;
127  int y1_;
128 
129  int x_;
130  int y_;
131 
132  int deltax_;
133  int deltay_;
134 
135  int curpixel_;
136 
139 };
140 
141 } // end namespace base_local_planner
142 
143 #endif // LINE_ITERATOR_H
base_local_planner::LineIterator::num_
int num_
Definition: line_iterator.h:138
base_local_planner::LineIterator::yinc1_
int yinc1_
Definition: line_iterator.h:137
base_local_planner::LineIterator::getY
int getY() const
Definition: line_iterator.h:115
base_local_planner::LineIterator::deltay_
int deltay_
Difference between Ys of endpoints.
Definition: line_iterator.h:133
base_local_planner::LineIterator::getX0
int getX0() const
Definition: line_iterator.h:117
base_local_planner::LineIterator::xinc1_
int xinc1_
Definition: line_iterator.h:137
base_local_planner::LineIterator::numadd_
int numadd_
Definition: line_iterator.h:138
base_local_planner::LineIterator::x0_
int x0_
X coordinate of first end point.
Definition: line_iterator.h:124
base_local_planner::LineIterator::x1_
int x1_
X coordinate of second end point.
Definition: line_iterator.h:126
base_local_planner::LineIterator::LineIterator
LineIterator(int x0, int y0, int x1, int y1)
Definition: line_iterator.h:41
base_local_planner::LineIterator::y1_
int y1_
Y coordinate of second end point.
Definition: line_iterator.h:127
base_local_planner::LineIterator::deltax_
int deltax_
Difference between Xs of endpoints.
Definition: line_iterator.h:132
base_local_planner::LineIterator::getX
int getX() const
Definition: line_iterator.h:114
base_local_planner::LineIterator::x_
int x_
X coordinate of current point.
Definition: line_iterator.h:129
base_local_planner::LineIterator::getX1
int getX1() const
Definition: line_iterator.h:120
base_local_planner::LineIterator::isValid
bool isValid() const
Definition: line_iterator.h:94
base_local_planner::LineIterator::numpixels_
int numpixels_
Definition: line_iterator.h:138
base_local_planner::LineIterator::getY1
int getY1() const
Definition: line_iterator.h:121
base_local_planner::LineIterator::advance
void advance()
Definition: line_iterator.h:99
base_local_planner::LineIterator::den_
int den_
Definition: line_iterator.h:138
base_local_planner::LineIterator::xinc2_
int xinc2_
Definition: line_iterator.h:137
base_local_planner::LineIterator::curpixel_
int curpixel_
index of current point in line loop.
Definition: line_iterator.h:135
base_local_planner::LineIterator
Definition: line_iterator.h:38
base_local_planner::LineIterator::y0_
int y0_
Y coordinate of first end point.
Definition: line_iterator.h:125
base_local_planner::LineIterator::y_
int y_
Y coordinate of current point.
Definition: line_iterator.h:130
base_local_planner::LineIterator::yinc2_
int yinc2_
Definition: line_iterator.h:137
base_local_planner
Definition: costmap_model.h:44
base_local_planner::LineIterator::getY0
int getY0() const
Definition: line_iterator.h:118


base_local_planner
Author(s): Eitan Marder-Eppstein, Eric Perko, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:24