Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
convolution.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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: convolution.h 3657 2011-12-26 23:11:38Z nizar $
00037  *
00038  */
00039 
00040 #ifndef PCL_COMMON_CONVOLUTION_H_
00041 #define PCL_COMMON_CONVOLUTION_H_
00042 
00043 #include <Eigen/Core>
00044 #include <pcl/common/point_operators.h>
00045 #include <pcl/common/spring.h>
00046 #include <pcl/exceptions.h>
00047 
00048 namespace pcl
00049 {
00050   namespace common
00051   {
00080     template <typename PointOperatorsType>
00081     class Convolution
00082     {
00083       public:
00084         typedef typename PointOperatorsType::PointIn PointIn;
00085         typedef typename PointOperatorsType::PointOut PointOut;
00086         typedef typename pcl::PointCloud<PointIn> PointCloudIn;
00087         typedef typename PointCloudIn::Ptr PointCloudInPtr;
00088         typedef typename PointCloudIn::ConstPtr PointCloudInConstPtr;
00089         typedef typename pcl::PointCloud<PointOut> PointCloudOut;
00090         typedef typename PointCloudOut::Ptr PointCloudOutPtr;
00091         typedef typename pcl::PointCloudSpring<PointIn> PointCloudSpring;
00092 
00094         enum BORDERS_POLICY { IGNORE = -1, MIRROR, DUPLICATE };
00096         Convolution () : borders_policy_ (IGNORE), convolve_direction_ (-1) {}
00098         void 
00099         setBordersPolicy (int policy) { borders_policy_ = policy; }
00101         int getBordersPolicy () { return (borders_policy_); }
00103         void 
00104         setConvolveDirection (int direction) { convolve_direction_ = direction; }
00106         int getConvolveDirection () { return (convolve_direction_); }        
00115         inline void
00116         convolve (const Eigen::ArrayXf& h_kernel, 
00117                   const Eigen::ArrayXf& v_kernel,
00118                   PointCloudOutPtr& out)
00119         {
00120           kernel_width_ = h_kernel.size ();
00121           convolve_direction_ = PointCloudSpring::BOTH;
00122           output_ = out;
00123           
00124           try
00125           {
00126             initCompute ();
00127             convolve_rows (h_kernel, output_);
00128             convolve_cols (v_kernel, output_);
00129             deinitCompute ();
00130           }
00131           catch (InitFailedException& e)
00132           {
00133             PCL_THROW_EXCEPTION (InitFailedException,
00134                                  "[pcl::common::Convolution::convolveCols] init failed " << e.what ());
00135           }          
00136         }
00143         inline void
00144         convolveRows (const Eigen::ArrayXf& kernel, PointCloudOutPtr& output)
00145         {
00146           kernel_width_ = kernel.size ();
00147           convolve_direction_ = PointCloudSpring::HORIZONTAL;
00148           output_ = output;
00149           try
00150           {
00151             initCompute ();
00152             convolve_rows (kernel, output_);
00153             deinitCompute ();
00154           }
00155           catch (InitFailedException& e)
00156           {
00157             PCL_THROW_EXCEPTION (InitFailedException,
00158                                  "[pcl::common::Convolution::convolveRows] init failed " << e.what ());
00159           }
00160         }
00167         inline void
00168         convolveCols (const Eigen::ArrayXf& kernel, PointCloudOutPtr& output)
00169         {
00170           kernel_width_ = kernel.size ();
00171           convolve_direction_ = PointCloudSpring::VERTICAL;
00172           output_ = output;
00173           
00174           try
00175           {
00176             initCompute ();
00177             convolve_cols (kernel, output_);
00178             deinitCompute ();
00179           }
00180           catch (InitFailedException& e)
00181           {
00182             PCL_THROW_EXCEPTION (InitFailedException,
00183                                  "[pcl::common::Convolution::convolveCols] init failed " << e.what ());
00184           }          
00185         }
00190         inline void
00191         setInputCloud (const PointCloudInConstPtr& cloud)
00192         {
00193           input_.reset (new PointCloudIn (*cloud));
00194         }
00200         inline void
00201         setInputCloud (const PointCloudInPtr& cloud)
00202         {
00203           input_ = cloud;
00204         }
00206         inline PointOut 
00207         add (const PointIn& lhs, const PointIn& rhs)
00208         {
00209           return (operators_.add (lhs, rhs));
00210         }
00212         inline PointOut
00213         minus (const PointIn& lhs, const PointIn& rhs)
00214         {
00215           return (operators_.minus (lhs, rhs));
00216         }
00218         inline PointOut 
00219         dot (const float& scalar, const PointIn& p) 
00220         {
00221           return (operators_.dot (scalar, p));
00222         }
00224         inline PointOut 
00225         dot (const PointIn& p, const float& scalar)
00226         {
00227           return (operators_.dot (scalar, p));
00228         }
00230         inline PointOut&
00231         plus_assign (PointOut& lhs, const PointOut& rhs)
00232         {
00233           return (operators_.plus_assign (lhs, rhs));
00234         }
00235 
00236       private:
00240         void
00241         initCompute ();
00245         inline void
00246         deinitCompute ()
00247         {
00248           if (borders_policy_ == MIRROR || borders_policy_ == DUPLICATE)
00249             spring_.shrink ();
00250         }
00252         inline void
00253         convolve_rows (const Eigen::ArrayXf& kernel, PointCloudOutPtr& out);
00255         inline void
00256         convolve_cols (const Eigen::ArrayXf& kernel, PointCloudOutPtr& out);
00258         PointOperatorsType operators_;
00260         int borders_policy_;
00262         int convolve_direction_;
00264         PointCloudOutPtr output_;
00266         PointCloudInPtr input_;
00268         int kernel_width_;
00270         int half_width_;
00272         PointCloudSpring spring_;
00273     };
00274   }
00275 }
00276 
00277 #include <pcl/common/impl/convolution.hpp>
00278 
00279 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines