41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_TORUS_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_TORUS_H_
45 #include <pcl/sample_consensus/sac_model_torus.h>
46 #include <pcl/common/concatenate.h>
49 #include <unsupported/Eigen/NonLinearOptimization>
51 template <
typename Po
intT,
typename Po
intNT>
56 if (samples.size() != sample_size_) {
57 PCL_ERROR(
"[pcl::SampleConsensusTorus::isSampleGood] Wrong number of samples (is "
58 "%lu, should be %lu)!\n",
64 Eigen::Vector3f n0 = Eigen::Vector3f((*normals_)[samples[0]].getNormalVector3fMap());
65 Eigen::Vector3f n1 = Eigen::Vector3f((*normals_)[samples[1]].getNormalVector3fMap());
66 Eigen::Vector3f n2 = Eigen::Vector3f((*normals_)[samples[2]].getNormalVector3fMap());
67 Eigen::Vector3f n3 = Eigen::Vector3f((*normals_)[samples[3]].getNormalVector3fMap());
70 if (std::abs((n0).cross(n1).squaredNorm()) <
71 Eigen::NumTraits<float>::dummy_precision() ||
72 std::abs((n0).cross(n2).squaredNorm()) <
73 Eigen::NumTraits<float>::dummy_precision() ||
74 std::abs((n0).cross(n3).squaredNorm()) <
75 Eigen::NumTraits<float>::dummy_precision() ||
76 std::abs((n1).cross(n2).squaredNorm()) <
77 Eigen::NumTraits<float>::dummy_precision() ||
78 std::abs((n1).cross(n3).squaredNorm()) <
79 Eigen::NumTraits<float>::dummy_precision()) {
80 PCL_ERROR(
"[pcl::SampleConsensusModelEllipse3D::isSampleGood] Sample points "
81 "normals too similar or collinear!\n");
89 crossDot(Eigen::Vector3f v1, Eigen::Vector3f v2, Eigen::Vector3f v3)
91 return v1.cross(v2).dot(v3);
95 template <
typename Po
intT,
typename Po
intNT>
98 const Indices& samples, Eigen::VectorXf& model_coefficients)
const
102 if (!isSampleGood(samples)) {
103 PCL_ERROR(
"[pcl::SampleConsensusModelTorus::computeModelCoefficients] Invalid set "
104 "of samples given!\n");
109 PCL_ERROR(
"[pcl::SampleConsensusModelTorus::computeModelCoefficients] No input "
110 "dataset containing normals was given!\n");
123 const Eigen::Vector3f n0 = Eigen::Vector3f((*normals_)[samples[0]].getNormalVector3fMap());
124 const Eigen::Vector3f n1 = Eigen::Vector3f((*normals_)[samples[1]].getNormalVector3fMap());
125 const Eigen::Vector3f n2 = Eigen::Vector3f((*normals_)[samples[2]].getNormalVector3fMap());
126 const Eigen::Vector3f n3 = Eigen::Vector3f((*normals_)[samples[3]].getNormalVector3fMap());
128 const Eigen::Vector3f p0 = Eigen::Vector3f((*input_)[samples[0]].getVector3fMap());
129 const Eigen::Vector3f p1 = Eigen::Vector3f((*input_)[samples[1]].getVector3fMap());
130 const Eigen::Vector3f p2 = Eigen::Vector3f((*input_)[samples[2]].getVector3fMap());
131 const Eigen::Vector3f p3 = Eigen::Vector3f((*input_)[samples[3]].getVector3fMap());
133 const float a01 = crossDot(n0, n1, n2);
134 const float b01 = crossDot(n0, n1, n3);
135 const float a0 = crossDot(p2 - p1, n0, n2);
136 const float a1 = crossDot(p0 - p2, n1, n2);
137 const float b0 = crossDot(p3 - p1, n0, n3);
138 const float b1 = crossDot(p0 - p3, n1, n3);
139 const float a = crossDot(p0 - p2, p1 - p0, n2);
140 const float b = crossDot(p0 - p3, p1 - p0, n3);
148 const float k = -(a1 - b1 * a01 / b01) / (a0 - b0 * a01 / b01);
149 const float p = -(a - b * a01 / b01) / (a0 - b0 * a01 / b01);
157 const float _a = (b01 * k);
158 const float _b = (b01 * p + b0 * k + b1);
159 const float _c = (b0 * p + b);
161 const float eps = Eigen::NumTraits<float>::dummy_precision();
164 if ((_b * _b - 4 * _a * _c) < 0 || std::abs(a0 - b0 * a01) < eps ||
165 std::abs(b01) < eps || std::abs(_a) < eps) {
166 PCL_DEBUG(
"[pcl::SampleConsensusModelTorus::computeModelCoefficients] Can't "
167 "compute model coefficients with this method!\n");
171 const float s0 = (-_b + std::sqrt(_b * _b - 4 * _a * _c)) / (2 * _a);
172 const float s1 = (-_b - std::sqrt(_b * _b - 4 * _a * _c)) / (2 * _a);
174 float r_maj_stddev_cycle1 = std::numeric_limits<float>::max();
176 for (
float s : {s0, s1}) {
179 const float t0 = k * t1 + p;
182 Eigen::Vector3f d = ((p1 + n1 * t1) - (p0 + n0 * t0));
209 Eigen::MatrixXf A(4, 2);
210 A << d.dot(n0), 1, d.dot(n1), 1, d.dot(n2), 1, d.dot(n3), 1;
212 Eigen::Matrix<float, -1, -1>
B(4, 1);
213 B << -d.dot(p0), -d.dot(p1), -d.dot(p2), -d.dot(p3);
215 Eigen::Matrix<float, -1, -1> sol;
216 sol = A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(
B);
218 const float r_min = -sol(0);
219 const float D = sol(1);
225 const Eigen::Vector3f Pany = (p1 + n1 * t1);
227 const float lambda = (-d.dot(Pany) - D) / d.dot(d);
229 const Eigen::Vector3f centroid = Pany + d * lambda;
233 const float r_maj = std::sqrt(((p0 - r_min * n0 - centroid).squaredNorm() +
234 (p1 - r_min * n1 - centroid).squaredNorm() +
235 (p2 - r_min * n2 - centroid).squaredNorm() +
236 (p3 - r_min * n3 - centroid).squaredNorm()) /
239 const float r_maj_stddev =
240 std::sqrt((std::pow(r_maj - (p0 - r_min * n0 - centroid).norm(), 2) +
241 std::pow(r_maj - (p1 - r_min * n1 - centroid).norm(), 2) +
242 std::pow(r_maj - (p2 - r_min * n2 - centroid).norm(), 2) +
243 std::pow(r_maj - (p3 - r_min * n3 - centroid).norm(), 2)) /
246 if (r_maj_stddev < r_maj_stddev_cycle1) {
247 r_maj_stddev_cycle1 = r_maj_stddev;
253 model_coefficients.resize(model_size_);
254 model_coefficients[0] = r_maj;
255 model_coefficients[1] = r_min;
257 model_coefficients[2] = centroid[0];
258 model_coefficients[3] = centroid[1];
259 model_coefficients[4] = centroid[2];
261 model_coefficients[5] = d[0];
262 model_coefficients[6] = d[1];
263 model_coefficients[7] = d[2];
269 template <
typename Po
intT,
typename Po
intNT>
272 const Eigen::VectorXf& model_coefficients, std::vector<double>& distances)
const
275 if (!isModelValid(model_coefficients)) {
280 distances.resize(indices_->size());
284 for (std::size_t i = 0; i < indices_->size(); ++i) {
285 const Eigen::Vector3f pt = (*input_)[(*indices_)[i]].getVector3fMap();
286 const Eigen::Vector3f pt_n = (*normals_)[(*indices_)[i]].getNormalVector3fMap();
288 Eigen::Vector3f torus_closest;
289 projectPointToTorus(pt, pt_n, model_coefficients, torus_closest);
291 distances[i] = (torus_closest - pt).norm();
296 template <
typename Po
intT,
typename Po
intNT>
299 const Eigen::VectorXf& model_coefficients,
const double threshold,
Indices& inliers)
302 if (!isModelValid(model_coefficients)) {
307 error_sqr_dists_.clear();
308 inliers.reserve(indices_->size());
309 error_sqr_dists_.reserve(indices_->size());
311 for (std::size_t i = 0; i < indices_->size(); ++i) {
312 const Eigen::Vector3f pt = (*input_)[(*indices_)[i]].getVector3fMap();
313 const Eigen::Vector3f pt_n = (*normals_)[(*indices_)[i]].getNormalVector3fMap();
315 Eigen::Vector3f torus_closest;
316 projectPointToTorus(pt, pt_n, model_coefficients, torus_closest);
318 const float distance = (torus_closest - pt).norm();
323 inliers.push_back((*indices_)[i]);
324 error_sqr_dists_.push_back(
distance);
330 template <
typename Po
intT,
typename Po
intNT>
333 const Eigen::VectorXf& model_coefficients,
const double threshold)
const
335 if (!isModelValid(model_coefficients))
338 std::size_t nr_p = 0;
340 for (std::size_t i = 0; i < indices_->size(); ++i) {
341 const Eigen::Vector3f pt = (*input_)[(*indices_)[i]].getVector3fMap();
342 const Eigen::Vector3f pt_n = (*normals_)[(*indices_)[i]].getNormalVector3fMap();
344 Eigen::Vector3f torus_closest;
345 projectPointToTorus(pt, pt_n, model_coefficients, torus_closest);
347 const float distance = (torus_closest - pt).norm();
357 template <
typename Po
intT,
typename Po
intNT>
361 const Eigen::VectorXf& model_coefficients,
362 Eigen::VectorXf& optimized_coefficients)
const
365 optimized_coefficients = model_coefficients;
368 if (!isModelValid(model_coefficients)) {
369 PCL_ERROR(
"[pcl::SampleConsensusModelTorus::optimizeModelCoefficients] Given model "
375 if (inliers.size() <= sample_size_) {
376 PCL_ERROR(
"[pcl::SampleConsensusModelTorus::optimizeModelCoefficients] Not enough "
377 "inliers to refine/optimize the model's coefficients (%lu)! Returning "
378 "the same coefficients.\n",
383 OptimizationFunctor functor(
this, inliers);
384 Eigen::NumericalDiff<OptimizationFunctor> num_diff(functor);
385 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm(
388 Eigen::VectorXd coeff = model_coefficients.cast<
double>();
389 int info = lm.minimize(coeff);
393 "[pcl::SampleConsensusModelTorus::optimizeModelCoefficients] Optimizer returned"
394 "with error (%i)! Returning ",
400 coeff.tail<3>().normalize();
401 optimized_coefficients = coeff.cast<
float>();
405 template <
typename Po
intT,
typename Po
intNT>
408 const Eigen::Vector3f& p_in,
409 const Eigen::Vector3f& p_n,
410 const Eigen::VectorXf& model_coefficients,
411 Eigen::Vector3f& pt_out)
const
415 const float& R = model_coefficients[0];
416 const float& r = model_coefficients[1];
418 const float& x0 = model_coefficients[2];
419 const float& y0 = model_coefficients[3];
420 const float& z0 = model_coefficients[4];
422 const float& nx = model_coefficients[5];
423 const float& ny = model_coefficients[6];
424 const float& nz = model_coefficients[7];
427 Eigen::Vector3f n{nx, ny, nz};
430 Eigen::Vector3f pt0{x0, y0, z0};
433 const float D = -n.dot(pt0);
443 Eigen::Vector3f pt_proj;
447 if (std::abs(n.dot(p_n)) > Eigen::NumTraits<float>::dummy_precision()) {
448 float lambda = (-D - n.dot(p_in)) / n.dot(p_n);
449 pt_proj = p_in + lambda * p_n;
456 const Eigen::Vector3f circle_closest = (pt_proj - pt0).normalized() * R + pt0;
460 pt_out = (p_in - circle_closest).normalized() * r + circle_closest;
464 template <
typename Po
intT,
typename Po
intNT>
468 const Eigen::VectorXf& model_coefficients,
470 bool copy_data_fields)
const
473 if (!isModelValid(model_coefficients)) {
475 "[pcl::SampleConsensusModelCylinder::projectPoints] Given model is invalid!\n");
480 if (copy_data_fields) {
482 projected_points.
resize(input_->size());
483 projected_points.
width = input_->width;
484 projected_points.
height = input_->height;
486 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
488 for (std::size_t i = 0; i < input_->size(); ++i)
490 pcl::for_each_type<FieldList>(
494 for (
const auto& inlier : inliers) {
496 const Eigen::Vector3f pt_n = (*normals_)[inlier].getNormalVector3fMap();
498 (*input_)[inlier].getVector3fMap(), pt_n, model_coefficients, q);
499 projected_points[inlier].getVector3fMap() = q;
504 projected_points.
resize(inliers.size());
505 projected_points.
width = inliers.size();
506 projected_points.
height = 1;
508 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
510 for (std::size_t i = 0; i < inliers.size(); ++i) {
513 (*input_)[inliers[i]], projected_points[i]));
516 for (
const auto& inlier : inliers) {
518 const Eigen::Vector3f pt_n = (*normals_)[inlier].getNormalVector3fMap();
520 (*input_)[inlier].getVector3fMap(), pt_n, model_coefficients, q);
521 projected_points[inlier].getVector3fMap() = q;
527 template <
typename Po
intT,
typename Po
intNT>
530 const std::set<index_t>& indices,
531 const Eigen::VectorXf& model_coefficients,
532 const double threshold)
const
535 for (
const auto& index : indices) {
536 const Eigen::Vector3f pt_n = (*normals_)[index].getNormalVector3fMap();
537 Eigen::Vector3f torus_closest;
538 projectPointToTorus((*input_)[index].getVector3fMap(), pt_n, model_coefficients, torus_closest);
540 if (((*input_)[index].getVector3fMap() - torus_closest).squaredNorm() > threshold * threshold)
547 template <
typename Po
intT,
typename Po
intNT>
550 const Eigen::VectorXf& model_coefficients)
const
555 if (radius_min_ != std::numeric_limits<double>::lowest() &&
556 (model_coefficients[0] < radius_min_ || model_coefficients[1] < radius_min_)) {
558 "[pcl::SampleConsensusModelTorus::isModelValid] Major radius OR minor radius "
559 "of torus is/are too small: should be larger than %g, but are {%g, %g}.\n",
561 model_coefficients[0],
562 model_coefficients[1]);
565 if (radius_max_ != std::numeric_limits<double>::max() &&
566 (model_coefficients[0] > radius_max_ || model_coefficients[1] > radius_max_)) {
568 "[pcl::SampleConsensusModelTorus::isModelValid] Major radius OR minor radius "
569 "of torus is/are too big: should be smaller than %g, but are {%g, %g}.\n",
571 model_coefficients[0],
572 model_coefficients[1]);
578 #define PCL_INSTANTIATE_SampleConsensusModelTorus(PointT, PointNT) \
579 template class PCL_EXPORTS pcl::SampleConsensusModelTorus<PointT, PointNT>;
PointCloud represents the base class in PCL for storing collections of 3D points.
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).
std::uint32_t height
The point cloud height (if organized as an image-structure).
SampleConsensusModel represents the base model class.
void projectPointToTorus(const Eigen::Vector3f &pt, const Eigen::Vector3f &pt_n, const Eigen::VectorXf &model_coefficients, Eigen::Vector3f &pt_proj) const
Project a point onto a torus given by its model coefficients (radii, torus_center_point,...
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.
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 torus model coefficients.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid torus model, compute the model coefficients fr...
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the torus 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 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 torus model.
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 torus model.
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.