approximate_voxel_grid.hpp
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.hpp 5026 2012-03-12 02:51:44Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_
00039 #define PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_
00040 
00041 #include <pcl/common/common.h>
00042 #include <pcl/filters/approximate_voxel_grid.h>
00043 
00045 template <typename PointT> void
00046 pcl::ApproximateVoxelGrid<PointT>::flush (PointCloud &output, size_t op, he *hhe, int rgba_index, int centroid_size)
00047 {
00048   hhe->centroid /= static_cast<float> (hhe->count);
00049   pcl::for_each_type <FieldList> (pcl::xNdCopyEigenPointFunctor <PointT> (hhe->centroid, output.points[op]));
00050   // ---[ RGB special case
00051   if (rgba_index >= 0)
00052   {
00053     // pack r/g/b into rgb
00054     float r = hhe->centroid[centroid_size-3], 
00055           g = hhe->centroid[centroid_size-2], 
00056           b = hhe->centroid[centroid_size-1];
00057     int rgb = (static_cast<int> (r)) << 16 | (static_cast<int> (g)) << 8 | (static_cast<int> (b));
00058     memcpy (reinterpret_cast<char*> (&output.points[op]) + rgba_index, &rgb, sizeof (float));
00059   }
00060 }
00061 
00063 template <typename PointT> void
00064 pcl::ApproximateVoxelGrid<PointT>::applyFilter (PointCloud &output)
00065 {
00066   int centroid_size = 4;
00067   if (downsample_all_data_)
00068     centroid_size = boost::mpl::size<FieldList>::value;
00069 
00070   // ---[ RGB special case
00071   std::vector<sensor_msgs::PointField> fields;
00072   int rgba_index = -1;
00073   rgba_index = pcl::getFieldIndex (*input_, "rgb", fields);
00074   if (rgba_index == -1)
00075     rgba_index = pcl::getFieldIndex (*input_, "rgba", fields);
00076   if (rgba_index >= 0)
00077   {
00078     rgba_index = fields[rgba_index].offset;
00079     centroid_size += 3;
00080   }
00081 
00082   for (size_t i = 0; i < histsize_; i++) 
00083   {
00084     history_[i].count = 0;
00085     history_[i].centroid = Eigen::VectorXf::Zero (centroid_size);
00086   }
00087   Eigen::VectorXf scratch = Eigen::VectorXf::Zero (centroid_size);
00088 
00089   output.points.resize (input_->points.size ());   // size output for worst case
00090   size_t op = 0;    // output pointer
00091   for (size_t cp = 0; cp < input_->points.size (); ++cp) 
00092   {
00093     int ix = static_cast<int> (floor (input_->points[cp].x * inverse_leaf_size_[0]));
00094     int iy = static_cast<int> (floor (input_->points[cp].y * inverse_leaf_size_[1]));
00095     int iz = static_cast<int> (floor (input_->points[cp].z * inverse_leaf_size_[2]));
00096     unsigned int hash = static_cast<unsigned int> ((ix * 7171 + iy * 3079 + iz * 4231) & (histsize_ - 1));
00097     he *hhe = &history_[hash];
00098     if (hhe->count && ((ix != hhe->ix) || (iy != hhe->iy) || (iz != hhe->iz))) 
00099     {
00100       flush (output, op++, hhe, rgba_index, centroid_size);
00101       hhe->count = 0;
00102       hhe->centroid.setZero ();// = Eigen::VectorXf::Zero (centroid_size);
00103     }
00104     hhe->ix = ix;
00105     hhe->iy = iy;
00106     hhe->iz = iz;
00107     hhe->count++;
00108 
00109     // Unpack the point into scratch, then accumulate
00110     // ---[ RGB special case
00111     if (rgba_index >= 0)
00112     {
00113       // fill r/g/b data
00114       pcl::RGB rgb;
00115       memcpy (&rgb, (reinterpret_cast<const char *> (&input_->points[cp])) + rgba_index, sizeof (RGB));
00116       scratch[centroid_size-3] = rgb.r;
00117       scratch[centroid_size-2] = rgb.g;
00118       scratch[centroid_size-1] = rgb.b;
00119     }
00120     pcl::for_each_type <FieldList> (xNdCopyPointEigenFunctor <PointT> (input_->points[cp], scratch));
00121     hhe->centroid += scratch;
00122   }
00123   for (size_t i = 0; i < histsize_; i++) 
00124   {
00125     he *hhe = &history_[i];
00126     if (hhe->count)
00127       flush (output, op++, hhe, rgba_index, centroid_size);
00128   }
00129   output.points.resize (op);
00130   output.width = static_cast<uint32_t> (output.points.size ());
00131   output.height       = 1;                    // downsampling breaks the organized structure
00132   output.is_dense     = false;                 // we filter out invalid points
00133 }
00134 
00135 #define PCL_INSTANTIATE_ApproximateVoxelGrid(T) template class PCL_EXPORTS pcl::ApproximateVoxelGrid<T>;
00136 
00137 #endif    // PCL_FILTERS_IMPL_FAST_VOXEL_GRID_H_


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:14:39