39 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
40 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_CONE_H_
42 #include <pcl/sample_consensus/sac_model_cone.h>
44 #include <pcl/common/concatenate.h>
47 template <
typename Po
intT,
typename Po
intNT>
bool
50 if (samples.size () != sample_size_)
52 PCL_ERROR (
"[pcl::SampleConsensusModelCone::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
59 template <
typename Po
intT,
typename Po
intNT>
bool
61 const Indices &samples, Eigen::VectorXf &model_coefficients)
const
64 if (!isSampleGood (samples))
66 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Invalid set of samples given\n");
72 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] No input dataset containing normals was given! Use setInputNormals\n");
76 Eigen::Vector4f p1 ((*input_)[samples[0]].x, (*input_)[samples[0]].y, (*input_)[samples[0]].z, 0.0f);
77 Eigen::Vector4f p2 ((*input_)[samples[1]].x, (*input_)[samples[1]].y, (*input_)[samples[1]].z, 0.0f);
78 Eigen::Vector4f p3 ((*input_)[samples[2]].x, (*input_)[samples[2]].y, (*input_)[samples[2]].z, 0.0f);
80 Eigen::Vector4f n1 ((*normals_)[samples[0]].normal[0], (*normals_)[samples[0]].normal[1], (*normals_)[samples[0]].normal[2], 0.0f);
81 Eigen::Vector4f n2 ((*normals_)[samples[1]].normal[0], (*normals_)[samples[1]].normal[1], (*normals_)[samples[1]].normal[2], 0.0f);
82 Eigen::Vector4f n3 ((*normals_)[samples[2]].normal[0], (*normals_)[samples[2]].normal[1], (*normals_)[samples[2]].normal[2], 0.0f);
85 Eigen::Vector4f ortho12 = n1.cross3(n2);
86 Eigen::Vector4f ortho23 = n2.cross3(n3);
87 Eigen::Vector4f ortho31 = n3.cross3(n1);
89 float denominator = n1.dot(ortho23);
90 if (std::abs(denominator) < Eigen::NumTraits<float>::dummy_precision ())
92 PCL_ERROR (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Impossible to compute stable model with these points.\n");
96 float d1 = p1.dot (n1);
97 float d2 = p2.dot (n2);
98 float d3 = p3.dot (n3);
100 Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
103 Eigen::Vector4f ap1 = p1 - apex;
104 Eigen::Vector4f ap2 = p2 - apex;
105 Eigen::Vector4f ap3 = p3 - apex;
107 Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
108 Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
109 Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
111 Eigen::Vector4f np1np2 = np2 - np1;
112 Eigen::Vector4f np1np3 = np3 - np1;
114 Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
115 axis_dir.normalize ();
123 float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
125 model_coefficients.resize (model_size_);
127 model_coefficients[0] = apex[0];
128 model_coefficients[1] = apex[1];
129 model_coefficients[2] = apex[2];
131 model_coefficients[3] = axis_dir[0];
132 model_coefficients[4] = axis_dir[1];
133 model_coefficients[5] = axis_dir[2];
135 model_coefficients[6] = opening_angle;
137 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
139 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
142 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
143 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
144 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
149 template <
typename Po
intT,
typename Po
intNT>
void
151 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
154 if (!isModelValid (model_coefficients))
160 distances.resize (indices_->size ());
162 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
163 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
164 const float sin_opening_angle = std::sin (model_coefficients[6]),
165 cos_opening_angle = std::cos (model_coefficients[6]),
166 tan_opening_angle = std::tan (model_coefficients[6]);
168 float apexdotdir = apex.dot (axis_dir);
169 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
171 for (std::size_t i = 0; i < indices_->size (); ++i)
173 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
176 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
177 Eigen::Vector4f pt_proj = apex + k * axis_dir;
180 Eigen::Vector4f height = apex - pt_proj;
181 float actual_cone_radius = tan_opening_angle * height.norm ();
185 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
188 Eigen::Vector4f dir = pt - pt_proj;
193 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * dir;
196 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
197 double d_normal = std::abs (
getAngle3D (n, cone_normal));
198 d_normal = (std::min) (d_normal,
M_PI - d_normal);
200 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
205 template <
typename Po
intT,
typename Po
intNT>
void
207 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
210 if (!isModelValid (model_coefficients))
217 error_sqr_dists_.clear ();
218 inliers.reserve (indices_->size ());
219 error_sqr_dists_.reserve (indices_->size ());
221 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
222 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
223 const float sin_opening_angle = std::sin (model_coefficients[6]),
224 cos_opening_angle = std::cos (model_coefficients[6]),
225 tan_opening_angle = std::tan (model_coefficients[6]);
227 float apexdotdir = apex.dot (axis_dir);
228 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
230 for (std::size_t i = 0; i < indices_->size (); ++i)
232 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
235 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
236 Eigen::Vector4f pt_proj = apex + k * axis_dir;
239 Eigen::Vector4f height = apex - pt_proj;
240 double actual_cone_radius = tan_opening_angle * height.norm ();
244 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
245 if (weighted_euclid_dist > threshold)
249 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
250 pp_pt_dir.normalize ();
254 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
257 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
258 double d_normal = std::abs (
getAngle3D (n, cone_normal));
259 d_normal = (std::min) (d_normal,
M_PI - d_normal);
261 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
266 inliers.push_back ((*indices_)[i]);
267 error_sqr_dists_.push_back (
distance);
273 template <
typename Po
intT,
typename Po
intNT> std::size_t
275 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
279 if (!isModelValid (model_coefficients))
282 std::size_t nr_p = 0;
284 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
285 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
286 const float sin_opening_angle = std::sin (model_coefficients[6]),
287 cos_opening_angle = std::cos (model_coefficients[6]),
288 tan_opening_angle = std::tan (model_coefficients[6]);
290 float apexdotdir = apex.dot (axis_dir);
291 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
293 for (std::size_t i = 0; i < indices_->size (); ++i)
295 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
298 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
299 Eigen::Vector4f pt_proj = apex + k * axis_dir;
302 Eigen::Vector4f height = apex - pt_proj;
303 double actual_cone_radius = tan_opening_angle * height.norm ();
307 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
308 if (weighted_euclid_dist > threshold)
312 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
313 pp_pt_dir.normalize ();
317 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
320 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
321 double d_normal = std::abs (
getAngle3D (n, cone_normal));
322 d_normal = (std::min) (d_normal,
M_PI - d_normal);
324 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
331 template <
typename Po
intT,
typename Po
intNT>
void
333 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
335 optimized_coefficients = model_coefficients;
338 if (!isModelValid (model_coefficients))
340 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
345 if (inliers.size () <= sample_size_)
347 PCL_ERROR (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
351 Eigen::ArrayXf pts_x(inliers.size());
352 Eigen::ArrayXf pts_y(inliers.size());
353 Eigen::ArrayXf pts_z(inliers.size());
355 for(
const auto& index : inliers) {
356 pts_x[pos] = (*input_)[index].x;
357 pts_y[pos] = (*input_)[index].y;
358 pts_z[pos] = (*input_)[index].z;
363 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Initial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
364 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
365 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]);
367 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
368 line_dir.normalize ();
369 optimized_coefficients[3] = line_dir[0];
370 optimized_coefficients[4] = line_dir[1];
371 optimized_coefficients[5] = line_dir[2];
375 template <
typename Po
intT,
typename Po
intNT>
void
377 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
380 if (!isModelValid (model_coefficients))
382 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
386 projected_points.
header = input_->header;
387 projected_points.
is_dense = input_->is_dense;
389 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
390 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
391 const float tan_opening_angle = std::tan (model_coefficients[6]);
393 float apexdotdir = apex.dot (axis_dir);
394 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
397 if (copy_data_fields)
400 projected_points.
resize (input_->size ());
401 projected_points.
width = input_->width;
402 projected_points.
height = input_->height;
404 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
406 for (std::size_t i = 0; i < projected_points.
size (); ++i)
411 for (
const auto &inlier : inliers)
413 Eigen::Vector4f pt ((*input_)[inlier].x,
418 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
421 pp.matrix () = apex + k * axis_dir;
423 Eigen::Vector4f dir = pt - pp;
427 Eigen::Vector4f height = apex - pp;
428 float actual_cone_radius = tan_opening_angle * height.norm ();
431 pp += dir * actual_cone_radius;
437 projected_points.
resize (inliers.size ());
438 projected_points.
width = inliers.size ();
439 projected_points.
height = 1;
441 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
443 for (std::size_t i = 0; i < inliers.size (); ++i)
448 for (std::size_t i = 0; i < inliers.size (); ++i)
453 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
455 pp.matrix () = apex + k * axis_dir;
457 Eigen::Vector4f dir = pt - pp;
461 Eigen::Vector4f height = apex - pp;
462 float actual_cone_radius = tan_opening_angle * height.norm ();
465 pp += dir * actual_cone_radius;
471 template <
typename Po
intT,
typename Po
intNT>
bool
473 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
476 if (!isModelValid (model_coefficients))
478 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
482 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
483 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
484 const float tan_opening_angle = std::tan (model_coefficients[6]);
486 float apexdotdir = apex.dot (axis_dir);
487 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
490 for (
const auto &index : indices)
492 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
495 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
496 Eigen::Vector4f pt_proj = apex + k * axis_dir;
497 Eigen::Vector4f dir = pt - pt_proj;
501 Eigen::Vector4f height = apex - pt_proj;
502 double actual_cone_radius = tan_opening_angle * height.norm ();
506 if (std::abs (
static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
514 template <
typename Po
intT,
typename Po
intNT>
double
516 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
518 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
519 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
524 template <
typename Po
intT,
typename Po
intNT>
bool
531 if (eps_angle_ > 0.0)
534 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
536 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
537 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
539 if (angle_diff > eps_angle_)
541 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
546 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
548 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too small: should be larger than %g, but is %g.\n",
549 min_angle_, model_coefficients[6]);
552 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
554 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too big: should be smaller than %g, but is %g.\n",
555 max_angle_, model_coefficients[6]);
562 #define PCL_INSTANTIATE_SampleConsensusModelCone(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelCone<PointT, PointNT>;
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).
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the cone coefficients using the given inlier set and return them to the user.
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 cone model.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given cone model.
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 isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid cone model, compute the model coefficients fro...
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
double pointToAxisDistance(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
Get the distance from a point to a line (represented by a point and a direction)
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 cone model coefficients.
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.
SampleConsensusModel represents the base model class.
Define standard C methods and C++ classes that are common to all methods.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2, const bool in_degree=false)
Compute the smallest angle between two 3D vectors in radians (default) or degree.
double sqrPointToLineDistance(const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir)
Get the square distance from a point to a line (represented by a point and a direction)
float distance(const PointT &p1, const PointT &p2)
PCL_EXPORTS int optimizeModelCoefficientsCone(Eigen::VectorXf &coeff, const Eigen::ArrayXf &pts_x, const Eigen::ArrayXf &pts_y, const Eigen::ArrayXf &pts_z)
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
const Eigen::Map< const Eigen::Vector4f, Eigen::Aligned > Vector4fMapConst
IndicesAllocator<> Indices
Type used for indices in PCL.
Helper functor structure for concatenate.