41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
44 #include <unsupported/Eigen/NonLinearOptimization>
45 #include <pcl/sample_consensus/sac_model_sphere.h>
48 template <
typename Po
intT>
bool
51 if (samples.size () != sample_size_)
53 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
60 template <
typename Po
intT>
bool
62 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
65 if (samples.size () != sample_size_)
67 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
73 for (
int i = 0; i < 4; i++)
75 temp (i, 0) = (*input_)[samples[i]].x;
76 temp (i, 1) = (*input_)[samples[i]].y;
77 temp (i, 2) = (*input_)[samples[i]].z;
80 const double m11 = temp.determinant ();
86 for (
int i = 0; i < 4; ++i)
88 temp (i, 0) = ((*input_)[samples[i]].x) * ((*input_)[samples[i]].x) +
89 ((*input_)[samples[i]].y) * ((*input_)[samples[i]].y) +
90 ((*input_)[samples[i]].z) * ((*input_)[samples[i]].z);
92 const double m12 = temp.determinant ();
94 for (
int i = 0; i < 4; ++i)
96 temp (i, 1) = temp (i, 0);
97 temp (i, 0) = (*input_)[samples[i]].x;
99 const double m13 = temp.determinant ();
101 for (
int i = 0; i < 4; ++i)
103 temp (i, 2) = temp (i, 1);
104 temp (i, 1) = (*input_)[samples[i]].y;
106 const double m14 = temp.determinant ();
108 for (
int i = 0; i < 4; ++i)
110 temp (i, 0) = temp (i, 2);
111 temp (i, 1) = (*input_)[samples[i]].x;
112 temp (i, 2) = (*input_)[samples[i]].y;
113 temp (i, 3) = (*input_)[samples[i]].z;
115 const double m15 = temp.determinant ();
118 model_coefficients.resize (model_size_);
119 model_coefficients[0] = 0.5f * m12 / m11;
120 model_coefficients[1] = 0.5f * m13 / m11;
121 model_coefficients[2] = 0.5f * m14 / m11;
123 model_coefficients[3] = std::sqrt (model_coefficients[0] * model_coefficients[0] +
124 model_coefficients[1] * model_coefficients[1] +
125 model_coefficients[2] * model_coefficients[2] - m15 / m11);
127 PCL_DEBUG (
"[pcl::SampleConsensusModelSphere::computeModelCoefficients] Model is (%g,%g,%g,%g)\n",
128 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3]);
132 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
138 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);
139 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);
140 const __m256 tmp3 = _mm256_sub_ps (_mm256_set_ps (AT(i ).z, AT(i+1).z, AT(i+2).z, AT(i+3).z, AT(i+4).z, AT(i+5).z, AT(i+6).z, AT(i+7).z), c_vec);
141 return _mm256_add_ps (_mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2)), _mm256_mul_ps(tmp3, tmp3));
149 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);
150 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);
151 const __m128 tmp3 = _mm_sub_ps (_mm_set_ps (AT(i ).z, AT(i+1).z, AT(i+2).z, AT(i+3).z), c_vec);
152 return _mm_add_ps (_mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2)), _mm_mul_ps(tmp3, tmp3));
159 template <
typename Po
intT>
void
161 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
164 if (!isModelValid (model_coefficients))
169 distances.resize (indices_->size ());
171 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
173 for (std::size_t i = 0; i < indices_->size (); ++i)
177 distances[i] = std::abs (((*input_)[(*indices_)[i]].getVector3fMap () - center).norm () - model_coefficients[3]);
182 template <
typename Po
intT>
void
184 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
187 if (!isModelValid (model_coefficients))
194 error_sqr_dists_.clear ();
195 inliers.reserve (indices_->size ());
196 error_sqr_dists_.reserve (indices_->size ());
198 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
199 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
200 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
202 for (std::size_t i = 0; i < indices_->size (); ++i)
206 const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
207 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
210 inliers.push_back ((*indices_)[i]);
212 error_sqr_dists_.push_back (
static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[3])));
218 template <
typename Po
intT> std::size_t
220 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
223 if (!isModelValid (model_coefficients))
226 #if defined (__AVX__) && defined (__AVX2__)
227 return countWithinDistanceAVX (model_coefficients, threshold);
228 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
229 return countWithinDistanceSSE (model_coefficients, threshold);
231 return countWithinDistanceStandard (model_coefficients, threshold);
236 template <
typename Po
intT> std::size_t
238 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
240 std::size_t nr_p = 0;
241 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
242 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
243 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
245 for (; i < indices_->size (); ++i)
249 const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
250 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
257 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
258 template <
typename Po
intT> std::size_t
260 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
262 std::size_t nr_p = 0;
263 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
264 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
265 const __m128 c_vec = _mm_set1_ps (model_coefficients[2]);
267 const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
268 const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
269 __m128i res = _mm_set1_epi32(0);
270 for (; (i + 4) <= indices_->size (); i += 4)
272 const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec, c_vec);
273 const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius));
274 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
281 nr_p += _mm_extract_epi32 (res, 0);
282 nr_p += _mm_extract_epi32 (res, 1);
283 nr_p += _mm_extract_epi32 (res, 2);
284 nr_p += _mm_extract_epi32 (res, 3);
287 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
293 #if defined (__AVX__) && defined (__AVX2__)
294 template <
typename Po
intT> std::size_t
296 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
298 std::size_t nr_p = 0;
299 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
300 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
301 const __m256 c_vec = _mm256_set1_ps (model_coefficients[2]);
303 const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
304 const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
305 __m256i res = _mm256_set1_epi32(0);
306 for (; (i + 8) <= indices_->size (); i += 8)
308 const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec, c_vec);
309 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));
310 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
321 nr_p += _mm256_extract_epi32 (res, 0);
322 nr_p += _mm256_extract_epi32 (res, 1);
323 nr_p += _mm256_extract_epi32 (res, 2);
324 nr_p += _mm256_extract_epi32 (res, 3);
325 nr_p += _mm256_extract_epi32 (res, 4);
326 nr_p += _mm256_extract_epi32 (res, 5);
327 nr_p += _mm256_extract_epi32 (res, 6);
328 nr_p += _mm256_extract_epi32 (res, 7);
331 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
337 template <
typename Po
intT>
void
339 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
341 optimized_coefficients = model_coefficients;
344 if (!isModelValid (model_coefficients))
346 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Given model is invalid!\n");
351 if (inliers.size () <= sample_size_)
353 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
357 OptimizationFunctor functor (
this, inliers);
358 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
359 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
360 int info = lm.minimize (optimized_coefficients);
363 PCL_DEBUG (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g \nFinal solution: %g %g %g %g\n",
364 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3]);
368 template <
typename Po
intT>
void
370 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
373 if (!isModelValid (model_coefficients))
375 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::projectPoints] Given model is invalid!\n");
379 projected_points.
header = input_->header;
380 projected_points.
is_dense = input_->is_dense;
383 const Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
385 const double r = model_coefficients[3];
388 if (copy_data_fields)
391 projected_points.
resize (input_->size ());
392 projected_points.
width = input_->width;
393 projected_points.
height = input_->height;
395 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
397 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
402 for (
const auto& inlier : inliers)
406 const Eigen::Vector3d P (input_->points[inlier].x, input_->points[inlier].y, input_->points[inlier].z);
408 const Eigen::Vector3d direction = (P - C).normalized();
411 const Eigen::Vector3d
K = C + r * direction;
413 projected_points.
points[inlier].x =
static_cast<float> (
K[0]);
414 projected_points.
points[inlier].y =
static_cast<float> (
K[1]);
415 projected_points.
points[inlier].z =
static_cast<float> (
K[2]);
421 projected_points.
resize (inliers.size ());
422 projected_points.
width =
static_cast<uint32_t
> (inliers.size ());
423 projected_points.
height = 1;
425 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
427 for (std::size_t i = 0; i < inliers.size (); ++i)
432 for (std::size_t i = 0; i < inliers.size (); ++i)
436 const Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
438 const Eigen::Vector3d direction = (P - C).normalized();
441 const Eigen::Vector3d
K = C + r * direction;
443 projected_points.
points[i].x =
static_cast<float> (
K[0]);
444 projected_points.
points[i].y =
static_cast<float> (
K[1]);
445 projected_points.
points[i].z =
static_cast<float> (
K[2]);
451 template <
typename Po
intT>
bool
453 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
456 if (!isModelValid (model_coefficients))
458 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Given model is invalid!\n");
462 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
463 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
464 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
465 for (
const auto &index : indices)
469 const float sqr_dist = ((*input_)[index].getVector3fMap () - center).squaredNorm ();
470 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
479 #define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<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).
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
SampleConsensusModelSphere defines a model for 3D sphere segmentation.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
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.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the sphere coefficients using the given inlier set and return them to the user.
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 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 sphere model coefficients.
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
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 sphere model.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid sphere model, compute the model coefficients f...
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.