41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
44 #include <unsupported/Eigen/NonLinearOptimization>
45 #include <pcl/sample_consensus/sac_model_circle.h>
46 #include <pcl/common/concatenate.h>
49 template <
typename Po
intT>
bool
52 if (samples.size () != sample_size_)
54 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
60 Eigen::Array2d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y);
61 Eigen::Array2d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y);
62 Eigen::Array2d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y);
71 if (std::abs (p1.x()*p2.y() - p2.x()*p1.y()) < Eigen::NumTraits<double>::dummy_precision ()) {
72 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::isSampleGood] Sample points too similar or collinear!\n");
80 template <
typename Po
intT>
bool
83 if (!isSampleGood (samples))
85 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Invalid set of samples given!\n");
89 model_coefficients.resize (model_size_);
91 Eigen::Vector2d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y);
92 Eigen::Vector2d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y);
93 Eigen::Vector2d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y);
95 Eigen::Vector2d u = (p0 + p1) / 2.0;
96 Eigen::Vector2d v = (p1 + p2) / 2.0;
98 Eigen::Vector2d p1p0dif = p1 - p0;
99 Eigen::Vector2d p2p1dif = p2 - p1;
100 Eigen::Vector2d uvdif = u - v;
102 Eigen::Vector2d m (- p1p0dif[0] / p1p0dif[1], - p2p1dif[0] / p2p1dif[1]);
105 model_coefficients[0] =
static_cast<float> ((m[0] * u[0] - m[1] * v[0] - uvdif[1] ) / (m[0] - m[1]));
106 model_coefficients[1] =
static_cast<float> ((m[0] * m[1] * uvdif[0] + m[0] * v[1] - m[1] * u[1]) / (m[0] - m[1]));
109 model_coefficients[2] =
static_cast<float> (sqrt ((model_coefficients[0] - p0[0]) * (model_coefficients[0] - p0[0]) +
110 (model_coefficients[1] - p0[1]) * (model_coefficients[1] - p0[1])));
111 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Model is (%g,%g,%g).\n",
112 model_coefficients[0], model_coefficients[1], model_coefficients[2]);
116 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
122 const __m256 tmp1 = _mm256_sub_ps (_mm256_set_ps (AT(i ).x, AT(i+1).x, AT(i+2).x, AT(i+3).x, AT(i+4).x, AT(i+5).x, AT(i+6).x, AT(i+7).x), a_vec);
123 const __m256 tmp2 = _mm256_sub_ps (_mm256_set_ps (AT(i ).y, AT(i+1).y, AT(i+2).y, AT(i+3).y, AT(i+4).y, AT(i+5).y, AT(i+6).y, AT(i+7).y), b_vec);
124 return _mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2));
132 const __m128 tmp1 = _mm_sub_ps (_mm_set_ps (AT(i ).x, AT(i+1).x, AT(i+2).x, AT(i+3).x), a_vec);
133 const __m128 tmp2 = _mm_sub_ps (_mm_set_ps (AT(i ).y, AT(i+1).y, AT(i+2).y, AT(i+3).y), b_vec);
134 return _mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2));
141 template <
typename Po
intT>
void
145 if (!isModelValid (model_coefficients))
150 distances.resize (indices_->size ());
153 for (std::size_t i = 0; i < indices_->size (); ++i)
156 distances[i] = std::abs (std::sqrt (
157 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
158 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
160 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
161 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] )
162 ) - model_coefficients[2]);
166 template <
typename Po
intT>
void
168 const Eigen::VectorXf &model_coefficients,
const double threshold,
172 if (!isModelValid (model_coefficients))
178 error_sqr_dists_.clear ();
179 inliers.reserve (indices_->size ());
180 error_sqr_dists_.reserve (indices_->size ());
182 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
183 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
185 for (std::size_t i = 0; i < indices_->size (); ++i)
189 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
190 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
191 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
192 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
193 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
196 inliers.push_back ((*indices_)[i]);
198 error_sqr_dists_.push_back (
static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[2])));
204 template <
typename Po
intT> std::size_t
206 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
209 if (!isModelValid (model_coefficients))
212 #if defined (__AVX__) && defined (__AVX2__)
213 return countWithinDistanceAVX (model_coefficients, threshold);
214 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
215 return countWithinDistanceSSE (model_coefficients, threshold);
217 return countWithinDistanceStandard (model_coefficients, threshold);
222 template <
typename Po
intT> std::size_t
224 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
226 std::size_t nr_p = 0;
227 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
228 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
230 for (; i < indices_->size (); ++i)
234 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
235 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
236 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
237 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
238 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
245 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
246 template <
typename Po
intT> std::size_t
248 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
250 std::size_t nr_p = 0;
251 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
252 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
254 const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
255 const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
256 __m128i res = _mm_set1_epi32(0);
257 for (; (i + 4) <= indices_->size (); i += 4)
259 const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec);
260 const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius));
261 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
268 nr_p += _mm_extract_epi32 (res, 0);
269 nr_p += _mm_extract_epi32 (res, 1);
270 nr_p += _mm_extract_epi32 (res, 2);
271 nr_p += _mm_extract_epi32 (res, 3);
274 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
280 #if defined (__AVX__) && defined (__AVX2__)
281 template <
typename Po
intT> std::size_t
283 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
285 std::size_t nr_p = 0;
286 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
287 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
289 const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
290 const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
291 __m256i res = _mm256_set1_epi32(0);
292 for (; (i + 8) <= indices_->size (); i += 8)
294 const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec);
295 const __m256 mask = _mm256_and_ps (_mm256_cmp_ps (sqr_inner_radius, sqr_dist, _CMP_LT_OQ), _mm256_cmp_ps (sqr_dist, sqr_outer_radius, _CMP_LT_OQ));
296 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
307 nr_p += _mm256_extract_epi32 (res, 0);
308 nr_p += _mm256_extract_epi32 (res, 1);
309 nr_p += _mm256_extract_epi32 (res, 2);
310 nr_p += _mm256_extract_epi32 (res, 3);
311 nr_p += _mm256_extract_epi32 (res, 4);
312 nr_p += _mm256_extract_epi32 (res, 5);
313 nr_p += _mm256_extract_epi32 (res, 6);
314 nr_p += _mm256_extract_epi32 (res, 7);
317 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
323 template <
typename Po
intT>
void
325 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
327 optimized_coefficients = model_coefficients;
330 if (!isModelValid (model_coefficients))
332 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Given model is invalid!\n");
337 if (inliers.size () <= sample_size_)
339 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
343 OptimizationFunctor functor (
this, inliers);
344 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
345 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
346 int info = lm.minimize (optimized_coefficients);
349 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g \nFinal solution: %g %g %g\n",
350 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2]);
354 template <
typename Po
intT>
void
356 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
357 PointCloud &projected_points,
bool copy_data_fields)
const
360 if (!isModelValid (model_coefficients))
362 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::projectPoints] Given model is invalid!\n");
366 projected_points.
header = input_->header;
367 projected_points.
is_dense = input_->is_dense;
370 if (copy_data_fields)
373 projected_points.
resize (input_->size ());
374 projected_points.
width = input_->width;
375 projected_points.
height = input_->height;
377 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
379 for (std::size_t i = 0; i < projected_points.
size (); ++i)
384 for (
const auto &inlier : inliers)
386 float dx = (*input_)[inlier].x - model_coefficients[0];
387 float dy = (*input_)[inlier].y - model_coefficients[1];
388 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
390 projected_points[inlier].x = a * dx + model_coefficients[0];
391 projected_points[inlier].y = a * dy + model_coefficients[1];
397 projected_points.
resize (inliers.size ());
398 projected_points.
width = inliers.size ();
399 projected_points.
height = 1;
401 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
403 for (std::size_t i = 0; i < inliers.size (); ++i)
408 for (std::size_t i = 0; i < inliers.size (); ++i)
410 float dx = (*input_)[inliers[i]].x - model_coefficients[0];
411 float dy = (*input_)[inliers[i]].y - model_coefficients[1];
412 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
414 projected_points[i].x = a * dx + model_coefficients[0];
415 projected_points[i].y = a * dy + model_coefficients[1];
421 template <
typename Po
intT>
bool
423 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
426 if (!isModelValid (model_coefficients))
428 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::doSamplesVerifyModel] Given model is invalid!\n");
432 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
433 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
434 for (
const auto &index : indices)
438 const float sqr_dist = ( (*input_)[index].x - model_coefficients[0] ) *
439 ( (*input_)[index].x - model_coefficients[0] ) +
440 ( (*input_)[index].y - model_coefficients[1] ) *
441 ( (*input_)[index].y - model_coefficients[1] );
442 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
449 template <
typename Po
intT>
bool
455 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[2] < radius_min_)
457 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
458 radius_min_, model_coefficients[2]);
461 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[2] > radius_max_)
463 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
464 radius_max_, model_coefficients[2]);
471 #define PCL_INSTANTIATE_SampleConsensusModelCircle2D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle2D<T>;
PointCloud represents the base class in PCL for storing collections of 3D points.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void resize(std::size_t count)
Resizes the container to contain count elements.
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane.
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.
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...
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 2d circle model.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 2D circle model.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 2D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 2d circle coefficients using the given inlier set and return them to the user.
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
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 2d circle model coefficients.
SampleConsensusModel represents the base model class.
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.