91 VoxelGrid(
double size_x,
double size_y,
double size_z,
double resolution,
double origin_x,
double origin_y,
92 double origin_z, T default_object);
120 void resize(
double size_x,
double size_y,
double size_z,
double resolution,
double origin_x,
double origin_y,
121 double origin_z, T default_object);
136 const T&
operator()(
double x,
double y,
double z)
const;
156 T&
getCell(
int x,
int y,
int z);
157 T&
getCell(
const Eigen::Vector3i& pos);
158 const T&
getCell(
int x,
int y,
int z)
const;
159 const T&
getCell(
const Eigen::Vector3i& pos)
const;
179 void setCell(
int x,
int y,
int z,
const T& obj);
180 void setCell(
const Eigen::Vector3i& pos,
const T& obj);
187 void reset(
const T& initial);
244 void gridToWorld(
int x,
int y,
int z,
double& world_x,
double& world_y,
double& world_z)
const;
263 bool worldToGrid(
double world_x,
double world_y,
double world_z,
int& x,
int& y,
int& z)
const;
276 bool isCellValid(
const Eigen::Vector3i& pos)
const;
311 int ref(
int x,
int y,
int z)
const;
341 template <
typename T>
343 double origin_y,
double origin_z, T default_object)
346 resize(size_x, size_y, size_z, resolution, origin_x, origin_y, origin_z, default_object);
349 template <
typename T>
350 VoxelGrid<T>::VoxelGrid() : data_(NULL)
356 origin_minus_[i] = 0;
360 oo_resolution_ = 1.0 / resolution_;
361 num_cells_total_ = 0;
366 template <
typename T>
367 void VoxelGrid<T>::resize(
double size_x,
double size_y,
double size_z,
double resolution,
double origin_x,
368 double origin_y,
double origin_z, T default_object)
373 size_[
DIM_X] = size_x;
375 size_[
DIM_Z] = size_z;
376 origin_[
DIM_X] = origin_x;
377 origin_[
DIM_Y] = origin_y;
378 origin_[
DIM_Z] = origin_z;
379 origin_minus_[
DIM_X] = origin_x - 0.5 * resolution;
380 origin_minus_[
DIM_Y] = origin_y - 0.5 * resolution;
381 origin_minus_[
DIM_Z] = origin_z - 0.5 * resolution;
382 num_cells_total_ = 1;
383 resolution_ = resolution;
384 oo_resolution_ = 1.0 / resolution_;
387 num_cells_[i] = size_[i] * oo_resolution_;
388 num_cells_total_ *= num_cells_[i];
391 default_object_ = default_object;
393 stride1_ = num_cells_[
DIM_Y] * num_cells_[
DIM_Z];
394 stride2_ = num_cells_[
DIM_Z];
397 if (num_cells_total_ > 0)
398 data_ =
new T[num_cells_total_];
401 template <
typename T>
407 template <
typename T>
408 inline bool VoxelGrid<T>::isCellValid(
int x,
int y,
int z)
const
410 return (x >= 0 && x < num_cells_[
DIM_X] && y >= 0 && y < num_cells_[
DIM_Y] && z >= 0 && z < num_cells_[
DIM_Z]);
413 template <
typename T>
414 inline bool VoxelGrid<T>::isCellValid(
const Eigen::Vector3i& pos)
const
416 return isCellValid(pos.x(), pos.y(), pos.z());
419 template <
typename T>
420 inline bool VoxelGrid<T>::isCellValid(
Dimension dim,
int cell)
const
422 return cell >= 0 && cell < num_cells_[dim];
425 template <
typename T>
426 inline int VoxelGrid<T>::ref(
int x,
int y,
int z)
const
428 return x * stride1_ +
y * stride2_ +
z;
431 template <
typename T>
432 inline double VoxelGrid<T>::getSize(
Dimension dim)
const
437 template <
typename T>
443 template <
typename T>
449 template <
typename T>
455 template <
typename T>
458 return num_cells_[dim];
461 template <
typename T>
464 int cell_x = getCellFromLocation(
DIM_X, x);
465 int cell_y = getCellFromLocation(
DIM_Y, y);
466 int cell_z = getCellFromLocation(
DIM_Z, z);
467 if (!isCellValid(cell_x, cell_y, cell_z))
468 return default_object_;
469 return getCell(cell_x, cell_y, cell_z);
472 template <
typename T>
475 return this->operator()(pos.x(), pos.y(), pos.z());
478 template <
typename T>
481 return data_[ref(
x,
y,
z)];
484 template <
typename T>
487 return data_[ref(
x,
y,
z)];
490 template <
typename T>
493 return data_[ref(pos.x(), pos.y(), pos.z())];
496 template <
typename T>
499 return data_[ref(pos.x(), pos.y(), pos.z())];
502 template <
typename T>
503 inline void VoxelGrid<T>::setCell(
int x,
int y,
int z,
const T& obj)
505 data_[ref(x, y, z)] =
obj;
508 template <
typename T>
511 data_[ref(pos.x(), pos.y(), pos.z())] =
obj;
514 template <
typename T>
531 return int(floor((loc - origin_minus_[dim]) * oo_resolution_));
534 template <
typename T>
537 return origin_[dim] + resolution_ * (double(cell));
540 template <
typename T>
543 std::fill(data_, data_ + num_cells_total_, initial);
546 template <
typename T>
549 world_x = getLocationFromCell(
DIM_X,
x);
550 world_y = getLocationFromCell(
DIM_Y,
y);
551 world_z = getLocationFromCell(
DIM_Z,
z);
554 template <
typename T>
557 world.x() = getLocationFromCell(
DIM_X, grid.x());
558 world.y() = getLocationFromCell(
DIM_Y, grid.y());
559 world.z() = getLocationFromCell(
DIM_Z, grid.z());
562 template <
typename T>
563 inline bool VoxelGrid<T>::worldToGrid(
double world_x,
double world_y,
double world_z,
int& x,
int& y,
int& z)
const
565 x = getCellFromLocation(
DIM_X, world_x);
566 y = getCellFromLocation(
DIM_Y, world_y);
568 return isCellValid(x, y, z);
571 template <
typename T>
574 grid.x() = getCellFromLocation(
DIM_X, world.x());
575 grid.y() = getCellFromLocation(
DIM_Y, world.y());
576 grid.z() = getCellFromLocation(
DIM_Z, world.z());
577 return isCellValid(grid.x(), grid.y(), grid.z());