41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
44 #include <pcl/sample_consensus/eigen.h>
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 ());
72 for (
int i = 0; i < 4; i++)
74 temp (i, 0) = (*input_)[samples[i]].x;
75 temp (i, 1) = (*input_)[samples[i]].y;
76 temp (i, 2) = (*input_)[samples[i]].z;
79 float m11 = temp.determinant ();
85 for (
int i = 0; i < 4; ++i)
87 temp (i, 0) = ((*input_)[samples[i]].x) * ((*input_)[samples[i]].x) +
88 ((*input_)[samples[i]].y) * ((*input_)[samples[i]].y) +
89 ((*input_)[samples[i]].z) * ((*input_)[samples[i]].z);
91 float m12 = temp.determinant ();
93 for (
int i = 0; i < 4; ++i)
95 temp (i, 1) = temp (i, 0);
96 temp (i, 0) = (*input_)[samples[i]].x;
98 float m13 = temp.determinant ();
100 for (
int i = 0; i < 4; ++i)
102 temp (i, 2) = temp (i, 1);
103 temp (i, 1) = (*input_)[samples[i]].y;
105 float m14 = temp.determinant ();
107 for (
int i = 0; i < 4; ++i)
109 temp (i, 0) = temp (i, 2);
110 temp (i, 1) = (*input_)[samples[i]].x;
111 temp (i, 2) = (*input_)[samples[i]].y;
112 temp (i, 3) = (*input_)[samples[i]].z;
114 float m15 = temp.determinant ();
117 model_coefficients.resize (model_size_);
118 model_coefficients[0] = 0.5f * m12 / m11;
119 model_coefficients[1] = 0.5f * m13 / m11;
120 model_coefficients[2] = 0.5f * m14 / m11;
122 model_coefficients[3] = std::sqrt (model_coefficients[0] * model_coefficients[0] +
123 model_coefficients[1] * model_coefficients[1] +
124 model_coefficients[2] * model_coefficients[2] - m15 / m11);
129 #define AT(POS) ((*input_)[(*indices_)[(POS)]])
135 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);
136 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);
137 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);
138 return _mm256_add_ps (_mm256_add_ps (_mm256_mul_ps (tmp1, tmp1), _mm256_mul_ps (tmp2, tmp2)), _mm256_mul_ps(tmp3, tmp3));
140 #endif // ifdef __AVX__
146 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);
147 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);
148 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);
149 return _mm_add_ps (_mm_add_ps (_mm_mul_ps (tmp1, tmp1), _mm_mul_ps (tmp2, tmp2)), _mm_mul_ps(tmp3, tmp3));
151 #endif // ifdef __SSE__
156 template <
typename Po
intT>
void
158 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
161 if (!isModelValid (model_coefficients))
166 distances.resize (indices_->size ());
168 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
170 for (std::size_t i = 0; i < indices_->size (); ++i)
174 distances[i] = std::abs (((*input_)[(*indices_)[i]].getVector3fMap () - center).norm () - model_coefficients[3]);
179 template <
typename Po
intT>
void
181 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
184 if (!isModelValid (model_coefficients))
191 error_sqr_dists_.clear ();
192 inliers.reserve (indices_->size ());
193 error_sqr_dists_.reserve (indices_->size ());
195 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
196 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
197 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
199 for (std::size_t i = 0; i < indices_->size (); ++i)
203 const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
204 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
207 inliers.push_back ((*indices_)[i]);
209 error_sqr_dists_.push_back (
static_cast<double> (std::abs (std::sqrt (sqr_dist) - model_coefficients[3])));
215 template <
typename Po
intT> std::size_t
217 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
220 if (!isModelValid (model_coefficients))
223 #if defined (__AVX__) && defined (__AVX2__)
224 return countWithinDistanceAVX (model_coefficients, threshold);
225 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
226 return countWithinDistanceSSE (model_coefficients, threshold);
228 return countWithinDistanceStandard (model_coefficients, threshold);
233 template <
typename Po
intT> std::size_t
235 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
237 std::size_t nr_p = 0;
238 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
239 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
240 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
242 for (; i < indices_->size (); ++i)
246 const float sqr_dist = ((*input_)[(*indices_)[i]].getVector3fMap () - center).squaredNorm ();
247 if ((sqr_dist <= sqr_outer_radius) && (sqr_dist >= sqr_inner_radius))
254 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
255 template <
typename Po
intT> std::size_t
257 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
259 std::size_t nr_p = 0;
260 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
261 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
262 const __m128 c_vec = _mm_set1_ps (model_coefficients[2]);
264 const __m128 sqr_inner_radius = _mm_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
265 const __m128 sqr_outer_radius = _mm_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
266 __m128i res = _mm_set1_epi32(0);
267 for (; (i + 4) <= indices_->size (); i += 4)
269 const __m128 sqr_dist = sqr_dist4 (i, a_vec, b_vec, c_vec);
270 const __m128 mask = _mm_and_ps (_mm_cmplt_ps (sqr_inner_radius, sqr_dist), _mm_cmplt_ps (sqr_dist, sqr_outer_radius));
271 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
278 nr_p += _mm_extract_epi32 (res, 0);
279 nr_p += _mm_extract_epi32 (res, 1);
280 nr_p += _mm_extract_epi32 (res, 2);
281 nr_p += _mm_extract_epi32 (res, 3);
284 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
290 #if defined (__AVX__) && defined (__AVX2__)
291 template <
typename Po
intT> std::size_t
293 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
295 std::size_t nr_p = 0;
296 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
297 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
298 const __m256 c_vec = _mm256_set1_ps (model_coefficients[2]);
300 const __m256 sqr_inner_radius = _mm256_set1_ps ((model_coefficients[3] <= threshold ? 0.0 : (model_coefficients[3]-threshold)*(model_coefficients[3]-threshold)));
301 const __m256 sqr_outer_radius = _mm256_set1_ps ((model_coefficients[3]+threshold)*(model_coefficients[3]+threshold));
302 __m256i res = _mm256_set1_epi32(0);
303 for (; (i + 8) <= indices_->size (); i += 8)
305 const __m256 sqr_dist = sqr_dist8 (i, a_vec, b_vec, c_vec);
306 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));
307 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
318 nr_p += _mm256_extract_epi32 (res, 0);
319 nr_p += _mm256_extract_epi32 (res, 1);
320 nr_p += _mm256_extract_epi32 (res, 2);
321 nr_p += _mm256_extract_epi32 (res, 3);
322 nr_p += _mm256_extract_epi32 (res, 4);
323 nr_p += _mm256_extract_epi32 (res, 5);
324 nr_p += _mm256_extract_epi32 (res, 6);
325 nr_p += _mm256_extract_epi32 (res, 7);
328 nr_p += countWithinDistanceStandard (model_coefficients, threshold, i);
334 template <
typename Po
intT>
void
336 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
338 optimized_coefficients = model_coefficients;
341 if (!isModelValid (model_coefficients))
343 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Given model is invalid!\n");
348 if (inliers.size () <= sample_size_)
350 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
354 OptimizationFunctor functor (
this, inliers);
355 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
356 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
357 int info = lm.minimize (optimized_coefficients);
360 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",
361 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]);
365 template <
typename Po
intT>
void
367 const Indices &,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool)
const
370 if (!isModelValid (model_coefficients))
372 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::projectPoints] Given model is invalid!\n");
377 projected_points.
resize (input_->size ());
378 projected_points.
header = input_->header;
379 projected_points.
width = input_->width;
380 projected_points.
height = input_->height;
381 projected_points.
is_dense = input_->is_dense;
383 PCL_WARN (
"[pcl::SampleConsensusModelSphere::projectPoints] Not implemented yet.\n");
384 projected_points.
points = input_->points;
388 template <
typename Po
intT>
bool
390 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
393 if (!isModelValid (model_coefficients))
395 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Given model is invalid!\n");
399 const float sqr_inner_radius = (model_coefficients[3] <= threshold ? 0.0f : (model_coefficients[3] - threshold) * (model_coefficients[3] - threshold));
400 const float sqr_outer_radius = (model_coefficients[3] + threshold) * (model_coefficients[3] + threshold);
401 const Eigen::Vector3f center (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
402 for (
const auto &index : indices)
406 const float sqr_dist = ((*input_)[index].getVector3fMap () - center).squaredNorm ();
407 if ((sqr_dist > sqr_outer_radius) || (sqr_dist < sqr_inner_radius))
416 #define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<T>;
418 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_