Point Cloud Library (PCL)  1.9.1-dev
from_meshes.h
1 #pragma once
2 
3 #include "pcl/features/normal_3d.h"
4 #include "pcl/Vertices.h"
5 
6 namespace pcl
7 {
8  namespace features
9  {
10 
11  /** \brief Compute approximate surface normals on a mesh.
12  * \param[in] cloud Point cloud containing the XYZ coordinates.
13  * \param[in] polygons Polygons from the mesh.
14  * \param[out] normals Point cloud with computed surface normals
15  */
16  template <typename PointT, typename PointNT> inline void
17  computeApproximateNormals(const pcl::PointCloud<PointT>& cloud, const std::vector<pcl::Vertices>& polygons, pcl::PointCloud<PointNT>& normals)
18  {
19  int nr_points = static_cast<int>(cloud.points.size());
20  int nr_polygons = static_cast<int>(polygons.size());
21 
22  normals.header = cloud.header;
23  normals.width = cloud.width;
24  normals.height = cloud.height;
25  normals.points.resize(nr_points);
26 
27  for ( int i = 0; i < nr_points; ++i )
28  normals.points[i].getNormalVector3fMap() = Eigen::Vector3f::Zero();
29 
30  // NOTE: for efficiency the weight is computed implicitly by using the
31  // cross product, this causes inaccurate normals for meshes containing
32  // non-triangle polygons (quads or other types)
33  for ( int i = 0; i < nr_polygons; ++i )
34  {
35  const int nr_points_polygon = (int)polygons[i].vertices.size();
36  if (nr_points_polygon < 3) continue;
37 
38  // compute normal for triangle
39  Eigen::Vector3f vec_a_b = cloud.points[polygons[i].vertices[0]].getVector3fMap() - cloud.points[polygons[i].vertices[1]].getVector3fMap();
40  Eigen::Vector3f vec_a_c = cloud.points[polygons[i].vertices[0]].getVector3fMap() - cloud.points[polygons[i].vertices[2]].getVector3fMap();
41  Eigen::Vector3f normal = vec_a_b.cross(vec_a_c);
42  pcl::flipNormalTowardsViewpoint(cloud.points[polygons[i].vertices[0]], 0.0f, 0.0f, 0.0f, normal(0), normal(1), normal(2));
43 
44  // add normal to all points in polygon
45  for ( int j = 0; j < nr_points_polygon; ++j )
46  normals.points[polygons[i].vertices[j]].getNormalVector3fMap() += normal;
47  }
48 
49  for ( int i = 0; i < nr_points; ++i )
50  {
51  normals.points[i].getNormalVector3fMap().normalize();
52  pcl::flipNormalTowardsViewpoint(cloud.points[i], 0.0f, 0.0f, 0.0f, normals.points[i].normal_x, normals.points[i].normal_y, normals.points[i].normal_z);
53  }
54  }
55 
56 
57  /** \brief Compute GICP-style covariance matrices given a point cloud and
58  * the corresponding surface normals.
59  * \param[in] cloud Point cloud containing the XYZ coordinates,
60  * \param[in] normals Point cloud containing the corresponding surface normals.
61  * \param[out] covariances Vector of computed covariances.
62  * \param[in] Optional: Epsilon for the expected noise along the surface normal (default: 0.001)
63  */
64  template <typename PointT, typename PointNT> inline void
66  const pcl::PointCloud<PointNT>& normals,
67  std::vector<Eigen::Matrix3d, Eigen::aligned_allocator<Eigen::Matrix3d> >& covariances,
68  double epsilon = 0.001)
69  {
70  assert(cloud.points.size() == normals.points.size());
71 
72  int nr_points = static_cast<int>(cloud.points.size());
73  covariances.resize(nr_points);
74  for (int i = 0; i < nr_points; ++i)
75  {
76  Eigen::Vector3d normal(normals.points[i].normal_x,
77  normals.points[i].normal_y,
78  normals.points[i].normal_z);
79 
80  // compute rotation matrix
81  Eigen::Matrix3d rot;
82  Eigen::Vector3d y;
83  y << 0, 1, 0;
84  rot.row(2) = normal;
85  y = y - normal(1) * normal;
86  y.normalize();
87  rot.row(1) = y;
88  rot.row(0) = normal.cross(rot.row(1));
89 
90  // comnpute approximate covariance
91  Eigen::Matrix3d cov;
92  cov << 1, 0, 0,
93  0, 1, 0,
94  0, 0, epsilon;
95  covariances[i] = rot.transpose()*cov*rot;
96  }
97  }
98 
99  }
100 }
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:414
void flipNormalTowardsViewpoint(const PointT &point, float vp_x, float vp_y, float vp_z, Eigen::Matrix< Scalar, 4, 1 > &normal)
Flip (in place) the estimated normal of a point towards a given viewpoint.
Definition: normal_3d.h:120
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:412
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:406
void computeApproximateCovariances(const pcl::PointCloud< PointT > &cloud, const pcl::PointCloud< PointNT > &normals, std::vector< Eigen::Matrix3d, Eigen::aligned_allocator< Eigen::Matrix3d > > &covariances, double epsilon=0.001)
Compute GICP-style covariance matrices given a point cloud and the corresponding surface normals...
Definition: from_meshes.h:65
void computeApproximateNormals(const pcl::PointCloud< PointT > &cloud, const std::vector< pcl::Vertices > &polygons, pcl::PointCloud< PointNT > &normals)
Compute approximate surface normals on a mesh.
Definition: from_meshes.h:17