Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
brute_force.hpp
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  */
00037 
00038 #ifndef PCL_SEARCH_IMPL_BRUTE_FORCE_SEARCH_H_
00039 #define PCL_SEARCH_IMPL_BRUTE_FORCE_SEARCH_H_
00040 
00041 #include "pcl/search/brute_force.h"
00042 #include <algorithm>
00043 
00044 template <typename PointT> float
00045 pcl::search::BruteForce<PointT>
00046 ::getDistSqr (const PointT& point1, const PointT& point2) const
00047 {
00048   return ((point1.x - point2.x) * (point1.x - point2.x) +
00049           (point1.y - point2.y) * (point1.y - point2.y) +
00050           (point1.z - point2.z) * (point1.z - point2.z) ) ;
00051 }
00052 
00054 
00055 template <typename PointT> int
00056 pcl::search::BruteForce<PointT>
00057 ::nearestKSearch (const PointT& point, int k, std::vector<int>& k_indices, std::vector<float>& k_distances)
00058 {
00059   k_indices.clear ();
00060   k_indices.reserve (k);
00061   k_distances.clear ();
00062   k_distances.reserve (k);
00063 
00064   std::vector<Entry> result;
00065   result.reserve (k + 1);
00066   const PointCloud& cloud = *cloud_;
00067   if (indices_ != NULL)
00068   {
00069     const std::vector<int>& indices = *indices_;
00070     Entry entry;
00071     // fill up queue with k elements
00072     for (entry.index = 0; entry.index < std::min ((unsigned) k, (unsigned) indices.size ()); ++entry.index)
00073     {
00074       result.push_back (Entry (indices[entry.index], getDistSqr (cloud[indices[entry.index]], point)));
00075     }
00076     // sort them
00077     std::sort (result.begin (), result.end ());
00078 
00079     // add the rest
00080     for (; entry.index < indices.size (); ++entry.index)
00081     {
00082       entry.distance = getDistSqr (cloud[indices[entry.index]], point);
00083       typename std::vector<Entry>::iterator it = std::upper_bound (result.begin (), result.end (), entry);
00084       if (it != result.end ())
00085       {
00086         result.insert ( it, entry );
00087         result.pop_back (); // remove the largest element
00088       }
00089     }
00090   }
00091   else
00092   {
00093     Entry entry;
00094     // fill up queue with k elements
00095     for (entry.index = 0; entry.index < std::min ((unsigned) k, (unsigned) cloud.size ()); ++entry.index)
00096     {
00097       entry.distance = getDistSqr (cloud[entry.index], point);
00098       result.push_back (entry);
00099     }
00100     // sort them
00101     std::sort (result.begin (), result.end ());
00102 
00103     // add the rest
00104     for (; entry.index < cloud.size (); ++entry.index)
00105     {
00106       entry.distance = getDistSqr (cloud[entry.index], point);
00107       typename std::vector<Entry>::iterator it = std::upper_bound (result.begin (), result.end (), entry);
00108       if (it != result.end ())
00109       {
00110         result.insert ( it, entry );
00111         result.pop_back (); // remove the largest element
00112       }
00113     }
00114   }
00115 
00116   for (typename std::vector<Entry>::const_iterator rIt = result.begin (); rIt != result.end (); ++rIt)
00117   {
00118     k_indices.push_back (rIt->index);
00119     k_distances.push_back (rIt->distance);
00120   }
00121   return result.size ();
00122 }
00123 
00124 template <typename PointT> int
00125 pcl::search::BruteForce<PointT>
00126 ::radiusSearch (const PointT& point, double radius, std::vector<int> &k_indices,
00127                 std::vector<float> &k_sqr_distances, int max_nn) const
00128 {
00129   k_indices.clear ();
00130   k_sqr_distances.clear ();
00131 
00132   radius *= radius;
00133   
00134   int reserve = max_nn;
00135   if (reserve < 0)
00136   {
00137     if (indices_ != NULL)
00138       reserve = std::min (indices_->size (), cloud_->size ());
00139     else
00140       reserve = cloud_->size ();
00141   }
00142   k_indices.reserve (reserve);
00143   k_sqr_distances.reserve (reserve);
00144 
00145   std::vector<Entry> result;
00146   result.reserve (reserve);
00147   const PointCloud& cloud = *cloud_;
00148   if (indices_ != NULL)
00149   {
00150     const std::vector<int>& indices = *indices_;
00151     Entry entry;
00152 
00153     // add the rest
00154     for (entry.index = 0; entry.index < indices.size (); ++entry.index)
00155     {
00156       entry.distance = getDistSqr (cloud[indices[entry.index]], point);
00157       if (entry.distance <= radius)
00158       {
00159         result.push_back (entry);
00160         if ((int) result.size () == max_nn) // never true if max_nn = -1
00161           break;
00162       }
00163     }
00164     std::sort (result.begin (), result.end ());
00165   }
00166   else
00167   {
00168     Entry entry;
00169 
00170     for (entry.index = 0; entry.index < cloud.size (); ++entry.index)
00171     {
00172       entry.distance = getDistSqr (cloud[entry.index], point);
00173       if (entry.distance < radius)
00174       {
00175         result.push_back (entry);
00176         if ((int)result.size () == max_nn) // never true if max_nn = -1
00177           break;
00178       }
00179     }
00180     std::sort (result.begin (), result.end ());
00181   }
00182 
00183   for (typename std::vector<Entry>::const_iterator rIt = result.begin (); rIt != result.end (); ++rIt)
00184   {
00185     k_indices.push_back (rIt->index);
00186     k_sqr_distances.push_back (rIt->distance);
00187   }
00188   return result.size ();
00189 }
00190 
00191 #define PCL_INSTANTIATE_BruteForce(T) template class PCL_EXPORTS pcl::search::BruteForce<T>;
00192 
00193 #endif //PCL_SEARCH_IMPL_BRUTE_FORCE_SEARCH_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines