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: random_sample.h 5026 2012-03-12 02:51:44Z rusu $ 00035 * 00036 */ 00037 00038 #ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_ 00039 #define PCL_FILTERS_RANDOM_SUBSAMPLE_H_ 00040 00041 #include <pcl/filters/filter_indices.h> 00042 #include <time.h> 00043 #include <limits.h> 00044 00045 namespace pcl 00046 { 00055 template<typename PointT> 00056 class RandomSample : public FilterIndices<PointT> 00057 { 00058 using FilterIndices<PointT>::filter_name_; 00059 using FilterIndices<PointT>::getClassName; 00060 using FilterIndices<PointT>::indices_; 00061 using FilterIndices<PointT>::input_; 00062 00063 typedef typename FilterIndices<PointT>::PointCloud PointCloud; 00064 typedef typename PointCloud::Ptr PointCloudPtr; 00065 typedef typename PointCloud::ConstPtr PointCloudConstPtr; 00066 00067 public: 00069 RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (NULL))) 00070 { 00071 filter_name_ = "RandomSample"; 00072 } 00073 00077 inline void 00078 setSample (unsigned int sample) 00079 { 00080 sample_ = sample; 00081 } 00082 00085 inline unsigned int 00086 getSample () 00087 { 00088 return (sample_); 00089 } 00090 00094 inline void 00095 setSeed (unsigned int seed) 00096 { 00097 seed_ = seed; 00098 } 00099 00102 inline unsigned int 00103 getSeed () 00104 { 00105 return (seed_); 00106 } 00107 00108 protected: 00109 00111 unsigned int sample_; 00113 unsigned int seed_; 00114 00118 void 00119 applyFilter (PointCloud &output); 00120 00124 void 00125 applyFilter (std::vector<int> &indices); 00126 00130 inline float 00131 unifRand () 00132 { 00133 return (static_cast<float>(rand () / double (RAND_MAX))); 00134 //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF); 00135 } 00136 }; 00137 00142 template<> 00143 class PCL_EXPORTS RandomSample<sensor_msgs::PointCloud2> : public FilterIndices<sensor_msgs::PointCloud2> 00144 { 00145 using FilterIndices<sensor_msgs::PointCloud2>::filter_name_; 00146 using FilterIndices<sensor_msgs::PointCloud2>::getClassName; 00147 00148 typedef sensor_msgs::PointCloud2 PointCloud2; 00149 typedef PointCloud2::Ptr PointCloud2Ptr; 00150 typedef PointCloud2::ConstPtr PointCloud2ConstPtr; 00151 00152 public: 00154 RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (NULL))) 00155 { 00156 filter_name_ = "RandomSample"; 00157 } 00158 00162 inline void 00163 setSample (unsigned int sample) 00164 { 00165 sample_ = sample; 00166 } 00167 00170 inline unsigned int 00171 getSample () 00172 { 00173 return (sample_); 00174 } 00175 00179 inline void 00180 setSeed (unsigned int seed) 00181 { 00182 seed_ = seed; 00183 } 00184 00187 inline unsigned int 00188 getSeed () 00189 { 00190 return (seed_); 00191 } 00192 00193 protected: 00194 00196 unsigned int sample_; 00198 unsigned int seed_; 00199 00203 void 00204 applyFilter (PointCloud2 &output); 00205 00209 void 00210 applyFilter (std::vector<int> &indices); 00211 00215 inline float 00216 unifRand () 00217 { 00218 return (static_cast<float> (rand () / double (RAND_MAX))); 00219 //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF); 00220 } 00221 }; 00222 } 00223 00224 #endif //#ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_