38 #ifndef PCL_FILTERS_IMPL_SAMPLING_SURFACE_NORMAL_H_
39 #define PCL_FILTERS_IMPL_SAMPLING_SURFACE_NORMAL_H_
43 #include <pcl/common/eigen.h>
44 #include <pcl/filters/sampling_surface_normal.h>
47 template<
typename Po
intT>
void
50 std::vector <int> indices;
51 int npts = int (input_->points.size ());
52 for (
unsigned int i = 0; i < npts; i++)
53 indices.push_back (i);
55 Vector max_vec (3, 1);
56 Vector min_vec (3, 1);
57 findXYZMaxMin (*input_, max_vec, min_vec);
59 partition (data, 0, npts, min_vec, max_vec, indices, output);
65 template<
typename Po
intT>
void
68 float maxval = cloud.
points[0].x;
69 float minval = cloud.
points[0].x;
71 for (
unsigned int i = 0; i < cloud.
points.size (); i++)
73 if (cloud.
points[i].x > maxval)
75 maxval = cloud.
points[i].x;
77 if (cloud.
points[i].x < minval)
79 minval = cloud.
points[i].x;
85 maxval = cloud.
points[0].y;
86 minval = cloud.
points[0].y;
88 for (
unsigned int i = 0; i < cloud.
points.size (); i++)
90 if (cloud.
points[i].y > maxval)
92 maxval = cloud.
points[i].y;
94 if (cloud.
points[i].y < minval)
96 minval = cloud.
points[i].y;
100 min_vec (1) = minval;
102 maxval = cloud.
points[0].z;
103 minval = cloud.
points[0].z;
105 for (
unsigned int i = 0; i < cloud.
points.size (); i++)
107 if (cloud.
points[i].z > maxval)
109 maxval = cloud.
points[i].z;
111 if (cloud.
points[i].z < minval)
113 minval = cloud.
points[i].z;
116 max_vec (2) = maxval;
117 min_vec (2) = minval;
121 template<
typename Po
intT>
void
123 const PointCloud& cloud,
const int first,
const int last,
124 const Vector min_values,
const Vector max_values,
125 std::vector<int>& indices,
PointCloud& output)
127 const int count (last - first);
128 if (count <= sample_)
130 samplePartition (cloud, first, last, indices, output);
134 (max_values - min_values).maxCoeff (&cutDim);
136 const int rightCount (count / 2);
137 const int leftCount (count - rightCount);
138 assert (last - rightCount == first + leftCount);
141 std::nth_element (indices.begin () + first, indices.begin () + first + leftCount,
142 indices.begin () + last, CompareDim (cutDim, cloud));
144 const int cutIndex (indices[first+leftCount]);
145 const float cutVal = findCutVal (cloud, cutDim, cutIndex);
148 Vector leftMaxValues (max_values);
149 leftMaxValues[cutDim] = cutVal;
151 Vector rightMinValues (min_values);
152 rightMinValues[cutDim] = cutVal;
155 partition (cloud, first, first + leftCount, min_values, leftMaxValues, indices, output);
156 partition (cloud, first + leftCount, last, rightMinValues, max_values, indices, output);
160 template<
typename Po
intT>
void
162 const PointCloud& data,
const int first,
const int last,
163 std::vector <int>& indices,
PointCloud& output)
167 for (
unsigned int i = first; i < last; i++)
170 pt.x = data.
points[indices[i]].x;
171 pt.y = data.
points[indices[i]].y;
172 pt.z = data.
points[indices[i]].z;
173 cloud.
points.push_back (pt);
178 Eigen::Vector4f normal;
182 computeNormal (cloud, normal, curvature);
184 for (
unsigned int i = 0; i < cloud.
points.size (); i++)
187 const float r = float (std::rand ()) / float (RAND_MAX);
192 pt.normal[0] = normal (0);
193 pt.normal[1] = normal (1);
194 pt.normal[2] = normal (2);
195 pt.curvature = curvature;
197 output.
points.push_back (pt);
203 template<
typename Po
intT>
void
207 Eigen::Vector4f xyz_centroid;
214 nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
226 normal (3) = -1 * normal.dot (xyz_centroid);
230 template <
typename Po
intT>
inline unsigned int
232 Eigen::Matrix3f &covariance_matrix,
233 Eigen::Vector4f ¢roid)
236 Eigen::Matrix<float, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<float, 1, 9, Eigen::RowMajor>::Zero ();
237 unsigned int point_count = 0;
238 for (
unsigned int i = 0; i < cloud.
points.size (); i++)
240 if (!isFinite (cloud[i]))
246 accu [0] += cloud[i].x * cloud[i].x;
247 accu [1] += cloud[i].x * cloud[i].y;
248 accu [2] += cloud[i].x * cloud[i].z;
249 accu [3] += cloud[i].y * cloud[i].y;
250 accu [4] += cloud[i].y * cloud[i].z;
251 accu [5] += cloud[i].z * cloud[i].z;
252 accu [6] += cloud[i].x;
253 accu [7] += cloud[i].y;
254 accu [8] += cloud[i].z;
257 accu /=
static_cast<float> (point_count);
258 centroid[0] = accu[6]; centroid[1] = accu[7]; centroid[2] = accu[8];
260 covariance_matrix.coeffRef (0) = accu [0] - accu [6] * accu [6];
261 covariance_matrix.coeffRef (1) = accu [1] - accu [6] * accu [7];
262 covariance_matrix.coeffRef (2) = accu [2] - accu [6] * accu [8];
263 covariance_matrix.coeffRef (4) = accu [3] - accu [7] * accu [7];
264 covariance_matrix.coeffRef (5) = accu [4] - accu [7] * accu [8];
265 covariance_matrix.coeffRef (8) = accu [5] - accu [8] * accu [8];
266 covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
267 covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
268 covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
270 return (static_cast<unsigned int> (point_count));
274 template <
typename Po
intT>
void
276 float &nx,
float &ny,
float &nz,
float &curvature)
281 pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
283 nx = eigen_vector [0];
284 ny = eigen_vector [1];
285 nz = eigen_vector [2];
288 float eig_sum = covariance_matrix.coeff (0) + covariance_matrix.coeff (4) + covariance_matrix.coeff (8);
290 curvature = fabsf (eigen_value / eig_sum);
296 template <
typename Po
intT>
float
298 const PointCloud& cloud,
const int cut_dim,
const int cut_index)
301 return (cloud.
points[cut_index].x);
302 else if (cut_dim == 1)
303 return (cloud.
points[cut_index].y);
304 else if (cut_dim == 2)
305 return (cloud.
points[cut_index].z);
311 #define PCL_INSTANTIATE_SamplingSurfaceNormal(T) template class PCL_EXPORTS pcl::SamplingSurfaceNormal<T>;
313 #endif // PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
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...
uint32_t width
The point cloud width (if organized as an image-structure).
void applyFilter(PointCloud &output)
Sample of point indices into a separate PointCloud.
SamplingSurfaceNormal divides the input space into grids until each grid contains a maximum of N poin...
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > ¢roid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
uint32_t height
The point cloud height (if organized as an image-structure).
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
struct pcl::_PointXYZHSV EIGEN_ALIGN16
void solvePlaneParameters(const Eigen::Matrix3f &covariance_matrix, const Eigen::Vector4f &point, Eigen::Vector4f &plane_parameters, float &curvature)
Solve the eigenvalues and eigenvectors of a given 3x3 covariance matrix, and estimate the least-squar...
A point structure representing Euclidean xyz coordinates, and the RGB color.