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_);
59 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
60 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
61 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
67 return (p1.dot (p2) < 0.000001);
71 template <
typename Po
intT>
bool
75 if (samples.size () != sample_size_)
77 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
81 model_coefficients.resize (model_size_);
83 Eigen::Vector3d p0 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z);
84 Eigen::Vector3d p1 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z);
85 Eigen::Vector3d p2 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z);
88 Eigen::Vector3d helper_vec01 = p0 - p1;
89 Eigen::Vector3d helper_vec02 = p0 - p2;
90 Eigen::Vector3d helper_vec10 = p1 - p0;
91 Eigen::Vector3d helper_vec12 = p1 - p2;
92 Eigen::Vector3d helper_vec20 = p2 - p0;
93 Eigen::Vector3d helper_vec21 = p2 - p1;
95 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
97 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
99 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
100 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
101 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
103 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
105 Eigen::Vector3d circle_radiusVector = circle_center - p0;
106 double circle_radius = circle_radiusVector.norm ();
107 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
109 model_coefficients[0] =
static_cast<float> (circle_center[0]);
110 model_coefficients[1] =
static_cast<float> (circle_center[1]);
111 model_coefficients[2] =
static_cast<float> (circle_center[2]);
112 model_coefficients[3] =
static_cast<float> (circle_radius);
113 model_coefficients[4] =
static_cast<float> (circle_normal[0]);
114 model_coefficients[5] =
static_cast<float> (circle_normal[1]);
115 model_coefficients[6] =
static_cast<float> (circle_normal[2]);
117 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
118 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
119 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
124 template <
typename Po
intT>
void
128 if (!isModelValid (model_coefficients))
133 distances.resize (indices_->size ());
136 for (std::size_t i = 0; i < indices_->size (); ++i)
146 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
148 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
150 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
152 double r = model_coefficients[3];
154 Eigen::Vector3d helper_vectorPC = P - C;
156 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
159 Eigen::Vector3d P_proj = P + lambda * N;
160 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
163 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
164 Eigen::Vector3d distanceVector = P -
K;
166 distances[i] = distanceVector.norm ();
171 template <
typename Po
intT>
void
173 const Eigen::VectorXf &model_coefficients,
const double threshold,
177 if (!isModelValid (model_coefficients))
183 inliers.reserve (indices_->size ());
185 const auto squared_threshold = threshold * threshold;
187 for (std::size_t i = 0; i < indices_->size (); ++i)
191 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
193 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
195 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
197 double r = model_coefficients[3];
199 Eigen::Vector3d helper_vectorPC = P - C;
201 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
203 Eigen::Vector3d P_proj = P + lambda * N;
204 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
207 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
208 Eigen::Vector3d distanceVector = P -
K;
210 if (distanceVector.squaredNorm () < squared_threshold)
213 inliers.push_back ((*indices_)[i]);
219 template <
typename Po
intT> std::size_t
221 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
224 if (!isModelValid (model_coefficients))
226 std::size_t nr_p = 0;
228 const auto squared_threshold = threshold * threshold;
230 for (std::size_t i = 0; i < indices_->size (); ++i)
234 Eigen::Vector3d P ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z);
236 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
238 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
240 double r = model_coefficients[3];
242 Eigen::Vector3d helper_vectorPC = P - C;
244 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
247 Eigen::Vector3d P_proj = P + lambda * N;
248 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
251 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
252 Eigen::Vector3d distanceVector = P -
K;
254 if (distanceVector.squaredNorm () < squared_threshold)
261 template <
typename Po
intT>
void
264 const Eigen::VectorXf &model_coefficients,
265 Eigen::VectorXf &optimized_coefficients)
const
267 optimized_coefficients = model_coefficients;
270 if (!isModelValid (model_coefficients))
272 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Given model is invalid!\n");
277 if (inliers.size () <= sample_size_)
279 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
283 OptimizationFunctor functor (
this, inliers);
284 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
285 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
286 Eigen::VectorXd coeff;
287 int info = lm.minimize (coeff);
288 for (Eigen::Index i = 0; i < coeff.size (); ++i)
289 optimized_coefficients[i] =
static_cast<float> (coeff[i]);
292 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",
293 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]);
297 template <
typename Po
intT>
void
299 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
300 PointCloud &projected_points,
bool copy_data_fields)
const
303 if (!isModelValid (model_coefficients))
305 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Given model is invalid!\n");
309 projected_points.
header = input_->header;
310 projected_points.
is_dense = input_->is_dense;
313 if (copy_data_fields)
316 projected_points.
resize (input_->size ());
317 projected_points.
width = input_->width;
318 projected_points.
height = input_->height;
320 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
322 for (std::size_t i = 0; i < projected_points.
size (); ++i)
327 for (std::size_t i = 0; i < inliers.size (); ++i)
331 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
333 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
335 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
337 double r = model_coefficients[3];
339 Eigen::Vector3d helper_vectorPC = P - C;
342 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
344 Eigen::Vector3d P_proj = P + lambda * N;
345 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
348 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
350 projected_points[i].x =
static_cast<float> (
K[0]);
351 projected_points[i].y =
static_cast<float> (
K[1]);
352 projected_points[i].z =
static_cast<float> (
K[2]);
358 projected_points.
resize (inliers.size ());
359 projected_points.
width = inliers.size ();
360 projected_points.
height = 1;
362 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
364 for (std::size_t i = 0; i < inliers.size (); ++i)
369 for (std::size_t i = 0; i < inliers.size (); ++i)
373 Eigen::Vector3d P ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z);
375 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
377 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
379 double r = model_coefficients[3];
381 Eigen::Vector3d helper_vectorPC = P - C;
383 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
385 Eigen::Vector3d P_proj = P + lambda * N;
386 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
389 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
391 projected_points[i].x =
static_cast<float> (
K[0]);
392 projected_points[i].y =
static_cast<float> (
K[1]);
393 projected_points[i].z =
static_cast<float> (
K[2]);
399 template <
typename Po
intT>
bool
401 const std::set<index_t> &indices,
402 const Eigen::VectorXf &model_coefficients,
403 const double threshold)
const
406 if (!isModelValid (model_coefficients))
408 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Given model is invalid!\n");
412 const auto squared_threshold = threshold * threshold;
413 for (
const auto &index : indices)
420 Eigen::Vector3d P ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z);
422 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
424 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
426 double r = model_coefficients[3];
427 Eigen::Vector3d helper_vectorPC = P - C;
429 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
431 Eigen::Vector3d P_proj = P + lambda * N;
432 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
435 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
436 Eigen::Vector3d distanceVector = P -
K;
438 if (distanceVector.squaredNorm () > squared_threshold)
445 template <
typename Po
intT>
bool
451 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
453 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too small: should be larger than %g, but is %g.\n",
454 radius_min_, model_coefficients[3]);
457 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
459 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle3D::isModelValid] Radius of circle is too big: should be smaller than %g, but is %g.\n",
460 radius_max_, model_coefficients[3]);
467 #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.