Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_normal_plane.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009-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: sac_model_normal_plane.hpp 5026 2012-03-12 02:51:44Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PLANE_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PLANE_H_
40 
42 
44 template <typename PointT, typename PointNT> void
46  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
47 {
48  if (!normals_)
49  {
50  PCL_ERROR ("[pcl::SampleConsensusModelNormalPlane::selectWithinDistance] No input dataset containing normals was given!\n");
51  inliers.clear ();
52  return;
53  }
54 
55  // Check if the model is valid given the user constraints
56  if (!isModelValid (model_coefficients))
57  {
58  inliers.clear ();
59  return;
60  }
61 
62  // Obtain the plane normal
63  Eigen::Vector4f coeff = model_coefficients;
64  coeff[3] = 0;
65 
66  int nr_p = 0;
67  inliers.resize (indices_->size ());
68  // Iterate through the 3d points and calculate the distances from them to the plane
69  for (size_t i = 0; i < indices_->size (); ++i)
70  {
71  // Calculate the distance from the point to the plane normal as the dot product
72  // D = (P-A).N/|N|
73  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
74  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
75  double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
76 
77  // Calculate the angular distance between the point normal and the plane normal
78  double d_normal = fabs (getAngle3D (n, coeff));
79  d_normal = (std::min) (d_normal, M_PI - d_normal);
80 
81  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
82  {
83  // Returns the indices of the points whose distances are smaller than the threshold
84  inliers[nr_p] = (*indices_)[i];
85  nr_p++;
86  }
87  }
88  inliers.resize (nr_p);
89 }
90 
92 template <typename PointT, typename PointNT> int
94  const Eigen::VectorXf &model_coefficients, const double threshold)
95 {
96  if (!normals_)
97  {
98  PCL_ERROR ("[pcl::SampleConsensusModelNormalPlane::countWithinDistance] No input dataset containing normals was given!\n");
99  return (0);
100  }
101 
102  // Check if the model is valid given the user constraints
103  if (!isModelValid (model_coefficients))
104  return (0);
105 
106  // Obtain the plane normal
107  Eigen::Vector4f coeff = model_coefficients;
108  coeff[3] = 0;
109 
110  int nr_p = 0;
111 
112  // Iterate through the 3d points and calculate the distances from them to the plane
113  for (size_t i = 0; i < indices_->size (); ++i)
114  {
115  // Calculate the distance from the point to the plane normal as the dot product
116  // D = (P-A).N/|N|
117  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
118  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
119  double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
120 
121  // Calculate the angular distance between the point normal and the plane normal
122  double d_normal = fabs (getAngle3D (n, coeff));
123  d_normal = (std::min) (d_normal, M_PI - d_normal);
124 
125  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
126  nr_p++;
127  }
128  return (nr_p);
129 }
130 
132 template <typename PointT, typename PointNT> void
134  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
135 {
136  if (!normals_)
137  {
138  PCL_ERROR ("[pcl::SampleConsensusModelNormalPlane::getDistancesToModel] No input dataset containing normals was given!\n");
139  return;
140  }
141 
142  // Check if the model is valid given the user constraints
143  if (!isModelValid (model_coefficients))
144  {
145  distances.clear ();
146  return;
147  }
148 
149  // Obtain the plane normal
150  Eigen::Vector4f coeff = model_coefficients;
151  coeff[3] = 0;
152 
153  distances.resize (indices_->size ());
154 
155  // Iterate through the 3d points and calculate the distances from them to the plane
156  for (size_t i = 0; i < indices_->size (); ++i)
157  {
158  // Calculate the distance from the point to the plane normal as the dot product
159  // D = (P-A).N/|N|
160  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
161  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
162  double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
163 
164  // Calculate the angular distance between the point normal and the plane normal
165  double d_normal = fabs (getAngle3D (n, coeff));
166  d_normal = (std::min) (d_normal, M_PI - d_normal);
167 
168  distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
169  }
170 }
171 
173 template <typename PointT, typename PointNT> bool
174 pcl::SampleConsensusModelNormalPlane<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients)
175 {
176  // Needs a valid model coefficients
177  if (model_coefficients.size () != 4)
178  {
179  PCL_ERROR ("[pcl::SampleConsensusModelNormalPlane::selectWithinDistance] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
180  return (false);
181  }
182 
183  return (true);
184 }
185 
186 #define PCL_INSTANTIATE_SampleConsensusModelNormalPlane(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalPlane<PointT, PointNT>;
187 
188 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PLANE_H_
189