OccupancyGrid.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/dynamic_bitset/dynamic_bitset.hpp>
4 
5 #include <Eigen/Core>
6 #include <Eigen/Geometry>
7 
8 namespace armarx
9 {
10 
11  // https://stackoverflow.com/questions/9255887/stl-function-to-test-whether-a-value-is-within-some-range
12  template<typename T>
13  class Interval
14  {
15  public:
16  Interval(T lo, T hi) : low(lo), high(hi) {}
17  bool contains(T value) const
18  {
19  return low <= value && value < high;
20  }
21  private:
22  T low;
23  T high;
24  };
25  template<typename T>
27  {
28  return Interval<T>(lo, hi);
29  }
30 
31 
33  {
34  void init(std::size_t gridSizeX,
35  std::size_t gridSizeY,
36  float originX,
37  float originY,
38  float gridStepSize)
39  {
40  sizeX = gridSizeX;
41  sizeY = gridSizeY;
42  this->originX = originX;
43  this->originY = originY;
44  this->stepSize = gridStepSize;
45  std::size_t gridSize = sizeX * sizeY;
46  occupied.clear();
47  occupied.resize(gridSize);
48  }
49 
50  bool isOccupied(std::size_t indexX, std::size_t indexY) const
51  {
52  std::size_t index = indexY * sizeX + indexX;
53  if (index < occupied.size())
54  {
55  return occupied[index];
56  }
57  return false;
58  }
59 
60  void setOccupied(std::size_t indexX, std::size_t indexY, bool posOccupied)
61  {
62  std::size_t index = indexY * sizeX + indexX;
63  if (index < occupied.size())
64  {
65  occupied[index] = posOccupied;
66  }
67  }
68 
69  Eigen::Vector2f getCentralPosition(std::size_t indexX, std::size_t indexY) const
70  {
71  float posX = originX + indexX * stepSize;
72  float posY = originY + indexY * stepSize;
73  return Eigen::Vector2f(posX, posY);
74  }
75 
76  bool isOccupied(float posX, float posY) const
77  {
78  std::size_t indexX = (std::size_t)std::roundf((posX - originX) / stepSize);
79  std::size_t indexY = (std::size_t)std::roundf((posY - originY) / stepSize);
80  return isOccupied(indexX, indexY);
81  }
82 
83  bool isInBounds(float posX, float posY) const
84  {
85  const int indexX = static_cast<int>(std::roundf((posX - originX) / stepSize));
86  const int indexY = static_cast<int>(std::roundf((posY - originY) / stepSize));
87 
88  return Interval<int>(0, sizeX).contains(indexX) && Interval<int>(0, sizeY).contains(indexY);
89  }
90 
91  float computeDistance(Eigen::Vector2f pos, Eigen::Vector2f dir)
92  {
93  // std::size_t indexX = (std::size_t)std::roundf((pos.x() - originX) / stepSize);
94  // std::size_t indexY = (std::size_t)std::roundf((pos.y() - originY) / stepSize);
95 
96 
97  // if (indexX >= sizeX || indexY >= sizeY)
98  // {
99  // return 0.0f;
100  // }
101 
102  // We are inside the grid
103  // float gridMaxX = originX + stepSize * sizeX;
104  // float gridMaxY = originY + stepSize * sizeY;
105  // float distanceX = 0.0f;
106  // if (dir.x() > 0.0f)
107  // {
108  // distanceX = std::abs((gridMaxX - pos.x()) / dir.x());
109  // }
110  // else if (dir.x() < 0.0f)
111  // {
112  // distanceX = std::abs((pos.x() - originX) / dir.x());
113  // }
114  // float distanceY = 0.0f;
115  // if (dir.y() > 0.0f)
116  // {
117  // distanceY = std::abs((gridMaxY - pos.y()) / dir.y());
118  // }
119  // else if (dir.y() < 0.0f)
120  // {
121  // distanceY = std::abs((pos.y() - originY) / dir.y());
122  // }
123 
124  // What is the max distance we can travel along dir until we leave the grid?
125  float maxDistance = 15'000; // TODO based on sensor specs
126  float testStepSize = 0.5f * stepSize;
127  Eigen::Vector2f testStep = testStepSize * dir;
128  std::size_t lineSteps = std::ceil(maxDistance / testStepSize);
129  for (std::size_t i = 0; i < lineSteps; ++i)
130  {
131  Eigen::Vector2f testPos = pos + i * testStep;
132  if (isInBounds(testPos.x(), testPos.y()) && isOccupied(testPos.x(), testPos.y()))
133  {
134  return i * testStepSize;
135  }
136  }
137 
138  return -1.F;
139  }
140 
141  std::size_t sizeX = 0;
142  std::size_t sizeY = 0;
143  float originX = 0.0f;
144  float originY = 0.0f;
145  float stepSize = 0.0f;
146  boost::dynamic_bitset<std::size_t> occupied;
147  };
148 
149 } // namespace armarx
armarx::OccupancyGrid::occupied
boost::dynamic_bitset< std::size_t > occupied
Definition: OccupancyGrid.h:146
armarx::armem::vision::OccupancyGrid
Definition: types.h:36
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::OccupancyGrid::isInBounds
bool isInBounds(float posX, float posY) const
Definition: OccupancyGrid.h:83
armarx::OccupancyGrid::computeDistance
float computeDistance(Eigen::Vector2f pos, Eigen::Vector2f dir)
Definition: OccupancyGrid.h:91
lo
#define lo(x)
Definition: AbstractInterface.h:43
armarx::OccupancyGrid::isOccupied
bool isOccupied(std::size_t indexX, std::size_t indexY) const
Definition: OccupancyGrid.h:50
armarx::OccupancyGrid::originX
float originX
Definition: OccupancyGrid.h:143
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::interval
Interval< T > interval(T lo, T hi)
Definition: OccupancyGrid.h:26
armarx::Interval
Definition: OccupancyGrid.h:13
armarx::OccupancyGrid::sizeX
std::size_t sizeX
Definition: OccupancyGrid.h:141
armarx::OccupancyGrid::init
void init(std::size_t gridSizeX, std::size_t gridSizeY, float originX, float originY, float gridStepSize)
Definition: OccupancyGrid.h:34
armarx::Interval::contains
bool contains(T value) const
Definition: OccupancyGrid.h:17
armarx::Interval::Interval
Interval(T lo, T hi)
Definition: OccupancyGrid.h:16
hi
#define hi(x)
Definition: AbstractInterface.h:42
armarx::OccupancyGrid::stepSize
float stepSize
Definition: OccupancyGrid.h:145
armarx::OccupancyGrid::setOccupied
void setOccupied(std::size_t indexX, std::size_t indexY, bool posOccupied)
Definition: OccupancyGrid.h:60
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::OccupancyGrid::sizeY
std::size_t sizeY
Definition: OccupancyGrid.h:142
armarx::OccupancyGrid::originY
float originY
Definition: OccupancyGrid.h:144
armarx::OccupancyGrid::getCentralPosition
Eigen::Vector2f getCentralPosition(std::size_t indexX, std::size_t indexY) const
Definition: OccupancyGrid.h:69
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::OccupancyGrid::isOccupied
bool isOccupied(float posX, float posY) const
Definition: OccupancyGrid.h:76