SlidingWindowIterator.cpp
Go to the documentation of this file.
1 /*
2  * SlidingWindowIterator.cpp
3  *
4  * Created on: Aug 17, 2017
5  * Author: Péter Fankhauser
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
11 
12 #include <iostream>
13 
14 namespace grid_map {
15 
16 SlidingWindowIterator::SlidingWindowIterator(const GridMap& gridMap, const std::string& layer,
17  const EdgeHandling& edgeHandling, const size_t windowSize)
18  : GridMapIterator(gridMap),
19  edgeHandling_(edgeHandling),
20  data_(gridMap[layer])
21 {
22  windowSize_ = windowSize;
23  setup(gridMap);
24 }
25 
27  : GridMapIterator(other),
29  data_(other->data_)
30 {
31  windowSize_ = other->windowSize_;
33 }
34 
35 void SlidingWindowIterator::setWindowLength(const GridMap& gridMap, const double windowLength)
36 {
37  windowSize_ = static_cast<size_t>(std::round(windowLength / gridMap.getResolution()));
38  if (windowSize_ % 2 != 1) {
39  ++windowSize_;
40  }
41  setup(gridMap);
42 }
43 
45 {
47  while (!isPastEnd()) {
49  if (dataInsideMap()) {
50  break;
51  }
52  }
53  } else {
55  }
56  return *this;
57 }
58 
60 {
61  const Index centerIndex(*(*this));
62  const Index windowMargin(Index::Constant(static_cast<int>(windowMargin_)));
63  const Index originalTopLeftIndex(centerIndex - windowMargin);
64  Index topLeftIndex(originalTopLeftIndex);
65  boundIndexToRange(topLeftIndex, size_);
66  Index bottomRightIndex(centerIndex + windowMargin);
67  boundIndexToRange(bottomRightIndex, size_);
68  const Size adjustedWindowSize(bottomRightIndex - topLeftIndex + Size::Ones());
69 
70  switch (edgeHandling_) {
72  case EdgeHandling::CROP:
73  return data_.block(topLeftIndex(0), topLeftIndex(1), adjustedWindowSize(0), adjustedWindowSize(1));
75  case EdgeHandling::MEAN:
76  const Matrix data = data_.block(topLeftIndex(0), topLeftIndex(1), adjustedWindowSize(0), adjustedWindowSize(1));
77  Matrix returnData(windowSize_, windowSize_);
79  returnData.setConstant(NAN);
80  } else if (edgeHandling_ == EdgeHandling::MEAN) {
81  returnData.setConstant(data.meanOfFinites());
82  }
83  const Index topLeftIndexShift(topLeftIndex - originalTopLeftIndex);
84  returnData.block(topLeftIndexShift(0), topLeftIndexShift(1), adjustedWindowSize(0), adjustedWindowSize(1)) =
85  data_.block(topLeftIndex(0), topLeftIndex(1), adjustedWindowSize(0), adjustedWindowSize(1));
86  return returnData;
87  }
88  return Matrix::Zero(0, 0);
89 }
90 
92 {
93  if (!gridMap.isDefaultStartIndex()) {
94  throw std::runtime_error("SlidingWindowIterator cannot be used with grid maps that don't have a default buffer start index.");
95  }
96  if (windowSize_ % 2 == 0) {
97  throw std::runtime_error("SlidingWindowIterator has a wrong window size!");
98  }
99  windowMargin_ = (windowSize_ - 1) / 2;
100 
102  if (!dataInsideMap()) {
103  operator++();
104  }
105  }
106 }
107 
109 {
110  const Index centerIndex(*(*this));
111  const Index windowMargin(Index::Constant(static_cast<int>(windowMargin_)));
112  const Index topLeftIndex(centerIndex - windowMargin);
113  const Index bottomRightIndex(centerIndex + windowMargin);
114  return checkIfIndexInRange(topLeftIndex, size_) && checkIfIndexInRange(bottomRightIndex, size_);
115 }
116 
117 } /* namespace grid_map */
const EdgeHandling edgeHandling_
Edge handling method.
bool dataInsideMap() const
Check if data for current index is fully inside map.
size_t windowSize_
Size of the sliding window.
bool isDefaultStartIndex() const
Definition: GridMap.cpp:673
size_t windowMargin_
Size of the border of the window around the center cell.
double getResolution() const
Definition: GridMap.cpp:657
void setWindowLength(const GridMap &gridMap, double windowLength)
Size size_
Size of the buffer.
Eigen::Array2i Index
Definition: TypeDefs.hpp:22
Eigen::Array2i Size
Definition: TypeDefs.hpp:23
Eigen::MatrixXf Matrix
Definition: TypeDefs.hpp:16
void setup(const GridMap &gridMap)
Setup members.
void boundIndexToRange(Index &index, const Size &bufferSize)
bool checkIfIndexInRange(const Index &index, const Size &bufferSize)
SlidingWindowIterator & operator++() override
SlidingWindowIterator(const GridMap &gridMap, const std::string &layer, const EdgeHandling &edgeHandling=EdgeHandling::CROP, size_t windowSize=3)
virtual GridMapIterator & operator++()


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