Point Cloud Library (PCL)  1.9.1-dev
plane_refinement_comparator.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: extract_clusters.h 5027 2012-03-12 03:10:45Z rusu $
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/segmentation/boost.h>
43 #include <pcl/segmentation/plane_coefficient_comparator.h>
44 
45 namespace pcl
46 {
47  /** \brief PlaneRefinementComparator is a Comparator that operates on plane coefficients,
48  * for use in planar segmentation.
49  * In conjunction with OrganizedConnectedComponentSegmentation, this allows planes to be segmented from organized data.
50  *
51  * \author Alex Trevor, Suat Gedikli
52  */
53  template<typename PointT, typename PointNT, typename PointLT>
55  {
56  public:
59 
61  using PointCloudNPtr = typename PointCloudN::Ptr;
63 
65  using PointCloudLPtr = typename PointCloudL::Ptr;
67 
68  using Ptr = boost::shared_ptr<PlaneRefinementComparator<PointT, PointNT, PointLT> >;
69  using ConstPtr = boost::shared_ptr<const PlaneRefinementComparator<PointT, PointNT, PointLT> >;
70 
75 
76 
77  /** \brief Empty constructor for PlaneCoefficientComparator. */
79  : labels_ ()
80  , depth_dependent_ (false)
81  {
82  }
83 
84  /** \brief Empty constructor for PlaneCoefficientComparator.
85  * \param[in] models
86  * \param[in] refine_labels
87  */
88  PlaneRefinementComparator (boost::shared_ptr<std::vector<pcl::ModelCoefficients> >& models,
89  boost::shared_ptr<std::vector<bool> >& refine_labels)
90  : models_ (models)
91  , labels_ ()
92  , refine_labels_ (refine_labels)
93  , depth_dependent_ (false)
94  {
95  }
96 
97  /** \brief Destructor for PlaneCoefficientComparator. */
98 
100  {
101  }
102 
103  /** \brief Set the vector of model coefficients to which we will compare.
104  * \param[in] models a vector of model coefficients produced by the initial segmentation step.
105  */
106  void
107  setModelCoefficients (boost::shared_ptr<std::vector<pcl::ModelCoefficients> >& models)
108  {
109  models_ = models;
110  }
111 
112  /** \brief Set the vector of model coefficients to which we will compare.
113  * \param[in] models a vector of model coefficients produced by the initial segmentation step.
114  */
115  void
116  setModelCoefficients (std::vector<pcl::ModelCoefficients>& models)
117  {
118  models_ = boost::make_shared<std::vector<pcl::ModelCoefficients> >(models);
119  }
120 
121  /** \brief Set which labels should be refined. This is a vector of bools 0-max_label, true if the label should be refined.
122  * \param[in] refine_labels A vector of bools 0-max_label, true if the label should be refined.
123  */
124  void
125  setRefineLabels (boost::shared_ptr<std::vector<bool> >& refine_labels)
126  {
127  refine_labels_ = refine_labels;
128  }
129 
130  /** \brief Set which labels should be refined. This is a vector of bools 0-max_label, true if the label should be refined.
131  * \param[in] refine_labels A vector of bools 0-max_label, true if the label should be refined.
132  */
133  void
134  setRefineLabels (std::vector<bool>& refine_labels)
135  {
136  refine_labels_ = boost::make_shared<std::vector<bool> >(refine_labels);
137  }
138 
139  /** \brief A mapping from label to index in the vector of models, allowing the model coefficients of a label to be accessed.
140  * \param[in] label_to_model A vector of size max_label, with the index of each corresponding model in models
141  */
142  inline void
143  setLabelToModel (boost::shared_ptr<std::vector<int> >& label_to_model)
144  {
145  label_to_model_ = label_to_model;
146  }
147 
148  /** \brief A mapping from label to index in the vector of models, allowing the model coefficients of a label to be accessed.
149  * \param[in] label_to_model A vector of size max_label, with the index of each corresponding model in models
150  */
151  inline void
152  setLabelToModel (std::vector<int>& label_to_model)
153  {
154  label_to_model_ = boost::make_shared<std::vector<int> >(label_to_model);
155  }
156 
157  /** \brief Get the vector of model coefficients to which we will compare. */
158  inline boost::shared_ptr<std::vector<pcl::ModelCoefficients> >
160  {
161  return (models_);
162  }
163 
164  /** \brief ...
165  * \param[in] labels
166  */
167  inline void
169  {
170  labels_ = labels;
171  }
172 
173  /** \brief Compare two neighboring points
174  * \param[in] idx1 The index of the first point.
175  * \param[in] idx2 The index of the second point.
176  */
177  bool
178  compare (int idx1, int idx2) const override
179  {
180  int current_label = labels_->points[idx1].label;
181  int next_label = labels_->points[idx2].label;
182 
183  if (!((*refine_labels_)[current_label] && !(*refine_labels_)[next_label]))
184  return (false);
185 
186  const pcl::ModelCoefficients& model_coeff = (*models_)[(*label_to_model_)[current_label]];
187 
188  PointT pt = input_->points[idx2];
189  double ptp_dist = std::fabs (model_coeff.values[0] * pt.x +
190  model_coeff.values[1] * pt.y +
191  model_coeff.values[2] * pt.z +
192  model_coeff.values[3]);
193 
194  // depth dependent
195  float threshold = distance_threshold_;
196  if (depth_dependent_)
197  {
198  //Eigen::Vector4f origin = input_->sensor_origin_;
199  Eigen::Vector3f vec = input_->points[idx1].getVector3fMap ();// - origin.head<3> ();
200 
201  float z = vec.dot (z_axis_);
202  threshold *= z * z;
203  }
204 
205  return (ptp_dist < threshold);
206  }
207 
208  protected:
209  boost::shared_ptr<std::vector<pcl::ModelCoefficients> > models_;
211  boost::shared_ptr<std::vector<bool> > refine_labels_;
212  boost::shared_ptr<std::vector<int> > label_to_model_;
215  };
216 }
typename PointCloudL::ConstPtr PointCloudLConstPtr
boost::shared_ptr< std::vector< bool > > refine_labels_
void setRefineLabels(std::vector< bool > &refine_labels)
Set which labels should be refined.
void setLabels(PointCloudLPtr &labels)
...
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: comparator.h:58
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
std::vector< float > values
boost::shared_ptr< std::vector< int > > label_to_model_
void setModelCoefficients(boost::shared_ptr< std::vector< pcl::ModelCoefficients > > &models)
Set the vector of model coefficients to which we will compare.
bool compare(int idx1, int idx2) const override
Compare two neighboring points.
void setLabelToModel(boost::shared_ptr< std::vector< int > > &label_to_model)
A mapping from label to index in the vector of models, allowing the model coefficients of a label to ...
~PlaneRefinementComparator()
Destructor for PlaneCoefficientComparator.
boost::shared_ptr< const Comparator< PointT > > ConstPtr
Definition: comparator.h:61
boost::shared_ptr< std::vector< pcl::ModelCoefficients > > models_
void setModelCoefficients(std::vector< pcl::ModelCoefficients > &models)
Set the vector of model coefficients to which we will compare.
void setRefineLabels(boost::shared_ptr< std::vector< bool > > &refine_labels)
Set which labels should be refined.
boost::shared_ptr< PointCloud< PointNT > > Ptr
Definition: point_cloud.h:411
PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar seg...
PlaneRefinementComparator(boost::shared_ptr< std::vector< pcl::ModelCoefficients > > &models, boost::shared_ptr< std::vector< bool > > &refine_labels)
Empty constructor for PlaneCoefficientComparator.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
boost::shared_ptr< Comparator< PointT > > Ptr
Definition: comparator.h:60
typename PointCloudN::ConstPtr PointCloudNConstPtr
PlaneRefinementComparator is a Comparator that operates on plane coefficients, for use in planar segm...
PlaneRefinementComparator()
Empty constructor for PlaneCoefficientComparator.
boost::shared_ptr< const PointCloud< PointNT > > ConstPtr
Definition: point_cloud.h:412
PointCloudConstPtr input_
Definition: comparator.h:99
A point structure representing Euclidean xyz coordinates, and the RGB color.
boost::shared_ptr< std::vector< pcl::ModelCoefficients > > getModelCoefficients() const
Get the vector of model coefficients to which we will compare.
void setLabelToModel(std::vector< int > &label_to_model)
A mapping from label to index in the vector of models, allowing the model coefficients of a label to ...
typename PointCloudL::Ptr PointCloudLPtr