Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/filters/include/pcl/filters/filter.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id$
00037  *
00038  */
00039 
00040 #ifndef PCL_FILTER_H_
00041 #define PCL_FILTER_H_
00042 
00043 #include <pcl/pcl_base.h>
00044 #include <pcl/conversions.h>
00045 #include <pcl/filters/boost.h>
00046 #include <cfloat>
00047 #include <pcl/PointIndices.h>
00048 
00049 namespace pcl
00050 {
00051   /** \brief Removes points with x, y, or z equal to NaN
00052     * \param[in] cloud_in the input point cloud
00053     * \param[out] cloud_out the input point cloud
00054     * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
00055     * \note The density of the point cloud is lost.
00056     * \note Can be called with cloud_in == cloud_out
00057     * \ingroup filters
00058     */
00059   template<typename PointT> void
00060   removeNaNFromPointCloud (const pcl::PointCloud<PointT> &cloud_in, 
00061                            pcl::PointCloud<PointT> &cloud_out, 
00062                            std::vector<int> &index);
00063 
00064   /** \brief Removes points that have their normals invalid (i.e., equal to NaN)
00065     * \param[in] cloud_in the input point cloud
00066     * \param[out] cloud_out the input point cloud
00067     * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
00068     * \note The density of the point cloud is lost.
00069     * \note Can be called with cloud_in == cloud_out
00070     * \ingroup filters
00071     */
00072   template<typename PointT> void
00073   removeNaNNormalsFromPointCloud (const pcl::PointCloud<PointT> &cloud_in, 
00074                                   pcl::PointCloud<PointT> &cloud_out, 
00075                                   std::vector<int> &index);
00076 
00077   ////////////////////////////////////////////////////////////////////////////////////////////
00078   /** \brief Filter represents the base filter class. All filters must inherit from this interface.
00079     * \author Radu B. Rusu
00080     * \ingroup filters
00081     */
00082   template<typename PointT>
00083   class Filter : public PCLBase<PointT>
00084   {
00085     public:
00086       using PCLBase<PointT>::indices_;
00087       using PCLBase<PointT>::input_;
00088 
00089       typedef boost::shared_ptr< Filter<PointT> > Ptr;
00090       typedef boost::shared_ptr< const Filter<PointT> > ConstPtr;
00091 
00092 
00093       typedef pcl::PointCloud<PointT> PointCloud;
00094       typedef typename PointCloud::Ptr PointCloudPtr;
00095       typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00096 
00097       /** \brief Empty constructor.
00098         * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a 
00099         * separate list. Default: false.
00100         */
00101       Filter (bool extract_removed_indices = false) : 
00102         removed_indices_ (new std::vector<int>),
00103         filter_name_ (),
00104         extract_removed_indices_ (extract_removed_indices)
00105       {
00106       }
00107 
00108       /** \brief Empty destructor */
00109       virtual ~Filter () {}
00110 
00111       /** \brief Get the point indices being removed */
00112       inline IndicesConstPtr const
00113       getRemovedIndices ()
00114       {
00115         return (removed_indices_);
00116       }
00117 
00118       /** \brief Get the point indices being removed 
00119         * \param[out] pi the resultant point indices that have been removed
00120         */
00121       inline void
00122       getRemovedIndices (PointIndices &pi)
00123       {
00124         pi.indices = *removed_indices_;
00125       }
00126 
00127       /** \brief Calls the filtering method and returns the filtered dataset in output.
00128         * \param[out] output the resultant filtered point cloud dataset
00129         */
00130       inline void
00131       filter (PointCloud &output)
00132       {
00133         if (!initCompute ())
00134           return;
00135 
00136         // Resize the output dataset
00137         //if (output.points.size () != indices_->size ())
00138         //  output.points.resize (indices_->size ());
00139 
00140         // Copy header at a minimum
00141         output.header = input_->header;
00142         output.sensor_origin_ = input_->sensor_origin_;
00143         output.sensor_orientation_ = input_->sensor_orientation_;
00144 
00145         // Apply the actual filter
00146         applyFilter (output);
00147 
00148         deinitCompute ();
00149       }
00150 
00151     protected:
00152 
00153       using PCLBase<PointT>::initCompute;
00154       using PCLBase<PointT>::deinitCompute;
00155 
00156       /** \brief Indices of the points that are removed */
00157       IndicesPtr removed_indices_;
00158 
00159       /** \brief The filter name. */
00160       std::string filter_name_;
00161 
00162       /** \brief Set to true if we want to return the indices of the removed points. */
00163       bool extract_removed_indices_;
00164 
00165       /** \brief Abstract filter method. 
00166         *
00167         * The implementation needs to set output.{points, width, height, is_dense}.
00168         *
00169         * \param[out] output the resultant filtered point cloud
00170         */
00171       virtual void
00172       applyFilter (PointCloud &output) = 0;
00173 
00174       /** \brief Get a string representation of the name of this class. */
00175       inline const std::string&
00176       getClassName () const
00177       {
00178         return (filter_name_);
00179       }
00180   };
00181 
00182   ////////////////////////////////////////////////////////////////////////////////////////////
00183   /** \brief Filter represents the base filter class. All filters must inherit from this interface.
00184     * \author Radu B. Rusu
00185     * \ingroup filters
00186     */
00187   template<>
00188   class PCL_EXPORTS Filter<pcl::PCLPointCloud2> : public PCLBase<pcl::PCLPointCloud2>
00189   {
00190     public:
00191       typedef boost::shared_ptr< Filter<pcl::PCLPointCloud2> > Ptr;
00192       typedef boost::shared_ptr< const Filter<pcl::PCLPointCloud2> > ConstPtr;
00193 
00194       typedef pcl::PCLPointCloud2 PCLPointCloud2;
00195       typedef PCLPointCloud2::Ptr PCLPointCloud2Ptr;
00196       typedef PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr;
00197 
00198       /** \brief Empty constructor. 
00199         * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a 
00200         * separate list. Default: false.
00201         */
00202       Filter (bool extract_removed_indices = false) : 
00203         removed_indices_ (new std::vector<int>),
00204         extract_removed_indices_ (extract_removed_indices),
00205         filter_name_ ()
00206       {
00207       }
00208       
00209       /** \brief Empty destructor */
00210       virtual ~Filter () {}
00211 
00212       /** \brief Get the point indices being removed */
00213       inline IndicesConstPtr const
00214       getRemovedIndices ()
00215       {
00216         return (removed_indices_);
00217       }
00218 
00219       /** \brief Get the point indices being removed 
00220         * \param[out] pi the resultant point indices that have been removed
00221         */
00222       inline void
00223       getRemovedIndices (PointIndices &pi)
00224       {
00225         pi.indices = *removed_indices_;
00226       }
00227 
00228       /** \brief Calls the filtering method and returns the filtered dataset in output.
00229         * \param[out] output the resultant filtered point cloud dataset
00230         */
00231       void
00232       filter (PCLPointCloud2 &output);
00233 
00234     protected:
00235 
00236       /** \brief Indices of the points that are removed */
00237       IndicesPtr removed_indices_;
00238 
00239       /** \brief Set to true if we want to return the indices of the removed points. */
00240       bool extract_removed_indices_;
00241 
00242       /** \brief The filter name. */
00243       std::string filter_name_;
00244 
00245       /** \brief Abstract filter method.
00246         *
00247         * The implementation needs to set output.{data, row_step, point_step, width, height, is_dense}.
00248         *
00249         * \param[out] output the resultant filtered point cloud
00250         */
00251       virtual void
00252       applyFilter (PCLPointCloud2 &output) = 0;
00253 
00254       /** \brief Get a string representation of the name of this class. */
00255       inline const std::string&
00256       getClassName () const
00257       {
00258         return (filter_name_);
00259       }
00260   };
00261 }
00262 
00263 #ifdef PCL_NO_PRECOMPILE
00264 #include <pcl/filters/impl/filter.hpp>
00265 #endif
00266 
00267 #endif  //#ifndef PCL_FILTER_H_