Point Cloud Library (PCL) 1.12.0
Loading...
Searching...
No Matches
sac_model_circle3d.hpp
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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 *
37 */
38
39#ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40#define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
41
42#include <cfloat> // for DBL_MAX
43
44#include <unsupported/Eigen/NonLinearOptimization> // for LevenbergMarquardt
45#include <pcl/sample_consensus/sac_model_circle3d.h>
46#include <pcl/common/concatenate.h>
47
48//////////////////////////////////////////////////////////////////////////
49template <typename PointT> bool
51 const Indices &samples) const
52{
53 if (samples.size () != sample_size_)
54 {
55 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
56 return (false);
57 }
58 // Get the values at the three points
59 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
60 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
61 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
62
63 // calculate vectors between points
64 p1 -= p0;
65 p2 -= p0;
66
67 return (p1.dot (p2) < 0.000001);
68}
69
70//////////////////////////////////////////////////////////////////////////
71template <typename PointT> bool
72pcl::SampleConsensusModelCircle3D<PointT>::computeModelCoefficients (const Indices &samples, Eigen::VectorXf &model_coefficients) const
73{
74 // Need 3 samples
75 if (samples.size () != sample_size_)
76 {
77 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
78 return (false);
79 }
80
81 model_coefficients.resize (model_size_); //needing 7 coefficients: centerX, centerY, centerZ, radius, normalX, normalY, normalZ
82
83 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
84 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
85 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
86
87
88 Eigen::Vector3d helper_vec01 = p0 - p1;
89 Eigen::Vector3d helper_vec02 = p0 - p2;
90 Eigen::Vector3d helper_vec10 = p1 - p0;
91 Eigen::Vector3d helper_vec12 = p1 - p2;
92 Eigen::Vector3d helper_vec20 = p2 - p0;
93 Eigen::Vector3d helper_vec21 = p2 - p1;
94
95 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
96
97 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
98
99 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
100 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
101 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
102
103 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
104
105 Eigen::Vector3d circle_radiusVector = circle_center - p0;
106 double circle_radius = circle_radiusVector.norm ();
107 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
108
109 model_coefficients[0] = static_cast<float> (circle_center[0]);
110 model_coefficients[1] = static_cast<float> (circle_center[1]);
111 model_coefficients[2] = static_cast<float> (circle_center[2]);
112 model_coefficients[3] = static_cast<float> (circle_radius);
113 model_coefficients[4] = static_cast<float> (circle_normal[0]);
114 model_coefficients[5] = static_cast<float> (circle_normal[1]);
115 model_coefficients[6] = static_cast<float> (circle_normal[2]);
116
117 PCL_DEBUG ("[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
118 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
119 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
120 return (true);
121}
122
123//////////////////////////////////////////////////////////////////////////
124template <typename PointT> void
125pcl::SampleConsensusModelCircle3D<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
126{
127 // Check if the model is valid given the user constraints
128 if (!isModelValid (model_coefficients))
129 {
130 distances.clear ();
131 return;
132 }
133 distances.resize (indices_->size ());
134
135 // Iterate through the 3d points and calculate the distances from them to the sphere
136 for (std::size_t i = 0; i < indices_->size (); ++i)
137 // Calculate the distance from the point to the circle:
138 // 1. calculate intersection point of the plane in which the circle lies and the
139 // line from the sample point with the direction of the plane normal (projected point)
140 // 2. calculate the intersection point of the line from the circle center to the projected point
141 // with the circle
142 // 3. calculate distance from corresponding point on the circle to the sample point
143 {
144 // what i have:
145 // P : Sample Point
146 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
147 // C : Circle Center
148 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
149 // N : Circle (Plane) Normal
150 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
151 // r : Radius
152 double r = model_coefficients[3];
153
154 Eigen::Vector3d helper_vectorPC = P - C;
155 // 1.1. get line parameter
156 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
157
158 // Projected Point on plane
159 Eigen::Vector3d P_proj = P + lambda * N;
160 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
162 // K : Point on Circle
163 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
164 Eigen::Vector3d distanceVector = P - K;
165
166 distances[i] = distanceVector.norm ();
167 }
168}
169
170//////////////////////////////////////////////////////////////////////////
171template <typename PointT> void
173 const Eigen::VectorXf &model_coefficients, const double threshold,
174 Indices &inliers)
175{
176 // Check if the model is valid given the user constraints
177 if (!isModelValid (model_coefficients))
178 {
179 inliers.clear ();
180 return;
181 }
182 inliers.clear ();
183 inliers.reserve (indices_->size ());
184
185 const auto squared_threshold = threshold * threshold;
186 // Iterate through the 3d points and calculate the distances from them to the sphere
187 for (std::size_t i = 0; i < indices_->size (); ++i)
188 {
189 // what i have:
190 // P : Sample Point
191 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
192 // C : Circle Center
193 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
194 // N : Circle (Plane) Normal
195 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
196 // r : Radius
197 double r = model_coefficients[3];
198
199 Eigen::Vector3d helper_vectorPC = P - C;
200 // 1.1. get line parameter
201 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
202 // Projected Point on plane
203 Eigen::Vector3d P_proj = P + lambda * N;
204 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
205
206 // K : Point on Circle
207 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
208 Eigen::Vector3d distanceVector = P - K;
210 if (distanceVector.squaredNorm () < squared_threshold)
211 {
212 // Returns the indices of the points whose distances are smaller than the threshold
213 inliers.push_back ((*indices_)[i]);
214 }
216}
217
218//////////////////////////////////////////////////////////////////////////
219template <typename PointT> std::size_t
221 const Eigen::VectorXf &model_coefficients, const double threshold) const
222{
223 // Check if the model is valid given the user constraints
224 if (!isModelValid (model_coefficients))
225 return (0);
226 std::size_t nr_p = 0;
227
228 const auto squared_threshold = threshold * threshold;
229 // Iterate through the 3d points and calculate the distances from them to the sphere
230 for (std::size_t i = 0; i < indices_->size (); ++i)
231 {
232 // what i have:
233 // P : Sample Point
234 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
235 // C : Circle Center
236 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
237 // N : Circle (Plane) Normal
238 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
239 // r : Radius
240 double r = model_coefficients[3];
241
242 Eigen::Vector3d helper_vectorPC = P - C;
243 // 1.1. get line parameter
244 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
245
246 // Projected Point on plane
247 Eigen::Vector3d P_proj = P + lambda * N;
248 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
249
250 // K : Point on Circle
251 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
252 Eigen::Vector3d distanceVector = P - K;
253
254 if (distanceVector.squaredNorm () < squared_threshold)
255 nr_p++;
256 }
257 return (nr_p);
258}
259
260//////////////////////////////////////////////////////////////////////////
261template <typename PointT> void
263 const Indices &inliers,
264 const Eigen::VectorXf &model_coefficients,
265 Eigen::VectorXf &optimized_coefficients) const
266{
267 optimized_coefficients = model_coefficients;
268
269 // Needs a set of valid model coefficients
270 if (!isModelValid (model_coefficients))
271 {
272 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
273 return;
274 }
275
276 // Need more than the minimum sample size to make a difference
277 if (inliers.size () <= sample_size_)
278 {
279 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
280 return;
281 }
282
283 OptimizationFunctor functor (this, inliers);
284 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
285 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, double> lm (num_diff);
286 Eigen::VectorXd coeff;
287 int info = lm.minimize (coeff);
288 for (Eigen::Index i = 0; i < coeff.size (); ++i)
289 optimized_coefficients[i] = static_cast<float> (coeff[i]);
290
291 // Compute the L2 norm of the residuals
292 PCL_DEBUG ("[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
293 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], model_coefficients[4], model_coefficients[5], model_coefficients[6], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5], optimized_coefficients[6]);
294}
295
296//////////////////////////////////////////////////////////////////////////
297template <typename PointT> void
299 const Indices &inliers, const Eigen::VectorXf &model_coefficients,
300 PointCloud &projected_points, bool copy_data_fields) const
301{
302 // Needs a valid set of model coefficients
303 if (!isModelValid (model_coefficients))
304 {
305 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
306 return;
307 }
308
309 projected_points.header = input_->header;
310 projected_points.is_dense = input_->is_dense;
311
312 // Copy all the data fields from the input cloud to the projected one?
313 if (copy_data_fields)
314 {
315 // Allocate enough space and copy the basics
316 projected_points.resize (input_->size ());
317 projected_points.width = input_->width;
318 projected_points.height = input_->height;
319
320 using FieldList = typename pcl::traits::fieldList<PointT>::type;
321 // Iterate over each point
322 for (std::size_t i = 0; i < projected_points.size (); ++i)
323 // Iterate over each dimension
324 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
325
326 // Iterate through the 3d points and calculate the distances from them to the plane
327 for (std::size_t i = 0; i < inliers.size (); ++i)
328 {
329 // what i have:
330 // P : Sample Point
331 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
332 // C : Circle Center
333 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
334 // N : Circle (Plane) Normal
335 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
336 // r : Radius
337 double r = model_coefficients[3];
338
339 Eigen::Vector3d helper_vectorPC = P - C;
340 // 1.1. get line parameter
341 //float lambda = (helper_vectorPC.dot(N)) / N.squaredNorm() ;
342 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
343 // Projected Point on plane
344 Eigen::Vector3d P_proj = P + lambda * N;
345 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
346
347 // K : Point on Circle
348 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
349
350 projected_points[i].x = static_cast<float> (K[0]);
351 projected_points[i].y = static_cast<float> (K[1]);
352 projected_points[i].z = static_cast<float> (K[2]);
353 }
354 }
355 else
356 {
357 // Allocate enough space and copy the basics
358 projected_points.resize (inliers.size ());
359 projected_points.width = inliers.size ();
360 projected_points.height = 1;
361
362 using FieldList = typename pcl::traits::fieldList<PointT>::type;
363 // Iterate over each point
364 for (std::size_t i = 0; i < inliers.size (); ++i)
365 // Iterate over each dimension
366 pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
367
368 // Iterate through the 3d points and calculate the distances from them to the plane
369 for (std::size_t i = 0; i < inliers.size (); ++i)
370 {
371 // what i have:
372 // P : Sample Point
373 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
374 // C : Circle Center
375 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
376 // N : Circle (Plane) Normal
377 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
378 // r : Radius
379 double r = model_coefficients[3];
380
381 Eigen::Vector3d helper_vectorPC = P - C;
382 // 1.1. get line parameter
383 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
384 // Projected Point on plane
385 Eigen::Vector3d P_proj = P + lambda * N;
386 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
387
388 // K : Point on Circle
389 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
390
391 projected_points[i].x = static_cast<float> (K[0]);
392 projected_points[i].y = static_cast<float> (K[1]);
393 projected_points[i].z = static_cast<float> (K[2]);
394 }
395 }
396}
397
398//////////////////////////////////////////////////////////////////////////
399template <typename PointT> bool
401 const std::set<index_t> &indices,
402 const Eigen::VectorXf &model_coefficients,
403 const double threshold) const
404{
405 // Needs a valid model coefficients
406 if (!isModelValid (model_coefficients))
407 {
408 PCL_ERROR ("[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
409 return (false);
410 }
411
412 const auto squared_threshold = threshold * threshold;
413 for (const auto &index : indices)
414 {
415 // Calculate the distance from the point to the sphere as the difference between
416 //dist(point,sphere_origin) and sphere_radius
417
418 // what i have:
419 // P : Sample Point
420 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
421 // C : Circle Center
422 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
423 // N : Circle (Plane) Normal
424 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
425 // r : Radius
426 double r = model_coefficients[3];
427 Eigen::Vector3d helper_vectorPC = P - C;
428 // 1.1. get line parameter
429 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
430 // Projected Point on plane
431 Eigen::Vector3d P_proj = P + lambda * N;
432 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
433
434 // K : Point on Circle
435 Eigen::Vector3d K = C + r * helper_vectorP_projC.normalized ();
436 Eigen::Vector3d distanceVector = P - K;
437
438 if (distanceVector.squaredNorm () > squared_threshold)
439 return (false);
440 }
441 return (true);
442}
443
444//////////////////////////////////////////////////////////////////////////
445template <typename PointT> bool
446pcl::SampleConsensusModelCircle3D<PointT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
447{
448 if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
449 return (false);
450
451 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
452 {
453 PCL_DEBUG ("[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
454 radius_min_, model_coefficients[3]);
455 return (false);
456 }
457 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
458 {
459 PCL_DEBUG ("[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
460 radius_max_, model_coefficients[3]);
461 return (false);
462 }
463
464 return (true);
465}
466
467#define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
468
469#endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_
470
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given 3d circle model coefficients.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the 3d circle model.
std::size_t 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 3D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 3d circle coefficients using the given inlier set and return them to the user.
typename SampleConsensusModel< PointT >::PointCloud PointCloud
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 3D circle model.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
SampleConsensusModel represents the base model class.
Definition sac_model.h:70
@ K
Definition norms.h:54
void for_each_type(F f)
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133