intensity_gradient.hpp

Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: intensity_gradient.hpp 1370 2011-06-19 01:06:01Z jspricke $
00035  *
00036  */
00037 
00038 #ifndef PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
00039 #define PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
00040 
00041 #include "pcl/features/intensity_gradient.h"
00042 
00043 template <typename PointInT, typename PointNT, typename PointOutT> void
00044 pcl::IntensityGradientEstimation<PointInT, PointNT, PointOutT>::computeFeature (PointCloudOut &output)
00045 {
00046   // Allocate enough space to hold the results
00047   // \note This resize is irrelevant for a radiusSearch ().
00048   std::vector<int> nn_indices (k_);
00049   std::vector<float> nn_dists (k_);
00050 
00051   // Iterating over the entire index vector
00052   for (size_t idx = 0; idx < indices_->size (); ++idx)
00053   {
00054     PointOutT &p_out = output.points[idx];
00055 
00056     if (!this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
00057     {
00058       p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
00059       continue;
00060     }
00061 
00062     Eigen::Vector4f centroid;
00063     compute3DCentroid (*surface_, nn_indices, centroid);
00064 
00065     Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[idx].normal);
00066     Eigen::Vector3f gradient;
00067     computePointIntensityGradient (*surface_, nn_indices, centroid.head<3> (), normal, gradient);
00068    
00069     p_out.gradient[0] = gradient[0];
00070     p_out.gradient[1] = gradient[1];
00071     p_out.gradient[2] = gradient[2];
00072    
00073   }
00074 }
00075 
00076 template <typename PointInT, typename PointNT, typename PointOutT> void
00077 pcl::IntensityGradientEstimation <PointInT, PointNT, PointOutT>::computePointIntensityGradient (
00078   const pcl::PointCloud <PointInT> &cloud, const std::vector <int> &indices, 
00079   const Eigen::Vector3f &point, const Eigen::Vector3f &normal, Eigen::Vector3f &gradient)
00080 {
00081   if (indices.size () < 3)
00082   { 
00083     gradient[0] = gradient[1] = gradient[2] = std::numeric_limits<float>::quiet_NaN ();
00084     return;
00085   }
00086 
00087   Eigen::Matrix3f A = Eigen::Matrix3f::Zero ();
00088   Eigen::Vector3f b = Eigen::Vector3f::Zero ();
00089 
00090   for (size_t i_point = 0; i_point < indices.size (); ++i_point)
00091   {
00092     PointInT p = cloud.points[indices[i_point]];
00093     if (!pcl_isfinite (p.x) || 
00094         !pcl_isfinite (p.y) || 
00095         !pcl_isfinite (p.z) || 
00096         !pcl_isfinite (p.intensity))
00097       continue;
00098 
00099     p.x -= point[0];
00100     p.y -= point[1];
00101     p.z -= point[2];
00102 
00103     A (0, 0) += p.x*p.x;
00104     A (0, 1) += p.x*p.y;
00105     A (0, 2) += p.x*p.z;
00106 
00107     A (1, 1) += p.y*p.y;
00108     A (1, 2) += p.y*p.z;
00109 
00110     A (2, 2) += p.z*p.z;
00111     
00112     b[0] += p.x * p.intensity;
00113     b[1] += p.y * p.intensity;
00114     b[2] += p.z * p.intensity;
00115   }
00116   // Fill in the lower triangle of A
00117   A (1, 0) = A (0, 1);
00118   A (2, 0) = A (0, 2);
00119   A (2, 1) = A (1, 2);
00120 
00121   // Fit a hyperplane to the data 
00122   Eigen::Vector3f x = A.colPivHouseholderQr ().solve (b);
00123 
00124   // Project the gradient vector, x, onto the tangent plane
00125   gradient = (Eigen::Matrix3f::Identity () - normal*normal.transpose ()) * x;
00126 }
00127 
00128 
00129 #define PCL_INSTANTIATE_IntensityGradientEstimation(InT,NT,OutT) template class PCL_EXPORTS pcl::IntensityGradientEstimation<InT,NT,OutT>;
00130 
00131 #endif    // PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_