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_);
58 Eigen::Array2d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y);
59 Eigen::Array2d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y);
60 Eigen::Array2d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y);
67 Eigen::Array2d dy1dy2 = p1 / p2;
69 return (dy1dy2[0] != dy1dy2[1]);
73 template <
typename Po
intT>
bool
77 if (samples.size () != sample_size_)
79 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
83 model_coefficients.resize (model_size_);
85 Eigen::Vector2d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y);
86 Eigen::Vector2d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y);
87 Eigen::Vector2d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y);
89 Eigen::Vector2d u = (p0 + p1) / 2.0;
90 Eigen::Vector2d v = (p1 + p2) / 2.0;
92 Eigen::Vector2d p1p0dif = p1 - p0;
93 Eigen::Vector2d p2p1dif = p2 - p1;
94 Eigen::Vector2d uvdif = u - v;
96 Eigen::Vector2d m (- p1p0dif[0] / p1p0dif[1], - p2p1dif[0] / p2p1dif[1]);
99 model_coefficients[0] =
static_cast<float> ((m[0] * u[0] - m[1] * v[0] - uvdif[1] ) / (m[0] - m[1]));
100 model_coefficients[1] =
static_cast<float> ((m[0] * m[1] * uvdif[0] + m[0] * v[1] - m[1] * u[1]) / (m[0] - m[1]));
103 model_coefficients[2] =
static_cast<float> (sqrt ((model_coefficients[0] - p0[0]) * (model_coefficients[0] - p0[0]) +
104 (model_coefficients[1] - p0[1]) * (model_coefficients[1] - p0[1])));
105 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Model is (%g,%g,%g).\n",
106 model_coefficients[0], model_coefficients[1], model_coefficients[2]);
110 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
116 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);
117 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);
118 return _mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2));
126 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);
127 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);
128 return _mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2));
135 template <
typename Po
intT>
void
139 if (!isModelValid (model_coefficients))
144 distances.resize (indices_->size ());
147 for (std::size_t i = 0; i < indices_->size (); ++i)
150 distances[i] = std::abs (std::sqrt (
151 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
152 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
154 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
155 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] )
156 ) - model_coefficients[2]);
160 template <
typename Po
intT>
void
162 const Eigen::VectorXf &model_coefficients,
const double threshold,
166 if (!isModelValid (model_coefficients))
172 error_sqr_dists_.clear ();
173 inliers.reserve (indices_->size ());
174 error_sqr_dists_.reserve (indices_->size ());
176 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
177 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
179 for (std::size_t i = 0; i < indices_->size (); ++i)
183 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
184 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
185 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
186 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
187 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
190 inliers.push_back ((*indices_)[i]);
192 error_sqr_dists_.push_back (
static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[2])));
198 template <
typename Po
intT> std::size_t
200 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
203 if (!isModelValid (model_coefficients))
206 #if defined (__AVX__) && defined (__AVX2__)
207 return countWithinDistanceAVX (model_coefficients, threshold);
208 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
209 return countWithinDistanceSSE (model_coefficients, threshold);
211 return countWithinDistanceStandard (model_coefficients, threshold);
216 template <
typename Po
intT> std::size_t
218 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
220 std::size_t nr_p = 0;
221 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
222 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
224 for (; i < indices_->size (); ++i)
228 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
229 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
230 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
231 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
232 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
239 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
240 template <
typename Po
intT> std::size_t
242 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
244 std::size_t nr_p = 0;
245 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
246 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
248 const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
249 const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
250 __m128i res = _mm_set1_epi32(0);
251 for (; (i + 4) <= indices_->size (); i += 4)
253 const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec);
254 const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius));
255 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
262 nr_p += _mm_extract_epi32 (res, 0);
263 nr_p += _mm_extract_epi32 (res, 1);
264 nr_p += _mm_extract_epi32 (res, 2);
265 nr_p += _mm_extract_epi32 (res, 3);
268 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
274 #if defined (__AVX__) && defined (__AVX2__)
275 template <
typename Po
intT> std::size_t
277 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
279 std::size_t nr_p = 0;
280 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
281 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
283 const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
284 const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
285 __m256i res = _mm256_set1_epi32(0);
286 for (; (i + 8) <= indices_->size (); i += 8)
288 const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec);
289 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));
290 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
301 nr_p += _mm256_extract_epi32 (res, 0);
302 nr_p += _mm256_extract_epi32 (res, 1);
303 nr_p += _mm256_extract_epi32 (res, 2);
304 nr_p += _mm256_extract_epi32 (res, 3);
305 nr_p += _mm256_extract_epi32 (res, 4);
306 nr_p += _mm256_extract_epi32 (res, 5);
307 nr_p += _mm256_extract_epi32 (res, 6);
308 nr_p += _mm256_extract_epi32 (res, 7);
311 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
317 template <
typename Po
intT>
void
319 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
321 optimized_coefficients = model_coefficients;
324 if (!isModelValid (model_coefficients))
326 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Given model is invalid!\n");
331 if (inliers.size () <= sample_size_)
333 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
337 OptimizationFunctor functor (
this, inliers);
338 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
339 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
340 int info = lm.minimize (optimized_coefficients);
343 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",
344 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2]);
348 template <
typename Po
intT>
void
350 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
351 PointCloud &projected_points,
bool copy_data_fields)
const
354 if (!isModelValid (model_coefficients))
356 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::projectPoints] Given model is invalid!\n");
360 projected_points.
header = input_->header;
361 projected_points.
is_dense = input_->is_dense;
364 if (copy_data_fields)
367 projected_points.
resize (input_->size ());
368 projected_points.
width = input_->width;
369 projected_points.
height = input_->height;
371 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
373 for (std::size_t i = 0; i < projected_points.
size (); ++i)
378 for (
const auto &inlier : inliers)
380 float dx = (*input_)[inlier].x - model_coefficients[0];
381 float dy = (*input_)[inlier].y - model_coefficients[1];
382 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
384 projected_points[inlier].x = a * dx + model_coefficients[0];
385 projected_points[inlier].y = a * dy + model_coefficients[1];
391 projected_points.
resize (inliers.size ());
392 projected_points.
width = inliers.size ();
393 projected_points.
height = 1;
395 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
397 for (std::size_t i = 0; i < inliers.size (); ++i)
402 for (std::size_t i = 0; i < inliers.size (); ++i)
404 float dx = (*input_)[inliers[i]].x - model_coefficients[0];
405 float dy = (*input_)[inliers[i]].y - model_coefficients[1];
406 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
408 projected_points[i].x = a * dx + model_coefficients[0];
409 projected_points[i].y = a * dy + model_coefficients[1];
415 template <
typename Po
intT>
bool
417 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
420 if (!isModelValid (model_coefficients))
422 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::doSamplesVerifyModel] Given model is invalid!\n");
426 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
427 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
428 for (
const auto &index : indices)
432 const float sqr_dist = ( (*input_)[index].x - model_coefficients[0] ) *
433 ( (*input_)[index].x - model_coefficients[0] ) +
434 ( (*input_)[index].y - model_coefficients[1] ) *
435 ( (*input_)[index].y - model_coefficients[1] );
436 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
443 template <
typename Po
intT>
bool
449 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[2] < radius_min_)
451 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
452 radius_min_, model_coefficients[2]);
455 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[2] > radius_max_)
457 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
458 radius_max_, model_coefficients[2]);
465 #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.