Point Cloud Library (PCL)  1.5.1
 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 4255 2012-02-05 06:58:59Z rusu $
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_); }        
00107         
00116         inline void
00117         convolve (const Eigen::ArrayXf& h_kernel, 
00118                   const Eigen::ArrayXf& v_kernel,
00119                   PointCloudOutPtr& out)
00120         {
00121           kernel_width_ = h_kernel.size ();
00122           convolve_direction_ = PointCloudSpring::BOTH;
00123           output_ = out;
00124           
00125           try
00126           {
00127             initCompute ();
00128             convolve_rows (h_kernel, output_);
00129             convolve_cols (v_kernel, output_);
00130             deinitCompute ();
00131           }
00132           catch (InitFailedException& e)
00133           {
00134             PCL_THROW_EXCEPTION (InitFailedException,
00135                                  "[pcl::common::Convolution::convolveCols] init failed " << e.what ());
00136           }          
00137         }
00144         inline void
00145         convolveRows (const Eigen::ArrayXf& kernel, PointCloudOutPtr& output)
00146         {
00147           kernel_width_ = kernel.size ();
00148           convolve_direction_ = PointCloudSpring::HORIZONTAL;
00149           output_ = output;
00150           try
00151           {
00152             initCompute ();
00153             convolve_rows (kernel, output_);
00154             deinitCompute ();
00155           }
00156           catch (InitFailedException& e)
00157           {
00158             PCL_THROW_EXCEPTION (InitFailedException,
00159                                  "[pcl::common::Convolution::convolveRows] init failed " << e.what ());
00160           }
00161         }
00168         inline void
00169         convolveCols (const Eigen::ArrayXf& kernel, PointCloudOutPtr& output)
00170         {
00171           kernel_width_ = kernel.size ();
00172           convolve_direction_ = PointCloudSpring::VERTICAL;
00173           output_ = output;
00174           
00175           try
00176           {
00177             initCompute ();
00178             convolve_cols (kernel, output_);
00179             deinitCompute ();
00180           }
00181           catch (InitFailedException& e)
00182           {
00183             PCL_THROW_EXCEPTION (InitFailedException,
00184                                  "[pcl::common::Convolution::convolveCols] init failed " << e.what ());
00185           }          
00186         }
00191         inline void
00192         setInputCloud (const PointCloudInConstPtr& cloud)
00193         {
00194           input_.reset (new PointCloudIn (*cloud));
00195         }
00201         inline void
00202         setInputCloud (const PointCloudInPtr& cloud)
00203         {
00204           input_ = cloud;
00205         }
00207         inline PointOut 
00208         add (const PointIn& lhs, const PointIn& rhs)
00209         {
00210           return (operators_.add (lhs, rhs));
00211         }
00213         inline PointOut
00214         minus (const PointIn& lhs, const PointIn& rhs)
00215         {
00216           return (operators_.minus (lhs, rhs));
00217         }
00219         inline PointOut 
00220         dot (const float& scalar, const PointIn& p) 
00221         {
00222           return (operators_.dot (scalar, p));
00223         }
00225         inline PointOut 
00226         dot (const PointIn& p, const float& scalar)
00227         {
00228           return (operators_.dot (scalar, p));
00229         }
00231         inline PointOut&
00232         plus_assign (PointOut& lhs, const PointOut& rhs)
00233         {
00234           return (operators_.plus_assign (lhs, rhs));
00235         }
00236 
00237       private:
00241         void
00242         initCompute ();
00246         inline void
00247         deinitCompute ()
00248         {
00249           if (borders_policy_ == MIRROR || borders_policy_ == DUPLICATE)
00250             spring_.shrink ();
00251         }
00253         inline void
00254         convolve_rows (const Eigen::ArrayXf& kernel, PointCloudOutPtr& out);
00256         inline void
00257         convolve_cols (const Eigen::ArrayXf& kernel, PointCloudOutPtr& out);
00259         PointOperatorsType operators_;
00261         int borders_policy_;
00263         int convolve_direction_;
00265         PointCloudOutPtr output_;
00267         PointCloudInPtr input_;
00269         int kernel_width_;
00271         int half_width_;
00273         PointCloudSpring spring_;
00274     };
00275   }
00276 }
00277 
00278 #include <pcl/common/impl/convolution.hpp>
00279 
00280 #endif