src/lib/iterators/line_iterator.cpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Includes
6 *****************************************************************************/
7 
10 
11 
12 using namespace std;
13 
14 namespace cost_map {
15 
16 LineIterator::LineIterator(const cost_map::CostMap& cost_map, const Position& start, const Position& end)
17 {
18  Index startIndex, endIndex;
19  if (getIndexLimitedToMapRange(cost_map, start, end, startIndex) &&
20  getIndexLimitedToMapRange(cost_map, end, start, endIndex))
21  {
22  initialize(cost_map, startIndex, endIndex);
23  }
24 }
25 
26 LineIterator::LineIterator(const cost_map::CostMap& cost_map, const Index& start, const Index& end)
27 {
28  initialize(cost_map, start, end);
29 }
30 
31 LineIterator& LineIterator::operator =(const LineIterator& other)
32 {
33  index_ = other.index_;
34  start_ = other.start_;
35  end_ = other.end_;
36  iCell_ = other.iCell_;
37  nCells_ = other.nCells_;
38  increment1_ = other.increment1_;
39  increment2_ = other.increment2_;
40  denominator_ = other.denominator_;
41  numerator_ = other.numerator_;
42  numeratorAdd_ = other.numeratorAdd_;
43  mapLength_ = other.mapLength_;
44  mapPosition_ = other.mapPosition_;
45  resolution_ = other.resolution_;
46  bufferSize_ = other.bufferSize_;
47  bufferStartIndex_ = other.bufferStartIndex_;
48  return *this;
49 }
50 
51 bool LineIterator::operator !=(const LineIterator& other) const
52 {
53  return (index_ != other.index_).any();
54 }
55 
56 const Index& LineIterator::operator *() const
57 {
58  return index_;
59 }
60 
61 LineIterator& LineIterator::operator ++()
62 {
63  numerator_ += numeratorAdd_; // Increase the numerator by the top of the fraction
64  if (numerator_ >= denominator_) {
65  numerator_ -= denominator_;
66  index_ += increment1_;
67  }
68  index_ += increment2_;
69  ++iCell_;
70  return *this;
71 }
72 
73 bool LineIterator::isPastEnd() const
74 {
75  return iCell_ >= nCells_;
76 }
77 
78 bool LineIterator::initialize(const cost_map::CostMap& cost_map, const Index& start, const Index& end)
79 {
80  start_ = start;
81  end_ = end;
82  mapLength_ = cost_map.getLength();
83  mapPosition_ = cost_map.getPosition();
84  resolution_ = cost_map.getResolution();
85  bufferSize_ = cost_map.getSize();
86  bufferStartIndex_ = cost_map.getStartIndex();
87  Index submapStartIndex;
88  Size submapBufferSize;
89  initializeIterationParameters();
90  return true;
91 }
92 
93 bool LineIterator::getIndexLimitedToMapRange(const cost_map::CostMap& cost_map,
94  const Position& start, const Position& end,
95  Index& index)
96 {
97  Position newStart = start;
98  Vector direction = (end - start).normalized();
99  while (!cost_map.getIndex(newStart, index)) {
100  newStart += (cost_map.getResolution() - std::numeric_limits<double>::epsilon()) * direction;
101  if ((end - newStart).norm() < cost_map.getResolution() - std::numeric_limits<double>::epsilon())
102  return false;
103  }
104  return true;
105 }
106 
107 void LineIterator::initializeIterationParameters()
108 {
109  iCell_ = 0;
110  index_ = start_;
111 
112  Size delta = (end_ - start_).abs();
113 
114  if (end_.x() >= start_.x()) {
115  // x-values increasing.
116  increment1_.x() = 1;
117  increment2_.x() = 1;
118  } else {
119  // x-values decreasing.
120  increment1_.x() = -1;
121  increment2_.x() = -1;
122  }
123 
124  if (end_.y() >= start_.y()) {
125  // y-values increasing.
126  increment1_.y() = 1;
127  increment2_.y() = 1;
128  } else {
129  // y-values decreasing.
130  increment1_.y() = -1;
131  increment2_.y() = -1;
132  }
133 
134  if (delta.x() >= delta.y()) {
135  // There is at least one x-value for every y-value.
136  increment1_.x() = 0; // Do not change the x when numerator >= denominator.
137  increment2_.y() = 0; // Do not change the y for every iteration.
138  denominator_ = delta.x();
139  numerator_ = delta.x() / 2;
140  numeratorAdd_ = delta.y();
141  nCells_ = delta.x() + 1; // There are more x-values than y-values.
142  } else {
143  // There is at least one y-value for every x-value
144  increment2_.x() = 0; // Do not change the x for every iteration.
145  increment1_.y() = 0; // Do not change the y when numerator >= denominator.
146  denominator_ = delta.y();
147  numerator_ = delta.y() / 2;
148  numeratorAdd_ = delta.x();
149  nCells_ = delta.y() + 1; // There are more y-values than x-values.
150  }
151 }
152 
153 } /* namespace cost_map */
const Length & getLength() const
Length mapLength_
Map information needed to get position from iterator.
const Size & getSize() const
double getResolution() const
Index start_
Starting index of the line.
Index index_
Current index.
grid_map::Size Size
Definition: common.hpp:46
Index end_
Ending index of the line.
grid_map::Vector Vector
Definition: common.hpp:42
Size increment1_
Helper variables for Bresenham Line Drawing algorithm.
bool getPosition(const Index &index, Position &position) const
grid_map::Index Index
Definition: common.hpp:45
grid_map::Position Position
Definition: common.hpp:41
unsigned int nCells_
Number of cells in the line.
unsigned int iCell_
Current cell number.
const Index & getStartIndex() const
bool getIndex(const Position &position, Index &index) const


cost_map_core
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:03:41