GridMapBase.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 __GridMapBase_h_
30 #define __GridMapBase_h_
31 
32 #include <Eigen/Geometry>
33 #include <Eigen/LU>
34 
35 #include "MapDimensionProperties.h"
36 
37 namespace hectorslam {
38 
43 template<typename ConcreteCellType>
45 {
46 
47 public:
48 
49  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 
55  bool hasGridValue(int x, int y) const
56  {
57  return (x >= 0) && (y >= 0) && (x < this->getSizeX()) && (y < this->getSizeY());
58  }
59 
60  const Eigen::Vector2i& getMapDimensions() const { return mapDimensionProperties.getMapDimensions(); };
61  int getSizeX() const { return mapDimensionProperties.getSizeX(); };
62  int getSizeY() const { return mapDimensionProperties.getSizeY(); };
63 
64  bool pointOutOfMapBounds(const Eigen::Vector2f& pointMapCoords) const
65  {
66  return mapDimensionProperties.pointOutOfMapBounds(pointMapCoords);
67  }
68 
69  virtual void reset()
70  {
71  this->clear();
72  }
73 
77  void clear()
78  {
79  int size = this->getSizeX() * this->getSizeY();
80 
81  for (int i = 0; i < size; ++i) {
82  this->mapArray[i].resetGridCell();
83  }
84 
85  //this->mapArray[0].set(1.0f);
86  //this->mapArray[size-1].set(1.0f);
87  }
88 
89 
91 
95  GridMapBase(float mapResolution, const Eigen::Vector2i& size, const Eigen::Vector2f& offset)
96  : mapArray(0)
97  , lastUpdateIndex(-1)
98  {
99  Eigen::Vector2i newMapDimensions (size);
100 
101  this->setMapGridSize(newMapDimensions);
102  sizeX = size[0];
103 
104  setMapTransformation(offset, mapResolution);
105 
106  this->clear();
107  }
108 
112  virtual ~GridMapBase()
113  {
114  deleteArray();
115  }
116 
120  void allocateArray(const Eigen::Vector2i& newMapDims)
121  {
122  int sizeX = newMapDims.x();
123  int sizeY = newMapDims.y();
124 
125  mapArray = new ConcreteCellType [sizeX*sizeY];
126 
128  }
129 
130  void deleteArray()
131  {
132  if (mapArray != 0){
133 
134  delete[] mapArray;
135 
136  mapArray = 0;
137  mapDimensionProperties.setMapCellDims(Eigen::Vector2i(-1,-1));
138  }
139  }
140 
141  ConcreteCellType& getCell(int x, int y)
142  {
143  return mapArray[y * sizeX + x];
144  }
145 
146  const ConcreteCellType& getCell(int x, int y) const
147  {
148  return mapArray[y * sizeX + x];
149  }
150 
151  ConcreteCellType& getCell(int index)
152  {
153  return mapArray[index];
154  }
155 
156  const ConcreteCellType& getCell(int index) const
157  {
158  return mapArray[index];
159  }
160 
161  void setMapGridSize(const Eigen::Vector2i& newMapDims)
162  {
163  if (newMapDims != mapDimensionProperties.getMapDimensions() ){
164  deleteArray();
165  allocateArray(newMapDims);
166  this->reset();
167  }
168  }
169 
173  GridMapBase(const GridMapBase& other)
174  {
176  *this = other;
177  }
178 
183  {
184  if ( !(this->mapDimensionProperties == other.mapDimensionProperties)){
186  }
187 
189 
190  this->worldTmap = other.worldTmap;
191  this->mapTworld = other.mapTworld;
192  this->worldTmap3D = other.worldTmap3D;
193 
194  this->scaleToMap = other.scaleToMap;
195 
196  //@todo potential resize
197  int sizeX = this->getSizeX();
198  int sizeY = this->getSizeY();
199 
200  size_t concreteCellSize = sizeof(ConcreteCellType);
201 
202  memcpy(this->mapArray, other.mapArray, sizeX*sizeY*concreteCellSize);
203 
204  return *this;
205  }
206 
210  inline Eigen::Vector2f getWorldCoords(const Eigen::Vector2f& mapCoords) const
211  {
212  return worldTmap * mapCoords;
213  }
214 
218  inline Eigen::Vector2f getMapCoords(const Eigen::Vector2f& worldCoords) const
219  {
220  return mapTworld * worldCoords;
221  }
222 
226  inline Eigen::Vector3f getWorldCoordsPose(const Eigen::Vector3f& mapPose) const
227  {
228  Eigen::Vector2f worldCoords (worldTmap * mapPose.head<2>());
229  return Eigen::Vector3f(worldCoords[0], worldCoords[1], mapPose[2]);
230  }
231 
235  inline Eigen::Vector3f getMapCoordsPose(const Eigen::Vector3f& worldPose) const
236  {
237  Eigen::Vector2f mapCoords (mapTworld * worldPose.head<2>());
238  return Eigen::Vector3f(mapCoords[0], mapCoords[1], worldPose[2]);
239  }
240 
241  void setDimensionProperties(const Eigen::Vector2f& topLeftOffsetIn, const Eigen::Vector2i& mapDimensionsIn, float cellLengthIn)
242  {
243  setDimensionProperties(MapDimensionProperties(topLeftOffsetIn,mapDimensionsIn,cellLengthIn));
244  }
245 
246  void setDimensionProperties(const MapDimensionProperties& newMapDimProps)
247  {
248  //Grid map cell number has changed
249  if (!newMapDimProps.hasEqualDimensionProperties(this->mapDimensionProperties)){
250  this->setMapGridSize(newMapDimProps.getMapDimensions());
251  }
252 
253  //Grid map transformation/cell size has changed
254  if(!newMapDimProps.hasEqualTransformationProperties(this->mapDimensionProperties)){
255  this->setMapTransformation(newMapDimProps.getTopLeftOffset(), newMapDimProps.getCellLength());
256  }
257  }
258 
265  void setMapTransformation(const Eigen::Vector2f& topLeftOffset, float cellLength)
266  {
269 
270  scaleToMap = 1.0f / cellLength;
271 
272  mapTworld = Eigen::AlignedScaling2f(scaleToMap, scaleToMap) * Eigen::Translation2f(topLeftOffset[0], topLeftOffset[1]);
273 
274  worldTmap3D = Eigen::AlignedScaling3f(scaleToMap, scaleToMap, 1.0f) * Eigen::Translation3f(topLeftOffset[0], topLeftOffset[1], 0);
275 
276  //std::cout << worldTmap3D.matrix() << std::endl;
277  worldTmap3D = worldTmap3D.inverse();
278 
279  worldTmap = mapTworld.inverse();
280  }
281 
282 
287  float getScaleToMap() const
288  {
289  return scaleToMap;
290  }
291 
296  float getCellLength() const
297  {
299  }
300 
305  const Eigen::Affine2f& getWorldTmap() const
306  {
307  return worldTmap;
308  }
309 
314  const Eigen::Affine3f& getWorldTmap3D() const
315  {
316  return worldTmap3D;
317  }
318 
323  const Eigen::Affine2f& getMapTworld() const
324  {
325  return mapTworld;
326  }
327 
329  int getUpdateIndex() const { return lastUpdateIndex; };
330 
334  bool getMapExtends(int& xMax, int& yMax, int& xMin, int& yMin) const
335  {
336  int lowerStart = -1;
337  int upperStart = 10000;
338 
339  int xMaxTemp = lowerStart;
340  int yMaxTemp = lowerStart;
341  int xMinTemp = upperStart;
342  int yMinTemp = upperStart;
343 
344  int sizeX = this->getSizeX();
345  int sizeY = this->getSizeY();
346 
347  for (int x = 0; x < sizeX; ++x) {
348  for (int y = 0; y < sizeY; ++y) {
349  if (this->mapArray[x][y].getValue() != 0.0f) {
350 
351  if (x > xMaxTemp) {
352  xMaxTemp = x;
353  }
354 
355  if (x < xMinTemp) {
356  xMinTemp = x;
357  }
358 
359  if (y > yMaxTemp) {
360  yMaxTemp = y;
361  }
362 
363  if (y < yMinTemp) {
364  yMinTemp = y;
365  }
366  }
367  }
368  }
369 
370  if ((xMaxTemp != lowerStart) &&
371  (yMaxTemp != lowerStart) &&
372  (xMinTemp != upperStart) &&
373  (yMinTemp != upperStart)) {
374 
375  xMax = xMaxTemp;
376  yMax = yMaxTemp;
377  xMin = xMinTemp;
378  yMin = yMinTemp;
379  return true;
380  } else {
381  return false;
382  }
383  }
384 
385 protected:
386 
387  ConcreteCellType *mapArray;
388 
389  float scaleToMap;
390 
391  Eigen::Affine2f worldTmap;
392  Eigen::Affine3f worldTmap3D;
393  Eigen::Affine2f mapTworld;
394 
396  int sizeX;
397 
398 private:
400 };
401 
402 }
403 
404 #endif
const Eigen::Vector2i & getMapDimensions() const
void setMapCellDims(const Eigen::Vector2i &newDims)
f
const Eigen::Affine3f & getWorldTmap3D() const
Definition: GridMapBase.h:314
Eigen::Affine2f worldTmap
Homogenous 2D transform from map to world coordinates.
Definition: GridMapBase.h:391
float getScaleToMap() const
Definition: GridMapBase.h:287
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool hasGridValue(int x, int y) const
Definition: GridMapBase.h:55
TFSIMD_FORCE_INLINE const tfScalar & y() const
const ConcreteCellType & getCell(int index) const
Definition: GridMapBase.h:156
void allocateArray(const Eigen::Vector2i &newMapDims)
Definition: GridMapBase.h:120
int getSizeY() const
Definition: GridMapBase.h:62
const MapDimensionProperties & getMapDimProperties() const
Definition: GridMapBase.h:90
void setDimensionProperties(const MapDimensionProperties &newMapDimProps)
Definition: GridMapBase.h:246
GridMapBase(const GridMapBase &other)
Definition: GridMapBase.h:173
bool hasEqualDimensionProperties(const MapDimensionProperties &other) const
int getUpdateIndex() const
Definition: GridMapBase.h:329
ConcreteCellType & getCell(int x, int y)
Definition: GridMapBase.h:141
ConcreteCellType * mapArray
Map representation used with plain pointer array.
Definition: GridMapBase.h:387
GridMapBase & operator=(const GridMapBase &other)
Definition: GridMapBase.h:182
virtual void reset()
Definition: GridMapBase.h:69
const ConcreteCellType & getCell(int x, int y) const
Definition: GridMapBase.h:146
const Eigen::Vector2i & getMapDimensions() const
Definition: GridMapBase.h:60
Eigen::Affine2f mapTworld
Homogenous 2D transform from world to map coordinates.
Definition: GridMapBase.h:393
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool pointOutOfMapBounds(const Eigen::Vector2f &coords) const
int getSizeX() const
Definition: GridMapBase.h:61
bool getMapExtends(int &xMax, int &yMax, int &xMin, int &yMin) const
Definition: GridMapBase.h:334
Eigen::Vector2f getWorldCoords(const Eigen::Vector2f &mapCoords) const
Definition: GridMapBase.h:210
float getCellLength() const
Definition: GridMapBase.h:296
const Eigen::Vector2f & getTopLeftOffset() const
void setDimensionProperties(const Eigen::Vector2f &topLeftOffsetIn, const Eigen::Vector2i &mapDimensionsIn, float cellLengthIn)
Definition: GridMapBase.h:241
void setMapTransformation(const Eigen::Vector2f &topLeftOffset, float cellLength)
Definition: GridMapBase.h:265
const Eigen::Affine2f & getWorldTmap() const
Definition: GridMapBase.h:305
void setTopLeftOffset(const Eigen::Vector2f &topLeftOffsetIn)
Eigen::Vector3f getMapCoordsPose(const Eigen::Vector3f &worldPose) const
Definition: GridMapBase.h:235
GridMapBase(float mapResolution, const Eigen::Vector2i &size, const Eigen::Vector2f &offset)
Definition: GridMapBase.h:95
const Eigen::Affine2f & getMapTworld() const
Definition: GridMapBase.h:323
bool hasEqualTransformationProperties(const MapDimensionProperties &other) const
ConcreteCellType & getCell(int index)
Definition: GridMapBase.h:151
void setMapGridSize(const Eigen::Vector2i &newMapDims)
Definition: GridMapBase.h:161
Eigen::Vector3f getWorldCoordsPose(const Eigen::Vector3f &mapPose) const
Definition: GridMapBase.h:226
Eigen::Vector2f getMapCoords(const Eigen::Vector2f &worldCoords) const
Definition: GridMapBase.h:218
Eigen::Affine3f worldTmap3D
Homogenous 3D transform from map to world coordinates.
Definition: GridMapBase.h:392
MapDimensionProperties mapDimensionProperties
Definition: GridMapBase.h:395
bool pointOutOfMapBounds(const Eigen::Vector2f &pointMapCoords) const
Definition: GridMapBase.h:64
float scaleToMap
Scaling factor from world to map.
Definition: GridMapBase.h:389


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