Point Cloud Library (PCL)  1.9.1-dev
sac_model_normal_sphere.hpp
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  * 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: sac_model_normal_sphere.hpp schrandt $
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
43 
44 #include <pcl/sample_consensus/sac_model_normal_sphere.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::SampleConsensusModelNormalSphere::selectWithinDistance] No input dataset containing normals was given!\n");
54  inliers.clear ();
55  return;
56  }
57 
58  // Check if the model is valid given the user constraints
59  if (!isModelValid (model_coefficients))
60  {
61  inliers.clear ();
62  return;
63  }
64 
65  // Obtain the sphere center
66  Eigen::Vector4f center = model_coefficients;
67  center[3] = 0;
68 
69  int nr_p = 0;
70  inliers.resize (indices_->size ());
71  error_sqr_dists_.resize (indices_->size ());
72 
73  // Iterate through the 3d points and calculate the distances from them to the plane
74  for (size_t i = 0; i < indices_->size (); ++i)
75  {
76  // Calculate the distance from the point to the sphere center as the difference between
77  // dist(point,sphere_origin) and sphere_radius
78  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
79  input_->points[(*indices_)[i]].y,
80  input_->points[(*indices_)[i]].z,
81  0);
82 
83  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
84  normals_->points[(*indices_)[i]].normal[1],
85  normals_->points[(*indices_)[i]].normal[2],
86  0);
87 
88  Eigen::Vector4f n_dir = p - center;
89  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
90 
91  // Calculate the angular distance between the point normal and the plane normal
92  double d_normal = fabs (getAngle3D (n, n_dir));
93  d_normal = (std::min) (d_normal, M_PI - d_normal);
94 
95  double distance = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
96  if (distance < threshold)
97  {
98  // Returns the indices of the points whose distances are smaller than the threshold
99  inliers[nr_p] = (*indices_)[i];
100  error_sqr_dists_[nr_p] = static_cast<double> (distance);
101  ++nr_p;
102  }
103  }
104  inliers.resize (nr_p);
105  error_sqr_dists_.resize (nr_p);
106 }
107 
108 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109 template <typename PointT, typename PointNT> int
111  const Eigen::VectorXf &model_coefficients, const double threshold) const
112 {
113  if (!normals_)
114  {
115  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::getDistancesToModel] No input dataset containing normals was given!\n");
116  return (0);
117  }
118 
119  // Check if the model is valid given the user constraints
120  if (!isModelValid (model_coefficients))
121  return(0);
122 
123 
124  // Obtain the shpere centroid
125  Eigen::Vector4f center = model_coefficients;
126  center[3] = 0;
127 
128  int nr_p = 0;
129 
130  // Iterate through the 3d points and calculate the distances from them to the plane
131  for (size_t i = 0; i < indices_->size (); ++i)
132  {
133  // Calculate the distance from the point to the sphere centroid as the difference between
134  // dist(point,sphere_origin) and sphere_radius
135  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
136  input_->points[(*indices_)[i]].y,
137  input_->points[(*indices_)[i]].z,
138  0);
139 
140  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
141  normals_->points[(*indices_)[i]].normal[1],
142  normals_->points[(*indices_)[i]].normal[2],
143  0);
144 
145  Eigen::Vector4f n_dir = (p-center);
146  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
147  //
148  // Calculate the angular distance between the point normal and the plane normal
149  double d_normal = fabs (getAngle3D (n, n_dir));
150  d_normal = (std::min) (d_normal, M_PI - d_normal);
151 
152  if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
153  nr_p++;
154  }
155  return (nr_p);
156 }
157 
158 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159 template <typename PointT, typename PointNT> void
161  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
162 {
163  if (!normals_)
164  {
165  PCL_ERROR ("[pcl::SampleConsensusModelNormalSphere::getDistancesToModel] No input dataset containing normals was given!\n");
166  return;
167  }
168 
169  // Check if the model is valid given the user constraints
170  if (!isModelValid (model_coefficients))
171  {
172  distances.clear ();
173  return;
174  }
175 
176  // Obtain the sphere centroid
177  Eigen::Vector4f center = model_coefficients;
178  center[3] = 0;
179 
180  distances.resize (indices_->size ());
181 
182  // Iterate through the 3d points and calculate the distances from them to the plane
183  for (size_t i = 0; i < indices_->size (); ++i)
184  {
185  // Calculate the distance from the point to the sphere as the difference between
186  // dist(point,sphere_origin) and sphere_radius
187  Eigen::Vector4f p (input_->points[(*indices_)[i]].x,
188  input_->points[(*indices_)[i]].y,
189  input_->points[(*indices_)[i]].z,
190  0);
191 
192  Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0],
193  normals_->points[(*indices_)[i]].normal[1],
194  normals_->points[(*indices_)[i]].normal[2],
195  0);
196 
197  Eigen::Vector4f n_dir = (p-center);
198  double d_euclid = fabs (n_dir.norm () - model_coefficients[3]);
199  //
200  // Calculate the angular distance between the point normal and the plane normal
201  double d_normal = fabs (getAngle3D (n, n_dir));
202  d_normal = (std::min) (d_normal, M_PI - d_normal);
203 
204  distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
205  }
206 }
207 
208 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 template <typename PointT, typename PointNT> bool
210 pcl::SampleConsensusModelNormalSphere<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
211 {
212  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
213  return (false);
214 
215  if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
216  return (false);
217  if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
218  return (false);
219 
220  return (true);
221 }
222 
223 #define PCL_INSTANTIATE_SampleConsensusModelNormalSphere(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalSphere<PointT, PointNT>;
224 
225 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_SPHERE_H_
226 
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers) override
Select all the points which respect the given model coefficients as inliers.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2, const bool in_degree=false)
Compute the smallest angle between two 3D vectors in radians (default) or degree. ...
Definition: common.hpp:46
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:67
int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given sphere model.