39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
44 #include <unsupported/Eigen/NonLinearOptimization>
45 #include <pcl/sample_consensus/sac_model_circle3d.h>
46 #include <pcl/common/concatenate.h>
49 template <
typename Po
intT>
bool
53 if (samples.size () != sample_size_)
55 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
61 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
62 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
63 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
68 if ((p1 - p0).cross(p1 - p2).squaredNorm() < Eigen::NumTraits<float>::dummy_precision ())
70 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::isSampleGood] Sample points too similar or collinear!\n");
78 template <
typename Po
intT>
bool
81 if (samples.size () != sample_size_)
83 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
87 model_coefficients.resize (model_size_);
89 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
90 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
91 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
94 Eigen::Vector3d helper_vec01 = p0 - p1;
95 Eigen::Vector3d helper_vec02 = p0 - p2;
96 Eigen::Vector3d helper_vec10 = p1 - p0;
97 Eigen::Vector3d helper_vec12 = p1 - p2;
98 Eigen::Vector3d helper_vec20 = p2 - p0;
99 Eigen::Vector3d helper_vec21 = p2 - p1;
101 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
105 if (common_helper_vec.squaredNorm() < Eigen::NumTraits<float>::dummy_precision ())
107 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Sample points too similar or collinear!\n");
111 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
113 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
114 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
115 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
117 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
119 Eigen::Vector3d circle_radiusVector = circle_center - p0;
120 double circle_radius = circle_radiusVector.norm ();
121 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
123 model_coefficients[0] =
static_cast<float> (circle_center[0]);
124 model_coefficients[1] =
static_cast<float> (circle_center[1]);
125 model_coefficients[2] =
static_cast<float> (circle_center[2]);
126 model_coefficients[3] =
static_cast<float> (circle_radius);
127 model_coefficients[4] =
static_cast<float> (circle_normal[0]);
128 model_coefficients[5] =
static_cast<float> (circle_normal[1]);
129 model_coefficients[6] =
static_cast<float> (circle_normal[2]);
131 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
132 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
133 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
138 template <
typename Po
intT>
void
142 if (!isModelValid (model_coefficients))
147 distances.resize (indices_->size ());
150 for (std::size_t i = 0; i < indices_->size (); ++i)
160 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
162 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
164 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
166 double r = model_coefficients[3];
168 Eigen::Vector3d helper_vectorPC = P - C;
170 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
173 Eigen::Vector3d P_proj = P + lambda * N;
174 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
177 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
178 Eigen::Vector3d distanceVector = P -
K;
180 distances[i] = distanceVector.norm ();
185 template <
typename Po
intT>
void
187 const Eigen::VectorXf &model_coefficients,
const double threshold,
191 if (!isModelValid (model_coefficients))
197 error_sqr_dists_.clear ();
198 inliers.reserve (indices_->size ());
199 error_sqr_dists_.reserve (indices_->size ());
201 const auto squared_threshold = threshold * threshold;
203 for (std::size_t i = 0; i < indices_->size (); ++i)
207 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
209 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
211 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
213 double r = model_coefficients[3];
215 Eigen::Vector3d helper_vectorPC = P - C;
217 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
219 Eigen::Vector3d P_proj = P + lambda * N;
220 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
223 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
224 Eigen::Vector3d distanceVector = P -
K;
226 const double sqr_dist = distanceVector.squaredNorm ();
227 if (sqr_dist < squared_threshold)
230 inliers.push_back ((*indices_)[i]);
231 error_sqr_dists_.push_back (sqr_dist);
237 template <
typename Po
intT> std::size_t
239 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
242 if (!isModelValid (model_coefficients))
244 std::size_t nr_p = 0;
246 const auto squared_threshold = threshold * threshold;
248 for (std::size_t i = 0; i < indices_->size (); ++i)
252 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
254 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
256 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
258 double r = model_coefficients[3];
260 Eigen::Vector3d helper_vectorPC = P - C;
262 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
265 Eigen::Vector3d P_proj = P + lambda * N;
266 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
269 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
270 Eigen::Vector3d distanceVector = P -
K;
272 if (distanceVector.squaredNorm () < squared_threshold)
279 template <
typename Po
intT>
void
282 const Eigen::VectorXf &model_coefficients,
283 Eigen::VectorXf &optimized_coefficients)
const
285 optimized_coefficients = model_coefficients;
288 if (!isModelValid (model_coefficients))
290 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
295 if (inliers.size () <= sample_size_)
297 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
301 OptimizationFunctor functor (
this, inliers);
302 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
303 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
304 Eigen::VectorXd coeff = optimized_coefficients.cast<
double>();
305 int info = lm.minimize (coeff);
306 coeff.tail(3).normalize();
307 for (Eigen::Index i = 0; i < coeff.size (); ++i)
308 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
311 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
312 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], model_coefficients[4], model_coefficients[5], model_coefficients[6], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5], optimized_coefficients[6]);
316 template <
typename Po
intT>
void
318 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
319 PointCloud &projected_points,
bool copy_data_fields)
const
322 if (!isModelValid (model_coefficients))
324 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
328 projected_points.
header = input_->header;
329 projected_points.
is_dense = input_->is_dense;
332 if (copy_data_fields)
335 projected_points.
resize (input_->size ());
336 projected_points.
width = input_->width;
337 projected_points.
height = input_->height;
339 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
341 for (std::size_t i = 0; i < projected_points.
size (); ++i)
346 for (
const auto &inlier : inliers)
350 Eigen::Vector3d P ((*input_)[inlier].x, (*input_)[inlier].y, (*input_)[inlier].z);
352 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
354 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
356 double r = model_coefficients[3];
358 Eigen::Vector3d helper_vectorPC = P - C;
361 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
363 Eigen::Vector3d P_proj = P + lambda * N;
364 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
367 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
369 projected_points[inlier].x =
static_cast<float> (
K[0]);
370 projected_points[inlier].y =
static_cast<float> (
K[1]);
371 projected_points[inlier].z =
static_cast<float> (
K[2]);
377 projected_points.
resize (inliers.size ());
378 projected_points.
width = inliers.size ();
379 projected_points.
height = 1;
381 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
383 for (std::size_t i = 0; i < inliers.size (); ++i)
388 for (std::size_t i = 0; i < inliers.size (); ++i)
392 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
394 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
396 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
398 double r = model_coefficients[3];
400 Eigen::Vector3d helper_vectorPC = P - C;
402 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
404 Eigen::Vector3d P_proj = P + lambda * N;
405 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
408 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
410 projected_points[i].x =
static_cast<float> (
K[0]);
411 projected_points[i].y =
static_cast<float> (
K[1]);
412 projected_points[i].z =
static_cast<float> (
K[2]);
418 template <
typename Po
intT>
bool
420 const std::set<index_t> &indices,
421 const Eigen::VectorXf &model_coefficients,
422 const double threshold)
const
425 if (!isModelValid (model_coefficients))
427 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
431 const auto squared_threshold = threshold * threshold;
432 for (
const auto &index : indices)
439 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
441 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
443 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
445 double r = model_coefficients[3];
446 Eigen::Vector3d helper_vectorPC = P - C;
448 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
450 Eigen::Vector3d P_proj = P + lambda * N;
451 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
454 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
455 Eigen::Vector3d distanceVector = P -
K;
457 if (distanceVector.squaredNorm () > squared_threshold)
464 template <
typename Po
intT>
bool
470 if (radius_min_ != std::numeric_limits<double>::lowest() && model_coefficients[3] < radius_min_)
472 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
473 radius_min_, model_coefficients[3]);
476 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
478 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
479 radius_max_, model_coefficients[3]);
486 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<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).
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
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 3d circle model coefficients.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
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 3d circle model.
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.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 3D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 3d circle coefficients using the given inlier set and return them to the user.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 3D circle model.
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...
SampleConsensusModel represents the base model class.
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.