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