Point Cloud Library (PCL)  1.15.0-dev
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT> void
48 pcl::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 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 template <typename PointT> void
67 {
68  int centroid_size = 4;
69  if (downsample_all_data_)
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_
void applyFilter(PointCloud &output) override
Downsample a Point Cloud using a voxelized grid approach.
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.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
std::size_t size() const
Definition: point_cloud.h:443
constexpr bool isXYZFinite(const PointT &) noexcept
Definition: point_tests.h:125
A structure representing RGB color information.
Helper functor structure for copying data between an Eigen::VectorXf and a PointT.
Helper functor structure for copying data between an Eigen::VectorXf and a PointT.