Point Cloud Library (PCL) 1.14.0
Loading...
Searching...
No Matches
voxel_grid_occlusion_estimation.hpp
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * Author : Christian Potthast
35 * Email : potthast@usc.edu
36 *
37 */
38
39#ifndef PCL_FILTERS_IMPL_VOXEL_GRID_OCCLUSION_ESTIMATION_H_
40#define PCL_FILTERS_IMPL_VOXEL_GRID_OCCLUSION_ESTIMATION_H_
41
42#include <pcl/filters/voxel_grid_occlusion_estimation.h>
43
44//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45template <typename PointT> void
47{
48 // initialization set to true
49 initialized_ = true;
50
51 // create the voxel grid and store the output cloud
52 this->filter (filtered_cloud_);
53
54 // Get the minimum and maximum bounding box dimensions
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]);
61
62 // set the sensor origin and sensor orientation
63 sensor_origin_ = filtered_cloud_.sensor_origin_;
64 sensor_orientation_ = filtered_cloud_.sensor_orientation_;
65
66 Eigen::Vector3i ijk = this->getGridCoordinates(sensor_origin_.x(), sensor_origin_.y(), sensor_origin_.z());
67 // first check whether the sensor origin is within the voxel grid bounding box, then whether it is occupied
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;
71 }
72}
73
74//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75template <typename PointT> int
77 const Eigen::Vector3i& in_target_voxel)
78{
79 if (!initialized_)
80 {
81 PCL_ERROR ("Voxel grid not initialized; call initializeVoxelGrid () first! \n");
82 return -1;
83 }
84
85 // estimate direction to target voxel
86 Eigen::Vector4f p = getCentroidCoordinate (in_target_voxel);
87 Eigen::Vector4f direction = p - sensor_origin_;
88 direction.normalize ();
89
90 // estimate entry point into the voxel grid
91 float tmin = rayBoxIntersection (sensor_origin_, direction);
92
93 if (tmin == -1)
94 {
95 PCL_ERROR ("The ray does not intersect with the bounding box \n");
96 return -1;
97 }
98
99 // ray traversal
100 out_state = rayTraversal (in_target_voxel, sensor_origin_, direction, tmin);
101
102 return 0;
103}
104
105//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106template <typename PointT> int
108 std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i> >& out_ray,
109 const Eigen::Vector3i& in_target_voxel)
110{
111 if (!initialized_)
112 {
113 PCL_ERROR ("Voxel grid not initialized; call initializeVoxelGrid () first! \n");
114 return -1;
115 }
116
117 // estimate direction to target voxel
118 Eigen::Vector4f p = getCentroidCoordinate (in_target_voxel);
119 Eigen::Vector4f direction = p - sensor_origin_;
120 direction.normalize ();
121
122 // estimate entry point into the voxel grid
123 float tmin = rayBoxIntersection (sensor_origin_, direction);
124
125 if (tmin == -1)
126 {
127 PCL_ERROR ("The ray does not intersect with the bounding box \n");
128 return -1;
129 }
130
131 // ray traversal
132 out_state = rayTraversal (out_ray, in_target_voxel, sensor_origin_, direction, tmin);
133
134 return 0;
135}
136
137//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138template <typename PointT> int
139pcl::VoxelGridOcclusionEstimation<PointT>::occlusionEstimationAll (std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i> >& occluded_voxels)
140{
141 if (!initialized_)
142 {
143 PCL_ERROR ("Voxel grid not initialized; call initializeVoxelGrid () first! \n");
144 return -1;
145 }
146
147 // reserve space for the ray vector
148 int reserve_size = div_b_[0] * div_b_[1] * div_b_[2];
149 occluded_voxels.reserve (reserve_size);
150
151 // iterate over the entire voxel grid
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)
155 {
156 Eigen::Vector3i ijk (ii, jj, kk);
157 // process all free voxels
158 int index = this->getCentroidIndexAt (ijk);
159 if (index == -1)
160 {
161 // estimate direction to target voxel
162 Eigen::Vector4f p = getCentroidCoordinate (ijk);
163 Eigen::Vector4f direction = p - sensor_origin_;
164 direction.normalize ();
165
166 // estimate entry point into the voxel grid
167 float tmin = rayBoxIntersection (sensor_origin_, direction);
168
169 // ray traversal
170 int state = rayTraversal (ijk, sensor_origin_, direction, tmin);
171
172 // if voxel is occluded
173 if (state == 1)
174 occluded_voxels.push_back (ijk);
175 }
176 }
177 return 0;
178}
179
180//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
181template <typename PointT> float
183 const Eigen::Vector4f& direction)
184{
185 float tmin, tmax, tymin, tymax, tzmin, tzmax;
186
187 if (direction[0] >= 0)
188 {
189 tmin = (b_min_[0] - origin[0]) / direction[0];
190 tmax = (b_max_[0] - origin[0]) / direction[0];
191 }
192 else
193 {
194 tmin = (b_max_[0] - origin[0]) / direction[0];
195 tmax = (b_min_[0] - origin[0]) / direction[0];
196 }
197
198 if (direction[1] >= 0)
199 {
200 tymin = (b_min_[1] - origin[1]) / direction[1];
201 tymax = (b_max_[1] - origin[1]) / direction[1];
202 }
203 else
204 {
205 tymin = (b_max_[1] - origin[1]) / direction[1];
206 tymax = (b_min_[1] - origin[1]) / direction[1];
207 }
208
209 if ((tmin > tymax) || (tymin > tmax))
210 {
211 PCL_ERROR ("no intersection with the bounding box \n");
212 tmin = -1.0f;
213 return tmin;
214 }
215
216 if (tymin > tmin)
217 tmin = tymin;
218 if (tymax < tmax)
219 tmax = tymax;
220
221 if (direction[2] >= 0)
222 {
223 tzmin = (b_min_[2] - origin[2]) / direction[2];
224 tzmax = (b_max_[2] - origin[2]) / direction[2];
225 }
226 else
227 {
228 tzmin = (b_max_[2] - origin[2]) / direction[2];
229 tzmax = (b_min_[2] - origin[2]) / direction[2];
230 }
231
232 if ((tmin > tzmax) || (tzmin > tmax))
233 {
234 PCL_ERROR ("no intersection with the bounding box \n");
235 tmin = -1.0f;
236 return tmin;
237 }
238
239 if (tzmin > tmin)
240 tmin = tzmin;
241 if (tzmax < tmax)
242 tmax = tzmax;
243 return std::max<float>(tmin, 0.0f); // We only want to go in "positive" direction here, not in negative. This is relevant if the origin is inside the bounding box
244}
245
246//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
247template <typename PointT> int
249 const Eigen::Vector4f& origin,
250 const Eigen::Vector4f& direction,
251 const float t_min)
252{
253 // coordinate of the boundary of the voxel grid
254 Eigen::Vector4f start = origin + t_min * direction;
255
256 // i,j,k coordinate of the voxel were the ray enters the voxel grid
257 Eigen::Vector3i ijk = getGridCoordinatesRound (start[0], start[1], start[2]);
258
259 // steps in which direction we have to travel in the voxel grid
260 int step_x, step_y, step_z;
261
262 // centroid coordinate of the entry voxel
263 Eigen::Vector4f voxel_max = getCentroidCoordinate (ijk);
264
265 if (direction[0] >= 0)
266 {
267 voxel_max[0] += leaf_size_[0] * 0.5f;
268 step_x = 1;
269 }
270 else
271 {
272 voxel_max[0] -= leaf_size_[0] * 0.5f;
273 step_x = -1;
274 }
275 if (direction[1] >= 0)
276 {
277 voxel_max[1] += leaf_size_[1] * 0.5f;
278 step_y = 1;
279 }
280 else
281 {
282 voxel_max[1] -= leaf_size_[1] * 0.5f;
283 step_y = -1;
284 }
285 if (direction[2] >= 0)
286 {
287 voxel_max[2] += leaf_size_[2] * 0.5f;
288 step_z = 1;
289 }
290 else
291 {
292 voxel_max[2] -= leaf_size_[2] * 0.5f;
293 step_z = -1;
294 }
295
296 float t_max_x = t_min + (voxel_max[0] - start[0]) / direction[0];
297 float t_max_y = t_min + (voxel_max[1] - start[1]) / direction[1];
298 float t_max_z = t_min + (voxel_max[2] - start[2]) / direction[2];
299
300 float t_delta_x = leaf_size_[0] / static_cast<float> (std::abs (direction[0]));
301 float t_delta_y = leaf_size_[1] / static_cast<float> (std::abs (direction[1]));
302 float t_delta_z = leaf_size_[2] / static_cast<float> (std::abs (direction[2]));
303
304 while ( (ijk[0] < max_b_[0]+1) && (ijk[0] >= min_b_[0]) &&
305 (ijk[1] < max_b_[1]+1) && (ijk[1] >= min_b_[1]) &&
306 (ijk[2] < max_b_[2]+1) && (ijk[2] >= min_b_[2]) )
307 {
308 // check if we reached target voxel
309 if (ijk[0] == target_voxel[0] && ijk[1] == target_voxel[1] && ijk[2] == target_voxel[2])
310 return 0;
311
312 // index of the point in the point cloud
313 int index = this->getCentroidIndexAt (ijk);
314 // check if voxel is occupied, if yes return 1 for occluded
315 if (index != -1)
316 return 1;
317
318 // estimate next voxel
319 if(t_max_x <= t_max_y && t_max_x <= t_max_z)
320 {
321 t_max_x += t_delta_x;
322 ijk[0] += step_x;
323 }
324 else if(t_max_y <= t_max_z && t_max_y <= t_max_x)
325 {
326 t_max_y += t_delta_y;
327 ijk[1] += step_y;
328 }
329 else
330 {
331 t_max_z += t_delta_z;
332 ijk[2] += step_z;
333 }
334 }
335 return 0;
336}
337
338//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
339template <typename PointT> int
340pcl::VoxelGridOcclusionEstimation<PointT>::rayTraversal (std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i> >& out_ray,
341 const Eigen::Vector3i& target_voxel,
342 const Eigen::Vector4f& origin,
343 const Eigen::Vector4f& direction,
344 const float t_min)
345{
346 // reserve space for the ray vector
347 int reserve_size = div_b_.maxCoeff () * div_b_.maxCoeff ();
348 out_ray.reserve (reserve_size);
349
350 // coordinate of the boundary of the voxel grid
351 Eigen::Vector4f start = origin + t_min * direction;
352
353 // i,j,k coordinate of the voxel were the ray enters the voxel grid
354 Eigen::Vector3i ijk = getGridCoordinatesRound (start[0], start[1], start[2]);
355 //Eigen::Vector3i ijk = this->getGridCoordinates (start_x, start_y, start_z);
356
357 // steps in which direction we have to travel in the voxel grid
358 int step_x, step_y, step_z;
359
360 // centroid coordinate of the entry voxel
361 Eigen::Vector4f voxel_max = getCentroidCoordinate (ijk);
362
363 if (direction[0] >= 0)
364 {
365 voxel_max[0] += leaf_size_[0] * 0.5f;
366 step_x = 1;
367 }
368 else
369 {
370 voxel_max[0] -= leaf_size_[0] * 0.5f;
371 step_x = -1;
372 }
373 if (direction[1] >= 0)
374 {
375 voxel_max[1] += leaf_size_[1] * 0.5f;
376 step_y = 1;
377 }
378 else
379 {
380 voxel_max[1] -= leaf_size_[1] * 0.5f;
381 step_y = -1;
382 }
383 if (direction[2] >= 0)
384 {
385 voxel_max[2] += leaf_size_[2] * 0.5f;
386 step_z = 1;
387 }
388 else
389 {
390 voxel_max[2] -= leaf_size_[2] * 0.5f;
391 step_z = -1;
392 }
393
394 float t_max_x = t_min + (voxel_max[0] - start[0]) / direction[0];
395 float t_max_y = t_min + (voxel_max[1] - start[1]) / direction[1];
396 float t_max_z = t_min + (voxel_max[2] - start[2]) / direction[2];
397
398 float t_delta_x = leaf_size_[0] / static_cast<float> (std::abs (direction[0]));
399 float t_delta_y = leaf_size_[1] / static_cast<float> (std::abs (direction[1]));
400 float t_delta_z = leaf_size_[2] / static_cast<float> (std::abs (direction[2]));
401
402 // the index of the cloud (-1 if empty)
403 int result = 0;
404
405 while ( (ijk[0] < max_b_[0]+1) && (ijk[0] >= min_b_[0]) &&
406 (ijk[1] < max_b_[1]+1) && (ijk[1] >= min_b_[1]) &&
407 (ijk[2] < max_b_[2]+1) && (ijk[2] >= min_b_[2]) )
408 {
409 // add voxel to ray
410 out_ray.push_back (ijk);
411
412 // check if we reached target voxel
413 if (ijk[0] == target_voxel[0] && ijk[1] == target_voxel[1] && ijk[2] == target_voxel[2])
414 break;
415
416 // check if voxel is occupied
417 int index = this->getCentroidIndexAt (ijk);
418 if (index != -1)
419 result = 1;
420
421 // estimate next voxel
422 if(t_max_x <= t_max_y && t_max_x <= t_max_z)
423 {
424 t_max_x += t_delta_x;
425 ijk[0] += step_x;
426 }
427 else if(t_max_y <= t_max_z && t_max_y <= t_max_x)
428 {
429 t_max_y += t_delta_y;
430 ijk[1] += step_y;
431 }
432 else
433 {
434 t_max_z += t_delta_z;
435 ijk[2] += step_z;
436 }
437 }
438 return result;
439}
440
441//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
442#define PCL_INSTANTIATE_VoxelGridOcclusionEstimation(T) template class PCL_EXPORTS pcl::VoxelGridOcclusionEstimation<T>;
443
444#endif // PCL_FILTERS_IMPL_VOXEL_GRID_OCCLUSION_ESTIMATION_H_
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...