Point Cloud Library (PCL)  1.14.0-dev
voxel_grid.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, 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  */
35 
36 #pragma once
37 
38 PCL_DEPRECATED_HEADER(1, 16, "The CUDA VoxelGrid filter does not work. Use the CPU VoxelGrid filter instead.")
39 
40 #include <pcl_cuda/filters/filter.h>
41 #include <pcl_cuda/filters/passthrough.h>
42 #include <thrust/count.h>
43 #include <thrust/remove.h>
44 #include <vector_types.h>
45 
46 namespace pcl_cuda
47 {
48  ///////////////////////////////////////////////////////////////////////////////////////////
49  /** \brief @b VoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
50  */
51  template <typename CloudT>
52  class VoxelGrid: public Filter<CloudT>
53  {
54  public:
56 
57  using PointCloud = typename PCLCUDABase<CloudT>::PointCloud;
58  using PointCloudPtr = typename PointCloud::Ptr;
60 
61  /** \brief Empty constructor. */
63  {
64  filter_name_ = "VoxelGrid";
65  };
66 
67  protected:
68  /** \brief Filter a Point Cloud.
69  * \param output the resultant point cloud message
70  */
71  void
73  {
74  std::cerr << "applyFilter" << std::endl;
75  }
76  };
77 
78  ///////////////////////////////////////////////////////////////////////////////////////////
79  template <>
80  class VoxelGrid<PointCloudAOS<Device> >: public Filter<PointCloudAOS<Device> >
81  {
82  public:
83  /** \brief Empty constructor. */
85  {
86  filter_name_ = "VoxelGridAOS";
87  };
88 
89  protected:
90  /** \brief Filter a Point Cloud.
91  * \param output the resultant point cloud message
92  */
93  void
95  {
96  // Allocate enough space
97  output.resize (input_->points.size ());
98  // Copy data
99  Device<PointXYZRGB>::type::iterator nr_points = thrust::copy_if (input_->points.begin (), input_->points.end (), output.begin (), isFiniteAOS ());
100  output.resize (nr_points - output.begin ());
101 
102  //std::cerr << "[applyFilterAOS]: ";
103  //std::cerr << input_->points.size () << " " << output.size () << std::endl;
104  }
105  };
106 
107  //////////////////////////////////////////////////////////////////////////////////////////
108  template <>
109  class VoxelGrid<PointCloudSOA<Device> >: public Filter<PointCloudSOA<Device> >
110  {
111  public:
112  /** \brief Empty constructor. */
113  VoxelGrid () : zip_(false)
114  {
115  filter_name_ = "VoxelGridSOA";
116  };
117 
118  inline void
119  setZip (bool zip)
120  {
121  zip_ = zip;
122  }
123 
124 
125  protected:
126  /** \brief Filter a Point Cloud.
127  * \param output the resultant point cloud message
128  */
129  void
131  {
132  if (!zip_)
133  {
134  // Allocate enough space
135  output.resize (input_->size ());
136  // Copy data
137  Device<float>::type::iterator nr_points = thrust::copy_if (input_->points_x.begin (), input_->points_x.end (), output.points_x.begin (), isFiniteSOA ());
138  nr_points = thrust::copy_if (input_->points_y.begin (), input_->points_y.end (), output.points_y.begin (), isFiniteSOA ());
139  nr_points = thrust::copy_if (input_->points_z.begin (), input_->points_z.end (), output.points_z.begin (), isFiniteSOA ());
140  output.resize (nr_points - output.points_z.begin ());
141 
142  //std::cerr << "[applyFilterSOA]: ";
143  //std::cerr << input_->size () << " " << output.size () << std::endl;
144  }
145 
146  else
147  {
148  output = *input_;
149  PointCloud::zip_iterator result = thrust::remove_if (output.zip_begin (), output.zip_end (), isFiniteZIPSOA ());
150  PointCloud::iterator_tuple result_tuple = result.get_iterator_tuple ();
151  PointCloud::float_iterator xiter = thrust::get<0> (result_tuple),
152  yiter = thrust::get<1> (result_tuple),
153  ziter = thrust::get<2> (result_tuple);
154 
155  unsigned badpoints = distance (xiter, output.points_x.end ());
156  unsigned goodpoints = distance (output.points_x.begin (), xiter);
157 
158  output.resize (goodpoints);
159 
160  //std::cerr << "[applyFilterSOA-ZIP]: ";
161  //std::cerr << input_->size () << " " << output.size () << std::endl;
162  }
163  }
164 
165  private:
166  bool zip_;
167  };
168 }
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
Removes points with x, y, or z equal to NaN.
Definition: filter.h:59
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: filter.h:68
typename PointCloud::Ptr PointCloudPtr
Definition: filter.h:67
typename PCLCUDABase< CloudT >::PointCloud PointCloud
Definition: filter.h:66
std::string filter_name_
The filter name.
Definition: filter.h:159
void applyFilter(PointCloud &output)
Filter a Point Cloud.
Definition: voxel_grid.h:94
void applyFilter(PointCloud &output)
Filter a Point Cloud.
Definition: voxel_grid.h:130
VoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
Definition: voxel_grid.h:53
void applyFilter(PointCloud &output)
Filter a Point Cloud.
Definition: voxel_grid.h:72
VoxelGrid()
Empty constructor.
Definition: voxel_grid.h:62
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
#define PCL_DEPRECATED_HEADER(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated headers for the Major....
Definition: pcl_macros.h:176
Check if a specific point is valid or not.
Definition: passthrough.h:48
Check if a specific point is valid or not.
Definition: passthrough.h:59
Check if a specific point is valid or not.
Definition: passthrough.h:69