Point Cloud Library (PCL)  1.8.1-dev
approximate_voxel_grid.h
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.h 1374 2011-06-19 02:29:56Z bouffa $
35  *
36  */
37 
38 #ifndef PCL_FILTERS_APPROXIMATE_VOXEL_GRID_MAP_H_
39 #define PCL_FILTERS_APPROXIMATE_VOXEL_GRID_MAP_H_
40 
41 #include <pcl/filters/boost.h>
42 #include <pcl/filters/filter.h>
43 
44 namespace pcl
45 {
46  /** \brief Helper functor structure for copying data between an Eigen::VectorXf and a PointT. */
47  template <typename PointT>
49  {
50  typedef typename traits::POD<PointT>::type Pod;
51 
52  xNdCopyEigenPointFunctor (const Eigen::VectorXf &p1, PointT &p2)
53  : p1_ (p1),
54  p2_ (reinterpret_cast<Pod&>(p2)),
55  f_idx_ (0) { }
56 
57  template<typename Key> inline void operator() ()
58  {
59  //boost::fusion::at_key<Key> (p2_) = p1_[f_idx_++];
61  uint8_t* data_ptr = reinterpret_cast<uint8_t*>(&p2_) + pcl::traits::offset<PointT, Key>::value;
62  *reinterpret_cast<T*>(data_ptr) = static_cast<T> (p1_[f_idx_++]);
63  }
64 
65  private:
66  const Eigen::VectorXf &p1_;
67  Pod &p2_;
68  int f_idx_;
69  };
70 
71  /** \brief Helper functor structure for copying data between an Eigen::VectorXf and a PointT. */
72  template <typename PointT>
74  {
75  typedef typename traits::POD<PointT>::type Pod;
76 
77  xNdCopyPointEigenFunctor (const PointT &p1, Eigen::VectorXf &p2)
78  : p1_ (reinterpret_cast<const Pod&>(p1)), p2_ (p2), f_idx_ (0) { }
79 
80  template<typename Key> inline void operator() ()
81  {
82  //p2_[f_idx_++] = boost::fusion::at_key<Key> (p1_);
84  const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(&p1_) + pcl::traits::offset<PointT, Key>::value;
85  p2_[f_idx_++] = static_cast<float> (*reinterpret_cast<const T*>(data_ptr));
86  }
87 
88  private:
89  const Pod &p1_;
90  Eigen::VectorXf &p2_;
91  int f_idx_;
92  };
93 
94  /** \brief ApproximateVoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
95  *
96  * \author James Bowman, Radu B. Rusu
97  * \ingroup filters
98  */
99  template <typename PointT>
100  class ApproximateVoxelGrid: public Filter<PointT>
101  {
106 
107  typedef typename Filter<PointT>::PointCloud PointCloud;
108  typedef typename PointCloud::Ptr PointCloudPtr;
109  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
110 
111  private:
112  struct he
113  {
114  he () : ix (), iy (), iz (), count (0), centroid () {}
115  int ix, iy, iz;
116  int count;
117  Eigen::VectorXf centroid;
118  };
119 
120  public:
121 
122  typedef boost::shared_ptr< ApproximateVoxelGrid<PointT> > Ptr;
123  typedef boost::shared_ptr< const ApproximateVoxelGrid<PointT> > ConstPtr;
124 
125 
126  /** \brief Empty constructor. */
128  pcl::Filter<PointT> (),
129  leaf_size_ (Eigen::Vector3f::Ones ()),
130  inverse_leaf_size_ (Eigen::Array3f::Ones ()),
131  downsample_all_data_ (true), histsize_ (512),
132  history_ (new he[histsize_])
133  {
134  filter_name_ = "ApproximateVoxelGrid";
135  }
136 
137  /** \brief Copy constructor.
138  * \param[in] src the approximate voxel grid to copy into this.
139  */
141  pcl::Filter<PointT> (),
142  leaf_size_ (src.leaf_size_),
145  histsize_ (src.histsize_),
146  history_ ()
147  {
148  history_ = new he[histsize_];
149  for (size_t i = 0; i < histsize_; i++)
150  history_[i] = src.history_[i];
151  }
152 
153 
154  /** \brief Destructor.
155  */
157  {
158  delete [] history_;
159  }
160 
161 
162  /** \brief Copy operator.
163  * \param[in] src the approximate voxel grid to copy into this.
164  */
165  inline ApproximateVoxelGrid&
167  {
168  leaf_size_ = src.leaf_size_;
171  histsize_ = src.histsize_;
172  history_ = new he[histsize_];
173  for (size_t i = 0; i < histsize_; i++)
174  history_[i] = src.history_[i];
175  return (*this);
176  }
177 
178  /** \brief Set the voxel grid leaf size.
179  * \param[in] leaf_size the voxel grid leaf size
180  */
181  inline void
182  setLeafSize (const Eigen::Vector3f &leaf_size)
183  {
184  leaf_size_ = leaf_size;
185  inverse_leaf_size_ = Eigen::Array3f::Ones () / leaf_size_.array ();
186  }
187 
188  /** \brief Set the voxel grid leaf size.
189  * \param[in] lx the leaf size for X
190  * \param[in] ly the leaf size for Y
191  * \param[in] lz the leaf size for Z
192  */
193  inline void
194  setLeafSize (float lx, float ly, float lz)
195  {
196  setLeafSize (Eigen::Vector3f (lx, ly, lz));
197  }
198 
199  /** \brief Get the voxel grid leaf size. */
200  inline Eigen::Vector3f
201  getLeafSize () const { return (leaf_size_); }
202 
203  /** \brief Set to true if all fields need to be downsampled, or false if just XYZ.
204  * \param downsample the new value (true/false)
205  */
206  inline void
207  setDownsampleAllData (bool downsample) { downsample_all_data_ = downsample; }
208 
209  /** \brief Get the state of the internal downsampling parameter (true if
210  * all fields need to be downsampled, false if just XYZ).
211  */
212  inline bool
214 
215  protected:
216  /** \brief The size of a leaf. */
217  Eigen::Vector3f leaf_size_;
218 
219  /** \brief Compute 1/leaf_size_ to avoid division later */
220  Eigen::Array3f inverse_leaf_size_;
221 
222  /** \brief Set to true if all fields need to be downsampled, or false if just XYZ. */
224 
225  /** \brief history buffer size, power of 2 */
226  size_t histsize_;
227 
228  /** \brief history buffer */
229  struct he* history_;
230 
232 
233  /** \brief Downsample a Point Cloud using a voxelized grid approach
234  * \param output the resultant point cloud message
235  */
236  void
237  applyFilter (PointCloud &output);
238 
239  /** \brief Write a single point from the hash to the output cloud
240  */
241  void
242  flush (PointCloud &output, size_t op, he *hhe, int rgba_index, int centroid_size);
243  };
244 }
245 
246 #ifdef PCL_NO_PRECOMPILE
247 #include <pcl/filters/impl/approximate_voxel_grid.hpp>
248 #endif
249 
250 #endif //#ifndef PCL_FILTERS_VOXEL_GRID_MAP_H_
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
ApproximateVoxelGrid & operator=(const ApproximateVoxelGrid &src)
Copy operator.
PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:73
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
bool getDownsampleAllData() const
Get the state of the internal downsampling parameter (true if all fields need to be downsampled...
xNdCopyEigenPointFunctor(const Eigen::VectorXf &p1, PointT &p2)
boost::shared_ptr< const ApproximateVoxelGrid< PointT > > ConstPtr
ApproximateVoxelGrid()
Empty constructor.
boost::shared_ptr< ApproximateVoxelGrid< PointT > > Ptr
ApproximateVoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the...
void applyFilter(PointCloud &output)
Downsample a Point Cloud using a voxelized grid approach.
size_t histsize_
history buffer size, power of 2
PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:72
Filter represents the base filter class.
Definition: filter.h:84
Eigen::Array3f inverse_leaf_size_
Compute 1/leaf_size_ to avoid division later.
traits::POD< PointT >::type Pod
void setLeafSize(float lx, float ly, float lz)
Set the voxel grid leaf size.
xNdCopyPointEigenFunctor(const PointT &p1, Eigen::VectorXf &p2)
Eigen::Vector3f leaf_size_
The size of a leaf.
void setDownsampleAllData(bool downsample)
Set to true if all fields need to be downsampled, or false if just XYZ.
void flush(PointCloud &output, size_t op, he *hhe, int rgba_index, int centroid_size)
Write a single point from the hash to the output cloud.
Helper functor structure for copying data between an Eigen::VectorXf and a PointT.
bool downsample_all_data_
Set to true if all fields need to be downsampled, or false if just XYZ.
Eigen::Vector3f getLeafSize() const
Get the voxel grid leaf size.
struct he * history_
history buffer
traits::POD< PointT >::type Pod
Helper functor structure for copying data between an Eigen::VectorXf and a PointT.
pcl::traits::fieldList< PointT >::type FieldList
std::string filter_name_
The filter name.
Definition: filter.h:166
A point structure representing Euclidean xyz coordinates, and the RGB color.
void setLeafSize(const Eigen::Vector3f &leaf_size)
Set the voxel grid leaf size.
ApproximateVoxelGrid(const ApproximateVoxelGrid &src)
Copy constructor.