Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_normal_sphere.hpp
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) 2009-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 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_normal_sphere.hpp schrandt $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
41 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
42 
44 
46 template <typename PointT, typename PointNT> void
48  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
49 {
50  if (!normals_)
51  {
52  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::selectWithinDistance] No input dataset containing normals was given!\n");
53  inliers.clear ();
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 sphere center
65  Eigen::Vector4f center = model_coefficients;
66  center[3] = 0;
67 
68  int nr_p = 0;
69  inliers.resize (indices_->size ());
70  // Iterate through the 3d points and calculate the distances from them to the plane
71  for (size_t i = 0; i < indices_->size (); ++i)
72  {
73  // Calculate the distance from the point to the sphere center as the difference between
74  // dist(point,sphere_origin) and sphere_radius
75  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
76  input_->points[(*indices_)[i]].y,
77  input_->points[(*indices_)[i]].z,
78  0);
79 
80  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
81  normals_->points[(*indices_)[i]].normal[1],
82  normals_->points[(*indices_)[i]].normal[2],
83  0);
84 
85  Eigen::Vector4f n_dir = p - center;
86  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
87 
88  // Calculate the angular distance between the point normal and the plane normal
89  double d_normal = fabs (getAngle3D (n, n_dir));
90  d_normal = (std::min) (d_normal, M_PI - d_normal);
91 
92  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
93  {
94  // Returns the indices of the points whose distances are smaller than the threshold
95  inliers[nr_p] = (*indices_)[i];
96  nr_p++;
97  }
98  }
99  inliers.resize (nr_p);
100 }
101 
103 template <typename PointT, typename PointNT> int
105  const Eigen::VectorXf &model_coefficients, const double threshold)
106 {
107  if (!normals_)
108  {
109  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::getDistancesToModel] No input dataset containing normals was given!\n");
110  return (0);
111  }
112 
113  // Check if the model is valid given the user constraints
114  if (!isModelValid (model_coefficients))
115  return(0);
116 
117 
118  // Obtain the shpere centroid
119  Eigen::Vector4f center = model_coefficients;
120  center[3] = 0;
121 
122  int nr_p = 0;
123 
124  // Iterate through the 3d points and calculate the distances from them to the plane
125  for (size_t i = 0; i < indices_->size (); ++i)
126  {
127  // Calculate the distance from the point to the sphere centroid as the difference between
128  // dist(point,sphere_origin) and sphere_radius
129  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
130  input_->points[(*indices_)[i]].y,
131  input_->points[(*indices_)[i]].z,
132  0);
133 
134  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
135  normals_->points[(*indices_)[i]].normal[1],
136  normals_->points[(*indices_)[i]].normal[2],
137  0);
138 
139  Eigen::Vector4f n_dir = (p-center);
140  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
141  //
142  // Calculate the angular distance between the point normal and the plane normal
143  double d_normal = fabs (getAngle3D (n, n_dir));
144  d_normal = (std::min) (d_normal, M_PI - d_normal);
145 
146  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
147  nr_p++;
148  }
149  return (nr_p);
150 }
151 
153 template <typename PointT, typename PointNT> void
155  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
156 {
157  if (!normals_)
158  {
159  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::getDistancesToModel] No input dataset containing normals was given!\n");
160  return;
161  }
162 
163  // Check if the model is valid given the user constraints
164  if (!isModelValid (model_coefficients))
165  {
166  distances.clear ();
167  return;
168  }
169 
170  // Obtain the sphere centroid
171  Eigen::Vector4f center = model_coefficients;
172  center[3] = 0;
173 
174  distances.resize (indices_->size ());
175 
176  // Iterate through the 3d points and calculate the distances from them to the plane
177  for (size_t i = 0; i < indices_->size (); ++i)
178  {
179  // Calculate the distance from the point to the sphere as the difference between
180  // dist(point,sphere_origin) and sphere_radius
181  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
182  input_->points[(*indices_)[i]].y,
183  input_->points[(*indices_)[i]].z,
184  0);
185 
186  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
187  normals_->points[(*indices_)[i]].normal[1],
188  normals_->points[(*indices_)[i]].normal[2],
189  0);
190 
191  Eigen::Vector4f n_dir = (p-center);
192  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
193  //
194  // Calculate the angular distance between the point normal and the plane normal
195  double d_normal = fabs (getAngle3D (n, n_dir));
196  d_normal = (std::min) (d_normal, M_PI - d_normal);
197 
198  distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
199  }
200 }
201 
203 template <typename PointT, typename PointNT> bool
204 pcl::SampleConsensusModelNormalSphere<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients)
205 {
206  // Needs a valid model coefficients
207  if (model_coefficients.size () != 4)
208  {
209  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::selectWithinDistance] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
210  return (false);
211  }
212 
213  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
214  return (false);
215  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
216  return (false);
217 
218  return (true);
219 }
220 
221 #define PCL_INSTANTIATE_SampleConsensusModelNormalSphere(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalSphere<PointT, PointNT>;
222 
223 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
224