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 inliers.reserve (indices_->size ());
199 const auto squared_threshold = threshold * threshold;
201 for (std::size_t i = 0; i < indices_->size (); ++i)
205 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
207 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
209 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
211 double r = model_coefficients[3];
213 Eigen::Vector3d helper_vectorPC = P - C;
215 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
217 Eigen::Vector3d P_proj = P + lambda * N;
218 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
221 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
222 Eigen::Vector3d distanceVector = P -
K;
224 if (distanceVector.squaredNorm () < squared_threshold)
227 inliers.push_back ((*indices_)[i]);
233 template <
typename Po
intT> std::size_t
235 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
238 if (!isModelValid (model_coefficients))
240 std::size_t nr_p = 0;
242 const auto squared_threshold = threshold * threshold;
244 for (std::size_t i = 0; i < indices_->size (); ++i)
248 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
250 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
252 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
254 double r = model_coefficients[3];
256 Eigen::Vector3d helper_vectorPC = P - C;
258 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
261 Eigen::Vector3d P_proj = P + lambda * N;
262 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
265 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
266 Eigen::Vector3d distanceVector = P -
K;
268 if (distanceVector.squaredNorm () < squared_threshold)
275 template <
typename Po
intT>
void
278 const Eigen::VectorXf &model_coefficients,
279 Eigen::VectorXf &optimized_coefficients)
const
281 optimized_coefficients = model_coefficients;
284 if (!isModelValid (model_coefficients))
286 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
291 if (inliers.size () <= sample_size_)
293 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
297 OptimizationFunctor functor (
this, inliers);
298 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
299 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
300 Eigen::VectorXd coeff;
301 int info = lm.minimize (coeff);
302 for (Eigen::Index i = 0; i < coeff.size (); ++i)
303 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
306 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",
307 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]);
311 template <
typename Po
intT>
void
313 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
314 PointCloud &projected_points,
bool copy_data_fields)
const
317 if (!isModelValid (model_coefficients))
319 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
323 projected_points.
header = input_->header;
324 projected_points.
is_dense = input_->is_dense;
327 if (copy_data_fields)
330 projected_points.
resize (input_->size ());
331 projected_points.
width = input_->width;
332 projected_points.
height = input_->height;
334 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
336 for (std::size_t i = 0; i < projected_points.
size (); ++i)
341 for (std::size_t i = 0; i < inliers.size (); ++i)
345 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
347 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
349 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
351 double r = model_coefficients[3];
353 Eigen::Vector3d helper_vectorPC = P - C;
356 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
358 Eigen::Vector3d P_proj = P + lambda * N;
359 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
362 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
364 projected_points[i].x =
static_cast<float> (
K[0]);
365 projected_points[i].y =
static_cast<float> (
K[1]);
366 projected_points[i].z =
static_cast<float> (
K[2]);
372 projected_points.
resize (inliers.size ());
373 projected_points.
width = inliers.size ();
374 projected_points.
height = 1;
376 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
378 for (std::size_t i = 0; i < inliers.size (); ++i)
383 for (std::size_t i = 0; i < inliers.size (); ++i)
387 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
389 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
391 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
393 double r = model_coefficients[3];
395 Eigen::Vector3d helper_vectorPC = P - C;
397 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
399 Eigen::Vector3d P_proj = P + lambda * N;
400 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
403 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
405 projected_points[i].x =
static_cast<float> (
K[0]);
406 projected_points[i].y =
static_cast<float> (
K[1]);
407 projected_points[i].z =
static_cast<float> (
K[2]);
413 template <
typename Po
intT>
bool
415 const std::set<index_t> &indices,
416 const Eigen::VectorXf &model_coefficients,
417 const double threshold)
const
420 if (!isModelValid (model_coefficients))
422 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
426 const auto squared_threshold = threshold * threshold;
427 for (
const auto &index : indices)
434 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
436 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
438 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
440 double r = model_coefficients[3];
441 Eigen::Vector3d helper_vectorPC = P - C;
443 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
445 Eigen::Vector3d P_proj = P + lambda * N;
446 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
449 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
450 Eigen::Vector3d distanceVector = P -
K;
452 if (distanceVector.squaredNorm () > squared_threshold)
459 template <
typename Po
intT>
bool
465 if (radius_min_ != std::numeric_limits<double>::lowest() && model_coefficients[3] < radius_min_)
467 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
468 radius_min_, model_coefficients[3]);
471 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
473 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
474 radius_max_, model_coefficients[3]);
481 #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.