OccGridMapBase.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Stefan Kohlbrecher, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Simulation, Systems Optimization and Robotics
13 // group, TU Darmstadt nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef __OccGridMapBase_h_
30 #define __OccGridMapBase_h_
31 
32 #include "GridMapBase.h"
33 
34 #include "../scan/DataPointContainer.h"
35 #include "../util/UtilFunctions.h"
36 
37 #include <Eigen/Geometry>
38 
39 namespace hectorslam {
40 
41 template<typename ConcreteCellType, typename ConcreteGridFunctions>
43  : public GridMapBase<ConcreteCellType>
44 {
45 
46 public:
47 
48  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 
50  OccGridMapBase(float mapResolution, const Eigen::Vector2i& size, const Eigen::Vector2f& offset)
51  : GridMapBase<ConcreteCellType>(mapResolution, size, offset)
52  , currUpdateIndex(0)
53  , currMarkOccIndex(-1)
54  , currMarkFreeIndex(-1)
55  {}
56 
57  virtual ~OccGridMapBase() {}
58 
59  void updateSetOccupied(int index)
60  {
61  concreteGridFunctions.updateSetOccupied(this->getCell(index));
62  }
63 
64  void updateSetFree(int index)
65  {
66  concreteGridFunctions.updateSetFree(this->getCell(index));
67  }
68 
69  void updateUnsetFree(int index)
70  {
71  concreteGridFunctions.updateUnsetFree(this->getCell(index));
72  }
73 
74  float getGridProbabilityMap(int index) const
75  {
76  return concreteGridFunctions.getGridProbability(this->getCell(index));
77  }
78 
79  bool isOccupied(int xMap, int yMap) const
80  {
81  return (this->getCell(xMap,yMap).isOccupied());
82  }
83 
84  bool isFree(int xMap, int yMap) const
85  {
86  return (this->getCell(xMap,yMap).isFree());
87  }
88 
89  bool isOccupied(int index) const
90  {
91  return (this->getCell(index).isOccupied());
92  }
93 
94  bool isFree(int index) const
95  {
96  return (this->getCell(index).isFree());
97  }
98 
99  float getObstacleThreshold() const
100  {
101  ConcreteCellType temp;
102  temp.resetGridCell();
103  return concreteGridFunctions.getGridProbability(temp);
104  }
105 
106  void setUpdateFreeFactor(float factor)
107  {
108  concreteGridFunctions.setUpdateFreeFactor(factor);
109  }
110 
111  void setUpdateOccupiedFactor(float factor)
112  {
113  concreteGridFunctions.setUpdateOccupiedFactor(factor);
114  }
115 
121  void updateByScan(const DataContainer& dataContainer, const Eigen::Vector3f& robotPoseWorld)
122  {
125 
126  //Get pose in map coordinates from pose in world coordinates
127  Eigen::Vector3f mapPose(this->getMapCoordsPose(robotPoseWorld));
128 
129  //Get a 2D homogenous transform that can be left-multiplied to a robot coordinates vector to get world coordinates of that vector
130  Eigen::Affine2f poseTransform((Eigen::Translation2f(
131  mapPose[0], mapPose[1]) * Eigen::Rotation2Df(mapPose[2])));
132 
133  //Get start point of all laser beams in map coordinates (same for alle beams, stored in robot coords in dataContainer)
134  Eigen::Vector2f scanBeginMapf(poseTransform * dataContainer.getOrigo());
135 
136  //Get integer vector of laser beams start point
137  Eigen::Vector2i scanBeginMapi(scanBeginMapf[0] + 0.5f, scanBeginMapf[1] + 0.5f);
138 
139  //Get number of valid beams in current scan
140  int numValidElems = dataContainer.getSize();
141 
142  //std::cout << "\n maxD: " << maxDist << " num: " << numValidElems << "\n";
143 
144  //Iterate over all valid laser beams
145  for (int i = 0; i < numValidElems; ++i) {
146 
147  //Get map coordinates of current beam endpoint
148  Eigen::Vector2f scanEndMapf(poseTransform * (dataContainer.getVecEntry(i)));
149  //std::cout << "\ns\n" << scanEndMapf << "\n";
150 
151  //add 0.5 to beam endpoint vector for following integer cast (to round, not truncate)
152  scanEndMapf.array() += (0.5f);
153 
154  //Get integer map coordinates of current beam endpoint
155  Eigen::Vector2i scanEndMapi(scanEndMapf.cast<int>());
156 
157  //Update map using a bresenham variant for drawing a line from beam start to beam endpoint in map coordinates
158  if (scanBeginMapi != scanEndMapi){
159  updateLineBresenhami(scanBeginMapi, scanEndMapi);
160  }
161  }
162 
163  //Tell the map that it has been updated
164  this->setUpdated();
165 
166  //Increase update index (used for updating grid cells only once per incoming scan)
167  currUpdateIndex += 3;
168  }
169 
170  inline void updateLineBresenhami( const Eigen::Vector2i& beginMap, const Eigen::Vector2i& endMap, unsigned int max_length = UINT_MAX){
171 
172  int x0 = beginMap[0];
173  int y0 = beginMap[1];
174 
175  //check if beam start point is inside map, cancel update if this is not the case
176  if ((x0 < 0) || (x0 >= this->getSizeX()) || (y0 < 0) || (y0 >= this->getSizeY())) {
177  return;
178  }
179 
180  int x1 = endMap[0];
181  int y1 = endMap[1];
182 
183  //std::cout << " x: "<< x1 << " y: " << y1 << " length: " << length << " ";
184 
185  //check if beam end point is inside map, cancel update if this is not the case
186  if ((x1 < 0) || (x1 >= this->getSizeX()) || (y1 < 0) || (y1 >= this->getSizeY())) {
187  return;
188  }
189 
190  int dx = x1 - x0;
191  int dy = y1 - y0;
192 
193  unsigned int abs_dx = abs(dx);
194  unsigned int abs_dy = abs(dy);
195 
196  int offset_dx = util::sign(dx);
197  int offset_dy = util::sign(dy) * this->sizeX;
198 
199  unsigned int startOffset = beginMap.y() * this->sizeX + beginMap.x();
200 
201  //if x is dominant
202  if(abs_dx >= abs_dy){
203  int error_y = abs_dx / 2;
204  bresenham2D(abs_dx, abs_dy, error_y, offset_dx, offset_dy, startOffset);
205  }else{
206  //otherwise y is dominant
207  int error_x = abs_dy / 2;
208  bresenham2D(abs_dy, abs_dx, error_x, offset_dy, offset_dx, startOffset);
209  }
210 
211  unsigned int endOffset = endMap.y() * this->sizeX + endMap.x();
212  this->bresenhamCellOcc(endOffset);
213 
214  }
215 
216  inline void bresenhamCellFree(unsigned int offset)
217  {
218  ConcreteCellType& cell (this->getCell(offset));
219 
220  if (cell.updateIndex < currMarkFreeIndex) {
221  concreteGridFunctions.updateSetFree(cell);
222  cell.updateIndex = currMarkFreeIndex;
223  }
224  }
225 
226  inline void bresenhamCellOcc(unsigned int offset)
227  {
228  ConcreteCellType& cell (this->getCell(offset));
229 
230  if (cell.updateIndex < currMarkOccIndex) {
231 
232  //if this cell has been updated as free in the current iteration, revert this
233  if (cell.updateIndex == currMarkFreeIndex) {
234  concreteGridFunctions.updateUnsetFree(cell);
235  }
236 
237  concreteGridFunctions.updateSetOccupied(cell);
238  //std::cout << " setOcc " << "\n";
239  cell.updateIndex = currMarkOccIndex;
240  }
241  }
242 
243  inline void bresenham2D( unsigned int abs_da, unsigned int abs_db, int error_b, int offset_a, int offset_b, unsigned int offset){
244 
245  this->bresenhamCellFree(offset);
246 
247  unsigned int end = abs_da-1;
248 
249  for(unsigned int i = 0; i < end; ++i){
250  offset += offset_a;
251  error_b += abs_db;
252 
253  if((unsigned int)error_b >= abs_da){
254  offset += offset_b;
255  error_b -= abs_da;
256  }
257 
258  this->bresenhamCellFree(offset);
259  }
260  }
261 
262 protected:
263 
264  ConcreteGridFunctions concreteGridFunctions;
268 };
269 
270 
271 }
272 
273 #endif
void setUpdateFreeFactor(float factor)
bool isOccupied(int index) const
void bresenham2D(unsigned int abs_da, unsigned int abs_db, int error_b, int offset_a, int offset_b, unsigned int offset)
void updateByScan(const DataContainer &dataContainer, const Eigen::Vector3f &robotPoseWorld)
f
void bresenhamCellOcc(unsigned int offset)
void bresenhamCellFree(unsigned int offset)
DataPointType getOrigo() const
ConcreteGridFunctions concreteGridFunctions
int getSizeY() const
Definition: GridMapBase.h:62
static int sign(int x)
Definition: UtilFunctions.h:55
EIGEN_MAKE_ALIGNED_OPERATOR_NEW OccGridMapBase(float mapResolution, const Eigen::Vector2i &size, const Eigen::Vector2f &offset)
bool isOccupied(int xMap, int yMap) const
const DataPointType & getVecEntry(int index) const
ConcreteCellType & getCell(int x, int y)
Definition: GridMapBase.h:141
void updateLineBresenhami(const Eigen::Vector2i &beginMap, const Eigen::Vector2i &endMap, unsigned int max_length=UINT_MAX)
void updateSetOccupied(int index)
int getSizeX() const
Definition: GridMapBase.h:61
void setUpdateOccupiedFactor(float factor)
float getGridProbabilityMap(int index) const
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
Eigen::Vector3f getMapCoordsPose(const Eigen::Vector3f &worldPose) const
Definition: GridMapBase.h:235
void updateSetFree(int index)
float getObstacleThreshold() const
bool isFree(int xMap, int yMap) const
void updateUnsetFree(int index)
bool isFree(int index) const


hector_mapping
Author(s): Stefan Kohlbrecher
autogenerated on Sun Nov 3 2019 03:18:33