41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
44 #include <pcl/sample_consensus/eigen.h>
45 #include <pcl/sample_consensus/sac_model_sphere.h>
48 template <
typename Po
intT>
bool
55 template <
typename Po
intT>
bool
57 const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
60 if (samples.size () != 4)
62 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
67 for (
int i = 0; i < 4; i++)
69 temp (i, 0) = input_->points[samples[i]].x;
70 temp (i, 1) = input_->points[samples[i]].y;
71 temp (i, 2) = input_->points[samples[i]].z;
74 float m11 = temp.determinant ();
78 for (
int i = 0; i < 4; ++i)
79 temp (i, 0) = (input_->points[samples[i]].x) * (input_->points[samples[i]].x) +
80 (input_->points[samples[i]].y) * (input_->points[samples[i]].y) +
81 (input_->points[samples[i]].z) * (input_->points[samples[i]].z);
82 float m12 = temp.determinant ();
84 for (
int i = 0; i < 4; ++i)
86 temp (i, 1) = temp (i, 0);
87 temp (i, 0) = input_->points[samples[i]].x;
89 float m13 = temp.determinant ();
91 for (
int i = 0; i < 4; ++i)
93 temp (i, 2) = temp (i, 1);
94 temp (i, 1) = input_->points[samples[i]].y;
96 float m14 = temp.determinant ();
98 for (
int i = 0; i < 4; ++i)
100 temp (i, 0) = temp (i, 2);
101 temp (i, 1) = input_->points[samples[i]].x;
102 temp (i, 2) = input_->points[samples[i]].y;
103 temp (i, 3) = input_->points[samples[i]].z;
105 float m15 = temp.determinant ();
108 model_coefficients.resize (4);
109 model_coefficients[0] = 0.5f * m12 / m11;
110 model_coefficients[1] = 0.5f * m13 / m11;
111 model_coefficients[2] = 0.5f * m14 / m11;
113 model_coefficients[3] = sqrtf (
114 model_coefficients[0] * model_coefficients[0] +
115 model_coefficients[1] * model_coefficients[1] +
116 model_coefficients[2] * model_coefficients[2] - m15 / m11);
122 template <
typename Po
intT>
void
124 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
127 if (!isModelValid (model_coefficients))
132 distances.resize (indices_->size ());
135 for (
size_t i = 0; i < indices_->size (); ++i)
138 distances[i] = fabs (sqrtf (
139 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
140 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
142 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
143 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
145 ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
146 ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
147 ) - model_coefficients[3]);
151 template <
typename Po
intT>
void
153 const Eigen::VectorXf &model_coefficients,
const double threshold, std::vector<int> &inliers)
156 if (!isModelValid (model_coefficients))
163 inliers.resize (indices_->size ());
164 error_sqr_dists_.resize (indices_->size ());
167 for (
size_t i = 0; i < indices_->size (); ++i)
169 double distance = fabs (sqrtf (
170 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
171 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
173 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
174 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
176 ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
177 ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
178 ) - model_coefficients[3]);
181 if (distance < threshold)
184 inliers[nr_p] = (*indices_)[i];
185 error_sqr_dists_[nr_p] =
static_cast<double> (distance);
189 inliers.resize (nr_p);
190 error_sqr_dists_.resize (nr_p);
194 template <
typename Po
intT>
int
196 const Eigen::VectorXf &model_coefficients,
const double threshold)
199 if (!isModelValid (model_coefficients))
205 for (
size_t i = 0; i < indices_->size (); ++i)
210 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) *
211 ( input_->points[(*indices_)[i]].x - model_coefficients[0] ) +
213 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) *
214 ( input_->points[(*indices_)[i]].y - model_coefficients[1] ) +
216 ( input_->points[(*indices_)[i]].z - model_coefficients[2] ) *
217 ( input_->points[(*indices_)[i]].z - model_coefficients[2] )
218 ) - model_coefficients[3]) < threshold)
225 template <
typename Po
intT>
void
227 const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
229 optimized_coefficients = model_coefficients;
232 if (model_coefficients.size () != 4)
234 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
239 if (inliers.size () <= 4)
241 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] Not enough inliers found to support a model (%lu)! Returning the same coefficients.\n", inliers.size ());
245 tmp_inliers_ = &inliers;
247 OptimizationFunctor functor (static_cast<int> (inliers.size ()),
this);
248 Eigen::NumericalDiff<OptimizationFunctor> num_diff (functor);
249 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>,
float> lm (num_diff);
250 int info = lm.minimize (optimized_coefficients);
253 PCL_DEBUG (
"[pcl::SampleConsensusModelSphere::optimizeModelCoefficients] LM solver finished with exit code %i, having a residual norm of %g. \nInitial solution: %g %g %g %g \nFinal solution: %g %g %g %g\n",
254 info, lm.fvec.norm (), model_coefficients[0], model_coefficients[1], model_coefficients[2], model_coefficients[3], optimized_coefficients[0], optimized_coefficients[1], optimized_coefficients[2], optimized_coefficients[3]);
258 template <
typename Po
intT>
void
260 const std::vector<int> &,
const Eigen::VectorXf &model_coefficients,
PointCloud &projected_points,
bool)
263 if (model_coefficients.size () != 4)
265 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::projectPoints] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
270 projected_points.
points.resize (input_->points.size ());
271 projected_points.
header = input_->header;
272 projected_points.
width = input_->width;
273 projected_points.
height = input_->height;
274 projected_points.
is_dense = input_->is_dense;
276 PCL_WARN (
"[pcl::SampleConsensusModelSphere::projectPoints] Not implemented yet.\n");
277 projected_points.
points = input_->points;
281 template <
typename Po
intT>
bool
283 const std::set<int> &indices,
const Eigen::VectorXf &model_coefficients,
const double threshold)
286 if (model_coefficients.size () != 4)
288 PCL_ERROR (
"[pcl::SampleConsensusModelSphere::doSamplesVerifyModel] Invalid number of model coefficients given (%lu)!\n", model_coefficients.size ());
292 for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
296 ( input_->points[*it].x - model_coefficients[0] ) *
297 ( input_->points[*it].x - model_coefficients[0] ) +
298 ( input_->points[*it].y - model_coefficients[1] ) *
299 ( input_->points[*it].y - model_coefficients[1] ) +
300 ( input_->points[*it].z - model_coefficients[2] ) *
301 ( input_->points[*it].z - model_coefficients[2] )
302 ) - model_coefficients[3]) > threshold)
308 #define PCL_INSTANTIATE_SampleConsensusModelSphere(T) template class PCL_EXPORTS pcl::SampleConsensusModelSphere<T>;
310 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_SPHERE_H_
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the sphere coefficients using the given inlier set and return them to the user...
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)
Create a new point cloud with inliers projected onto the sphere model.
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).
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid sphere model, compute the model coefficients f...
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given sphere model coefficients.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values).
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given sphere model.
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.