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


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Tue Jun 25 2019 20:02:08