Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
approximate_voxel_grid.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: approximate_voxel_grid.h 3746 2011-12-31 22:19:47Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_FILTERS_APPROXIMATE_VOXEL_GRID_MAP_H_
00039 #define PCL_FILTERS_APPROXIMATE_VOXEL_GRID_MAP_H_
00040 
00041 #include "pcl/filters/filter.h"
00042 #include <boost/mpl/size.hpp>
00043 
00044 namespace pcl
00045 {
00046   struct he 
00047   {
00048     int ix, iy, iz;
00049     int count;
00050     Eigen::VectorXf centroid;
00051   };
00052 
00054   template <typename PointT>
00055   struct xNdCopyEigenPointFunctor
00056   {
00057     typedef typename traits::POD<PointT>::type Pod;
00058     
00059     xNdCopyEigenPointFunctor (const Eigen::VectorXf &p1, PointT &p2)
00060       : p1_ (p1),
00061         p2_ (reinterpret_cast<Pod&>(p2)),
00062         f_idx_ (0) { }
00063 
00064     template<typename Key> inline void operator() ()
00065     {
00066       //boost::fusion::at_key<Key> (p2_) = p1_[f_idx_++];
00067       typedef typename pcl::traits::datatype<PointT, Key>::type T;
00068       uint8_t* data_ptr = reinterpret_cast<uint8_t*>(&p2_) + pcl::traits::offset<PointT, Key>::value;
00069       *reinterpret_cast<T*>(data_ptr) = p1_[f_idx_++];
00070     }
00071 
00072     private:
00073       const Eigen::VectorXf &p1_;
00074       Pod &p2_;
00075       int f_idx_;
00076   };
00077 
00079   template <typename PointT>
00080   struct xNdCopyPointEigenFunctor
00081   {
00082     typedef typename traits::POD<PointT>::type Pod;
00083     
00084     xNdCopyPointEigenFunctor (const PointT &p1, Eigen::VectorXf &p2)
00085       : p1_ (reinterpret_cast<const Pod&>(p1)), p2_ (p2), f_idx_ (0) { }
00086 
00087     template<typename Key> inline void operator() ()
00088     {
00089       //p2_[f_idx_++] = boost::fusion::at_key<Key> (p1_);
00090       typedef typename pcl::traits::datatype<PointT, Key>::type T;
00091       const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(&p1_) + pcl::traits::offset<PointT, Key>::value;
00092       p2_[f_idx_++] = *reinterpret_cast<const T*>(data_ptr);
00093     }
00094 
00095     private:
00096       const Pod &p1_;
00097       Eigen::VectorXf &p2_;
00098       int f_idx_;
00099   };
00100 
00106   template <typename PointT>
00107   class ApproximateVoxelGrid: public Filter<PointT>
00108   {
00109     using Filter<PointT>::filter_name_;
00110     using Filter<PointT>::getClassName;
00111     using Filter<PointT>::input_;
00112     using Filter<PointT>::indices_;
00113 
00114     typedef typename Filter<PointT>::PointCloud PointCloud;
00115     typedef typename PointCloud::Ptr PointCloudPtr;
00116     typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00117 
00118     public:
00120       ApproximateVoxelGrid () : downsample_all_data_ (true), histsize_ (512)
00121       {
00122         setLeafSize (1, 1, 1);
00123         filter_name_ = "ApproximateVoxelGrid";
00124         history_ = new he[histsize_];
00125       }
00126 
00128       virtual ~ApproximateVoxelGrid ()
00129       {
00130       }
00131 
00135       inline void 
00136       setLeafSize (const Eigen::Vector3f &leaf_size) 
00137       { 
00138         leaf_size_ = leaf_size; 
00139         inverse_leaf_size_ = Eigen::Array3f::Ones () / leaf_size_.array ();
00140       }
00141 
00147       inline void
00148       setLeafSize (float lx, float ly, float lz)
00149       {
00150         setLeafSize(Eigen::Vector3f(lx, ly, lz));
00151       }
00152 
00154       inline Eigen::Vector3f 
00155       getLeafSize () { return leaf_size_; }
00156 
00160       inline void 
00161       setDownsampleAllData (bool downsample) { downsample_all_data_ = downsample; }
00162 
00166       inline bool 
00167       getDownsampleAllData () { return (downsample_all_data_); }
00168 
00169     protected:
00171       Eigen::Vector3f leaf_size_;
00172 
00174       Eigen::Array3f inverse_leaf_size_;
00175 
00177       bool downsample_all_data_;
00178 
00180       size_t histsize_;
00181 
00183       struct he *history_;
00184 
00185       typedef typename pcl::traits::fieldList<PointT>::type FieldList;
00186 
00190       void 
00191       applyFilter (PointCloud &output);
00192 
00195       void flush(PointCloud &output, size_t op, he *hhe, int rgba_index, int centroid_size);
00196   };
00197 }
00198 
00199 #endif  //#ifndef PCL_FILTERS_VOXEL_GRID_MAP_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines