Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
approximate_voxel_grid.h
Go to the documentation of this file.
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  * $Id: approximate_voxel_grid.h 5026 2012-03-12 02:51:44Z rusu $
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/filter.h>
42 #include <boost/mpl/size.hpp>
43 
44 namespace pcl
45 {
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 
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 
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:
123  pcl::Filter<PointT> (),
124  leaf_size_ (Eigen::Vector3f::Ones ()),
125  inverse_leaf_size_ (Eigen::Array3f::Ones ()),
126  downsample_all_data_ (true), histsize_ (512),
127  history_ (new he[histsize_])
128  {
129  filter_name_ = "ApproximateVoxelGrid";
130  }
131 
136  pcl::Filter<PointT> (),
137  leaf_size_ (src.leaf_size_),
138  inverse_leaf_size_ (src.inverse_leaf_size_),
139  downsample_all_data_ (src.downsample_all_data_),
140  histsize_ (src.histsize_),
141  history_ ()
142  {
143  history_ = new he[histsize_];
144  for (size_t i = 0; i < histsize_; i++)
145  history_[i] = src.history_[i];
146  }
147 
151  inline ApproximateVoxelGrid&
153  {
154  leaf_size_ = src.leaf_size_;
155  inverse_leaf_size_ = src.inverse_leaf_size_;
156  downsample_all_data_ = src.downsample_all_data_;
157  histsize_ = src.histsize_;
158  history_ = new he[histsize_];
159  for (size_t i = 0; i < histsize_; i++)
160  history_[i] = src.history_[i];
161  return (*this);
162  }
163 
167  inline void
168  setLeafSize (const Eigen::Vector3f &leaf_size)
169  {
170  leaf_size_ = leaf_size;
171  inverse_leaf_size_ = Eigen::Array3f::Ones () / leaf_size_.array ();
172  }
173 
179  inline void
180  setLeafSize (float lx, float ly, float lz)
181  {
182  setLeafSize (Eigen::Vector3f (lx, ly, lz));
183  }
184 
186  inline Eigen::Vector3f
187  getLeafSize () const { return (leaf_size_); }
188 
192  inline void
193  setDownsampleAllData (bool downsample) { downsample_all_data_ = downsample; }
194 
198  inline bool
199  getDownsampleAllData () const { return (downsample_all_data_); }
200 
201  protected:
203  Eigen::Vector3f leaf_size_;
204 
206  Eigen::Array3f inverse_leaf_size_;
207 
209  bool downsample_all_data_;
210 
212  size_t histsize_;
213 
215  struct he* history_;
216 
217  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
218 
222  void
223  applyFilter (PointCloud &output);
224 
227  void
228  flush(PointCloud &output, size_t op, he *hhe, int rgba_index, int centroid_size);
229  };
230 }
231 
232 #endif //#ifndef PCL_FILTERS_VOXEL_GRID_MAP_H_