Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/segmentation/include/pcl/segmentation/extract_polygonal_prism_data.h
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 the copyright holder(s) 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$
00035  *
00036  */
00037 
00038 #ifndef PCL_EXTRACT_POLYGONAL_PRISM_DATA_H_
00039 #define PCL_EXTRACT_POLYGONAL_PRISM_DATA_H_
00040 
00041 #include <pcl/pcl_base.h>
00042 #include <pcl/sample_consensus/sac_model_plane.h>
00043 
00044 namespace pcl
00045 {
00046   /** \brief General purpose method for checking if a 3D point is inside or
00047     * outside a given 2D polygon. 
00048     * \note this method accepts any general 3D point that is projected onto the
00049     * 2D polygon, but performs an internal XY projection of both the polygon and the point. 
00050     * \param point a 3D point projected onto the same plane as the polygon
00051     * \param polygon a polygon
00052     * \ingroup segmentation
00053     */
00054   template <typename PointT> bool 
00055   isPointIn2DPolygon (const PointT &point, const pcl::PointCloud<PointT> &polygon);
00056 
00057   /** \brief Check if a 2d point (X and Y coordinates considered only!) is
00058     * inside or outside a given polygon. This method assumes that both the point
00059     * and the polygon are projected onto the XY plane.
00060     *
00061     * \note (This is highly optimized code taken from http://www.visibone.com/inpoly/)
00062     *       Copyright (c) 1995-1996 Galacticomm, Inc.  Freeware source code.
00063     * \param point a 3D point projected onto the same plane as the polygon
00064     * \param polygon a polygon
00065     * \ingroup segmentation
00066     */
00067   template <typename PointT> bool 
00068   isXYPointIn2DXYPolygon (const PointT &point, const pcl::PointCloud<PointT> &polygon);
00069 
00070   ////////////////////////////////////////////////////////////////////////////////////////////
00071   /** \brief @b ExtractPolygonalPrismData uses a set of point indices that
00072     * represent a planar model, and together with a given height, generates a 3D
00073     * polygonal prism. The polygonal prism is then used to segment all points
00074     * lying inside it.
00075     *
00076     * An example of its usage is to extract the data lying within a set of 3D
00077     * boundaries (e.g., objects supported by a plane).
00078     *
00079     * Example usage:
00080     * \code{.cpp}
00081     * double z_min = 0., z_max = 0.05; // we want the points above the plane, no farther than 5 cm from the surface
00082     * pcl::PointCloud<pcl::PointXYZ>::Ptr hull_points (new pcl::PointCloud<pcl::PointXYZ> ());
00083     * pcl::ConvexHull<pcl::PointXYZ> hull;
00084     * // hull.setDimension (2); // not necessarily needed, but we need to check the dimensionality of the output
00085     * hull.setInputCloud (cloud);
00086     * hull.reconstruct (hull_points);
00087     * if (hull.getDimension () == 2)
00088     * {
00089     *   pcl::ExtractPolygonalPrismData<pcl::PointXYZ> prism;
00090     *   prism.setInputCloud (point_cloud);
00091     *   prism.setInputPlanarHull (hull_points);
00092     *   prism.setHeightLimits (z_min, z_max);
00093     *   prism.segment (cloud_indices);
00094     * }
00095     * else
00096     *  PCL_ERROR ("The input cloud does not represent a planar surface.\n");
00097     * \endcode
00098     * \author Radu Bogdan Rusu
00099     * \ingroup segmentation
00100     */
00101   template <typename PointT>
00102   class ExtractPolygonalPrismData : public PCLBase<PointT>
00103   {
00104     using PCLBase<PointT>::input_;
00105     using PCLBase<PointT>::indices_;
00106     using PCLBase<PointT>::initCompute;
00107     using PCLBase<PointT>::deinitCompute;
00108 
00109     public:
00110       typedef pcl::PointCloud<PointT> PointCloud;
00111       typedef typename PointCloud::Ptr PointCloudPtr;
00112       typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00113 
00114       typedef PointIndices::Ptr PointIndicesPtr;
00115       typedef PointIndices::ConstPtr PointIndicesConstPtr;
00116 
00117       /** \brief Empty constructor. */
00118       ExtractPolygonalPrismData () : planar_hull_ (), min_pts_hull_ (3), 
00119                                      height_limit_min_ (0), height_limit_max_ (FLT_MAX),
00120                                      vpx_ (0), vpy_ (0), vpz_ (0)
00121       {};
00122 
00123       /** \brief Provide a pointer to the input planar hull dataset.
00124         * \note Please see the example in the class description for how to obtain this.
00125         * \param[in] hull the input planar hull dataset
00126         */
00127       inline void 
00128       setInputPlanarHull (const PointCloudConstPtr &hull) { planar_hull_ = hull; }
00129 
00130       /** \brief Get a pointer the input planar hull dataset. */
00131       inline PointCloudConstPtr 
00132       getInputPlanarHull () const { return (planar_hull_); }
00133 
00134       /** \brief Set the height limits. All points having distances to the
00135         * model outside this interval will be discarded.
00136         *
00137         * \param[in] height_min the minimum allowed distance to the plane model value
00138         * \param[in] height_max the maximum allowed distance to the plane model value
00139         */
00140       inline void
00141       setHeightLimits (double height_min, double height_max)
00142       {
00143         height_limit_min_ = height_min;
00144         height_limit_max_ = height_max;
00145       }
00146 
00147       /** \brief Get the height limits (min/max) as set by the user. The
00148         * default values are -FLT_MAX, FLT_MAX. 
00149         * \param[out] height_min the resultant min height limit
00150         * \param[out] height_max the resultant max height limit
00151         */
00152       inline void
00153       getHeightLimits (double &height_min, double &height_max) const
00154       {
00155         height_min = height_limit_min_;
00156         height_max = height_limit_max_;
00157       }
00158 
00159       /** \brief Set the viewpoint.
00160         * \param[in] vpx the X coordinate of the viewpoint
00161         * \param[in] vpy the Y coordinate of the viewpoint
00162         * \param[in] vpz the Z coordinate of the viewpoint
00163         */
00164       inline void
00165       setViewPoint (float vpx, float vpy, float vpz)
00166       {
00167         vpx_ = vpx;
00168         vpy_ = vpy;
00169         vpz_ = vpz;
00170       }
00171 
00172       /** \brief Get the viewpoint. */
00173       inline void
00174       getViewPoint (float &vpx, float &vpy, float &vpz) const
00175       {
00176         vpx = vpx_;
00177         vpy = vpy_;
00178         vpz = vpz_;
00179       }
00180 
00181       /** \brief Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
00182         * \param[out] output the resultant point indices that support the model found (inliers)
00183         */
00184       void 
00185       segment (PointIndices &output);
00186 
00187     protected:
00188       /** \brief A pointer to the input planar hull dataset. */
00189       PointCloudConstPtr planar_hull_;
00190 
00191       /** \brief The minimum number of points needed on the convex hull. */
00192       int min_pts_hull_;
00193 
00194       /** \brief The minimum allowed height (distance to the model) a point
00195         * will be considered from. 
00196         */
00197       double height_limit_min_;
00198 
00199       /** \brief The maximum allowed height (distance to the model) a point
00200         * will be considered from. 
00201         */
00202       double height_limit_max_;
00203 
00204       /** \brief Values describing the data acquisition viewpoint. Default: 0,0,0. */
00205       float vpx_, vpy_, vpz_;
00206 
00207       /** \brief Class getName method. */
00208       virtual std::string 
00209       getClassName () const { return ("ExtractPolygonalPrismData"); }
00210   };
00211 }
00212 
00213 #ifdef PCL_NO_PRECOMPILE
00214 #include <pcl/segmentation/impl/extract_polygonal_prism_data.hpp>
00215 #endif
00216 
00217 #endif  //#ifndef PCL_EXTRACT_POLYGONAL_PRISM_DATA_H_