Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
organized.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: organized.h 3548 2011-12-14 23:42:10Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_SEARCH_ORGANIZED_NEIGHBOR_SEARCH_H_
00041 #define PCL_SEARCH_ORGANIZED_NEIGHBOR_SEARCH_H_
00042 
00043 #include <pcl/point_cloud.h>
00044 #include <pcl/point_types.h>
00045 #include <pcl/search/search.h>
00046 
00047 #include <algorithm>
00048 #include <queue>
00049 #include <vector>
00050 
00051 namespace pcl
00052 {
00053   namespace search
00054   {
00067     template<typename PointT>
00068     class OrganizedNeighbor : public pcl::search::Search<PointT>
00069     {
00070 
00071       public:
00072         // public typedefs
00073         typedef pcl::PointCloud<PointT> PointCloud;
00074         typedef boost::shared_ptr<PointCloud> PointCloudPtr;
00075 
00076         typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
00077         typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
00078 
00079         typedef boost::shared_ptr<pcl::search::OrganizedNeighbor<PointT> > Ptr;
00080         typedef boost::shared_ptr<const pcl::search::OrganizedNeighbor<PointT> > ConstPtr;
00081 
00083         OrganizedNeighbor ()
00084         {
00085           max_distance_ = std::numeric_limits<double>::max ();
00086           oneOverFocalLength_ = 0.0f; //Indicate if it's not initialised
00087           horizontal_window_ = 0;
00088           vertical_window_ = 0;
00089           radiusLookupTableWidth_ =-1; 
00090           radiusLookupTableHeight_ =-1; 
00091           exactFocalLength_ = 0;      // we haven't estimated it yet or we haven't set it 
00092           precision_ = 0;
00093         }
00094 
00096         ~OrganizedNeighbor () {}
00097 
00101         inline void
00102         setInputCloud (const PointCloudConstPtr &cloud)
00103         {
00104           if (input_ != cloud)
00105             input_ = cloud;
00106 
00107           if (precision_ == 1)
00108           {
00109             if(!exactFocalLength_)
00110             {
00111               estimateFocalLengthFromInputCloud (*cloud);
00112               generateRadiusLookupTable (cloud->width, cloud->height);
00113               exactFocalLength_ = 1;
00114             }
00115           }
00116           else
00117           {
00118             oneOverFocalLength_ = 1.0f;
00119           }
00120         }
00121 
00126         inline void
00127         setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices)
00128         {
00129           if (input_ != cloud)
00130             input_ = cloud;
00131 
00132           indices_ = indices;
00133 
00134           if (precision_ == 1)
00135           {
00136             if (!exactFocalLength_)
00137             {
00138               estimateFocalLengthFromInputCloud (*cloud);
00139               generateRadiusLookupTable (cloud->width, cloud->height);
00140               exactFocalLength_ = 1;
00141             }
00142           }
00143           else
00144             oneOverFocalLength_ = 1.0f;
00145         }
00146 
00150         PointCloudConstPtr 
00151         getInputCloud () { return input_; }
00152 
00156         inline IndicesConstPtr const 
00157         getIndices () { return (indices_); }
00158 
00163         inline void 
00164         setPrecision (int precision) { precision_ = precision; }
00165 
00167         inline int 
00168         getPrecision () { return (precision_); }
00169 
00173         inline void 
00174         setOneOverFocalLength (double oneOverFocalLength) 
00175         {
00176           oneOverFocalLength_ = oneOverFocalLength;
00177           exactFocalLength_ = 1;
00178         }
00179 
00181         inline double 
00182         getOneOverFocalLength () { return (oneOverFocalLength_); }
00183 
00185         inline double 
00186         getMaxDistance () const { return (max_distance_); }
00187 
00191         inline void 
00192         setMaxDistance (double max_dist) { max_distance_ = max_dist; }
00193 
00198         inline void
00199         setSearchWindow (int horizontal, int vertical)
00200         {
00201           horizontal_window_ = horizontal;
00202           vertical_window_ = vertical;
00203         }
00204 
00208         void
00209         setSearchWindowAsK (int k);
00210 
00212         int 
00213         getHorizontalSearchWindow () const { return (horizontal_window_); }
00214 
00216         int 
00217         getVerticalSearchWindow () const { return (vertical_window_); }
00218 
00228         inline int
00229         radiusSearch (const PointCloud& cloud, 
00230                       int index, 
00231                       double radius, 
00232                       std::vector<int>& k_indices,
00233                       std::vector<float>& k_sqr_distances, 
00234                       int max_nn = std::numeric_limits<int>::max ())
00235         {
00236           return (radiusSearch (cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
00237         }
00238 
00248         int
00249         radiusSearch (const PointCloudConstPtr &cloud, 
00250                       int index, 
00251                       double radius,
00252                       std::vector<int> &k_indices, 
00253                       std::vector<float> &k_sqr_distances,
00254                       int max_nn = std::numeric_limits<int>::max ());
00255 
00265         int
00266         radiusSearch (int index, 
00267                       const double radius, 
00268                       std::vector<int> &k_indices,
00269                       std::vector<float> &k_sqr_distances, 
00270                       int max_nn = std::numeric_limits<int>::max ()) const;
00271 
00280         int
00281         radiusSearch (const PointT &p_q, 
00282                       const double radius, 
00283                       std::vector<int> &k_indices,
00284                       std::vector<float> &k_sqr_distances, 
00285                       int max_nn = std::numeric_limits<int>::max ()) const;
00286 
00291         double
00292         estimateFocalLengthFromInputCloud (const pcl::PointCloud<PointT> &cloud);
00293 
00301         int
00302         exactNearestKSearch (const PointT &p_q,
00303                              int k, 
00304                              std::vector<int> &k_indices,
00305                              std::vector<float> &k_sqr_distances);
00306 
00315         inline int
00316         exactNearestKSearch (int index, 
00317                              int k, 
00318                              std::vector<int> &k_indices, 
00319                              std::vector<float> &k_sqr_distances);
00320 
00329         inline int
00330         exactNearestKSearch (const pcl::PointCloud<PointT> &cloud, 
00331                              int index, 
00332                              int k, 
00333                              std::vector<int> &k_indices, 
00334                              std::vector<float> &k_sqr_distances);
00335 
00345         int
00346         nearestKSearch (const PointT &p_q,
00347                         int k, 
00348                         std::vector<int> &k_indices,
00349                         std::vector<float> &k_sqr_distances)
00350         {
00351           PCL_ERROR ("[pcl::search::OrganizedNeighbor::approxNearestKSearch] Method not implemented!\n");
00352           return (0);
00353         }
00354 
00364         int
00365         nearestKSearch (int index, int k, std::vector<int> &k_indices, std::vector<float> &k_sqr_distances);
00366 
00376         int
00377         nearestKSearch (const pcl::PointCloud<PointT> &cloud, int index, int k, 
00378                         std::vector<int> &k_indices, std::vector<float> &k_sqr_distances);
00379 
00380       protected:
00385         class RadiusSearchLoopkupEntry
00386         {
00387           public:
00389             RadiusSearchLoopkupEntry () {}
00390 
00392             virtual ~RadiusSearchLoopkupEntry () {}
00393 
00398             void
00399             defineShiftedSearchPoint (int x_shift, int y_shift)
00400             {
00401               x_diff_ = x_shift;
00402               y_diff_ = y_shift;
00403 
00404               squared_distance_ = x_diff_ * x_diff_ + y_diff_ * y_diff_;
00405             }
00406 
00408             bool
00409             operator< (const RadiusSearchLoopkupEntry& rhs) const
00410             {
00411               return (this->squared_distance_ < rhs.squared_distance_);
00412             }
00413 
00414             // Public globals
00415             int x_diff_;int y_diff_;int squared_distance_;
00416         };
00417 
00422         class NearestNeighborCandidate
00423         {
00424           public:
00426             NearestNeighborCandidate () {}
00427 
00429             virtual ~NearestNeighborCandidate () {}
00430 
00432             bool
00433             operator< (const NearestNeighborCandidate& rhs) const
00434             {
00435               return (this->squared_distance_ < rhs.squared_distance_);
00436             }
00437 
00438             // Public globals
00439             int index_;
00440             double squared_distance_;
00441         };
00442 
00447         const PointT&
00448         getPointByIndex (const unsigned int index) const;
00449 
00455         void
00456         generateRadiusLookupTable (unsigned int width, unsigned int height);
00457 
00463         inline void
00464         pointPlaneProjection (const PointT& point, int& xpos, int& ypos) const
00465         {
00466           xpos = (int)pcl_round (point.x / (point.z * oneOverFocalLength_));
00467           ypos = (int)pcl_round (point.y / (point.z * oneOverFocalLength_));
00468         }
00469 
00478         void
00479         getProjectedRadiusSearchBox (const PointT& point, double squared_radius, int& minX, int& minY,
00480                                      int& maxX, int& maxY) const;
00481 
00482 
00484         virtual std::string 
00485         getName () const { return ("Organized_Neighbor_Search"); }
00486 
00488         int horizontal_window_;
00490         int vertical_window_;
00492         int min_pts_;
00493 
00495         PointCloudConstPtr input_;
00497         IndicesConstPtr indices_;
00498 
00500         double max_distance_;
00501 
00503         double oneOverFocalLength_;
00504 
00506         std::vector<RadiusSearchLoopkupEntry> radiusSearchLookup_;
00507 
00508         int radiusLookupTableWidth_;
00509         int radiusLookupTableHeight_;
00510 
00512         int precision_;
00513 
00515         bool exactFocalLength_;
00516     };
00517   }
00518 }
00519 
00520 #endif
00521 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines