normal_3d.h
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, Willow Garage, Inc.
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#ifndef PCL_NORMAL_3D_H_
#define PCL_NORMAL_3D_H_
#include <pcl/features/feature.h>
#include <pcl/common/centroid.h>
namespace pcl
{
/** \brief Compute the Least-Squares plane fit for a given set of points, and return the estimated plane
* parameters together with the surface curvature.
* \param cloud the input point cloud
* \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
* \param curvature the estimated surface curvature as a measure of
* \f[
* \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
* \f]
* \ingroup features
*/
template <typename PointT> inline void
computePointNormal (const pcl::PointCloud<PointT> &cloud,
Eigen::Vector4f &plane_parameters, float &curvature)
{
// Placeholder for the 3x3 covariance matrix at each surface patch
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
// 16-bytes aligned placeholder for the XYZ centroid of a surface patch
Eigen::Vector4f xyz_centroid;
if (cloud.size () < 3 ||
computeMeanAndCovarianceMatrix (cloud, covariance_matrix, xyz_centroid) == 0)
{
plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
curvature = std::numeric_limits<float>::quiet_NaN ();
return;
}
// Get the plane normal and surface curvature
solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
}
/** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
* and return the estimated plane parameters together with the surface curvature.
* \param cloud the input point cloud
* \param indices the point cloud indices that need to be used
* \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
* \param curvature the estimated surface curvature as a measure of
* \f[
* \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
* \f]
* \ingroup features
*/
template <typename PointT> inline void
computePointNormal (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices,
Eigen::Vector4f &plane_parameters, float &curvature)
{
// Placeholder for the 3x3 covariance matrix at each surface patch
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
// 16-bytes aligned placeholder for the XYZ centroid of a surface patch
Eigen::Vector4f xyz_centroid;
if (indices.size () < 3 ||
computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix, xyz_centroid) == 0)
{
plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
curvature = std::numeric_limits<float>::quiet_NaN ();
return;
}
// Get the plane normal and surface curvature
solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
}
/** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
* \param point a given point
* \param vp_x the X coordinate of the viewpoint
* \param vp_y the X coordinate of the viewpoint
* \param vp_z the X coordinate of the viewpoint
* \param normal the plane normal to be flipped
* \ingroup features
*/
template <typename PointT, typename Scalar> inline void
flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
Eigen::Matrix<Scalar, 4, 1>& normal)
{
Eigen::Matrix <Scalar, 4, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z, 0);
// Dot product between the (viewpoint - point) and the plane normal
float cos_theta = vp.dot (normal);
// Flip the plane normal
if (cos_theta < 0)
{
normal *= -1;
normal[3] = 0.0f;
// Hessian form (D = nc . p_plane (centroid here) + p)
normal[3] = -1 * normal.dot (point.getVector4fMap ());
}
}
/** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
* \param point a given point
* \param vp_x the X coordinate of the viewpoint
* \param vp_y the X coordinate of the viewpoint
* \param vp_z the X coordinate of the viewpoint
* \param normal the plane normal to be flipped
* \ingroup features
*/
template <typename PointT, typename Scalar> inline void
flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
Eigen::Matrix<Scalar, 3, 1>& normal)
{
Eigen::Matrix <Scalar, 3, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z);
// Flip the plane normal
if (vp.dot (normal) < 0)
normal *= -1;
}
/** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
* \param point a given point
* \param vp_x the X coordinate of the viewpoint
* \param vp_y the X coordinate of the viewpoint
* \param vp_z the X coordinate of the viewpoint
* \param nx the resultant X component of the plane normal
* \param ny the resultant Y component of the plane normal
* \param nz the resultant Z component of the plane normal
* \ingroup features
*/
template <typename PointT> inline void
flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
float &nx, float &ny, float &nz)
{
// See if we need to flip any plane normals
vp_x -= point.x;
vp_y -= point.y;
vp_z -= point.z;
// Dot product between the (viewpoint - point) and the plane normal
float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);
// Flip the plane normal
if (cos_theta < 0)
{
nx *= -1;
ny *= -1;
nz *= -1;
}
}
/** \brief NormalEstimation estimates local surface properties (surface normals and curvatures)at each
* 3D point. If PointOutT is specified as pcl::Normal, the normal is stored in the first 3 components (0-2),
* and the curvature is stored in component 3.
*
* \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
* \ref NormalEstimationOMP for a parallel implementation.
* \author Radu B. Rusu
* \ingroup features
*/
template <typename PointInT, typename PointOutT>
class NormalEstimation: public Feature<PointInT, PointOutT>
{
public:
typedef boost::shared_ptr<NormalEstimation<PointInT, PointOutT> > Ptr;
typedef boost::shared_ptr<const NormalEstimation<PointInT, PointOutT> > ConstPtr;
using Feature<PointInT, PointOutT>::feature_name_;
using Feature<PointInT, PointOutT>::getClassName;
using Feature<PointInT, PointOutT>::indices_;
using Feature<PointInT, PointOutT>::input_;
using Feature<PointInT, PointOutT>::surface_;
using Feature<PointInT, PointOutT>::k_;
using Feature<PointInT, PointOutT>::search_radius_;
using Feature<PointInT, PointOutT>::search_parameter_;
typedef typename Feature<PointInT, PointOutT>::PointCloudOut PointCloudOut;
typedef typename Feature<PointInT, PointOutT>::PointCloudConstPtr PointCloudConstPtr;
/** \brief Empty constructor. */
NormalEstimation ()
: vpx_ (0)
, vpy_ (0)
, vpz_ (0)
, covariance_matrix_ ()
, xyz_centroid_ ()
, use_sensor_origin_ (true)
{
feature_name_ = "NormalEstimation";
};
/** \brief Empty destructor */
virtual ~NormalEstimation () {}
/** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
* and return the estimated plane parameters together with the surface curvature.
* \param cloud the input point cloud
* \param indices the point cloud indices that need to be used
* \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
* \param curvature the estimated surface curvature as a measure of
* \f[
* \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
* \f]
*/
inline void
computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
Eigen::Vector4f &plane_parameters, float &curvature)
{
if (indices.size () < 3 ||
computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix_, xyz_centroid_) == 0)
{
plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
curvature = std::numeric_limits<float>::quiet_NaN ();
return;
}
// Get the plane normal and surface curvature
solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
}
/** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
* and return the estimated plane parameters together with the surface curvature.
* \param cloud the input point cloud
* \param indices the point cloud indices that need to be used
* \param nx the resultant X component of the plane normal
* \param ny the resultant Y component of the plane normal
* \param nz the resultant Z component of the plane normal
* \param curvature the estimated surface curvature as a measure of
* \f[
* \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
* \f]
*/
inline void
computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
float &nx, float &ny, float &nz, float &curvature)
{
if (indices.size () < 3 ||
computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix_, xyz_centroid_) == 0)
{
nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
return;
}
// Get the plane normal and surface curvature
solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
}
/** \brief Provide a pointer to the input dataset
* \param cloud the const boost shared pointer to a PointCloud message
*/
virtual inline void
setInputCloud (const PointCloudConstPtr &cloud)
{
input_ = cloud;
if (use_sensor_origin_)
{
vpx_ = input_->sensor_origin_.coeff (0);
vpy_ = input_->sensor_origin_.coeff (1);
vpz_ = input_->sensor_origin_.coeff (2);
}
}
/** \brief Set the viewpoint.
* \param vpx the X coordinate of the viewpoint
* \param vpy the Y coordinate of the viewpoint
* \param vpz the Z coordinate of the viewpoint
*/
inline void
setViewPoint (float vpx, float vpy, float vpz)
{
vpx_ = vpx;
vpy_ = vpy;
vpz_ = vpz;
use_sensor_origin_ = false;
}
/** \brief Get the viewpoint.
* \param [out] vpx x-coordinate of the view point
* \param [out] vpy y-coordinate of the view point
* \param [out] vpz z-coordinate of the view point
* \note this method returns the currently used viewpoint for normal flipping.
* If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
* If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
*/
inline void
getViewPoint (float &vpx, float &vpy, float &vpz)
{
vpx = vpx_;
vpy = vpy_;
vpz = vpz_;
}
/** \brief sets whether the sensor origin or a user given viewpoint should be used. After this method, the
* normal estimation method uses the sensor origin of the input cloud.
* to use a user defined view point, use the method setViewPoint
*/
inline void
useSensorOriginAsViewPoint ()
{
use_sensor_origin_ = true;
if (input_)
{
vpx_ = input_->sensor_origin_.coeff (0);
vpy_ = input_->sensor_origin_.coeff (1);
vpz_ = input_->sensor_origin_.coeff (2);
}
else
{
vpx_ = 0;
vpy_ = 0;
vpz_ = 0;
}
}
protected:
/** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
* setSearchSurface () and the spatial locator in setSearchMethod ()
* \note In situations where not enough neighbors are found, the normal and curvature values are set to -1.
* \param output the resultant point cloud model dataset that contains surface normals and curvatures
*/
void
computeFeature (PointCloudOut &output);
/** \brief Values describing the viewpoint ("pinhole" camera model assumed). For per point viewpoints, inherit
* from NormalEstimation and provide your own computeFeature (). By default, the viewpoint is set to 0,0,0. */
float vpx_, vpy_, vpz_;
/** \brief Placeholder for the 3x3 covariance matrix at each surface patch. */
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_;
/** \brief 16-bytes aligned placeholder for the XYZ centroid of a surface patch. */
Eigen::Vector4f xyz_centroid_;
/** whether the sensor origin of the input cloud or a user given viewpoint should be used.*/
bool use_sensor_origin_;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
}
#ifdef PCL_NO_PRECOMPILE
#include <pcl/features/impl/normal_3d.hpp>
#endif
#endif //#ifndef PCL_NORMAL_3D_H_
pcl::NormalEstimation::Ptr
boost::shared_ptr< NormalEstimation< PointInT, PointOutT > > Ptr
Definition: normal_3d.h:200
pcl::PointCloud::size
size_t size() const
Definition: point_cloud.h:440
pcl::NormalEstimation::PointCloudOut
Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: normal_3d.h:211
pcl::PCLBase< PointInT >::PointCloudConstPtr
PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:73
pcl::NormalEstimation::setViewPoint
void setViewPoint(float vpx, float vpy, float vpz)
Set the viewpoint.
Definition: normal_3d.h:303
pcl::EIGEN_ALIGN16
struct pcl::_PointXYZHSV EIGEN_ALIGN16
pcl::NormalEstimation::PointCloudConstPtr
Feature< PointInT, PointOutT >::PointCloudConstPtr PointCloudConstPtr
Definition: normal_3d.h:212
pcl::NormalEstimation::~NormalEstimation
virtual ~NormalEstimation()
Empty destructor.
Definition: normal_3d.h:227
pcl::computeMeanAndCovarianceMatrix
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:490
pcl::Feature::feature_name_
std::string feature_name_
The feature name.
Definition: feature.h:222
pcl
Definition: cloud_iterator.h:46
pcl::NormalEstimation::vpy_
float vpy_
Definition: normal_3d.h:360
pcl::NormalEstimation::vpz_
float vpz_
Definition: normal_3d.h:360
pcl::solvePlaneParameters
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...
Definition: feature.hpp:48
pcl::NormalEstimation
NormalEstimation estimates local surface properties (surface normals and curvatures)at each 3D point...
Definition: normal_3d.h:197
pcl::NormalEstimation::computePointNormal
void computePointNormal(const pcl::PointCloud< PointInT > &cloud, const std::vector< int > &indices, Eigen::Vector4f &plane_parameters, float &curvature)
Compute the Least-Squares plane fit for a given set of points, using their indices, and return the estimated plane parameters together with the surface curvature.
Definition: normal_3d.h:240
pcl::flipNormalTowardsViewpoint
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:119
pcl::NormalEstimation::xyz_centroid_
Eigen::Vector4f xyz_centroid_
16-bytes aligned placeholder for the XYZ centroid of a surface patch.
Definition: normal_3d.h:366
pcl::NormalEstimation::computePointNormal
void computePointNormal(const pcl::PointCloud< PointInT > &cloud, const std::vector< int > &indices, float &nx, float &ny, float &nz, float &curvature)
Compute the Least-Squares plane fit for a given set of points, using their indices, and return the estimated plane parameters together with the surface curvature.
Definition: normal_3d.h:268
pcl::NormalEstimation::NormalEstimation
NormalEstimation()
Empty constructor.
Definition: normal_3d.h:215
pcl::NormalEstimation::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: normal_3d.h:286
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points. ...
Definition: projection_matrix.h:53
pcl::NormalEstimation::covariance_matrix_
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_
Placeholder for the 3x3 covariance matrix at each surface patch.
Definition: normal_3d.h:363
pcl::NormalEstimation::ConstPtr
boost::shared_ptr< const NormalEstimation< PointInT, PointOutT > > ConstPtr
Definition: normal_3d.h:201
pcl::NormalEstimation::use_sensor_origin_
bool use_sensor_origin_
whether the sensor origin of the input cloud or a user given viewpoint should be used.
Definition: normal_3d.h:369
pcl::NormalEstimation::computeFeature
void computeFeature(PointCloudOut &output)
Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in setSe...
Definition: normal_3d.hpp:48
pcl::PCLBase< PointInT >::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
pcl::Feature
Feature represents the base feature class.
Definition: feature.h:105
pcl::computePointNormal
void computePointNormal(const pcl::PointCloud< PointT > &cloud, Eigen::Vector4f &plane_parameters, float &curvature)
Compute the Least-Squares plane fit for a given set of points, and return the estimated plane paramet...
Definition: normal_3d.h:60
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:594
pcl::NormalEstimation::getViewPoint
void getViewPoint(float &vpx, float &vpy, float &vpz)
Get the viewpoint.
Definition: normal_3d.h:320
pcl::NormalEstimation::vpx_
float vpx_
Values describing the viewpoint ("pinhole" camera model assumed).
Definition: normal_3d.h:360
pcl::NormalEstimation::useSensorOriginAsViewPoint
void useSensorOriginAsViewPoint()
sets whether the sensor origin or a user given viewpoint should be used.
Definition: normal_3d.h:332