Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_plane.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, 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 Willow Garage, Inc. 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: sac_model_plane.h 4915 2012-03-05 17:31:53Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
41 #define PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
42 
45 
46 namespace pcl
47 {
48 
54  template <typename Point> inline void
55  projectPoint (const Point &p, const Eigen::Vector4f &model_coefficients, Point &q)
56  {
57  // Calculate the distance from the point to the plane
58  Eigen::Vector4f pp (p.x, p.y, p.z, 1);
59  // use normalized coefficients to calculate the scalar projection
60  float distance_to_plane = pp.dot(model_coefficients);
61 
62 
63  //TODO: Why doesn't getVector4Map work here?
64  //Eigen::Vector4f q_e = q.getVector4fMap ();
65  //q_e = pp - model_coefficients * distance_to_plane;
66 
67  Eigen::Vector4f q_e = pp - distance_to_plane * model_coefficients;
68  q.x = q_e[0];
69  q.y = q_e[1];
70  q.z = q_e[2];
71  }
72 
81  template <typename Point> inline double
82  pointToPlaneDistanceSigned (const Point &p, double a, double b, double c, double d)
83  {
84  return (a * p.x + b * p.y + c * p.z + d);
85  }
86 
92  template <typename Point> inline double
93  pointToPlaneDistanceSigned (const Point &p, const Eigen::Vector4f &plane_coefficients)
94  {
95  return ( plane_coefficients[0] * p.x + plane_coefficients[1] * p.y + plane_coefficients[2] * p.z + plane_coefficients[3] );
96  }
97 
106  template <typename Point> inline double
107  pointToPlaneDistance (const Point &p, double a, double b, double c, double d)
108  {
109  return (fabs (pointToPlaneDistanceSigned (p, a, b, c, d)) );
110  }
111 
117  template <typename Point> inline double
118  pointToPlaneDistance (const Point &p, const Eigen::Vector4f &plane_coefficients)
119  {
120  return ( fabs (pointToPlaneDistanceSigned (p, plane_coefficients)) );
121  }
122 
124 
134  template <typename PointT>
136  {
137  public:
140 
144 
145  typedef boost::shared_ptr<SampleConsensusModelPlane> Ptr;
146 
151 
156  SampleConsensusModelPlane (const PointCloudConstPtr &cloud, const std::vector<int> &indices) : SampleConsensusModel<PointT> (cloud, indices) {};
157 
164  bool
165  computeModelCoefficients (const std::vector<int> &samples,
166  Eigen::VectorXf &model_coefficients);
167 
172  void
173  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
174  std::vector<double> &distances);
175 
181  void
182  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
183  const double threshold,
184  std::vector<int> &inliers);
185 
192  virtual int
193  countWithinDistance (const Eigen::VectorXf &model_coefficients,
194  const double threshold);
195 
202  void
203  optimizeModelCoefficients (const std::vector<int> &inliers,
204  const Eigen::VectorXf &model_coefficients,
205  Eigen::VectorXf &optimized_coefficients);
206 
213  void
214  projectPoints (const std::vector<int> &inliers,
215  const Eigen::VectorXf &model_coefficients,
216  PointCloud &projected_points,
217  bool copy_data_fields = true);
218 
224  bool
225  doSamplesVerifyModel (const std::set<int> &indices,
226  const Eigen::VectorXf &model_coefficients,
227  const double threshold);
228 
230  inline pcl::SacModel
231  getModelType () const { return (SACMODEL_PLANE); }
232 
233  protected:
237  inline bool
238  isModelValid (const Eigen::VectorXf &model_coefficients)
239  {
240  // Needs a valid model coefficients
241  if (model_coefficients.size () != 4)
242  {
243  PCL_ERROR ("[pcl::SampleConsensusModelPlane::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
244  return (false);
245  }
246  return (true);
247  }
248 
249  private:
254  virtual bool
255  isSampleGood (const std::vector<int> &samples) const;
256  };
257 }
258 
259 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_