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 (std::abs (input_->points[samples[0]].x - input_->points[samples[1]].x) <= std::numeric_limits<float>::epsilon () &&
79 std::abs (input_->points[samples[0]].y - input_->points[samples[1]].y) <= std::numeric_limits<float>::epsilon () &&
80 std::abs (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 (std::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 (std::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> std::size_t
166 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
169 if (!isModelValid (model_coefficients))
172 double sqr_threshold = threshold * threshold;
174 std::size_t nr_p = 0;
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 (std::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];
226 EIGEN_ALIGN16 Eigen::Vector3f eigen_values;
227 EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
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 (std::size_t i = 0; i < projected_points.
points.size (); ++i)
266 for (
const int &inlier : inliers)
268 Eigen::Vector4f pt (input_->points[inlier].x, input_->points[inlier].y, input_->points[inlier].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[inlier].x = pp[0];
275 projected_points.
points[inlier].y = pp[1];
276 projected_points.
points[inlier].z = pp[2];
282 projected_points.
points.resize (inliers.size ());
283 projected_points.
width =
static_cast<std::uint32_t
> (inliers.size ());
284 projected_points.
height = 1;
288 for (std::size_t i = 0; i < inliers.size (); ++i)
293 for (std::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 (
const int &index : indices)
328 if ((line_pt - input_->points[index].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_