30 #ifndef _MIRA_GRIDMAP_H_ 31 #define _MIRA_GRIDMAP_H_ 34 #include <type_traits> 42 namespace mira {
namespace maps {
66 template<
typename T,
int Channels = 1>
74 static_assert(std::is_void<T>::value || std::is_arithmetic<T>::value,
"GridMap can be used with arithmetic types only");
102 Base(getOffsetAndSize(region,cellSize).second),
104 mOffset(getOffsetAndSize(region,cellSize).first)
273 return Rect2i(lowerLeft, upperRight);
279 return Rect2i(lowerLeft, upperRight);
287 template<
typename Derived>
291 r.
member(
"Offset" ,
mOffset,
"The cell index that corresponds to the origin of the map");
295 template<
typename Derived>
299 r.
member(
"Offset" ,
mOffset,
"The cell index that corresponds to the origin of the map");
306 r.
member(
"Offset" ,
mOffset,
"The cell index that corresponds to the origin of the map");
311 static std::pair<Point2i, Size2i> getOffsetAndSize(
const Rect2f& region,
float cellSize);
320 template<
typename T,
int Channels>
324 if(getRegion().x0()<=region.x0() &&
325 getRegion().y0()<=region.y0() &&
326 getRegion().x1()>=region.x1() &&
327 getRegion().y1()>=region.y1())
331 Rect2f o = region | getRegion();
332 Size2i oldSize = this->size();
334 auto offsetSize = getOffsetAndSize(o,mCellSize);
336 Point2i growLowerLeft = offsetSize.first - mOffset;
337 Point2i growUpperRight = offsetSize.second - oldSize - growLowerLeft;
339 assert(growLowerLeft.x()>=0 && growLowerLeft.y()>=0);
340 assert(growUpperRight.x()>=0 && growUpperRight.y()>=0);
341 grow(growLowerLeft,growUpperRight, valueForNewCells);
344 template<
typename T,
int Channels>
348 if(getMapRegion().contains(region))
352 const auto o = region | getMapRegion();
354 const auto newOffset = -o.minCorner;
355 const auto newSize = o.size();
357 const Size2i oldSize = this->size();
358 const Point2i growLowerLeft = newOffset - mOffset;
359 const Point2i growUpperRight = newSize - oldSize - growLowerLeft;
361 assert(growLowerLeft.x()>=0 && growLowerLeft.y()>=0);
362 assert(growUpperRight.x()>=0 && growUpperRight.y()>=0);
363 grow(growLowerLeft,growUpperRight, valueForNewCells);
366 template<
typename T,
int Channels>
370 assert(growLowerLeft.x()>=0 && growLowerLeft.y()>=0);
371 assert(growUpperRight.x()>=0 && growUpperRight.y()>=0);
373 Size2i oldSize = this->size();
374 Size2i newSize = oldSize + growLowerLeft + growUpperRight;
376 Base& oldBuffer = *
this;
377 Base newBuffer(newSize);
380 for(
int y=0; y<oldBuffer.
height(); ++y)
383 CellType* dest = newBuffer[y+growLowerLeft.y()];
384 memcpy(dest+growLowerLeft.x(), src, oldBuffer.
width()*
sizeof(
CellType));
403 for(
int y=0; y<growLowerLeft.y(); ++y)
406 for(
int x=0; x<newBuffer.
width(); ++x)
407 dest[x] = valueForNewCells;
411 for(
int y=growLowerLeft.y(); y<growLowerLeft.y()+oldBuffer.
height(); ++y)
415 for(
int x=0; x<growLowerLeft.x(); ++x)
416 dest[x] = valueForNewCells;
418 for(
int x=growLowerLeft.x()+oldBuffer.
width(); x<newBuffer.
width(); ++x)
419 dest[x] = valueForNewCells;
423 for(
int y=growLowerLeft.y()+oldBuffer.
height(); y<newBuffer.
height(); ++y)
426 for(
int x=0; x<newBuffer.
width(); ++x)
427 dest[x] = valueForNewCells;
431 oldBuffer = newBuffer;
432 mOffset = mOffset + growLowerLeft;
435 template<
typename T,
int Channels>
438 Point2i lowerLeft (std::floor(region.x0() / mCellSize),
439 std::floor(region.y0() / mCellSize));
440 Point2i upperRight(std::ceil(region.x1() / mCellSize),
441 std::ceil(region.y1() / mCellSize));
442 Rect2i r(lowerLeft, upperRight);
444 clip(r,valueForNewCells);
447 template<
typename T,
int Channels>
450 Rect2i oldRegion = getMapRegion();
451 if(region==oldRegion)
468 Point2i copyRegionInNewLL(0,0);
469 Point2i copyRegionInOldLL(0,0);
481 Rect2i intersection = oldRegion & region;
483 Base& oldBuffer = *
this;
484 Base newBuffer(region.size());
488 Point2i copyRegionInNewUR = copyRegionInNewLL + intersection.
size();
489 Point2i copyRegionInOldUR = copyRegionInOldLL + intersection.
size();
507 assert(copyRegionInNewLL.y()<=newBuffer.height());
508 for(
int y=0; y<copyRegionInNewLL.y(); ++y)
511 for(
int x=0; x<newBuffer.width(); ++x)
512 dest[x] = valueForNewCells;
516 assert(copyRegionInNewLL.y()>=0 && copyRegionInNewUR.y()<=newBuffer.height());
517 assert(copyRegionInNewUR.x()>=0 && copyRegionInNewLL.x()<=newBuffer.width());
518 for(
int y=copyRegionInNewLL.y(); y<copyRegionInNewUR.y(); ++y)
522 for(
int x=0; x<copyRegionInNewLL.x(); ++x)
523 dest[x] = valueForNewCells;
525 for(
int x=copyRegionInNewUR.x(); x<newBuffer.width(); ++x)
526 dest[x] = valueForNewCells;
530 assert(copyRegionInNewUR.y()>=0);
531 for(
int y=copyRegionInNewUR.y(); y<newBuffer.height(); ++y)
534 for(
int x=0; x<newBuffer.width(); ++x)
535 dest[x] = valueForNewCells;
539 for(
int y=0; y<intersection.height(); ++y)
541 const CellType* src = oldBuffer[y+copyRegionInOldLL.y()]+copyRegionInOldLL.x();
542 CellType* dest = newBuffer[y+copyRegionInNewLL.y()]+copyRegionInNewLL.x();
543 memcpy(dest, src, intersection.width()*
sizeof(
CellType));
548 newBuffer = valueForNewCells;
551 mOffset = -region.minCorner;
552 oldBuffer = newBuffer;
556 template<
typename T,
int Channels>
559 Point2i lowerLeft (std::floor(region.x0() / cellSize),
560 std::floor(region.y0() / cellSize));
561 Point2i upperRight(std::ceil(region.x1() / cellSize),
562 std::ceil(region.y1() / cellSize));
565 Size2i size = upperRight - lowerLeft;
567 return std::make_pair(offset,size);
575 template <
typename TPixel,
int TChannels>
Point2f map2world(const Point2i &p) const
Convert a given point to world coordinates.
Definition: GridMap.h:243
Point2f map2world(const Point2f &p) const
Convert a given point to world coordinates.
Definition: GridMap.h:254
ImgPixel< T, Channels > Pixel
Rect2i world2map(const Rect2f &r, bool includeBorder=true) const
Returns the Rect in cell coordinates that is covered by the given rect that is specified in world coo...
Definition: GridMap.h:267
GridMap clone() const
Definition: GridMap.h:148
void grow(const Rect2f ®ion, const CellType &valueForNewCells)
Grows the map so that the specified region is covered by the map.
Definition: GridMap.h:321
GridMap(float cellSize=0.1f)
Constructs an empty map with a default cell size of 0.1 m.
Definition: GridMap.h:88
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Self & operator=(const Pixel &p)
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Point2i getMapOffset() const
Provided for backward compatibility. Use getOffset() instead.
Definition: GridMap.h:133
void clip(const Rect2i ®ion, const CellType &valueForNewCells)
Grows and optionally crops the map to cover the specified region while minimizing copying of memory...
Definition: GridMap.h:448
Point2i mOffset
cell index that corresponds to the origin of the map
Definition: GridMap.h:315
float mCellSize
width of one cell in m
Definition: GridMap.h:314
void reflect(BinaryDeserializer< Derived > &r)
Reflect method for deserialization.
Definition: GridMap.h:296
Rect2i getMapRegion() const
Returns the region that is covered by the GridMap in grid cells.
Definition: GridMap.h:166
Base::Pixel CellType
Definition: GridMap.h:79
void reflect(BinarySerializer< Derived > &r)
Reflect method for serialization.
Definition: GridMap.h:288
GridMap(const Rect2f ®ion, float cellSize)
Creates a new grid map that covers the specified region.
Definition: GridMap.h:101
GridMap(const Base &data, float cellSize, const Point2i &offset=Point2i(0, 0))
Construct a grid map from existing image.
Definition: GridMap.h:118
GridMap(const Size2i &size, float cellSize, const Point2i &offset=Point2i(0, 0))
Definition: GridMap.h:90
float getCellSize() const
Returns the size of each cell in meter.
Definition: GridMap.h:126
Point2i world2map(const Point2f &p, bool roundDown=true) const
Convert a given point to map coordinates.
Definition: GridMap.h:217
Img< T, Channels > clone() const
Point< float, 2 > Point2f
#define MIRA_NO_GENERIC_REFLECT_MEMBER(Type)
void reflect(JSONSerializer &r)
Reflect method for text visualization.
Definition: GridMap.h:303
Point2i getOffset() const
Returns the offset of the map, e.g. the index of the cell that is located in the origin.
Definition: GridMap.h:130
Point2f world2mapf(const Point2f &p) const
Convert a given point to map coordinates.
Definition: GridMap.h:232
GridMap & operator=(const CellType &c)
Definition: GridMap.h:143
Point2f getWorldOffset() const
Returns the offset of the map in metric coordinates.
Definition: GridMap.h:136
Rect2f getRegion() const
Returns the region that is covered by the GridMap.
Definition: GridMap.h:157
GridMap(const Rect2i ®ion, float cellSize)
Definition: GridMap.h:108