38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_
46 template <
typename Po
intT>
bool
50 (input_->points[samples[0]].x != input_->points[samples[1]].x)
52 (input_->points[samples[0]].y != input_->points[samples[1]].y)
54 (input_->points[samples[0]].z != input_->points[samples[1]].z))
61 template <
typename Po
intT>
bool
63 const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
66 if (samples.size () != 2)
68 PCL_ERROR (
"[pcl::SampleConsensusModelLine::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
72 model_coefficients.resize (6);
73 model_coefficients[0] = input_->points[samples[0]].x;
74 model_coefficients[1] = input_->points[samples[0]].y;
75 model_coefficients[2] = input_->points[samples[0]].z;
77 model_coefficients[3] = input_->points[samples[1]].x - model_coefficients[0];
78 model_coefficients[4] = input_->points[samples[1]].y - model_coefficients[1];
79 model_coefficients[5] = input_->points[samples[1]].z - model_coefficients[2];
81 model_coefficients.template tail<3> ().normalize ();
86 template <
typename Po
intT>
void
88 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
91 if (!isModelValid (model_coefficients))
94 distances.resize (indices_->size ());
97 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
98 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
99 line_dir.normalize ();
102 for (
size_t i = 0; i < indices_->size (); ++i)
107 distances[i] =
sqrt ((line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ());
112 template <
typename Po
intT>
void
114 const Eigen::VectorXf &model_coefficients,
const double threshold, std::vector<int> &inliers)
117 if (!isModelValid (model_coefficients))
120 double sqr_threshold = threshold * threshold;
123 inliers.resize (indices_->size ());
126 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
127 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
128 line_dir.normalize ();
131 for (
size_t i = 0; i < indices_->size (); ++i)
135 double sqr_distance = (line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
137 if (sqr_distance < sqr_threshold)
140 inliers[nr_p] = (*indices_)[i];
144 inliers.resize (nr_p);
148 template <
typename Po
intT>
int
150 const Eigen::VectorXf &model_coefficients,
const double threshold)
153 if (!isModelValid (model_coefficients))
156 double sqr_threshold = threshold * threshold;
161 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
162 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
163 line_dir.normalize ();
166 for (
size_t i = 0; i < indices_->size (); ++i)
170 double sqr_distance = (line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
172 if (sqr_distance < sqr_threshold)
179 template <
typename Po
intT>
void
181 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
184 if (!isModelValid (model_coefficients))
186 optimized_coefficients = model_coefficients;
191 if (inliers.size () <= 2)
193 PCL_ERROR (
"[pcl::SampleConsensusModelLine::optimizeModelCoefficients] Not enough inliers found to support a model (%zu)! Returning the same coefficients.\n", inliers.size ());
194 optimized_coefficients = model_coefficients;
198 optimized_coefficients.resize (6);
201 Eigen::Vector4f centroid;
203 Eigen::Matrix3f covariance_matrix;
205 optimized_coefficients[0] = centroid[0];
206 optimized_coefficients[1] = centroid[1];
207 optimized_coefficients[2] = centroid[2];
216 optimized_coefficients.template tail<3> () = eigen_vector;
220 template <
typename Po
intT>
void
222 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool copy_data_fields)
225 if (!isModelValid (model_coefficients))
229 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
230 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
232 projected_points.
header = input_->header;
233 projected_points.
is_dense = input_->is_dense;
236 if (copy_data_fields)
239 projected_points.
points.resize (input_->points.size ());
240 projected_points.
width = input_->width;
241 projected_points.
height = input_->height;
245 for (
size_t i = 0; i < projected_points.
points.size (); ++i)
250 for (
size_t i = 0; i < inliers.size (); ++i)
252 Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
254 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
256 Eigen::Vector4f pp = line_pt + k * line_dir;
258 projected_points.
points[inliers[i]].x = pp[0];
259 projected_points.
points[inliers[i]].y = pp[1];
260 projected_points.
points[inliers[i]].z = pp[2];
266 projected_points.
points.resize (inliers.size ());
267 projected_points.
width =
static_cast<uint32_t
> (inliers.size ());
268 projected_points.
height = 1;
272 for (
size_t i = 0; i < inliers.size (); ++i)
277 for (
size_t i = 0; i < inliers.size (); ++i)
279 Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
281 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
283 Eigen::Vector4f pp = line_pt + k * line_dir;
285 projected_points.
points[i].x = pp[0];
286 projected_points.
points[i].y = pp[1];
287 projected_points.
points[i].z = pp[2];
293 template <
typename Po
intT>
bool
295 const std::set<int> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
298 if (!isModelValid (model_coefficients))
302 Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
303 Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
304 line_dir.normalize ();
306 double sqr_threshold = threshold * threshold;
308 for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
312 if ((line_pt - input_->points[*it].getVector4fMap ()).cross3 (line_dir).squaredNorm () > sqr_threshold)
319 #define PCL_INSTANTIATE_SampleConsensusModelLine(T) template class PCL_EXPORTS pcl::SampleConsensusModelLine<T>;
321 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_