Point Cloud Library (PCL)  1.3.1
sac_model_normal_parallel_plane.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009-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: sac_model_normal_parallel_plane.hpp 2328 2011-08-31 08:11:00Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
00039 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
00040 
00041 #include "pcl/sample_consensus/sac_model_normal_parallel_plane.h"
00042 
00044 template <typename PointT, typename PointNT> void
00045 pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>::selectWithinDistance (
00046       const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
00047 {
00048   if (!normals_)
00049   {
00050     PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::selectWithinDistance] No input dataset containing normals was given!\n");
00051     return;
00052   }
00053 
00054   // Check if the model is valid given the user constraints
00055   if (!isModelValid (model_coefficients))
00056   {
00057     inliers.clear ();
00058     return;
00059   }
00060 
00061   // Obtain the plane normal
00062   Eigen::Vector4f coeff = model_coefficients;
00063   coeff[3] = 0;
00064 
00065   int nr_p = 0;
00066   inliers.resize (indices_->size ());
00067   // Iterate through the 3d points and calculate the distances from them to the plane
00068   for (size_t i = 0; i < indices_->size (); ++i)
00069   {
00070     // Calculate the distance from the point to the plane normal as the dot product
00071     // D = (P-A).N/|N|
00072     Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
00073     Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
00074     double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
00075 
00076     // Calculate the angular distance between the point normal and the plane normal
00077     double d_normal = fabs (getAngle3D (n, coeff));
00078     d_normal = (std::min) (d_normal, M_PI - d_normal);
00079 
00080     if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
00081     {
00082       // Returns the indices of the points whose distances are smaller than the threshold
00083       inliers[nr_p] = (*indices_)[i];
00084       nr_p++;
00085     }
00086   }
00087   inliers.resize (nr_p);
00088 }
00089 
00091 template <typename PointT, typename PointNT> int
00092 pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>::countWithinDistance (
00093       const Eigen::VectorXf &model_coefficients, const double threshold)
00094 {
00095   if (!normals_)
00096   {
00097     PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::countWithinDistance] No input dataset containing normals was given!\n");
00098     return (0);
00099   }
00100 
00101   // Check if the model is valid given the user constraints
00102   if (!isModelValid (model_coefficients))
00103     return (0);
00104 
00105   // Obtain the plane normal
00106   Eigen::Vector4f coeff = model_coefficients;
00107   coeff[3] = 0;
00108 
00109   int nr_p = 0;
00110 
00111   // Iterate through the 3d points and calculate the distances from them to the plane
00112   for (size_t i = 0; i < indices_->size (); ++i)
00113   {
00114     // Calculate the distance from the point to the plane normal as the dot product
00115     // D = (P-A).N/|N|
00116     Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
00117     Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
00118     double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
00119 
00120     // Calculate the angular distance between the point normal and the plane normal
00121     double d_normal = fabs (getAngle3D (n, coeff));
00122     d_normal = (std::min) (d_normal, M_PI - d_normal);
00123 
00124     if (fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid) < threshold)
00125       nr_p++;
00126   }
00127   return (nr_p);
00128 }
00129 
00130 
00132 template <typename PointT, typename PointNT> void
00133 pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>::getDistancesToModel (
00134       const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
00135 {
00136   if (!normals_)
00137   {
00138     PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::getDistancesToModel] No input dataset containing normals was given!\n");
00139     return;
00140   }
00141 
00142   // Check if the model is valid given the user constraints
00143   if (!isModelValid (model_coefficients))
00144   {
00145     distances.clear ();
00146     return;
00147   }
00148 
00149   // Obtain the plane normal
00150   Eigen::Vector4f coeff = model_coefficients;
00151   coeff[3] = 0;
00152 
00153   distances.resize (indices_->size ());
00154 
00155   // Iterate through the 3d points and calculate the distances from them to the plane
00156   for (size_t i = 0; i < indices_->size (); ++i)
00157   {
00158     // Calculate the distance from the point to the plane normal as the dot product
00159     // D = (P-A).N/|N|
00160     Eigen::Vector4f p (input_->points[(*indices_)[i]].x, input_->points[(*indices_)[i]].y, input_->points[(*indices_)[i]].z, 0);
00161     Eigen::Vector4f n (normals_->points[(*indices_)[i]].normal[0], normals_->points[(*indices_)[i]].normal[1], normals_->points[(*indices_)[i]].normal[2], 0);
00162     double d_euclid = fabs (coeff.dot (p) + model_coefficients[3]);
00163 
00164     // Calculate the angular distance between the point normal and the plane normal
00165     double d_normal = fabs (getAngle3D (n, coeff));
00166     d_normal = (std::min) (d_normal, M_PI - d_normal);
00167 
00168     distances[i] = fabs (normal_distance_weight_ * d_normal + (1 - normal_distance_weight_) * d_euclid);
00169   }
00170 }
00171 
00173 template <typename PointT, typename PointNT> bool
00174 pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>::isModelValid (const Eigen::VectorXf &model_coefficients)
00175 {
00176   // Needs a valid model coefficients
00177   if (model_coefficients.size () != 4)
00178   {
00179     PCL_ERROR ("[pcl::SampleConsensusModelNormalParallelPlane::isModelValid] Invalid number of model coefficients given (%lu)!\n", (unsigned long)model_coefficients.size ());
00180     return (false);
00181   }
00182 
00183   // Check against template, if given
00184   if (eps_angle_ > 0.0)
00185   {
00186     // Obtain the plane normal
00187     Eigen::Vector4f coeff = model_coefficients;
00188     coeff[3] = 0;
00189 
00190     Eigen::Vector4f axis (axis_[0], axis_[1], axis_[2], 0);
00191     double angle_deviation_from_90 = fabs (getAngle3D (axis, coeff));
00192     angle_deviation_from_90 = fabs (angle_deviation_from_90 - (M_PI/2.0));
00193     // Check whether the current plane model satisfies our angle threshold criterion with respect to the given axis
00194     if (angle_deviation_from_90 > eps_angle_)
00195       return (false);
00196   }
00197 
00198   if (eps_dist_ > 0.0)
00199   {
00200     if (fabs (-model_coefficients[3] - distance_from_origin_) > eps_dist_)
00201       return (false);
00202   }
00203 
00204   return (true);
00205 }
00206 
00207 #define PCL_INSTANTIATE_SampleConsensusModelNormalParallelPlane(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalParallelPlane<PointT, PointNT>;
00208 
00209 #endif    // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PARALLEL_PLANE_H_
00210 
00211 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines