Point Cloud Library (PCL)  1.3.1
conditional_removal.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, 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: conditional_removal.h 1370 2011-06-19 01:06:01Z jspricke $
00035  *
00036  */
00037 
00038 #ifndef PCL_FILTER_FIELD_VAL_CONDITION_H_
00039 #define PCL_FILTER_FIELD_VAL_CONDITION_H_
00040 
00041 #include <pcl/filters/filter.h>
00042 
00043 namespace pcl
00044 {
00046   namespace ComparisonOps
00047   {
00051     typedef enum
00052     {
00053       GT, GE, LT, LE, EQ
00054     } CompareOp;
00055   }
00056 
00058 
00059   template<typename PointT>
00060   class PointDataAtOffset
00061   {
00062     public:
00064       PointDataAtOffset (uint8_t datatype, uint32_t offset) :
00065         datatype_ (datatype), offset_ (offset)
00066       {
00067       }
00068 
00073       int
00074       compare (const PointT& p, const double& val);
00075     protected:
00077       uint8_t datatype_;
00078 
00080       uint32_t offset_;
00081     private:
00082       PointDataAtOffset ()
00083       {
00084       }
00085   };
00086 
00088 
00089   template<typename PointT>
00090   class ComparisonBase
00091   {
00092     public:
00093       typedef boost::shared_ptr<ComparisonBase<PointT> > Ptr;
00094       typedef boost::shared_ptr<const ComparisonBase<PointT> > ConstPtr;
00095 
00097       inline bool
00098       isCapable () const
00099       {
00100         return (capable_);
00101       }
00102 
00104       virtual bool
00105       evaluate (const PointT &point) const = 0;
00106 
00107     protected:
00109       bool capable_;
00110 
00112       std::string field_name_;
00113 
00115       uint32_t offset_;
00116 
00118       ComparisonOps::CompareOp op_;
00119   };
00120 
00122 
00123   template<typename PointT>
00124   class FieldComparison : public ComparisonBase<PointT>
00125   {
00126     using ComparisonBase<PointT>::field_name_;
00127     using ComparisonBase<PointT>::op_;
00128     using ComparisonBase<PointT>::capable_;
00129 
00130     public:
00131       typedef boost::shared_ptr<FieldComparison<PointT> > Ptr;
00132       typedef boost::shared_ptr<const FieldComparison<PointT> > ConstPtr;
00133 
00139       FieldComparison (std::string field_name, ComparisonOps::CompareOp op, double compare_val);
00140 
00142       virtual ~FieldComparison ();
00143 
00148       virtual bool
00149       evaluate (const PointT &point) const;
00150 
00151     protected:
00153       double compare_val_;
00154 
00156       PointDataAtOffset<PointT>* point_data_;
00157 
00158     private:
00159       FieldComparison ()
00160       {
00161       } // not allowed
00162   };
00163 
00165 
00166   template<typename PointT>
00167   class PackedRGBComparison : public ComparisonBase<PointT>
00168   {
00169     using ComparisonBase<PointT>::capable_;
00170     using ComparisonBase<PointT>::op_;
00171 
00172     public:
00178       PackedRGBComparison (std::string component_name, ComparisonOps::CompareOp op, double compare_val);
00179 
00184       virtual bool
00185       evaluate (const PointT &point) const;
00186 
00187     protected:
00189       std::string component_name_;
00190 
00192       uint32_t component_offset_;
00193 
00195       double compare_val_;
00196 
00197     private:
00198       PackedRGBComparison ()
00199       {
00200       } // not allowed
00201 
00202   };
00203 
00205 
00206   template<typename PointT>
00207   class PackedHSIComparison : public ComparisonBase<PointT>
00208   {
00209     using ComparisonBase<PointT>::capable_;
00210     using ComparisonBase<PointT>::op_;
00211 
00212     public:
00218       PackedHSIComparison (std::string component_name, ComparisonOps::CompareOp op, double compare_val);
00219 
00224       virtual bool
00225       evaluate (const PointT &point) const;
00226 
00227       typedef enum
00228       {
00229         H, // -128 to 127 corresponds to -pi to pi
00230         S, // 0 to 255
00231         I,
00232       // 0 to 255
00233       } ComponentId;
00234 
00235     protected:
00237       std::string component_name_;
00238 
00240       ComponentId component_id_;
00241 
00243       double compare_val_;
00244 
00246       uint32_t rgb_offset_;
00247 
00248     private:
00249       PackedHSIComparison ()
00250       {
00251       } // not allowed
00252   };
00253 
00255 
00256   template<typename PointT>
00257   class ConditionBase
00258   {
00259     public:
00260       typedef typename pcl::ComparisonBase<PointT> ComparisonBase;
00261       typedef typename ComparisonBase::Ptr ComparisonBasePtr;
00262       typedef typename ComparisonBase::ConstPtr ComparisonBaseConstPtr;
00263 
00264       typedef boost::shared_ptr<ConditionBase<PointT> > Ptr;
00265       typedef boost::shared_ptr<const ConditionBase<PointT> > ConstPtr;
00266 
00268       ConditionBase () : capable_ (true)
00269       {
00270       }
00271 
00273       virtual ~ConditionBase ()
00274       {
00275         // comparisons are boost::shared_ptr.will take care of themselves
00276         comparisons_.clear ();
00277 
00278         // conditions are boost::shared_ptr. will take care of themselves
00279         conditions_.clear ();
00280       }
00281 
00285       void
00286       addComparison (ComparisonBaseConstPtr comparison);
00287 
00291       void
00292       addCondition (Ptr condition);
00293 
00295       inline bool
00296       isCapable () const
00297       {
00298         return (capable_);
00299       }
00300 
00304       virtual bool
00305       evaluate (const PointT &point) const = 0;
00306 
00307     protected:
00309       bool capable_;
00310 
00312       std::vector<ComparisonBaseConstPtr> comparisons_;
00313 
00315       std::vector<Ptr> conditions_;
00316   };
00317 
00319 
00320   template<typename PointT>
00321   class ConditionAnd : public ConditionBase<PointT>
00322   {
00323     using ConditionBase<PointT>::conditions_;
00324     using ConditionBase<PointT>::comparisons_;
00325 
00326     public:
00327       typedef boost::shared_ptr<ConditionAnd<PointT> > Ptr;
00328       typedef boost::shared_ptr<const ConditionAnd<PointT> > ConstPtr;
00329 
00331       ConditionAnd () :
00332         ConditionBase<PointT> ()
00333       {
00334       }
00335 
00342       virtual bool
00343       evaluate (const PointT &point) const;
00344   };
00345 
00347 
00348   template<typename PointT>
00349   class ConditionOr : public ConditionBase<PointT>
00350   {
00351     using ConditionBase<PointT>::conditions_;
00352     using ConditionBase<PointT>::comparisons_;
00353 
00354     public:
00355       typedef boost::shared_ptr<ConditionOr<PointT> > Ptr;
00356       typedef boost::shared_ptr<const ConditionOr<PointT> > ConstPtr;
00357 
00359       ConditionOr () :
00360         ConditionBase<PointT> ()
00361       {
00362       }
00363 
00370       virtual bool
00371       evaluate (const PointT &point) const;
00372   };
00373 
00375 
00405   template<typename PointT>
00406   class ConditionalRemoval : public Filter<PointT>
00407   {
00408     using Filter<PointT>::input_;
00409     using Filter<PointT>::filter_name_;
00410     using Filter<PointT>::getClassName;
00411 
00412     using Filter<PointT>::removed_indices_;
00413     using Filter<PointT>::extract_removed_indices_;
00414 
00415     typedef typename Filter<PointT>::PointCloud PointCloud;
00416     typedef typename PointCloud::Ptr PointCloudPtr;
00417     typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00418 
00419     public:
00420       typedef typename pcl::ConditionBase<PointT> ConditionBase;
00421       typedef typename ConditionBase::Ptr ConditionBasePtr;
00422       typedef typename ConditionBase::ConstPtr ConditionBaseConstPtr;
00423 
00430       ConditionalRemoval (int extract_removed_indices = false) :
00431         Filter<PointT>::Filter (extract_removed_indices), keep_organized_ (false), condition_ (),
00432         user_filter_value_ (std::numeric_limits<float>::quiet_NaN ())
00433       {
00434         filter_name_ = "ConditionalRemoval";
00435       }
00436 
00442       ConditionalRemoval (ConditionBasePtr condition, bool extract_removed_indices = false) :
00443         Filter<PointT>::Filter (extract_removed_indices), keep_organized_ (false), condition_ (),
00444         user_filter_value_ (std::numeric_limits<float>::quiet_NaN ())
00445       {
00446         filter_name_ = "ConditionalRemoval";
00447         setCondition (condition);
00448       }
00449 
00458       inline void
00459       setKeepOrganized (bool val)
00460       {
00461         keep_organized_ = val;
00462       }
00463 
00464       inline bool
00465       getKeepOrganized ()
00466       {
00467         return (keep_organized_);
00468       }
00469 
00475       inline void
00476       setUserFilterValue (float val)
00477       {
00478         user_filter_value_ = val;
00479       }
00480 
00487       void
00488       setCondition (ConditionBasePtr condition);
00489 
00490     protected:
00494       void
00495       applyFilter (PointCloud &output);
00496 
00497       typedef typename pcl::traits::fieldList<PointT>::type FieldList;
00498 
00500       bool capable_;
00501 
00505       bool keep_organized_;
00506 
00508       ConditionBasePtr condition_;
00509 
00513       float user_filter_value_;
00514   };
00515 }
00516 
00517 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines