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 namespace grid_map {
13 
15  const Position& end)
16 {
17  Index startIndex;
18  Index endIndex;
19  if (getIndexLimitedToMapRange(gridMap, start, end, startIndex)
20  && getIndexLimitedToMapRange(gridMap, end, start, endIndex)) {
21  initialize(gridMap, startIndex, endIndex);
22  }
23  else {
24  throw std::invalid_argument("Failed to construct LineIterator.");
25  }
26 }
27 
28 LineIterator::LineIterator(const grid_map::GridMap& gridMap, const Index& start, const Index& end)
29 {
30  initialize(gridMap, start, end);
31 }
32 
33 bool LineIterator::operator !=(const LineIterator& other) const
34 {
35  return (index_ != other.index_).any();
36 }
37 
39 {
40  return index_;
41 }
42 
44 {
45  numerator_ += numeratorAdd_; // Increase the numerator by the top of the fraction.
46  if (numerator_ >= denominator_) {
50  }
53  ++iCell_;
54  return *this;
55 }
56 
58 {
59  return iCell_ >= nCells_;
60 }
61 
62 bool LineIterator::initialize(const grid_map::GridMap& gridMap, const Index& start, const Index& end)
63 {
64  start_ = start;
65  end_ = end;
66  mapLength_ = gridMap.getLength();
67  mapPosition_ = gridMap.getPosition();
68  resolution_ = gridMap.getResolution();
69  bufferSize_ = gridMap.getSize();
70  bufferStartIndex_ = gridMap.getStartIndex();
72  return true;
73 }
74 
76  const Position& start, const Position& end,
77  Index& index)
78 {
79  Position newStart = start;
80  Vector direction = (end - start).normalized();
81  while (!gridMap.getIndex(newStart, index)) {
82  newStart += (gridMap.getResolution() - std::numeric_limits<double>::epsilon()) * direction;
83  if ((end - newStart).norm() < gridMap.getResolution() - std::numeric_limits<double>::epsilon()) {
84  return false;
85  }
86  }
87  return true;
88 }
89 
91 {
92  iCell_ = 0;
93  index_ = start_;
94 
97  const Size delta = (unwrappedEnd - unwrappedStart).abs();
98 
99  if (unwrappedEnd.x() >= unwrappedStart.x()) {
100  // x-values increasing.
101  increment1_.x() = 1;
102  increment2_.x() = 1;
103  } else {
104  // x-values decreasing.
105  increment1_.x() = -1;
106  increment2_.x() = -1;
107  }
108 
109  if (unwrappedEnd.y() >= unwrappedStart.y()) {
110  // y-values increasing.
111  increment1_.y() = 1;
112  increment2_.y() = 1;
113  } else {
114  // y-values decreasing.
115  increment1_.y() = -1;
116  increment2_.y() = -1;
117  }
118 
119  if (delta.x() >= delta.y()) {
120  // There is at least one x-value for every y-value.
121  increment1_.x() = 0; // Do not change the x when numerator >= denominator.
122  increment2_.y() = 0; // Do not change the y for every iteration.
123  denominator_ = delta.x();
124  numerator_ = delta.x() / 2;
125  numeratorAdd_ = delta.y();
126  nCells_ = delta.x() + 1; // There are more x-values than y-values.
127  } else {
128  // There is at least one y-value for every x-value
129  increment2_.x() = 0; // Do not change the x for every iteration.
130  increment1_.y() = 0; // Do not change the y when numerator >= denominator.
131  denominator_ = delta.y();
132  numerator_ = delta.y() / 2;
133  numeratorAdd_ = delta.x();
134  nCells_ = delta.y() + 1; // There are more y-values than x-values.
135  }
136 }
137 
138 } /* namespace grid_map */
bool operator!=(const LineIterator &other) const
unsigned int iCell_
Current cell number.
unsigned int nCells_
Number of cells in the line.
const Index & getStartIndex() const
Definition: GridMap.cpp:669
Index getIndexFromBufferIndex(const Index &bufferIndex, const Size &bufferSize, const Index &bufferStartIndex)
Index end_
Ending index of the line.
const Size & getSize() const
Definition: GridMap.cpp:661
LineIterator(const grid_map::GridMap &gridMap, const Position &start, const Position &end)
double getResolution() const
Definition: GridMap.cpp:657
Length mapLength_
Map information needed to get position from iterator.
static bool getIndexLimitedToMapRange(const grid_map::GridMap &gridMap, const Position &start, const Position &end, Index &index)
const Index & operator*() const
Eigen::Vector2d Position
Definition: TypeDefs.hpp:18
Eigen::Array2i Index
Definition: TypeDefs.hpp:22
Eigen::Array2i Size
Definition: TypeDefs.hpp:23
LineIterator & operator++()
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:228
bool getPosition(const Index &index, Position &position) const
Definition: GridMap.cpp:232
bool initialize(const grid_map::GridMap &gridMap, const Index &start, const Index &end)
void initializeIterationParameters()
Eigen::Vector2d Vector
Definition: TypeDefs.hpp:19
const Length & getLength() const
Definition: GridMap.cpp:649
Index index_
Current index.


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Wed Jul 5 2023 02:23:35