|
Point Cloud Library (PCL)
1.4.0
|
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: octree_search.h 3749 2011-12-31 22:58:01Z rusu $ 00037 */ 00038 00039 #ifndef PCL_OCTREE_SEARCH_H_ 00040 #define PCL_OCTREE_SEARCH_H_ 00041 00042 #include <pcl/point_cloud.h> 00043 #include <pcl/point_types.h> 00044 00045 #include "octree_pointcloud.h" 00046 00047 #include "octree_base.h" 00048 #include "octree2buf_base.h" 00049 00050 #include "octree_nodes.h" 00051 00052 namespace pcl 00053 { 00054 namespace octree 00055 { 00062 template<typename PointT, typename LeafT = OctreeLeafDataTVector<int> , typename OctreeT = OctreeBase<int, LeafT> > 00063 class OctreePointCloudSearch : public OctreePointCloud<PointT, LeafT, OctreeT> 00064 { 00065 public: 00066 // public typedefs 00067 typedef boost::shared_ptr<std::vector<int> > IndicesPtr; 00068 typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr; 00069 00070 typedef pcl::PointCloud<PointT> PointCloud; 00071 typedef boost::shared_ptr<PointCloud> PointCloudPtr; 00072 typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr; 00073 00074 // public typedefs for single/double buffering 00075 typedef OctreePointCloudSearch<PointT, LeafT, OctreeBase<int, LeafT> > SingleBuffer; 00076 typedef OctreePointCloudSearch<PointT, LeafT, Octree2BufBase<int, LeafT> > DoubleBuffer; 00077 typedef OctreePointCloudSearch<PointT, LeafT, OctreeLowMemBase<int, LeafT> > LowMem; 00078 00079 // Boost shared pointers 00080 typedef boost::shared_ptr<OctreePointCloudSearch<PointT, LeafT, OctreeT> > Ptr; 00081 typedef boost::shared_ptr<const OctreePointCloudSearch<PointT, LeafT, OctreeT> > ConstPtr; 00082 00083 // Eigen aligned allocator 00084 typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > AlignedPointTVector; 00085 00086 typedef typename OctreeT::OctreeBranch OctreeBranch; 00087 typedef typename OctreeT::OctreeKey OctreeKey; 00088 typedef typename OctreeT::OctreeLeaf OctreeLeaf; 00089 00093 OctreePointCloudSearch (const double resolution) : 00094 OctreePointCloud<PointT, LeafT, OctreeT> (resolution) 00095 { 00096 } 00097 00099 virtual 00100 ~OctreePointCloudSearch () 00101 { 00102 } 00103 00109 bool 00110 voxelSearch (const PointT& point, std::vector<int>& pointIdx_data); 00111 00117 bool 00118 voxelSearch (const int index, std::vector<int>& pointIdx_data); 00119 00129 inline int 00130 nearestKSearch (const PointCloud &cloud, int index, int k, std::vector<int> &k_indices, 00131 std::vector<float> &k_sqr_distances) 00132 { 00133 return (nearestKSearch (cloud[index], k, k_indices, k_sqr_distances)); 00134 } 00135 00143 int 00144 nearestKSearch (const PointT &p_q, int k, std::vector<int> &k_indices, 00145 std::vector<float> &k_sqr_distances); 00146 00156 int 00157 nearestKSearch (int index, int k, std::vector<int> &k_indices, 00158 std::vector<float> &k_sqr_distances); 00159 00167 inline void 00168 approxNearestSearch (const PointCloud &cloud, int query_index, int &result_index, 00169 float &sqr_distance) 00170 { 00171 return (approxNearestSearch (cloud.points[query_index], result_index, sqr_distance)); 00172 } 00173 00179 void 00180 approxNearestSearch (const PointT &p_q, int &result_index, float &sqr_distance); 00181 00189 void 00190 approxNearestSearch (int query_index, int &result_index, float &sqr_distance); 00191 00201 int 00202 radiusSearch (const PointCloud &cloud, int index, double radius, 00203 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances, 00204 int max_nn = INT_MAX) 00205 { 00206 return (radiusSearch (cloud.points[index], radius, k_indices, k_sqr_distances, max_nn)); 00207 } 00208 00217 int 00218 radiusSearch (const PointT &p_q, const double radius, std::vector<int> &k_indices, 00219 std::vector<float> &k_sqr_distances, int max_nn = INT_MAX) const; 00220 00230 int 00231 radiusSearch (int index, const double radius, std::vector<int> &k_indices, 00232 std::vector<float> &k_sqr_distances, int max_nn = INT_MAX) const; 00233 00240 int 00241 getIntersectedVoxelCenters (Eigen::Vector3f origin, Eigen::Vector3f direction, 00242 AlignedPointTVector &voxelCenterList) const; 00243 00250 int 00251 getIntersectedVoxelIndices (Eigen::Vector3f origin, Eigen::Vector3f direction, 00252 std::vector<int> &k_indices) const; 00253 00254 00255 protected: 00257 // Octree-based search routines & helpers 00259 00263 class prioBranchQueueEntry 00264 { 00265 public: 00267 prioBranchQueueEntry () 00268 { 00269 } 00270 00276 prioBranchQueueEntry (OctreeNode* node, OctreeKey& key, double pointDistance) 00277 { 00278 node = node; 00279 pointDistance = pointDistance; 00280 key = key; 00281 } 00282 00286 bool 00287 operator< (const prioBranchQueueEntry rhs) const 00288 { 00289 return (this->pointDistance > rhs.pointDistance); 00290 } 00291 00293 const OctreeNode* node; 00294 00296 double pointDistance; 00297 00299 OctreeKey key; 00300 }; 00301 00303 00307 class prioPointQueueEntry 00308 { 00309 public: 00310 00312 prioPointQueueEntry () 00313 { 00314 } 00315 00320 prioPointQueueEntry (unsigned int& pointIdx, double pointDistance) 00321 { 00322 pointIdx_ = pointIdx; 00323 pointDistance_ = pointDistance; 00324 } 00325 00329 bool 00330 operator< (const prioPointQueueEntry& rhs) const 00331 { 00332 return (this->pointDistance_ < rhs.pointDistance_); 00333 } 00334 00336 int pointIdx_; 00337 00339 double pointDistance_; 00340 }; 00341 00347 double 00348 pointSquaredDist (const PointT& pointA, const PointT& pointB) const; 00349 00351 // Recursive search routine methods 00353 00364 void 00365 getNeighborsWithinRadiusRecursive (const PointT& point, const double radiusSquared, 00366 const OctreeBranch* node, const OctreeKey& key, 00367 unsigned int treeDepth, std::vector<int>& k_indices, 00368 std::vector<float>& k_sqr_distances, int max_nn) const; 00369 00380 double 00381 getKNearestNeighborRecursive (const PointT& point, unsigned int K, const OctreeBranch* node, 00382 const OctreeKey& key, unsigned int treeDepth, 00383 const double squaredSearchRadius, 00384 std::vector<prioPointQueueEntry>& pointCandidates) const; 00385 00394 void 00395 approxNearestSearchRecursive (const PointT& point, const OctreeBranch* node, const OctreeKey& key, 00396 unsigned int treeDepth, int& result_index, float& sqr_distance); 00397 00413 int 00414 getIntersectedVoxelCentersRecursive (double minX, double minY, double minZ, double maxX, double maxY, 00415 double maxZ, unsigned char a, const OctreeNode* node, 00416 const OctreeKey& key, AlignedPointTVector &voxelCenterList) const; 00417 00433 int 00434 getIntersectedVoxelIndicesRecursive (double minX, double minY, double minZ, 00435 double maxX, double maxY, double maxZ, 00436 unsigned char a, const OctreeNode* node, const OctreeKey& key, 00437 std::vector<int> &k_indices) const; 00438 00450 inline void 00451 initIntersectedVoxel (Eigen::Vector3f &origin, Eigen::Vector3f &direction, 00452 double &minX, double &minY, double &minZ, 00453 double &maxX, double &maxY, double &maxZ, 00454 unsigned char &a) const 00455 { 00456 // Account for division by zero when direction vector is 0.0 00457 const double epsilon = 1e-10; 00458 if (direction.x () == 0.0) 00459 direction.x () = epsilon; 00460 if (direction.y () == 0.0) 00461 direction.y () = epsilon; 00462 if (direction.z () == 0.0) 00463 direction.z () = epsilon; 00464 00465 // Voxel childIdx remapping 00466 a = 0; 00467 00468 // Handle negative axis direction vector 00469 if (direction.x () < 0.0) 00470 { 00471 origin.x () = this->minX_ + this->maxX_ - origin.x (); 00472 direction.x () = -direction.x (); 00473 a |= 4; 00474 } 00475 if (direction.y () < 0.0) 00476 { 00477 origin.y () = this->minY_ + this->maxY_ - origin.y (); 00478 direction.y () = -direction.y (); 00479 a |= 2; 00480 } 00481 if (direction.z () < 0.0) 00482 { 00483 origin.z () = this->minZ_ + this->maxZ_ - origin.z (); 00484 direction.z () = -direction.z (); 00485 a |= 1; 00486 } 00487 minX = (this->minX_ - origin.x ()) / direction.x (); 00488 maxX = (this->maxX_ - origin.x ()) / direction.x (); 00489 minY = (this->minY_ - origin.y ()) / direction.y (); 00490 maxY = (this->maxY_ - origin.y ()) / direction.y (); 00491 minZ = (this->minZ_ - origin.z ()) / direction.z (); 00492 maxZ = (this->maxZ_ - origin.z ()) / direction.z (); 00493 } 00494 00504 inline int 00505 getFirstIntersectedNode (double minX, double minY, double minZ, double midX, double midY, double midZ) const 00506 { 00507 int currNode = 0; 00508 00509 if (minX > minY) 00510 { 00511 if (minX > minZ) 00512 { 00513 // max(minX, minY, minZ) is minX. Entry plane is YZ. 00514 if (midY < minX) 00515 currNode |= 2; 00516 if (midZ < minX) 00517 currNode |= 1; 00518 } 00519 else 00520 { 00521 // max(minX, minY, minZ) is minZ. Entry plane is XY. 00522 if (midX < minZ) 00523 currNode |= 4; 00524 if (midY < minZ) 00525 currNode |= 2; 00526 } 00527 } 00528 else 00529 { 00530 if (minY > minZ) 00531 { 00532 // max(minX, minY, minZ) is minY. Entry plane is XZ. 00533 if (midX < minY) 00534 currNode |= 4; 00535 if (midZ < minY) 00536 currNode |= 1; 00537 } 00538 else 00539 { 00540 // max(minX, minY, minZ) is minZ. Entry plane is XY. 00541 if (midX < minZ) 00542 currNode |= 4; 00543 if (midY < minZ) 00544 currNode |= 2; 00545 } 00546 } 00547 00548 return currNode; 00549 } 00550 00563 inline int 00564 getNextIntersectedNode (double x, double y, double z, int a, int b, int c) const 00565 { 00566 if (x < y) 00567 { 00568 if (x < z) 00569 return a; 00570 else 00571 return c; 00572 } 00573 else 00574 { 00575 if (y < z) 00576 return b; 00577 else 00578 return c; 00579 } 00580 00581 return 0; 00582 } 00583 00584 }; 00585 } 00586 } 00587 00588 #define PCL_INSTANTIATE_OctreePointCloudSearch(T) template class PCL_EXPORTS pcl::octree::OctreePointCloudSearch<T>; 00589 00590 #endif // PCL_OCTREE_SEARCH_H_
1.7.6.1