3 #include <VirtualRobot/math/Helpers.h>
11 template <
typename Derived>
14 template <
typename Derived>
17 Derived result = matrix;
18 for (
typename Derived::Index i = 0; i < result.size(); ++i)
20 result(i) =
function(result(i));
26 const Eigen::IOFormat VoxelGridStructure::_iofVector = {5, 0,
" ",
" ",
"",
"",
"[",
"]"};
27 const Eigen::IOFormat VoxelGridStructure::_iofTimes = {5, 0,
"",
" x ",
"",
"",
"",
""};
35 int gridSize,
float voxelSize,
38 Eigen::Vector3f::Constant(voxelSize),
43 const Eigen::Vector3i& gridSize,
float voxelSize,
49 int gridSize,
const Eigen::Vector3f& voxelSize,
55 const Eigen::Vector3i& gridSize,
const Eigen::Vector3f& voxelSize,
58 _voxelSize(voxelSize),
60 _orientation(orientation)
94 _voxelSize = voxelSize;
99 return static_cast<std::size_t
>(_gridSize.prod());
109 const Eigen::Vector3f _halfGridSizes = halfGridSize();
113 long index =
static_cast<long>(
114 (
indices.x() + _halfGridSizes.x()) * _gridSize.z() * _gridSize.y()
115 + (
indices.y() + _halfGridSizes.y()) * _gridSize.z()
116 + (
indices.z() + _halfGridSizes.z()));
119 return static_cast<std::size_t
>(
index);
132 int x =
static_cast<int>(
index /
static_cast<std::size_t
>(_gridSize.z() * _gridSize.y()));
133 int rest =
static_cast<int>(
index %
static_cast<std::size_t
>(_gridSize.z() * _gridSize.y()));
134 int y = rest / _gridSize.z();
135 int z = rest % _gridSize.z();
143 Eigen::Vector3f pointLocal;
151 pointLocal = math::Helpers::TransformPosition(math::Helpers::InvertedPose(
getPose()), point);
154 Eigen::Vector3f scaled = (pointLocal.array() / _voxelSize.array()).matrix();
156 return cwise<Eigen::Vector3f>(&std::roundf, scaled).template cast<int>();
165 return - cwise<Eigen::Vector3f>(&floorf, halfGridSize()).template cast<int>();
174 const Eigen::Vector3f half = halfGridSize().array() - 0.5f;
175 return cwise<Eigen::Vector3f>(&floorf, half).template cast<int>();
191 auto center = _voxelSize.array() *
indices.array().cast<
float>();
198 return math::Helpers::TransformPosition(
getPose(), center);
219 _orientation = orientation;
224 return _origin + getOriginToGridCenter(
false);
229 setOrigin(center - getOriginToGridCenter(
false));
261 return _gridSize.cast<
float>().array() * _voxelSize.array();
267 bb.col(0) -= _voxelSize / 2;
268 bb.col(1) += _voxelSize / 2;
285 for (
int i = 0; i <
indices.size(); ++i)
304 <<
"\n- given: " <<
indices.format(_iofVector)
311 return _gridSize == rhs._gridSize
312 && _voxelSize.isApprox(rhs._voxelSize)
313 && _origin.isApprox(rhs._origin)
314 && _orientation.isApprox(rhs._orientation);
319 return !(*
this == rhs);
322 Eigen::Vector3f VoxelGridStructure::halfGridSize()
const
324 return _gridSize.cast<
float>() / 2;
327 Eigen::Vector3f VoxelGridStructure::getOriginToGridCenter(
bool local)
const
329 Eigen::Vector3f shift = shift.Zero();
330 for (Eigen::Vector3f::Index i = 0; i < shift.size(); ++i)
332 if (_gridSize(i) % 2 == 0)
334 shift(i) = - _voxelSize(i) / 2;
337 return local ? shift : (_orientation * shift);
343 os <<
"Structure with " << rhs.
getNumVoxels() <<
" voxels";
344 os <<
"\n- Origin: \t" << rhs._origin.format(rhs._iofVector);
345 os <<
"\n- Voxel size:\t" << rhs._voxelSize.format(rhs._iofTimes);
346 os <<
"\n- Grid size: \t" << rhs._gridSize.format(rhs._iofTimes);
347 os <<
"\n- Indices: \t" << rhs.
getGridIndexMin().format(rhs._iofVector)