39 #ifndef PCL_FILTERS_IMPL_VOXEL_GRID_OCCLUSION_ESTIMATION_H_
40 #define PCL_FILTERS_IMPL_VOXEL_GRID_OCCLUSION_ESTIMATION_H_
42 #include <pcl/filters/voxel_grid_occlusion_estimation.h>
45 template <
typename Po
intT>
void
52 this->filter (filtered_cloud_);
55 b_min_[0] = (
static_cast<float> ( min_b_[0]) * leaf_size_[0]);
56 b_min_[1] = (
static_cast<float> ( min_b_[1]) * leaf_size_[1]);
57 b_min_[2] = (
static_cast<float> ( min_b_[2]) * leaf_size_[2]);
58 b_max_[0] = (
static_cast<float> ( (max_b_[0]) + 1) * leaf_size_[0]);
59 b_max_[1] = (
static_cast<float> ( (max_b_[1]) + 1) * leaf_size_[1]);
60 b_max_[2] = (
static_cast<float> ( (max_b_[2]) + 1) * leaf_size_[2]);
63 sensor_origin_ = filtered_cloud_.sensor_origin_;
64 sensor_orientation_ = filtered_cloud_.sensor_orientation_;
66 Eigen::Vector3i ijk = this->getGridCoordinates(sensor_origin_.x(), sensor_origin_.y(), sensor_origin_.z());
68 if((ijk[0]>=min_b_[0] && ijk[1]>=min_b_[1] && ijk[2]>=min_b_[2] && ijk[0]<=max_b_[0] && ijk[1]<=max_b_[1] && ijk[2]<=max_b_[2]) && this->getCentroidIndexAt (ijk) != -1) {
69 PCL_WARN (
"[VoxelGridOcclusionEstimation::initializeVoxelGrid] The voxel containing the sensor origin (%g, %g, %g) is marked as occupied. We will instead assume that it is free, to be able to perform the occlusion estimation.\n", sensor_origin_.x(), sensor_origin_.y(), sensor_origin_.z());
70 this->leaf_layout_[((Eigen::Vector4i() << ijk, 0).finished() - min_b_).dot (this->divb_mul_)] = -1;
75 template <
typename Po
intT>
int
77 const Eigen::Vector3i& in_target_voxel)
81 PCL_ERROR (
"Voxel grid not initialized; call initializeVoxelGrid () first! \n");
86 Eigen::Vector4f p = getCentroidCoordinate (in_target_voxel);
87 Eigen::Vector4f direction = p - sensor_origin_;
88 direction.normalize ();
91 float tmin = rayBoxIntersection (sensor_origin_, direction);
95 PCL_ERROR (
"The ray does not intersect with the bounding box \n");
100 out_state = rayTraversal (in_target_voxel, sensor_origin_, direction, tmin);
106 template <
typename Po
intT>
int
108 std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i> >& out_ray,
109 const Eigen::Vector3i& in_target_voxel)
113 PCL_ERROR (
"Voxel grid not initialized; call initializeVoxelGrid () first! \n");
118 Eigen::Vector4f p = getCentroidCoordinate (in_target_voxel);
119 Eigen::Vector4f direction = p - sensor_origin_;
120 direction.normalize ();
123 float tmin = rayBoxIntersection (sensor_origin_, direction);
127 PCL_ERROR (
"The ray does not intersect with the bounding box \n");
132 out_state = rayTraversal (out_ray, in_target_voxel, sensor_origin_, direction, tmin);
138 template <
typename Po
intT>
int
143 PCL_ERROR (
"Voxel grid not initialized; call initializeVoxelGrid () first! \n");
148 int reserve_size = div_b_[0] * div_b_[1] * div_b_[2];
149 occluded_voxels.reserve (reserve_size);
152 for (
int kk = min_b_.z (); kk <= max_b_.z (); ++kk)
153 for (
int jj = min_b_.y (); jj <= max_b_.y (); ++jj)
154 for (
int ii = min_b_.x (); ii <= max_b_.x (); ++ii)
156 Eigen::Vector3i ijk (ii, jj, kk);
158 int index = this->getCentroidIndexAt (ijk);
162 Eigen::Vector4f p = getCentroidCoordinate (ijk);
163 Eigen::Vector4f direction = p - sensor_origin_;
164 direction.normalize ();
167 float tmin = rayBoxIntersection (sensor_origin_, direction);
170 int state = rayTraversal (ijk, sensor_origin_, direction, tmin);
174 occluded_voxels.push_back (ijk);
181 template <
typename Po
intT>
float
183 const Eigen::Vector4f& direction)
185 float tmin, tmax, tymin, tymax, tzmin, tzmax;
187 if (direction[0] >= 0)
189 tmin = (b_min_[0] - origin[0]) / direction[0];
190 tmax = (b_max_[0] - origin[0]) / direction[0];
194 tmin = (b_max_[0] - origin[0]) / direction[0];
195 tmax = (b_min_[0] - origin[0]) / direction[0];
198 if (direction[1] >= 0)
200 tymin = (b_min_[1] - origin[1]) / direction[1];
201 tymax = (b_max_[1] - origin[1]) / direction[1];
205 tymin = (b_max_[1] - origin[1]) / direction[1];
206 tymax = (b_min_[1] - origin[1]) / direction[1];
209 if ((tmin > tymax) || (tymin > tmax))
211 PCL_ERROR (
"no intersection with the bounding box \n");
221 if (direction[2] >= 0)
223 tzmin = (b_min_[2] - origin[2]) / direction[2];
224 tzmax = (b_max_[2] - origin[2]) / direction[2];
228 tzmin = (b_max_[2] - origin[2]) / direction[2];
229 tzmax = (b_min_[2] - origin[2]) / direction[2];
232 if ((tmin > tzmax) || (tzmin > tmax))
234 PCL_ERROR (
"no intersection with the bounding box \n");
243 return std::max<float>(tmin, 0.0f);
247 template <
typename Po
intT>
int
249 const Eigen::Vector4f& origin,
250 const Eigen::Vector4f& direction,
255 Eigen::Vector4f start = origin + (t_min > 0.0f ? (t_min + std::numeric_limits<float>::epsilon()) : t_min) * direction;
258 Eigen::Vector3i ijk = this->getGridCoordinates(start[0], start[1], start[2]);
261 int step_x, step_y, step_z;
264 Eigen::Vector4f voxel_max = getCentroidCoordinate (ijk);
266 if (direction[0] >= 0)
268 voxel_max[0] += leaf_size_[0] * 0.5f;
273 voxel_max[0] -= leaf_size_[0] * 0.5f;
276 if (direction[1] >= 0)
278 voxel_max[1] += leaf_size_[1] * 0.5f;
283 voxel_max[1] -= leaf_size_[1] * 0.5f;
286 if (direction[2] >= 0)
288 voxel_max[2] += leaf_size_[2] * 0.5f;
293 voxel_max[2] -= leaf_size_[2] * 0.5f;
297 float t_max_x = t_min + (voxel_max[0] - start[0]) / direction[0];
298 float t_max_y = t_min + (voxel_max[1] - start[1]) / direction[1];
299 float t_max_z = t_min + (voxel_max[2] - start[2]) / direction[2];
301 float t_delta_x = leaf_size_[0] /
static_cast<float> (std::abs (direction[0]));
302 float t_delta_y = leaf_size_[1] /
static_cast<float> (std::abs (direction[1]));
303 float t_delta_z = leaf_size_[2] /
static_cast<float> (std::abs (direction[2]));
305 while ( (ijk[0] < max_b_[0]+1) && (ijk[0] >= min_b_[0]) &&
306 (ijk[1] < max_b_[1]+1) && (ijk[1] >= min_b_[1]) &&
307 (ijk[2] < max_b_[2]+1) && (ijk[2] >= min_b_[2]) )
310 if (ijk[0] == target_voxel[0] && ijk[1] == target_voxel[1] && ijk[2] == target_voxel[2])
314 int index = this->getCentroidIndexAt (ijk);
320 if(t_max_x <= t_max_y && t_max_x <= t_max_z)
322 t_max_x += t_delta_x;
325 else if(t_max_y <= t_max_z && t_max_y <= t_max_x)
327 t_max_y += t_delta_y;
332 t_max_z += t_delta_z;
340 template <
typename Po
intT>
int
342 const Eigen::Vector3i& target_voxel,
343 const Eigen::Vector4f& origin,
344 const Eigen::Vector4f& direction,
348 int reserve_size = div_b_.maxCoeff () * div_b_.maxCoeff ();
349 out_ray.reserve (reserve_size);
353 Eigen::Vector4f start = origin + (t_min > 0.0f ? (t_min + std::numeric_limits<float>::epsilon()) : t_min) * direction;
356 Eigen::Vector3i ijk = this->getGridCoordinates (start[0], start[1], start[2]);
359 int step_x, step_y, step_z;
362 Eigen::Vector4f voxel_max = getCentroidCoordinate (ijk);
364 if (direction[0] >= 0)
366 voxel_max[0] += leaf_size_[0] * 0.5f;
371 voxel_max[0] -= leaf_size_[0] * 0.5f;
374 if (direction[1] >= 0)
376 voxel_max[1] += leaf_size_[1] * 0.5f;
381 voxel_max[1] -= leaf_size_[1] * 0.5f;
384 if (direction[2] >= 0)
386 voxel_max[2] += leaf_size_[2] * 0.5f;
391 voxel_max[2] -= leaf_size_[2] * 0.5f;
395 float t_max_x = t_min + (voxel_max[0] - start[0]) / direction[0];
396 float t_max_y = t_min + (voxel_max[1] - start[1]) / direction[1];
397 float t_max_z = t_min + (voxel_max[2] - start[2]) / direction[2];
399 float t_delta_x = leaf_size_[0] /
static_cast<float> (std::abs (direction[0]));
400 float t_delta_y = leaf_size_[1] /
static_cast<float> (std::abs (direction[1]));
401 float t_delta_z = leaf_size_[2] /
static_cast<float> (std::abs (direction[2]));
406 while ( (ijk[0] < max_b_[0]+1) && (ijk[0] >= min_b_[0]) &&
407 (ijk[1] < max_b_[1]+1) && (ijk[1] >= min_b_[1]) &&
408 (ijk[2] < max_b_[2]+1) && (ijk[2] >= min_b_[2]) )
411 out_ray.push_back (ijk);
414 if (ijk[0] == target_voxel[0] && ijk[1] == target_voxel[1] && ijk[2] == target_voxel[2])
418 int index = this->getCentroidIndexAt (ijk);
423 if(t_max_x <= t_max_y && t_max_x <= t_max_z)
425 t_max_x += t_delta_x;
428 else if(t_max_y <= t_max_z && t_max_y <= t_max_x)
430 t_max_y += t_delta_y;
435 t_max_z += t_delta_z;
443 #define PCL_INSTANTIATE_VoxelGridOcclusionEstimation(T) template class PCL_EXPORTS pcl::VoxelGridOcclusionEstimation<T>;
void initializeVoxelGrid()
Initialize the voxel grid, needs to be called first Builts the voxel grid and computes additional val...
int occlusionEstimationAll(std::vector< Eigen::Vector3i, Eigen::aligned_allocator< Eigen::Vector3i > > &occluded_voxels)
Computes the voxel coordinates (i, j, k) of all occluded voxels in the voxel grid.
float rayBoxIntersection(const Eigen::Vector4f &origin, const Eigen::Vector4f &direction)
Returns the scaling value (tmin) were the ray intersects with the voxel grid bounding box.
int rayTraversal(const Eigen::Vector3i &target_voxel, const Eigen::Vector4f &origin, const Eigen::Vector4f &direction, const float t_min)
Returns the state of the target voxel (0 = visible, 1 = occupied) using a ray traversal algorithm.
int occlusionEstimation(int &out_state, const Eigen::Vector3i &in_target_voxel)
Computes the state (free = 0, occluded = 1) of the voxel after utilizing a ray traversal algorithm to...