Point Cloud Library (PCL) 1.15.1
Loading...
Searching...
No Matches
approximate_voxel_grid.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 the copyright holder(s) 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 * $Id: voxel_grid.hpp 1600 2011-07-07 16:55:51Z shapovalov $
35 *
36 */
37
38#ifndef PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_
39#define PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_
40
41#include <pcl/common/io.h>
42#include <pcl/common/point_tests.h>
43#include <pcl/filters/approximate_voxel_grid.h>
44#include <boost/mpl/size.hpp> // for size
45
46//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47template <typename PointT> void
48pcl::ApproximateVoxelGrid<PointT>::flush (PointCloud &output, std::size_t op, he *hhe, int rgba_index, int centroid_size)
49{
50 hhe->centroid /= static_cast<float> (hhe->count);
51 pcl::for_each_type <FieldList> (pcl::xNdCopyEigenPointFunctor <PointT> (hhe->centroid, output[op]));
52 // ---[ RGB special case
53 if (rgba_index >= 0)
54 {
55 // pack r/g/b into rgb
56 float r = hhe->centroid[centroid_size-3],
57 g = hhe->centroid[centroid_size-2],
58 b = hhe->centroid[centroid_size-1];
59 int rgb = (static_cast<int> (r)) << 16 | (static_cast<int> (g)) << 8 | (static_cast<int> (b));
60 memcpy (reinterpret_cast<char*> (&output[op]) + rgba_index, &rgb, sizeof (float));
61 }
62}
63
64//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65template <typename PointT> void
67{
68 int centroid_size = 4;
70 centroid_size = boost::mpl::size<FieldList>::value;
71
72 // ---[ RGB special case
73 std::vector<pcl::PCLPointField> fields;
74 int rgba_index = -1;
75 rgba_index = pcl::getFieldIndex<PointT> ("rgb", fields);
76 if (rgba_index == -1)
77 rgba_index = pcl::getFieldIndex<PointT> ("rgba", fields);
78 if (rgba_index >= 0)
79 {
80 rgba_index = fields[rgba_index].offset;
81 centroid_size += 3;
82 }
83
84 for (std::size_t i = 0; i < histsize_; i++)
85 {
86 history_[i].count = 0;
87 history_[i].centroid = Eigen::VectorXf::Zero (centroid_size);
88 }
89 Eigen::VectorXf scratch = Eigen::VectorXf::Zero (centroid_size);
90
91 output.resize (input_->size ()); // size output for worst case
92 std::size_t op = 0; // output pointer
93 for (const auto& point: *input_)
94 {
95 if(!pcl::isXYZFinite(point))
96 continue;
97 int ix = static_cast<int> (std::floor (point.x * inverse_leaf_size_[0]));
98 int iy = static_cast<int> (std::floor (point.y * inverse_leaf_size_[1]));
99 int iz = static_cast<int> (std::floor (point.z * inverse_leaf_size_[2]));
100 auto hash = static_cast<unsigned int> ((ix * 7171 + iy * 3079 + iz * 4231) & (histsize_ - 1));
101 he *hhe = &history_[hash];
102 if (hhe->count && ((ix != hhe->ix) || (iy != hhe->iy) || (iz != hhe->iz)))
103 {
104 flush (output, op++, hhe, rgba_index, centroid_size);
105 hhe->count = 0;
106 hhe->centroid.setZero ();// = Eigen::VectorXf::Zero (centroid_size);
107 }
108 hhe->ix = ix;
109 hhe->iy = iy;
110 hhe->iz = iz;
111 hhe->count++;
112
113 // Unpack the point into scratch, then accumulate
114 // ---[ RGB special case
115 if (rgba_index >= 0)
116 {
117 // fill r/g/b data
118 pcl::RGB rgb;
119 memcpy (&rgb, (reinterpret_cast<const char *> (&point)) + rgba_index, sizeof (RGB));
120 scratch[centroid_size-3] = rgb.r;
121 scratch[centroid_size-2] = rgb.g;
122 scratch[centroid_size-1] = rgb.b;
123 }
124 pcl::for_each_type <FieldList> (xNdCopyPointEigenFunctor <PointT> (point, scratch));
125 hhe->centroid += scratch;
126 }
127 for (std::size_t i = 0; i < histsize_; i++)
128 {
129 he *hhe = &history_[i];
130 if (hhe->count)
131 flush (output, op++, hhe, rgba_index, centroid_size);
132 }
133 output.resize (op);
134 output.width = output.size ();
135 output.height = 1; // downsampling breaks the organized structure
136 output.is_dense = true; // we filter out invalid points
137}
138
139#define PCL_INSTANTIATE_ApproximateVoxelGrid(T) template class PCL_EXPORTS pcl::ApproximateVoxelGrid<T>;
140
141#endif // PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_
bool downsample_all_data_
Set to true if all fields need to be downsampled, or false if just XYZ.
Eigen::Array3f inverse_leaf_size_
Compute 1/leaf_size_ to avoid division later.
std::size_t histsize_
history buffer size, power of 2
void applyFilter(PointCloud &output) override
Downsample a Point Cloud using a voxelized grid approach.
struct he * history_
history buffer
void flush(PointCloud &output, std::size_t op, he *hhe, int rgba_index, int centroid_size)
Write a single point from the hash to the output cloud.
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
int getFieldIndex(const pcl::PointCloud< PointT > &, const std::string &field_name, std::vector< pcl::PCLPointField > &fields)
Definition io.hpp:52
void for_each_type(F f)
constexpr bool isXYZFinite(const PointT &) noexcept
A structure representing RGB color information.