Point Cloud Library (PCL)  1.11.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/memory.h> // for pcl::make_shared
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 = shared_ptr<PlaneRefinementComparator<PointT, PointNT, PointLT> >;
69  using ConstPtr = 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 (shared_ptr<std::vector<pcl::ModelCoefficients> >& models,
89  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. */
99  {
100  }
101 
102  /** \brief Set the vector of model coefficients to which we will compare.
103  * \param[in] models a vector of model coefficients produced by the initial segmentation step.
104  */
105  void
106  setModelCoefficients (shared_ptr<std::vector<pcl::ModelCoefficients> >& models)
107  {
108  models_ = models;
109  }
110 
111  /** \brief Set the vector of model coefficients to which we will compare.
112  * \param[in] models a vector of model coefficients produced by the initial segmentation step.
113  */
114  void
115  setModelCoefficients (std::vector<pcl::ModelCoefficients>& models)
116  {
117  models_ = pcl::make_shared<std::vector<pcl::ModelCoefficients> >(models);
118  }
119 
120  /** \brief Set which labels should be refined. This is a vector of bools 0-max_label, true if the label should be refined.
121  * \param[in] refine_labels A vector of bools 0-max_label, true if the label should be refined.
122  */
123  void
124  setRefineLabels (shared_ptr<std::vector<bool> >& refine_labels)
125  {
126  refine_labels_ = refine_labels;
127  }
128 
129  /** \brief Set which labels should be refined. This is a vector of bools 0-max_label, true if the label should be refined.
130  * \param[in] refine_labels A vector of bools 0-max_label, true if the label should be refined.
131  */
132  void
133  setRefineLabels (std::vector<bool>& refine_labels)
134  {
135  refine_labels_ = pcl::make_shared<std::vector<bool> >(refine_labels);
136  }
137 
138  /** \brief A mapping from label to index in the vector of models, allowing the model coefficients of a label to be accessed.
139  * \param[in] label_to_model A vector of size max_label, with the index of each corresponding model in models
140  */
141  inline void
142  setLabelToModel (shared_ptr<std::vector<int> >& label_to_model)
143  {
144  label_to_model_ = label_to_model;
145  }
146 
147  /** \brief A mapping from label to index in the vector of models, allowing the model coefficients of a label to be accessed.
148  * \param[in] label_to_model A vector of size max_label, with the index of each corresponding model in models
149  */
150  inline void
151  setLabelToModel (std::vector<int>& label_to_model)
152  {
153  label_to_model_ = pcl::make_shared<std::vector<int> >(label_to_model);
154  }
155 
156  /** \brief Get the vector of model coefficients to which we will compare. */
157  inline shared_ptr<std::vector<pcl::ModelCoefficients> >
159  {
160  return (models_);
161  }
162 
163  /** \brief ...
164  * \param[in] labels
165  */
166  inline void
168  {
169  labels_ = labels;
170  }
171 
172  /** \brief Compare two neighboring points
173  * \param[in] idx1 The index of the first point.
174  * \param[in] idx2 The index of the second point.
175  */
176  bool
177  compare (int idx1, int idx2) const override
178  {
179  int current_label = (*labels_)[idx1].label;
180  int next_label = (*labels_)[idx2].label;
181 
182  if (!((*refine_labels_)[current_label] && !(*refine_labels_)[next_label]))
183  return (false);
184 
185  const pcl::ModelCoefficients& model_coeff = (*models_)[(*label_to_model_)[current_label]];
186 
187  PointT pt = (*input_)[idx2];
188  double ptp_dist = std::fabs (model_coeff.values[0] * pt.x +
189  model_coeff.values[1] * pt.y +
190  model_coeff.values[2] * pt.z +
191  model_coeff.values[3]);
192 
193  // depth dependent
194  float threshold = distance_threshold_;
195  if (depth_dependent_)
196  {
197  //Eigen::Vector4f origin = input_->sensor_origin_;
198  Eigen::Vector3f vec = (*input_)[idx1].getVector3fMap ();// - origin.head<3> ();
199 
200  float z = vec.dot (z_axis_);
201  threshold *= z * z;
202  }
203 
204  return (ptp_dist < threshold);
205  }
206 
207  protected:
208  shared_ptr<std::vector<pcl::ModelCoefficients> > models_;
210  shared_ptr<std::vector<bool> > refine_labels_;
211  shared_ptr<std::vector<int> > label_to_model_;
214  };
215 }
pcl
Definition: convolution.h:46
pcl::PlaneRefinementComparator::labels_
PointCloudLPtr labels_
Definition: plane_refinement_comparator.h:209
pcl::PlaneRefinementComparator::setLabelToModel
void setLabelToModel(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 ...
Definition: plane_refinement_comparator.h:142
pcl::PlaneRefinementComparator::setModelCoefficients
void setModelCoefficients(std::vector< pcl::ModelCoefficients > &models)
Set the vector of model coefficients to which we will compare.
Definition: plane_refinement_comparator.h:115
pcl::PlaneCoefficientComparator::PointCloudNPtr
typename PointCloudN::Ptr PointCloudNPtr
Definition: plane_coefficient_comparator.h:62
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::ModelCoefficients::values
std::vector< float > values
Definition: ModelCoefficients.h:19
pcl::PlaneRefinementComparator::PointCloudLConstPtr
typename PointCloudL::ConstPtr PointCloudLConstPtr
Definition: plane_refinement_comparator.h:66
pcl::PlaneRefinementComparator::PointCloudLPtr
typename PointCloudL::Ptr PointCloudLPtr
Definition: plane_refinement_comparator.h:65
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::Comparator::ConstPtr
shared_ptr< const Comparator< PointT > > ConstPtr
Definition: comparator.h:62
pcl::PlaneRefinementComparator::setLabels
void setLabels(PointCloudLPtr &labels)
...
Definition: plane_refinement_comparator.h:167
pcl::PlaneRefinementComparator::setRefineLabels
void setRefineLabels(shared_ptr< std::vector< bool > > &refine_labels)
Set which labels should be refined.
Definition: plane_refinement_comparator.h:124
pcl::PlaneCoefficientComparator::distance_threshold_
float distance_threshold_
Definition: plane_coefficient_comparator.h:205
pcl::PlaneCoefficientComparator::PointCloudNConstPtr
typename PointCloudN::ConstPtr PointCloudNConstPtr
Definition: plane_coefficient_comparator.h:63
pcl::ModelCoefficients
Definition: ModelCoefficients.h:11
pcl::PlaneRefinementComparator::~PlaneRefinementComparator
~PlaneRefinementComparator()
Destructor for PlaneCoefficientComparator.
Definition: plane_refinement_comparator.h:98
pcl::PlaneRefinementComparator::models_
shared_ptr< std::vector< pcl::ModelCoefficients > > models_
Definition: plane_refinement_comparator.h:208
pcl::PlaneRefinementComparator::refine_labels_
shared_ptr< std::vector< bool > > refine_labels_
Definition: plane_refinement_comparator.h:210
pcl::PlaneRefinementComparator::compare
bool compare(int idx1, int idx2) const override
Compare two neighboring points.
Definition: plane_refinement_comparator.h:177
pcl::PlaneRefinementComparator::PlaneRefinementComparator
PlaneRefinementComparator()
Empty constructor for PlaneCoefficientComparator.
Definition: plane_refinement_comparator.h:78
pcl::PlaneRefinementComparator::setLabelToModel
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 ...
Definition: plane_refinement_comparator.h:151
pcl::PlaneRefinementComparator::depth_dependent_
bool depth_dependent_
Definition: plane_refinement_comparator.h:212
pcl::PlaneRefinementComparator::getModelCoefficients
shared_ptr< std::vector< pcl::ModelCoefficients > > getModelCoefficients() const
Get the vector of model coefficients to which we will compare.
Definition: plane_refinement_comparator.h:158
pcl::PlaneRefinementComparator::setModelCoefficients
void setModelCoefficients(shared_ptr< std::vector< pcl::ModelCoefficients > > &models)
Set the vector of model coefficients to which we will compare.
Definition: plane_refinement_comparator.h:106
pcl::PlaneRefinementComparator::setRefineLabels
void setRefineLabels(std::vector< bool > &refine_labels)
Set which labels should be refined.
Definition: plane_refinement_comparator.h:133
pcl::Comparator::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: comparator.h:59
pcl::PlaneCoefficientComparator::z_axis_
Eigen::Vector3f z_axis_
Definition: plane_coefficient_comparator.h:207
pcl::PointCloud< PointNT >::Ptr
shared_ptr< PointCloud< PointNT > > Ptr
Definition: point_cloud.h:406
pcl::PlaneRefinementComparator::PlaneRefinementComparator
PlaneRefinementComparator(shared_ptr< std::vector< pcl::ModelCoefficients > > &models, shared_ptr< std::vector< bool > > &refine_labels)
Empty constructor for PlaneCoefficientComparator.
Definition: plane_refinement_comparator.h:88
pcl::PointCloud< PointNT >::ConstPtr
shared_ptr< const PointCloud< PointNT > > ConstPtr
Definition: point_cloud.h:407
pcl::PlaneRefinementComparator
PlaneRefinementComparator is a Comparator that operates on plane coefficients, for use in planar segm...
Definition: plane_refinement_comparator.h:54
pcl::PlaneRefinementComparator::label_to_model_
shared_ptr< std::vector< int > > label_to_model_
Definition: plane_refinement_comparator.h:211
pcl::Comparator::Ptr
shared_ptr< Comparator< PointT > > Ptr
Definition: comparator.h:61
memory.h
Defines functions, macros and traits for allocating and using memory.
pcl::PlaneCoefficientComparator
PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar seg...
Definition: plane_coefficient_comparator.h:55