39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE_3D_HPP_
42 #include <pcl/sample_consensus/eigen.h>
43 #include <pcl/sample_consensus/sac_model_circle3d.h>
44 #include <pcl/common/concatenate.h>
47 template <
typename Po
intT>
bool
49 const std::vector<int> &samples)
const
52 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
53 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
54 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
60 if (p1.dot (p2) < 0.000001)
67 template <
typename Po
intT>
bool
71 if (samples.size () != 3)
73 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
77 model_coefficients.resize (7);
79 Eigen::Vector3d p0 (input_->points[samples[0]].x, input_->points[samples[0]].y, input_->points[samples[0]].z);
80 Eigen::Vector3d p1 (input_->points[samples[1]].x, input_->points[samples[1]].y, input_->points[samples[1]].z);
81 Eigen::Vector3d p2 (input_->points[samples[2]].x, input_->points[samples[2]].y, input_->points[samples[2]].z);
84 Eigen::Vector3d helper_vec01 = p0 - p1;
85 Eigen::Vector3d helper_vec02 = p0 - p2;
86 Eigen::Vector3d helper_vec10 = p1 - p0;
87 Eigen::Vector3d helper_vec12 = p1 - p2;
88 Eigen::Vector3d helper_vec20 = p2 - p0;
89 Eigen::Vector3d helper_vec21 = p2 - p1;
91 Eigen::Vector3d common_helper_vec = helper_vec01.cross (helper_vec12);
93 double commonDividend = 2.0 * common_helper_vec.squaredNorm ();
95 double alpha = (helper_vec12.squaredNorm () * helper_vec01.dot (helper_vec02)) / commonDividend;
96 double beta = (helper_vec02.squaredNorm () * helper_vec10.dot (helper_vec12)) / commonDividend;
97 double gamma = (helper_vec01.squaredNorm () * helper_vec20.dot (helper_vec21)) / commonDividend;
99 Eigen::Vector3d circle_center = alpha * p0 + beta * p1 + gamma * p2;
101 Eigen::Vector3d circle_radiusVector = circle_center - p0;
102 double circle_radius = circle_radiusVector.norm ();
103 Eigen::Vector3d circle_normal = common_helper_vec.normalized ();
105 model_coefficients[0] =
static_cast<float> (circle_center[0]);
106 model_coefficients[1] =
static_cast<float> (circle_center[1]);
107 model_coefficients[2] =
static_cast<float> (circle_center[2]);
108 model_coefficients[3] =
static_cast<float> (circle_radius);
109 model_coefficients[4] =
static_cast<float> (circle_normal[0]);
110 model_coefficients[5] =
static_cast<float> (circle_normal[1]);
111 model_coefficients[6] =
static_cast<float> (circle_normal[2]);
117 template <
typename Po
intT>
void
121 if (!isModelValid (model_coefficients))
126 distances.resize (indices_->size ());
129 for (
size_t i = 0; i < indices_->size (); ++i)
139 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
141 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
143 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
145 double r = model_coefficients[3];
147 Eigen::Vector3d helper_vectorPC = P - C;
149 double lambda = (helper_vectorPC.dot (N)) / N.squaredNorm ();
152 Eigen::Vector3d P_proj = P + lambda * N;
153 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
156 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
157 Eigen::Vector3d distanceVector = P -
K;
159 distances[i] = distanceVector.norm ();
164 template <
typename Po
intT>
void
166 const Eigen::VectorXf &model_coefficients,
const double threshold,
167 std::vector<int> &inliers)
170 if (!isModelValid (model_coefficients))
176 inliers.resize (indices_->size ());
179 for (
size_t i = 0; i < indices_->size (); ++i)
183 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
185 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
187 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
189 double r = model_coefficients[3];
191 Eigen::Vector3d helper_vectorPC = P - C;
193 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
195 Eigen::Vector3d P_proj = P + lambda * N;
196 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
199 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
200 Eigen::Vector3d distanceVector = P -
K;
202 if (distanceVector.norm () < threshold)
205 inliers[nr_p] = (*indices_)[i];
209 inliers.resize (nr_p);
213 template <
typename Po
intT>
int
215 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
218 if (!isModelValid (model_coefficients))
223 for (
size_t i = 0; i < indices_->size (); ++i)
227 Eigen::Vector3d P (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z);
229 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
231 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
233 double r = model_coefficients[3];
235 Eigen::Vector3d helper_vectorPC = P - C;
237 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
240 Eigen::Vector3d P_proj = P + lambda * N;
241 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
244 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
245 Eigen::Vector3d distanceVector = P -
K;
247 if (distanceVector.norm () < threshold)
254 template <
typename Po
intT>
void
256 const std::vector<int> &inliers,
257 const Eigen::VectorXf &model_coefficients,
258 Eigen::VectorXf &optimized_coefficients)
const
260 optimized_coefficients = model_coefficients;
263 if (model_coefficients.size () != 7)
265 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
270 if (inliers.size () <= 3)
272 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients] Not enough inliers found to support a model (%lu)! Returning the same coefficients.\n", inliers.size ());
276 OptimizationFunctor functor (
this, inliers);
277 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
278 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
double> lm (num_diff);
279 Eigen::VectorXd coeff;
280 int info = lm.minimize (coeff);
281 for (
int i = 0; i < coeff.size (); ++i)
282 optimized_coefficients[i] = static_cast<float> (coeff[i]);
285 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",
286 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]);
290 template <
typename Po
intT>
void
292 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
293 PointCloud &projected_points,
bool copy_data_fields)
const
296 if (model_coefficients.size () != 7)
298 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::projectPoints] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
302 projected_points.
header = input_->header;
303 projected_points.
is_dense = input_->is_dense;
306 if (copy_data_fields)
309 projected_points.
points.resize (input_->points.size ());
310 projected_points.
width = input_->width;
311 projected_points.
height = input_->height;
315 for (
size_t i = 0; i < projected_points.
points.size (); ++i)
320 for (
size_t i = 0; i < inliers.size (); ++i)
324 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
326 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
328 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
330 double r = model_coefficients[3];
332 Eigen::Vector3d helper_vectorPC = P - C;
335 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
337 Eigen::Vector3d P_proj = P + lambda * N;
338 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
341 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
343 projected_points.
points[i].x =
static_cast<float> (K[0]);
344 projected_points.
points[i].y =
static_cast<float> (K[1]);
345 projected_points.
points[i].z =
static_cast<float> (K[2]);
351 projected_points.
points.resize (inliers.size ());
352 projected_points.
width = uint32_t (inliers.size ());
353 projected_points.
height = 1;
357 for (
size_t i = 0; i < inliers.size (); ++i)
362 for (
size_t i = 0; i < inliers.size (); ++i)
366 Eigen::Vector3d P (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z);
368 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
370 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
372 double r = model_coefficients[3];
374 Eigen::Vector3d helper_vectorPC = P - C;
376 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
378 Eigen::Vector3d P_proj = P + lambda * N;
379 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
382 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
384 projected_points.
points[i].x =
static_cast<float> (K[0]);
385 projected_points.
points[i].y =
static_cast<float> (K[1]);
386 projected_points.
points[i].z =
static_cast<float> (K[2]);
392 template <
typename Po
intT>
bool
394 const std::set<int> &indices,
395 const Eigen::VectorXf &model_coefficients,
396 const double threshold)
const
399 if (model_coefficients.size () != 7)
401 PCL_ERROR (
"[pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
405 for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
412 Eigen::Vector3d P (input_->points[*it].x, input_->points[*it].y, input_->points[*it].z);
414 Eigen::Vector3d C (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
416 Eigen::Vector3d N (model_coefficients[4], model_coefficients[5], model_coefficients[6]);
418 double r = model_coefficients[3];
419 Eigen::Vector3d helper_vectorPC = P - C;
421 double lambda = (-(helper_vectorPC.dot (N))) / N.dot (N);
423 Eigen::Vector3d P_proj = P + lambda * N;
424 Eigen::Vector3d helper_vectorP_projC = P_proj - C;
427 Eigen::Vector3d
K = C + r * helper_vectorP_projC.normalized ();
428 Eigen::Vector3d distanceVector = P -
K;
430 if (distanceVector.norm () > threshold)
437 template <
typename Po
intT>
bool
443 if (radius_min_ != -DBL_MAX && model_coefficients[3] < radius_min_)
445 if (radius_max_ != DBL_MAX && model_coefficients[3] > radius_max_)
451 #define PCL_INSTANTIATE_SampleConsensusModelCircle3D(T) template class PCL_EXPORTS pcl::SampleConsensusModelCircle3D<T>;
453 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CIRCLE3D_HPP_
uint32_t width
The point cloud width (if organized as an image-structure).
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Compute all distances from the cloud data to a given 3D circle model.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the 3d circle coefficients using the given inlier set and return them to the user...
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all distances from the cloud data to a given 3D circle model.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
SampleConsensusModel represents the base model class.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const
Create a new point cloud with inliers projected onto the 3d circle model.
uint32_t height
The point cloud height (if organized as an image-structure).
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
Verify whether a subset of indices verifies the given 3d circle model coefficients.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
Helper functor structure for concatenate.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
pcl::PCLHeader header
The point cloud header.