Point Cloud Library (PCL)  1.7.0
sac_model_normal_parallel_plane.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
43 
44 #include <pcl/sample_consensus/sac_model_normal_parallel_plane.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT, typename PointNT> void
49  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
50 {
51  if (!normals_)
52  {
53  PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::selectWithinDistance] No input dataset containing normals was given!\n");
54  return;
55  }
56 
57  // Check if the model is valid given the user constraints
58  if (!isModelValid (model_coefficients))
59  {
60  inliers.clear ();
61  return;
62  }
63 
64  // Obtain the plane normal
65  Eigen::Vector4f coeff = model_coefficients;
66 
67  int nr_p = 0;
68  inliers.resize (indices_->size ());
69  error_sqr_dists_.resize (indices_->size ());
70 
71  // Iterate through the 3d points and calculate the distances from them to the plane
72  for (size_t i = 0; i < indices_->size (); ++i)
73  {
74  // Calculate the distance from the point to the plane normal as the dot product
75  // D = (P-A).N/|N|
76  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 1);
77  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
78  double d_euclid = fabs (coeff.dot (p));
79 
80  // Calculate the angular distance between the point normal and the plane normal
81  double d_normal = getAngle3D (n, coeff);
82  d_normal = (std::min) (d_normal, fabs(M_PI - d_normal));
83 
84  double distance = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
85  if (distance < threshold)
86  {
87  // Returns the indices of the points whose distances are smaller than the threshold
88  inliers[nr_p] = (*indices_)[i];
89  error_sqr_dists_[nr_p] = distance;
90  ++nr_p;
91  }
92  }
93  inliers.resize (nr_p);
94  error_sqr_dists_.resize (nr_p);
95 }
96 
97 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98 template <typename PointT, typename PointNT> int
100  const Eigen::VectorXf &model_coefficients, const double threshold)
101 {
102  if (!normals_)
103  {
104  PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::countWithinDistance] No input dataset containing normals was given!\n");
105  return (0);
106  }
107 
108  // Check if the model is valid given the user constraints
109  if (!isModelValid (model_coefficients))
110  return (0);
111 
112  // Obtain the plane normal
113  Eigen::Vector4f coeff = model_coefficients;
114 
115  int nr_p = 0;
116 
117  // Iterate through the 3d points and calculate the distances from them to the plane
118  for (size_t i = 0; i < indices_->size (); ++i)
119  {
120  // Calculate the distance from the point to the plane normal as the dot product
121  // D = (P-A).N/|N|
122  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 1);
123  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
124  double d_euclid = fabs (coeff.dot (p));
125 
126  // Calculate the angular distance between the point normal and the plane normal
127  double d_normal = fabs (getAngle3D (n, coeff));
128  d_normal = (std::min) (d_normal, fabs(M_PI - d_normal));
129 
130  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
131  nr_p++;
132  }
133  return (nr_p);
134 }
135 
136 
137 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138 template <typename PointT, typename PointNT> void
140  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
141 {
142  if (!normals_)
143  {
144  PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::getDistancesToModel] No input dataset containing normals was given!\n");
145  return;
146  }
147 
148  // Check if the model is valid given the user constraints
149  if (!isModelValid (model_coefficients))
150  {
151  distances.clear ();
152  return;
153  }
154 
155  // Obtain the plane normal
156  Eigen::Vector4f coeff = model_coefficients;
157 
158  distances.resize (indices_->size ());
159 
160  // Iterate through the 3d points and calculate the distances from them to the plane
161  for (size_t i = 0; i < indices_->size (); ++i)
162  {
163  // Calculate the distance from the point to the plane normal as the dot product
164  // D = (P-A).N/|N|
165  Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 1);
166  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
167  double d_euclid = fabs (coeff.dot (p));
168 
169  // Calculate the angular distance between the point normal and the plane normal
170  double d_normal = getAngle3D (n, coeff);
171  d_normal = (std::min) (d_normal, fabs (M_PI - d_normal));
172 
173  distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
174  }
175 }
176 
177 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178 template <typename PointT, typename PointNT> bool
180 {
181  // Needs a valid model coefficients
182  if (model_coefficients.size () != 4)
183  {
184  PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
185  return (false);
186  }
187 
188  // Check against template, if given
189  if (eps_angle_ > 0.0)
190  {
191  // Obtain the plane normal
192  Eigen::Vector4f coeff = model_coefficients;
193  coeff[3] = 0;
194  coeff.normalize ();
195 
196  if (fabs (axis_.dot (coeff)) < cos_angle_)
197  return (false);
198  }
199 
200  if (eps_dist_ > 0.0)
201  {
202  if (fabs (-model_coefficients[3] - distance_from_origin_) > eps_dist_)
203  return (false);
204  }
205 
206  return (true);
207 }
208 
209 #define PCL_INSTANTIATE_SampleConsensusModelNormalParallelPlane(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>;
210 
211 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
212 
213