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!\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);
91 float d1 = p1.dot (n1);
92 float d2 = p2.dot (n2);
93 float d3 = p3.dot (n3);
95 Eigen::Vector4f apex = (d1 * ortho23 + d2 * ortho31 + d3 * ortho12) / denominator;
98 Eigen::Vector4f ap1 = p1 - apex;
99 Eigen::Vector4f ap2 = p2 - apex;
100 Eigen::Vector4f ap3 = p3 - apex;
102 Eigen::Vector4f np1 = apex + (ap1/ap1.norm ());
103 Eigen::Vector4f np2 = apex + (ap2/ap2.norm ());
104 Eigen::Vector4f np3 = apex + (ap3/ap3.norm ());
106 Eigen::Vector4f np1np2 = np2 - np1;
107 Eigen::Vector4f np1np3 = np3 - np1;
109 Eigen::Vector4f axis_dir = np1np2.cross3 (np1np3);
110 axis_dir.normalize ();
118 float opening_angle = ( std::acos (ap1.dot (axis_dir)) + std::acos (ap2.dot (axis_dir)) + std::acos (ap3.dot (axis_dir)) ) / 3.0f;
120 model_coefficients.resize (model_size_);
122 model_coefficients[0] = apex[0];
123 model_coefficients[1] = apex[1];
124 model_coefficients[2] = apex[2];
126 model_coefficients[3] = axis_dir[0];
127 model_coefficients[4] = axis_dir[1];
128 model_coefficients[5] = axis_dir[2];
130 model_coefficients[6] = opening_angle;
132 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
134 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
137 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g,%g).\n",
138 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
139 model_coefficients[4], model_coefficients[5], model_coefficients[6]);
144 template <
typename Po
intT,
typename Po
intNT>
void
146 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
149 if (!isModelValid (model_coefficients))
155 distances.resize (indices_->size ());
157 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
158 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
159 const float sin_opening_angle = std::sin (model_coefficients[6]),
160 cos_opening_angle = std::cos (model_coefficients[6]),
161 tan_opening_angle = std::tan (model_coefficients[6]);
163 float apexdotdir = apex.dot (axis_dir);
164 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
166 for (std::size_t i = 0; i < indices_->size (); ++i)
168 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
171 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
172 Eigen::Vector4f pt_proj = apex + k * axis_dir;
175 Eigen::Vector4f height = apex - pt_proj;
176 float actual_cone_radius = tan_opening_angle * height.norm ();
180 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
183 Eigen::Vector4f dir = pt - pt_proj;
188 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * dir;
191 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
192 double d_normal = std::abs (
getAngle3D (n, cone_normal));
193 d_normal = (std::min) (d_normal,
M_PI - d_normal);
195 distances[i] = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
200 template <
typename Po
intT,
typename Po
intNT>
void
202 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
205 if (!isModelValid (model_coefficients))
212 error_sqr_dists_.clear ();
213 inliers.reserve (indices_->size ());
214 error_sqr_dists_.reserve (indices_->size ());
216 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
217 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
218 const float sin_opening_angle = std::sin (model_coefficients[6]),
219 cos_opening_angle = std::cos (model_coefficients[6]),
220 tan_opening_angle = std::tan (model_coefficients[6]);
222 float apexdotdir = apex.dot (axis_dir);
223 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
225 for (std::size_t i = 0; i < indices_->size (); ++i)
227 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
230 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
231 Eigen::Vector4f pt_proj = apex + k * axis_dir;
234 Eigen::Vector4f height = apex - pt_proj;
235 double actual_cone_radius = tan_opening_angle * height.norm ();
239 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
240 if (weighted_euclid_dist > threshold)
244 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
245 pp_pt_dir.normalize ();
249 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
252 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
253 double d_normal = std::abs (
getAngle3D (n, cone_normal));
254 d_normal = (std::min) (d_normal,
M_PI - d_normal);
256 double distance = std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist);
261 inliers.push_back ((*indices_)[i]);
262 error_sqr_dists_.push_back (
distance);
268 template <
typename Po
intT,
typename Po
intNT> std::size_t
270 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
274 if (!isModelValid (model_coefficients))
277 std::size_t nr_p = 0;
279 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
280 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
281 const float sin_opening_angle = std::sin (model_coefficients[6]),
282 cos_opening_angle = std::cos (model_coefficients[6]),
283 tan_opening_angle = std::tan (model_coefficients[6]);
285 float apexdotdir = apex.dot (axis_dir);
286 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
288 for (std::size_t i = 0; i < indices_->size (); ++i)
290 Eigen::Vector4f pt ((*input_)[(*indices_)[i]].x, (*input_)[(*indices_)[i]].y, (*input_)[(*indices_)[i]].z, 0.0f);
293 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
294 Eigen::Vector4f pt_proj = apex + k * axis_dir;
297 Eigen::Vector4f height = apex - pt_proj;
298 double actual_cone_radius = tan_opening_angle * height.norm ();
302 const double weighted_euclid_dist = (1.0 - normal_distance_weight_) * std::abs (pointToAxisDistance (pt, model_coefficients) - actual_cone_radius);
303 if (weighted_euclid_dist > threshold)
307 Eigen::Vector4f pp_pt_dir = pt - pt_proj;
308 pp_pt_dir.normalize ();
312 Eigen::Vector4f cone_normal = sin_opening_angle * height + cos_opening_angle * pp_pt_dir;
315 Eigen::Vector4f n ((*normals_)[(*indices_)[i]].normal[0], (*normals_)[(*indices_)[i]].normal[1], (*normals_)[(*indices_)[i]].normal[2], 0.0f);
316 double d_normal = std::abs (
getAngle3D (n, cone_normal));
317 d_normal = (std::min) (d_normal,
M_PI - d_normal);
319 if (std::abs (normal_distance_weight_ * d_normal + weighted_euclid_dist) < threshold)
326 template <
typename Po
intT,
typename Po
intNT>
void
328 const Indices &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const
330 optimized_coefficients = model_coefficients;
333 if (!isModelValid (model_coefficients))
335 PCL_ERROR (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Given model is invalid!\n");
340 if (inliers.size () <= sample_size_)
342 PCL_ERROR (
"[pcl::SampleConsensusModelCone:optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
346 Eigen::ArrayXf pts_x(inliers.size());
347 Eigen::ArrayXf pts_y(inliers.size());
348 Eigen::ArrayXf pts_z(inliers.size());
350 for(
const auto& index : inliers) {
351 pts_x[pos] = (*input_)[index].x;
352 pts_y[pos] = (*input_)[index].y;
353 pts_z[pos] = (*input_)[index].z;
358 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::optimizeModelCoefficients] Initial solution: %g %g %g %g %g %g %g \nFinal solution: %g %g %g %g %g %g %g\n",
359 model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3],
360 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]);
362 Eigen::Vector3f line_dir (optimized_coefficients[3], optimized_coefficients[4], optimized_coefficients[5]);
363 line_dir.normalize ();
364 optimized_coefficients[3] = line_dir[0];
365 optimized_coefficients[4] = line_dir[1];
366 optimized_coefficients[5] = line_dir[2];
370 template <
typename Po
intT,
typename Po
intNT>
void
372 const Indices &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const
375 if (!isModelValid (model_coefficients))
377 PCL_ERROR (
"[pcl::SampleConsensusModelCone::projectPoints] Given model is invalid!\n");
381 projected_points.
header = input_->header;
382 projected_points.
is_dense = input_->is_dense;
384 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
385 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
386 const float tan_opening_angle = std::tan (model_coefficients[6]);
388 float apexdotdir = apex.dot (axis_dir);
389 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
392 if (copy_data_fields)
395 projected_points.
resize (input_->size ());
396 projected_points.
width = input_->width;
397 projected_points.
height = input_->height;
399 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
401 for (std::size_t i = 0; i < projected_points.
size (); ++i)
406 for (
const auto &inlier : inliers)
408 Eigen::Vector4f pt ((*input_)[inlier].x,
413 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
416 pp.matrix () = apex + k * axis_dir;
418 Eigen::Vector4f dir = pt - pp;
422 Eigen::Vector4f height = apex - pp;
423 float actual_cone_radius = tan_opening_angle * height.norm ();
426 pp += dir * actual_cone_radius;
432 projected_points.
resize (inliers.size ());
433 projected_points.
width = inliers.size ();
434 projected_points.
height = 1;
436 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
438 for (std::size_t i = 0; i < inliers.size (); ++i)
443 for (std::size_t i = 0; i < inliers.size (); ++i)
448 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
450 pp.matrix () = apex + k * axis_dir;
452 Eigen::Vector4f dir = pt - pp;
456 Eigen::Vector4f height = apex - pp;
457 float actual_cone_radius = tan_opening_angle * height.norm ();
460 pp += dir * actual_cone_radius;
466 template <
typename Po
intT,
typename Po
intNT>
bool
468 const std::set<index_t> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const
471 if (!isModelValid (model_coefficients))
473 PCL_ERROR (
"[pcl::SampleConsensusModelCone::doSamplesVerifyModel] Given model is invalid!\n");
477 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
478 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
479 const float tan_opening_angle = std::tan (model_coefficients[6]);
481 float apexdotdir = apex.dot (axis_dir);
482 float dirdotdir = 1.0f / axis_dir.dot (axis_dir);
485 for (
const auto &index : indices)
487 Eigen::Vector4f pt ((*input_)[index].x, (*input_)[index].y, (*input_)[index].z, 0.0f);
490 float k = (pt.dot (axis_dir) - apexdotdir) * dirdotdir;
491 Eigen::Vector4f pt_proj = apex + k * axis_dir;
492 Eigen::Vector4f dir = pt - pt_proj;
496 Eigen::Vector4f height = apex - pt_proj;
497 double actual_cone_radius = tan_opening_angle * height.norm ();
501 if (std::abs (
static_cast<double>(pointToAxisDistance (pt, model_coefficients) - actual_cone_radius)) > threshold)
509 template <
typename Po
intT,
typename Po
intNT>
double
511 const Eigen::Vector4f &pt,
const Eigen::VectorXf &model_coefficients)
const
513 Eigen::Vector4f apex (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
514 Eigen::Vector4f axis_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
519 template <
typename Po
intT,
typename Po
intNT>
bool
526 if (eps_angle_ > 0.0)
529 const Eigen::Vector3f coeff(model_coefficients[3], model_coefficients[4], model_coefficients[5]);
531 double angle_diff = std::abs (
getAngle3D (axis_, coeff));
532 angle_diff = (std::min) (angle_diff,
M_PI - angle_diff);
534 if (angle_diff > eps_angle_)
536 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] Angle between cone direction and given axis is too large.\n");
541 if (model_coefficients[6] != -std::numeric_limits<double>::max() && model_coefficients[6] < min_angle_)
543 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too small: should be larger than %g, but is %g.\n",
544 min_angle_, model_coefficients[6]);
547 if (model_coefficients[6] != std::numeric_limits<double>::max() && model_coefficients[6] > max_angle_)
549 PCL_DEBUG (
"[pcl::SampleConsensusModelCone::isModelValid] The opening angle is too big: should be smaller than %g, but is %g.\n",
550 max_angle_, model_coefficients[6]);
557 #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)
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.