30 #ifndef _MIRA_VOXELMAP_H_ 31 #define _MIRA_VOXELMAP_H_ 34 #include <type_traits> 41 namespace mira {
namespace maps {
52 static_assert(std::has_trivial_destructor<T>::value,
53 "Can be used with types that have trivial destructor only");
58 static_assert(std::has_trivial_default_constructor<T>::value,
59 "Can be used with types that have a trivial default constructor only");
60 static_assert(std::has_trivial_copy_constructor<T>::value,
61 "Can be used with types that have a trivial copy constructor only");
66 typedef typename boost::multi_array<T,3>
Array;
67 typedef typename boost::multi_array::extent_range
Range;
71 void init(
const Size3i& size,
float cellSize,
const Point3i& offset)
80 mData = createArray(a,b);
84 typename Array::extent_gen extents;
93 VoxelMap(
float cellSize=0.1f) : mCellSize(cellSize), mOffset(0, 0, 0) { mData =
new Array; }
96 init(size, cellSize, offset);
109 auto p = getOffsetAndSize(region,cellSize);
110 init(p.second, cellSize, p.first);
136 for(
int i=a(0); i<b(0); ++i)
137 for(
int j=a(1); j<b(1); ++j)
138 for(
int k=a(2);
k<b(2); ++
k)
139 (*mData)[i][j][
k] = value;
162 return data()[idx(0)][idx(1)][idx(2)];
166 return data()[idx(0)][idx(1)][idx(2)];
179 float ioff = 1.0f / mCellSize;
180 return p * ioff + mOffset;
189 return (p - mOffset) * mCellSize;
195 void clip(
const Box3i& region,
const T& valueForNewCells);
199 void grow(
const Index& pMin,
const Index& pMax)
201 if(pMin >= mMin && pMax <= mMax)
208 for(
int i=0; i<3; ++i) {
209 min(i) = std::min(mMin(i), pMin(i));
210 max(i) = std::max(mMax(i), pMax(i));
213 typename Array::extent_gen extents;
215 [
Range(min(1),max(1)+1)]
216 [
Range(min(2),max(2)+1)]);
219 for(
int i=mMin(0); i<=mMax(0); ++i)
220 for(
int j=mMin(1); j<=mMax(1); ++j)
221 for(
int k=mMin(2);
k<=mMax(2); ++
k)
222 (*newData)[i][j][
k] = (*mData)[i][j][
k];
224 Array* oldData = mData;
231 void grow(
const MetricIndex& pMin,
const MetricIndex& pMax) {
232 grow(metricToCell(pMin), metricToCell(pMax));
236 void resize(
const Index& min,
const Index& max)
238 if(min == mMin && max == mMax)
243 typename Array::extent_gen extents;
245 [
Range(min(1),max(1)+1)]
246 [
Range(min(2),max(2)+1)]);
252 for(
int i=0; i<3; ++i) {
253 interMin(i) = std::max(mMin(i), min(i));
254 interMax(i) = std::min(mMax(i), max(i));
258 for(
int i=interMin(0); i<=interMax(0); ++i)
259 for(
int j=interMin(1); j<=interMax(1); ++j)
260 for(
int k=interMin(2);
k<=interMax(2); ++
k)
261 (*newData)[i][j][
k] = (*mData)[i][j][
k];
263 Array* oldData = mData;
270 void resize(
const MetricIndex& pMin,
const MetricIndex& pMax) {
271 resize(metricToCell(pMin), metricToCell(pMax));
279 static std::pair<Point3i, Size3i> getOffsetAndSize(
const Box3f& region,
float cellSize);
294 Box3i oldRegion = getMapRegion();
295 if(region==oldRegion)
312 Point3i copyRegionInNewLL(0,0,0);
313 Point3i copyRegionInOldLL(0,0,0);
315 for(
int i=0; i<3; ++i)
323 Box3i intersection = oldRegion & region;
325 Array* oldBuffer = this->mData;
326 Array* newBuffer = createArray(region.minCorner, region.maxCorner);
330 Point3i copyRegionInNewUR = copyRegionInNewLL + intersection.
size();
331 Point3i copyRegionInOldUR = copyRegionInOldLL + intersection.
size();
349 assert(copyRegionInNewLL.y()<=newBuffer.height());
350 for(
int y=0; y<copyRegionInNewLL.y(); ++y)
352 CellType* dest = newBuffer[y];
353 for(
int x=0; x<newBuffer.width(); ++x)
354 dest[x] = valueForNewCells;
358 assert(copyRegionInNewLL.y()>=0 && copyRegionInNewUR.y()<=newBuffer.height());
359 assert(copyRegionInNewUR.x()>=0 && copyRegionInNewLL.x()<=newBuffer.width());
360 for(
int y=copyRegionInNewLL.y(); y<copyRegionInNewUR.y(); ++y)
362 CellType* dest = newBuffer[y];
364 for(
int x=0; x<copyRegionInNewLL.x(); ++x)
365 dest[x] = valueForNewCells;
367 for(
int x=copyRegionInNewUR.x(); x<newBuffer.width(); ++x)
368 dest[x] = valueForNewCells;
372 assert(copyRegionInNewUR.y()>=0);
373 for(
int y=copyRegionInNewUR.y(); y<newBuffer.height(); ++y)
375 CellType* dest = newBuffer[y];
376 for(
int x=0; x<newBuffer.width(); ++x)
377 dest[x] = valueForNewCells;
381 for(
int y=0; y<intersection.height(); ++y)
383 const CellType* src = oldBuffer[y+copyRegionInOldLL.y()]+copyRegionInOldLL.x();
384 CellType* dest = newBuffer[y+copyRegionInNewLL.y()]+copyRegionInNewLL.x();
385 memcpy(dest, src, intersection.width()*
sizeof(CellType));
390 newBuffer = valueForNewCells;
393 mOffset = -region.minCorner;
394 oldBuffer = newBuffer;
401 Point3i lowerLeft (std::floor(region.x0() / cellSize),
402 std::floor(region.y0() / cellSize),
403 std::floor(region.z0() / cellSize));
404 Point3i upperRight(std::ceil(region.x1() / cellSize),
405 std::ceil(region.y1() / cellSize),
406 std::ceil(region.z1() / cellSize));
409 Size3i size = upperRight - lowerLeft;
411 return std::make_pair(offset,size);
VoxelMap(const Size3i &size, float cellSize, const Point3i &offset=Point3i(0, 0, 0))
Definition: VoxelMap.h:95
boost::multi_array::extent_range Range
Definition: VoxelMap.h:67
Box3i getMapRegion() const
Retuns the region that is covered by the GridMap in grid cells.
Definition: VoxelMap.h:153
VoxelMap & operator=(const T &value)
Set all voxel cells to the specified value.
Definition: VoxelMap.h:132
json_spirit::mArray Array
VoxelMap(const Box3i ®ion, float cellSize)
Definition: VoxelMap.h:113
boost::multi_array< T, 3 > Array
Definition: VoxelMap.h:53
const T & operator()(const Point3i &idx) const
Definition: VoxelMap.h:165
void clip(const Box3i ®ion, const T &valueForNewCells)
Definition: VoxelMap.h:292
Point3f map2world(const Point3i &p) const
Convert a given point to world coordinates.
Definition: VoxelMap.h:188
T & operator()(const Point3i &idx)
Definition: VoxelMap.h:161
Definition: VoxelMap.h:46
const Array & data() const
Definition: VoxelMap.h:159
~VoxelMap()
Definition: VoxelMap.h:117
Point3i getOffset() const
Returns the offset of the map, e.g. the index of the cell that is located in the origin.
Definition: VoxelMap.h:125
VoxelMap(float cellSize=0.1f)
Definition: VoxelMap.h:93
VoxelMap(const Box3f ®ion, float cellSize)
Creates a new voxel map that covers the specified region.
Definition: VoxelMap.h:107
Box3f getRegion() const
Retuns the region that is covered by the GridMap.
Definition: VoxelMap.h:148
Array & data()
Definition: VoxelMap.h:158
Point3i world2map(const Point3f &p) const
Convert a given point to map coordinates.
Definition: VoxelMap.h:178
float getCellSize() const
Returns the size of each cell in meter.
Definition: VoxelMap.h:122