41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_H_
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_circle.h>
46 #include <pcl/common/concatenate.h>
49 template <
typename Po
intT>
bool
52 if (samples.size () != sample_size_)
54 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
58 Eigen::Array2d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y);
59 Eigen::Array2d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y);
60 Eigen::Array2d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y);
67 Eigen::Array2d dy1dy2 = p1 / p2;
69 return (dy1dy2[0] != dy1dy2[1]);
73 template <
typename Po
intT>
bool
77 if (samples.size () != sample_size_)
79 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
83 model_coefficients.resize (model_size_);
85 Eigen::Vector2d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y);
86 Eigen::Vector2d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y);
87 Eigen::Vector2d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y);
89 Eigen::Vector2d u = (p0 + p1) / 2.0;
90 Eigen::Vector2d v = (p1 + p2) / 2.0;
92 Eigen::Vector2d p1p0dif = p1 - p0;
93 Eigen::Vector2d p2p1dif = p2 - p1;
94 Eigen::Vector2d uvdif = u - v;
96 Eigen::Vector2d m (- p1p0dif[0] / p1p0dif[1], - p2p1dif[0] / p2p1dif[1]);
99 model_coefficients[0] =
static_cast<float> ((m[0] * u[0] - m[1] * v[0] - uvdif[1] ) / (m[0] - m[1]));
100 model_coefficients[1] =
static_cast<float> ((m[0] * m[1] * uvdif[0] + m[0] * v[1] - m[1] * u[1]) / (m[0] - m[1]));
103 model_coefficients[2] =
static_cast<float> (sqrt ((model_coefficients[0] - p0[0]) * (model_coefficients[0] - p0[0]) +
104 (model_coefficients[1] - p0[1]) * (model_coefficients[1] - p0[1])));
109 template <
typename Po
intT>
void
113 if (!isModelValid (model_coefficients))
118 distances.resize (indices_->size ());
121 for (std::size_t i = 0; i < indices_->size (); ++i)
124 distances[i] = std::abs (std::sqrt (
125 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
126 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
128 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
129 ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
130 ) - model_coefficients[2]);
134 template <
typename Po
intT>
void
136 const Eigen::VectorXf &model_coefficients,
const double threshold,
140 if (!isModelValid (model_coefficients))
146 error_sqr_dists_.clear ();
147 inliers.reserve (indices_->size ());
148 error_sqr_dists_.reserve (indices_->size ());
151 for (std::size_t i = 0; i < indices_->size (); ++i)
155 float distance = std::abs (std::sqrt (
156 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
157 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
159 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
160 ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
161 ) - model_coefficients[2]);
165 inliers.push_back ((*indices_)[i]);
166 error_sqr_dists_.push_back (
static_cast<double> (
distance));
172 template <
typename Po
intT> std::size_t
174 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
177 if (!isModelValid (model_coefficients))
179 std::size_t nr_p = 0;
182 for (std::size_t i = 0; i < indices_->size (); ++i)
186 float distance = std::abs (std::sqrt (
187 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
188 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
190 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
191 ( input_->points[(*indices_)[i]].y - model_coefficients[1] )
192 ) - model_coefficients[2]);
200 template <
typename Po
intT>
void
202 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
204 optimized_coefficients = model_coefficients;
207 if (!isModelValid (model_coefficients))
209 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Given model is invalid!\n");
214 if (inliers.size () <= sample_size_)
216 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
220 OptimizationFunctor functor (
this, inliers);
221 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
222 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
223 int info = lm.minimize (optimized_coefficients);
226 PCL_DEBUG (
"[pcl::SampleConsensusModelCircle2D::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g \nFinal solution: %g %g %g\n",
227 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2]);
231 template <
typename Po
intT>
void
233 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
234 PointCloud &projected_points,
bool copy_data_fields)
const
237 if (!isModelValid (model_coefficients))
239 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::projectPoints] Given model is invalid!\n");
243 projected_points.
header = input_->header;
244 projected_points.
is_dense = input_->is_dense;
247 if (copy_data_fields)
250 projected_points.
points.resize (input_->points.size ());
251 projected_points.
width = input_->width;
252 projected_points.
height = input_->height;
256 for (std::size_t i = 0; i < projected_points.
points.size (); ++i)
261 for (
const auto &inlier : inliers)
263 float dx = input_->points[inlier].x - model_coefficients[0];
264 float dy = input_->points[inlier].y - model_coefficients[1];
265 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
267 projected_points.
points[inlier].x = a * dx + model_coefficients[0];
268 projected_points.
points[inlier].y = a * dy + model_coefficients[1];
274 projected_points.
points.resize (inliers.size ());
276 projected_points.
height = 1;
280 for (std::size_t i = 0; i < inliers.size (); ++i)
285 for (std::size_t i = 0; i < inliers.size (); ++i)
287 float dx = input_->points[inliers[i]].x - model_coefficients[0];
288 float dy = input_->points[inliers[i]].y - model_coefficients[1];
289 float a = std::sqrt ( (model_coefficients[2] * model_coefficients[2]) / (dx * dx + dy * dy) );
291 projected_points.
points[i].x = a * dx + model_coefficients[0];
292 projected_points.
points[i].y = a * dy + model_coefficients[1];
298 template <
typename Po
intT>
bool
300 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
303 if (!isModelValid (model_coefficients))
305 PCL_ERROR (
"[pcl::SampleConsensusModelCircle2D::doSamplesVerifyModel] Given model is invalid!\n");
309 for (
const auto &index : indices)
312 if (std::abs (std::sqrt (
313 ( input_->points[index].x - model_coefficients[0] ) *
314 ( input_->points[index].x - model_coefficients[0] ) +
315 ( input_->points[index].y - model_coefficients[1] ) *
316 ( input_->points[index].y - model_coefficients[1] )
317 ) - model_coefficients[2]) > threshold)
324 template <
typename Po
intT>
bool
330 if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[2] < radius_min_)
332 if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[2] > radius_max_)
338 #define PCL_INSTANTIATE_SampleConsensusModelCircle2D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle2D<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).
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).
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
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 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...
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 2d circle model.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 2D circle model.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 2D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 2d circle coefficients using the given inlier set and return them to the user.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
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 2d circle model coefficients.
SampleConsensusModel represents the base model class.
float distance(const PointT &p1, const PointT &p2)
std::vector< index_t > Indices
Type used for indices in PCL.
Helper functor structure for concatenate.