41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_ 42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_ 44 #include <pcl/sample_consensus/sac_model_line.h> 46 #include <pcl/common/concatenate.h> 49 template <
typename Po
intT>
bool 54 (input_->points[samples[0]].x != input_->points[samples[1]].x)
56 (input_->points[samples[0]].y != input_->points[samples[1]].y)
58 (input_->points[samples[0]].z != input_->points[samples[1]].z))
67 template <
typename Po
intT>
bool 69 const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
const 72 if (samples.size () != 2)
74 PCL_ERROR (
"[pcl::SampleConsensusModelLine::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
78 if (fabs (input_->points[samples[0]].x - input_->points[samples[1]].x) <= std::numeric_limits<float>::epsilon () &&
79 fabs (input_->points[samples[0]].y - input_->points[samples[1]].y) <= std::numeric_limits<float>::epsilon () &&
80 fabs (input_->points[samples[0]].z - input_->points[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
85 model_coefficients.resize (6);
86 model_coefficients[0] = input_->points[samples[0]].x;
87 model_coefficients[1] = input_->points[samples[0]].y;
88 model_coefficients[2] = input_->points[samples[0]].z;
90 model_coefficients[3] = input_->points[samples[1]].x - model_coefficients[0];
91 model_coefficients[4] = input_->points[samples[1]].y - model_coefficients[1];
92 model_coefficients[5] = input_->points[samples[1]].z - model_coefficients[2];
94 model_coefficients.template tail<3> ().normalize ();
99 template <
typename Po
intT>
void 101 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const 104 if (!isModelValid (model_coefficients))
107 distances.resize (indices_->size ());
110 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
111 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
112 line_dir.normalize ();
115 for (
size_t i = 0; i < indices_->size (); ++i)
120 distances[i] = sqrt ((line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ());
125 template <
typename Po
intT>
void 127 const Eigen::VectorXf &model_coefficients,
const double threshold, std::vector<int> &inliers)
130 if (!isModelValid (model_coefficients))
133 double sqr_threshold = threshold * threshold;
136 inliers.resize (indices_->size ());
137 error_sqr_dists_.resize (indices_->size ());
140 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
141 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
142 line_dir.normalize ();
145 for (
size_t i = 0; i < indices_->size (); ++i)
149 double sqr_distance = (line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
151 if (sqr_distance < sqr_threshold)
154 inliers[nr_p] = (*indices_)[i];
155 error_sqr_dists_[nr_p] = sqr_distance;
159 inliers.resize (nr_p);
160 error_sqr_dists_.resize (nr_p);
164 template <
typename Po
intT>
int 166 const Eigen::VectorXf &model_coefficients,
const double threshold)
const 169 if (!isModelValid (model_coefficients))
172 double sqr_threshold = threshold * threshold;
177 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
178 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
179 line_dir.normalize ();
182 for (
size_t i = 0; i < indices_->size (); ++i)
186 double sqr_distance = (line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
188 if (sqr_distance < sqr_threshold)
195 template <
typename Po
intT>
void 197 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
const 200 if (!isModelValid (model_coefficients))
202 optimized_coefficients = model_coefficients;
207 if (inliers.size () <= 2)
209 PCL_ERROR (
"[pcl::SampleConsensusModelLine::optimizeModelCoefficients] Not enough inliers found to support a model (%lu)! Returning the same coefficients.\n", inliers.size ());
210 optimized_coefficients = model_coefficients;
214 optimized_coefficients.resize (6);
217 Eigen::Vector4f centroid;
219 Eigen::Matrix3f covariance_matrix;
221 optimized_coefficients[0] = centroid[0];
222 optimized_coefficients[1] = centroid[1];
223 optimized_coefficients[2] = centroid[2];
232 optimized_coefficients.template tail<3> ().matrix () = eigen_vector;
236 template <
typename Po
intT>
void 238 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
const 241 if (!isModelValid (model_coefficients))
245 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
246 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
248 projected_points.
header = input_->header;
249 projected_points.
is_dense = input_->is_dense;
252 if (copy_data_fields)
255 projected_points.
points.resize (input_->points.size ());
256 projected_points.
width = input_->width;
257 projected_points.
height = input_->height;
261 for (
size_t i = 0; i < projected_points.
points.size (); ++i)
266 for (
size_t i = 0; i < inliers.size (); ++i)
268 Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
270 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
272 Eigen::Vector4f pp = line_pt + k * line_dir;
274 projected_points.
points[inliers[i]].x = pp[0];
275 projected_points.
points[inliers[i]].y = pp[1];
276 projected_points.
points[inliers[i]].z = pp[2];
282 projected_points.
points.resize (inliers.size ());
283 projected_points.
width = static_cast<uint32_t> (inliers.size ());
284 projected_points.
height = 1;
288 for (
size_t i = 0; i < inliers.size (); ++i)
293 for (
size_t i = 0; i < inliers.size (); ++i)
295 Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
297 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
299 Eigen::Vector4f pp = line_pt + k * line_dir;
301 projected_points.
points[i].x = pp[0];
302 projected_points.
points[i].y = pp[1];
303 projected_points.
points[i].z = pp[2];
309 template <
typename Po
intT>
bool 311 const std::set<int> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
const 314 if (!isModelValid (model_coefficients))
318 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
319 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
320 line_dir.normalize ();
322 double sqr_threshold = threshold * threshold;
324 for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
328 if ((line_pt - input_->points[*it].getVector4fMap ()).cross3 (line_dir).squaredNorm () > sqr_threshold)
335 #define PCL_INSTANTIATE_SampleConsensusModelLine(T) template class PCL_EXPORTS pcl::SampleConsensusModelLine<T>; 337 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_ void computeCorrespondingEigenVector(const Matrix &mat, const typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the corresponding eigenvector to the given eigenvalue of the symmetric positive semi defin...
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
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 line model.
struct pcl::PointXYZIEdge EIGEN_ALIGN16
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the line coefficients using the given inlier set and return them to the user.
uint32_t height
The point cloud height (if organized as an image-structure).
uint32_t width
The point cloud width (if organized as an image-structure).
unsigned int computeCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix)
Compute the 3x3 covariance matrix of a given set of points.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid line model, compute the model coefficients fro...
PointCloud represents the base class in PCL for storing collections of 3D points.
pcl::PCLHeader header
The point cloud header.
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Select all the points which respect the given model coefficients as inliers.
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > ¢roid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
Helper functor structure for concatenate.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
Define methods for centroid estimation and covariance matrix calculus.
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 line model coefficients.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all squared distances from the cloud data to a given line model.