41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
44 #include <pcl/sample_consensus/eigen.h>
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])));
108 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
114 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);
115 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);
116 return _mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2));
118 #endif // ifdef __AVX__
124 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);
125 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);
126 return _mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2));
128 #endif // ifdef __SSE__
133 template <
typename Po
intT>
void
137 if (!isModelValid (model_coefficients))
142 distances.resize (indices_->size ());
145 for (std::size_t i = 0; i < indices_->size (); ++i)
148 distances[i] = std::abs (std::sqrt (
149 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
150 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
152 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
153 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] )
154 ) - model_coefficients[2]);
158 template <
typename Po
intT>
void
160 const Eigen::VectorXf &model_coefficients,
const double threshold,
164 if (!isModelValid (model_coefficients))
170 error_sqr_dists_.clear ();
171 inliers.reserve (indices_->size ());
172 error_sqr_dists_.reserve (indices_->size ());
174 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
175 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
177 for (std::size_t i = 0; i < indices_->size (); ++i)
181 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
182 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
183 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
184 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
185 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
188 inliers.push_back ((*indices_)[i]);
190 error_sqr_dists_.push_back (
static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[2])));
196 template <
typename Po
intT> std::size_t
198 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
201 if (!isModelValid (model_coefficients))
204 #if defined (__AVX__) && defined (__AVX2__)
205 return countWithinDistanceAVX (model_coefficients, threshold);
206 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
207 return countWithinDistanceSSE (model_coefficients, threshold);
209 return countWithinDistanceStandard (model_coefficients, threshold);
214 template <
typename Po
intT> std::size_t
216 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
218 std::size_t nr_p = 0;
219 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
220 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
222 for (; i < indices_->size (); ++i)
226 const float sqr_dist = ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) *
227 ( (*input_)[(*indices_)[i]].x - model_coefficients[0] ) +
228 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] ) *
229 ( (*input_)[(*indices_)[i]].y - model_coefficients[1] );
230 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
237 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
238 template <
typename Po
intT> std::size_t
240 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
242 std::size_t nr_p = 0;
243 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
244 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
246 const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
247 const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
248 __m128i res = _mm_set1_epi32(0);
249 for (; (i + 4) <= indices_->size (); i += 4)
251 const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec);
252 const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius));
253 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
260 nr_p += _mm_extract_epi32 (res, 0);
261 nr_p += _mm_extract_epi32 (res, 1);
262 nr_p += _mm_extract_epi32 (res, 2);
263 nr_p += _mm_extract_epi32 (res, 3);
266 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
272 #if defined (__AVX__) && defined (__AVX2__)
273 template <
typename Po
intT> std::size_t
275 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
277 std::size_t nr_p = 0;
278 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
279 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
281 const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[2] <= threshold ? 0.0 : (model_coefficients[2]-threshold)*(model_coefficients[2]-threshold)));
282 const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[2]+threshold)*(model_coefficients[2]+threshold));
283 __m256i res = _mm256_set1_epi32(0);
284 for (; (i + 8) <= indices_->size (); i += 8)
286 const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec);
287 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));
288 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
299 nr_p += _mm256_extract_epi32 (res, 0);
300 nr_p += _mm256_extract_epi32 (res, 1);
301 nr_p += _mm256_extract_epi32 (res, 2);
302 nr_p += _mm256_extract_epi32 (res, 3);
303 nr_p += _mm256_extract_epi32 (res, 4);
304 nr_p += _mm256_extract_epi32 (res, 5);
305 nr_p += _mm256_extract_epi32 (res, 6);
306 nr_p += _mm256_extract_epi32 (res, 7);
309 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
315 template <
typename Po
intT>
void
317 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
319 optimized_coefficients = model_coefficients;
322 if (!isModelValid (model_coefficients))
324 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Given model is invalid!\n");
329 if (inliers.size () <= sample_size_)
331 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
335 OptimizationFunctor functor (
this, inliers);
336 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
337 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
338 int info = lm.minimize (optimized_coefficients);
341 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",
342 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2]);
346 template <
typename Po
intT>
void
348 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
349 PointCloud &projected_points,
bool copy_data_fields)
const
352 if (!isModelValid (model_coefficients))
354 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::projectPoints] Given model is invalid!\n");
358 projected_points.
header = input_->header;
359 projected_points.
is_dense = input_->is_dense;
362 if (copy_data_fields)
365 projected_points.
resize (input_->size ());
366 projected_points.
width = input_->width;
367 projected_points.
height = input_->height;
369 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
371 for (std::size_t i = 0; i < projected_points.
size (); ++i)
376 for (
const auto &inlier : inliers)
378 float dx = (*input_)[inlier].x - model_coefficients[0];
379 float dy = (*input_)[inlier].y - model_coefficients[1];
380 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
382 projected_points[inlier].x = a * dx + model_coefficients[0];
383 projected_points[inlier].y = a * dy + model_coefficients[1];
389 projected_points.
resize (inliers.size ());
390 projected_points.
width = inliers.size ();
391 projected_points.
height = 1;
393 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
395 for (std::size_t i = 0; i < inliers.size (); ++i)
400 for (std::size_t i = 0; i < inliers.size (); ++i)
402 float dx = (*input_)[inliers[i]].x - model_coefficients[0];
403 float dy = (*input_)[inliers[i]].y - model_coefficients[1];
404 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
406 projected_points[i].x = a * dx + model_coefficients[0];
407 projected_points[i].y = a * dy + model_coefficients[1];
413 template <
typename Po
intT>
bool
415 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
418 if (!isModelValid (model_coefficients))
420 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::doSamplesVerifyModel] Given model is invalid!\n");
424 const float sqr_inner_radius = (model_coefficients[2] <= threshold ? 0.0f : (model_coefficients[2] - threshold) * (model_coefficients[2] - threshold));
425 const float sqr_outer_radius = (model_coefficients[2] + threshold) * (model_coefficients[2] + threshold);
426 for (
const auto &index : indices)
430 const float sqr_dist = ( (*input_)[index].x - model_coefficients[0] ) *
431 ( (*input_)[index].x - model_coefficients[0] ) +
432 ( (*input_)[index].y - model_coefficients[1] ) *
433 ( (*input_)[index].y - model_coefficients[1] );
434 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
441 template <
typename Po
intT>
bool
447 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[2] < radius_min_)
449 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[2] > radius_max_)
455 #define PCL_INSTANTIATE_SampleConsensusModelCircle2D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle2D<T>;
457 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_