SpiralIterator.cpp
Go to the documentation of this file.
1 /*
2  * SpiralIterator.hpp
3  *
4  * Created on: Jul 7, 2015
5  * Author: Martin Wermelinger
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
11 
12 #include <cmath>
13 #include <utility>
14 
15 
16 namespace grid_map {
17 
18 SpiralIterator::SpiralIterator(const grid_map::GridMap& gridMap, Eigen::Vector2d center,
19  const double radius)
20  : center_(std::move(center)),
21  radius_(radius),
22  distance_(0)
23 {
25  mapLength_ = gridMap.getLength();
26  mapPosition_ = gridMap.getPosition();
27  resolution_ = gridMap.getResolution();
28  bufferSize_ = gridMap.getSize();
29  gridMap.getIndex(center_, indexCenter_);
30  nRings_ = std::ceil(radius_ / resolution_);
32  pointsRing_.push_back(indexCenter_);
33  } else {
34  while (pointsRing_.empty() && !isPastEnd()) {
35  generateRing();
36  }
37  }
38 }
39 
40 bool SpiralIterator::operator !=(const SpiralIterator& /*other*/) const
41 {
42  return (pointsRing_.back() != pointsRing_.back()).any();
43 }
44 
45 const Eigen::Array2i& SpiralIterator::operator *() const
46 {
47  return pointsRing_.back();
48 }
49 
51 {
52  pointsRing_.pop_back();
53  if (pointsRing_.empty() && !isPastEnd()) {
54  generateRing();
55  }
56  return *this;
57 }
58 
60 {
61  return (distance_ == nRings_ && pointsRing_.empty());
62 }
63 
64 bool SpiralIterator::isInside(const Index& index) const
65 {
66  Eigen::Vector2d position;
68  double squareNorm = (position - center_).array().square().sum();
69  return (squareNorm <= radiusSquare_);
70 }
71 
73 {
74  distance_++;
75  Index point(distance_, 0);
76  Index pointInMap;
77  Index normal;
78  do {
79  pointInMap.x() = point.x() + indexCenter_.x();
80  pointInMap.y() = point.y() + indexCenter_.y();
81  if (checkIfIndexInRange(pointInMap, bufferSize_)) {
82  if (distance_ == nRings_ || distance_ == nRings_ - 1) {
83  if (isInside(pointInMap)) {
84  pointsRing_.push_back(pointInMap);
85  }
86  } else {
87  pointsRing_.push_back(pointInMap);
88  }
89  }
90  normal.x() = -signum(point.y());
91  normal.y() = signum(point.x());
92  if (normal.x() != 0 && static_cast<unsigned int>(Vector(point.x() + normal.x(), point.y()).norm()) == distance_) {
93  point.x() += normal.x();
94  } else if (normal.y() != 0 && static_cast<unsigned int>(Vector(point.x(), point.y() + normal.y()).norm()) == distance_) {
95  point.y() += normal.y();
96  } else {
97  point.x() += normal.x();
98  point.y() += normal.y();
99  }
100  } while (static_cast<unsigned int>(point.x()) != distance_ || point.y() != 0);
101 }
102 
104 {
105  Index radius = *(*this) - indexCenter_;
106  return radius.matrix().norm() * resolution_;
107 }
108 
109 } /* namespace grid_map */
110 
static int signum(const int val)
const Size & getSize() const
Definition: GridMap.cpp:661
Length mapLength_
Map information needed to get position from iterator.
double getResolution() const
Definition: GridMap.cpp:657
std::vector< Index > pointsRing_
unsigned int nRings_
Number of rings into the circle is divided.
double radius_
Radius of the circle.
const Eigen::Array2i & operator*() const
bool isInside(const Index &index) const
Eigen::Array2i Index
Definition: TypeDefs.hpp:22
double radiusSquare_
Square of the radius for efficiency.
bool checkIfIndexInRange(const Index &index, const Size &bufferSize)
bool operator!=(const SpiralIterator &other) const
bool getPositionFromIndex(Position &position, const Index &index, const Length &mapLength, const Position &mapPosition, const double &resolution, const Size &bufferSize, const Index &bufferStartIndex=Index::Zero())
SpiralIterator & operator++()
double getCurrentRadius() const
bool getIndex(const Position &position, Index &index) const
Definition: GridMap.cpp:228
bool getPosition(const Index &index, Position &position) const
Definition: GridMap.cpp:232
SpiralIterator(const grid_map::GridMap &gridMap, Eigen::Vector2d center, double radius)
Eigen::Vector2d Vector
Definition: TypeDefs.hpp:19
const Length & getLength() const
Definition: GridMap.cpp:649
Position center_
Position of the circle center;.


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