Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conditional_removal.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, 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: conditional_removal.h 5656 2012-05-01 06:22:33Z rusu $
35  *
36  */
37 
38 #ifndef PCL_FILTER_FIELD_VAL_CONDITION_H_
39 #define PCL_FILTER_FIELD_VAL_CONDITION_H_
40 
41 #include <pcl/filters/filter.h>
42 
43 namespace pcl
44 {
46  namespace ComparisonOps
47  {
51  typedef enum
52  {
53  GT, GE, LT, LE, EQ
54  } CompareOp;
55  }
56 
58 
59  template<typename PointT>
61  {
62  public:
64  PointDataAtOffset (uint8_t datatype, uint32_t offset) :
65  datatype_ (datatype), offset_ (offset)
66  {
67  }
68 
73  int
74  compare (const PointT& p, const double& val);
75  protected:
77  uint8_t datatype_;
78 
80  uint32_t offset_;
81  private:
82  PointDataAtOffset () : datatype_ (), offset_ () {}
83  };
84 
86 
87  template<typename PointT>
89  {
90  public:
91  typedef boost::shared_ptr<ComparisonBase<PointT> > Ptr;
92  typedef boost::shared_ptr<const ComparisonBase<PointT> > ConstPtr;
93 
95  ComparisonBase () : capable_ (false), field_name_ (), offset_ (), op_ () {}
96 
98  virtual ~ComparisonBase () {}
99 
101  inline bool
102  isCapable () const
103  {
104  return (capable_);
105  }
106 
108  virtual bool
109  evaluate (const PointT &point) const = 0;
110 
111  protected:
113  bool capable_;
114 
116  std::string field_name_;
117 
119  uint32_t offset_;
120 
123  };
124 
126 
127  template<typename PointT>
128  class FieldComparison : public ComparisonBase<PointT>
129  {
133 
134  public:
135  typedef boost::shared_ptr<FieldComparison<PointT> > Ptr;
136  typedef boost::shared_ptr<const FieldComparison<PointT> > ConstPtr;
137 
143  FieldComparison (std::string field_name, ComparisonOps::CompareOp op, double compare_val);
144 
149  compare_val_ (src.compare_val_), point_data_ (src.point_data_)
150  {
151  }
152 
156  inline FieldComparison&
158  {
159  compare_val_ = src.compare_val_;
160  point_data_ = src.point_data_;
161  return (*this);
162  }
163 
165  virtual ~FieldComparison ();
166 
171  virtual bool
172  evaluate (const PointT &point) const;
173 
174  protected:
176  double compare_val_;
177 
179  PointDataAtOffset<PointT>* point_data_;
180 
181  private:
182  FieldComparison () :
183  compare_val_ (), point_data_ ()
184  {
185  } // not allowed
186  };
187 
189 
190  template<typename PointT>
191  class PackedRGBComparison : public ComparisonBase<PointT>
192  {
195 
196  public:
202  PackedRGBComparison (std::string component_name, ComparisonOps::CompareOp op, double compare_val);
203 
205  virtual ~PackedRGBComparison () {}
206 
211  virtual bool
212  evaluate (const PointT &point) const;
213 
214  protected:
216  std::string component_name_;
217 
219  uint32_t component_offset_;
220 
222  double compare_val_;
223 
224  private:
226  component_name_ (), component_offset_ (), compare_val_ ()
227  {
228  } // not allowed
229 
230  };
231 
233 
234  template<typename PointT>
235  class PackedHSIComparison : public ComparisonBase<PointT>
236  {
239 
240  public:
246  PackedHSIComparison (std::string component_name, ComparisonOps::CompareOp op, double compare_val);
247 
249  virtual ~PackedHSIComparison () {}
250 
255  virtual bool
256  evaluate (const PointT &point) const;
257 
258  typedef enum
259  {
260  H, // -128 to 127 corresponds to -pi to pi
261  S, // 0 to 255
262  I // 0 to 255
263  } ComponentId;
264 
265  protected:
267  std::string component_name_;
268 
270  ComponentId component_id_;
271 
273  double compare_val_;
274 
276  uint32_t rgb_offset_;
277 
278  private:
280  component_name_ (), component_id_ (), compare_val_ (), rgb_offset_ ()
281  {
282  } // not allowed
283  };
284 
286 
292  template<typename PointT>
294  {
295  public:
296  EIGEN_MAKE_ALIGNED_OPERATOR_NEW //needed whenever there is a fixed size Eigen:: vector or matrix in a class
297 
298  typedef boost::shared_ptr<TfQuadraticXYZComparison<PointT> > Ptr;
299  typedef boost::shared_ptr<const TfQuadraticXYZComparison<PointT> > ConstPtr;
300 
304 
312  TfQuadraticXYZComparison (const pcl::ComparisonOps::CompareOp op, const Eigen::Matrix3f &comparison_matrix,
313  const Eigen::Vector3f &comparison_vector, const float &comparison_scalar,
314  const Eigen::Affine3f &comparison_transform = Eigen::Affine3f::Identity ());
315 
318  inline void
320  {
321  op_ = op;
322  }
323 
326  inline void
327  setComparisonMatrix (const Eigen::Matrix3f &matrix)
328  {
329  //define comp_matr_ as an homogeneous matrix of the given matrix
330  comp_matr_.block<3, 3> (0, 0) = matrix;
331  comp_matr_.col (3) << 0, 0, 0, 1;
332  comp_matr_.block<1, 3> (3, 0) << 0, 0, 0;
333  tf_comp_matr_ = comp_matr_;
334  }
335 
338  inline void
339  setComparisonMatrix (const Eigen::Matrix4f &homogeneousMatrix)
340  {
341  comp_matr_ = homogeneousMatrix;
342  tf_comp_matr_ = comp_matr_;
343  }
344 
347  inline void
348  setComparisonVector (const Eigen::Vector3f &vector)
349  {
350  comp_vect_ = vector.homogeneous ();
351  tf_comp_vect_ = comp_vect_;
352  }
353 
356  inline void
357  setComparisonVector (const Eigen::Vector4f &homogeneousVector)
358  {
359  comp_vect_ = homogeneousVector;
360  tf_comp_vect_ = comp_vect_;
361  }
362 
365  inline void
366  setComparisonScalar (const float &scalar)
367  {
368  comp_scalar_ = scalar;
369  }
370 
380  inline void
381  transformComparison (const Eigen::Matrix4f &transform)
382  {
383  tf_comp_matr_ = transform.transpose () * comp_matr_ * transform;
384  tf_comp_vect_ = comp_vect_.transpose () * transform;
385  }
386 
396  inline void
397  transformComparison (const Eigen::Affine3f &transform)
398  {
399  transformComparison (transform.matrix ());
400  }
401 
406  virtual bool
407  evaluate (const PointT &point) const;
408 
409  protected:
412 
413  Eigen::Matrix4f comp_matr_;
414  Eigen::Vector4f comp_vect_;
415 
416  float comp_scalar_;
417 
418  private:
419  Eigen::Matrix4f tf_comp_matr_;
420  Eigen::Vector4f tf_comp_vect_;
421  };
422 
424 
425  template<typename PointT>
427  {
428  public:
432 
433  typedef boost::shared_ptr<ConditionBase<PointT> > Ptr;
434  typedef boost::shared_ptr<const ConditionBase<PointT> > ConstPtr;
435 
437  ConditionBase () : capable_ (true), comparisons_ (), conditions_ ()
438  {
439  }
440 
442  virtual ~ConditionBase ()
443  {
444  // comparisons are boost::shared_ptr.will take care of themselves
445  comparisons_.clear ();
446 
447  // conditions are boost::shared_ptr. will take care of themselves
448  conditions_.clear ();
449  }
450 
454  void
456 
460  void
461  addCondition (Ptr condition);
462 
464  inline bool
465  isCapable () const
466  {
467  return (capable_);
468  }
469 
473  virtual bool
474  evaluate (const PointT &point) const = 0;
475 
476  protected:
478  bool capable_;
479 
481  std::vector<ComparisonBaseConstPtr> comparisons_;
482 
484  std::vector<Ptr> conditions_;
485  };
486 
488 
489  template<typename PointT>
490  class ConditionAnd : public ConditionBase<PointT>
491  {
494 
495  public:
496  typedef boost::shared_ptr<ConditionAnd<PointT> > Ptr;
497  typedef boost::shared_ptr<const ConditionAnd<PointT> > ConstPtr;
498 
502  {
503  }
504 
511  virtual bool
512  evaluate (const PointT &point) const;
513  };
514 
516 
517  template<typename PointT>
518  class ConditionOr : public ConditionBase<PointT>
519  {
522 
523  public:
524  typedef boost::shared_ptr<ConditionOr<PointT> > Ptr;
525  typedef boost::shared_ptr<const ConditionOr<PointT> > ConstPtr;
526 
530  {
531  }
532 
539  virtual bool
540  evaluate (const PointT &point) const;
541  };
542 
544 
574  template<typename PointT>
575  class ConditionalRemoval : public Filter<PointT>
576  {
580 
583 
584  typedef typename Filter<PointT>::PointCloud PointCloud;
585  typedef typename PointCloud::Ptr PointCloudPtr;
586  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
587 
588  public:
592 
599  ConditionalRemoval (int extract_removed_indices = false) :
600  Filter<PointT>::Filter (extract_removed_indices), capable_ (false), keep_organized_ (false), condition_ (),
601  user_filter_value_ (std::numeric_limits<float>::quiet_NaN ())
602  {
603  filter_name_ = "ConditionalRemoval";
604  }
605 
611  ConditionalRemoval (ConditionBasePtr condition, bool extract_removed_indices = false) :
612  Filter<PointT>::Filter (extract_removed_indices), capable_ (false), keep_organized_ (false), condition_ (),
613  user_filter_value_ (std::numeric_limits<float>::quiet_NaN ())
614  {
615  filter_name_ = "ConditionalRemoval";
616  setCondition (condition);
617  }
618 
627  inline void
628  setKeepOrganized (bool val)
629  {
630  keep_organized_ = val;
631  }
632 
633  inline bool
635  {
636  return (keep_organized_);
637  }
638 
644  inline void
645  setUserFilterValue (float val)
646  {
647  user_filter_value_ = val;
648  }
649 
656  void
657  setCondition (ConditionBasePtr condition);
658 
659  protected:
663  void
664  applyFilter (PointCloud &output);
665 
666  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
667 
669  bool capable_;
670 
674  bool keep_organized_;
675 
677  ConditionBasePtr condition_;
678 
682  float user_filter_value_;
683  };
684 }
685 
686 #endif