NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
organized_neighbor2.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef POINT_CLOUD_COMMON_ORGANIZED_NEIGHBOR2_H_
41 #define POINT_CLOUD_COMMON_ORGANIZED_NEIGHBOR2_H_
42 
43 #include <pcl/point_cloud.h>
44 #include <pcl/point_types.h>
45 #include <pcl/search/search.h>
46 #include <pcl/common/eigen.h>
47 
48 #include <algorithm>
49 #include <queue>
50 #include <vector>
51 
52 // Modified version of pcl organized neighbor to enable preseting the camera intrinsics matrix.
53 namespace pcl {
54 namespace search {
59 template <typename PointT>
60 class OrganizedNeighbor2 : public pcl::search::Search<PointT> {
61  public:
62  // public typedefs
63  typedef pcl::PointCloud<PointT> PointCloud;
64  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
65 
66  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
67  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
68 
69  typedef boost::shared_ptr<pcl::search::OrganizedNeighbor2<PointT> > Ptr;
70  typedef boost::shared_ptr<const pcl::search::OrganizedNeighbor2<PointT> > ConstPtr;
71 
72  using pcl::search::Search<PointT>::indices_;
73  using pcl::search::Search<PointT>::sorted_results_;
74  using pcl::search::Search<PointT>::input_;
75 
80  explicit OrganizedNeighbor2(bool sorted_results = false)
81  : Search<PointT>("OrganizedNeighbor2", sorted_results), mask_() {}
82 
84  virtual ~OrganizedNeighbor2() {}
85 
90  virtual void setInputCloud(const PointCloudConstPtr& cloud, const IndicesConstPtr& indices = IndicesConstPtr()) {
91  input_ = cloud;
92 
93  mask_.resize(input_->size());
94  input_ = cloud;
95  indices_ = indices;
96 
97  if (indices_.get() != NULL && indices_->size() != 0) {
98  mask_.assign(input_->size(), 0);
99  for (std::vector<int>::const_iterator iIt = indices_->begin(); iIt != indices_->end(); ++iIt) mask_[*iIt] = 1;
100  } else {
101  mask_.assign(input_->size(), 1);
102  }
103  }
104 
115  int radiusSearch(const PointT& p_q, double radius, std::vector<int>& k_indices, std::vector<float>& k_sqr_distances,
116  unsigned int max_nn = 0) const;
117 
126  int nearestKSearch(const PointT& p_q, int k, std::vector<int>& k_indices, std::vector<float>& k_sqr_distances) const;
127 
132  Eigen::Vector2f projectPoint(const Eigen::Vector3f& p) const;
133 
134  void setIntrinsicsMatrix(const Eigen::Matrix3d& intrinsics_matrix) {
135  intrinsics_matrix_ = intrinsics_matrix.cast<float>();
137  }
138 
139  protected:
140  struct Entry {
141  Entry(int idx, float dist) : index(idx), distance(dist) {}
142  Entry() : index(0), distance(0) {}
143  unsigned index;
144  float distance;
145 
146  inline bool operator<(const Entry& other) const { return (distance < other.distance); }
147  };
148 
156  inline bool testPoint(const PointT& query, unsigned k, std::priority_queue<Entry>& queue, unsigned index) const {
157  const PointT& point = input_->points[index];
158  if (mask_[index] && pcl_isfinite(point.x)) {
159  // float squared_distance = (point.getVector3fMap () - query.getVector3fMap ()).squaredNorm ();
160  float dist_x = point.x - query.x;
161  float dist_y = point.y - query.y;
162  float dist_z = point.z - query.z;
163  float squared_distance = dist_x * dist_x + dist_y * dist_y + dist_z * dist_z;
164  if (queue.size() < k) {
165  queue.push(Entry(index, squared_distance));
166  } else if (queue.top().distance > squared_distance) {
167  queue.pop();
168  queue.push(Entry(index, squared_distance));
169  return true; // top element has changed!
170  }
171  }
172  return false;
173  }
174 
175  inline void clipRange(int& begin, int& end, int min, int max) const {
176  begin = std::max(std::min(begin, max), min);
177  end = std::min(std::max(end, min), max);
178  }
179 
188  void getProjectedRadiusSearchBox(const PointT& point, float squared_radius, unsigned& minX, unsigned& minY,
189  unsigned& maxX, unsigned& maxY) const;
190 
192  Eigen::Matrix3f intrinsics_matrix_;
193 
194  Eigen::Matrix3f KR_KRT_;
195 
197  std::vector<unsigned char> mask_;
198 
199  public:
200  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
201 };
202 } // namespace search
203 } // namespace pcl
204 
205 #ifdef PCL_NO_PRECOMPILE
207 #endif
208 
209 #endif // POINT_CLOUD_COMMON_ORGANIZED_NEIGHBOR2_H_
pcl::search::OrganizedNeighbor2::ConstPtr
boost::shared_ptr< const pcl::search::OrganizedNeighbor2< PointT > > ConstPtr
Definition: organized_neighbor2.h:70
pcl
Definition: correspondence_rejection_surface_normal2.h:45
pcl::search::OrganizedNeighbor2::Entry::index
unsigned index
Definition: organized_neighbor2.h:143
pcl::search::OrganizedNeighbor2::~OrganizedNeighbor2
virtual ~OrganizedNeighbor2()
Empty deconstructor.
Definition: organized_neighbor2.h:84
pcl::search::OrganizedNeighbor2::PointCloud
pcl::PointCloud< PointT > PointCloud
Definition: organized_neighbor2.h:63
pcl::search::OrganizedNeighbor2::KR_KRT_
Eigen::Matrix3f KR_KRT_
Definition: organized_neighbor2.h:194
pcl::search::OrganizedNeighbor2::intrinsics_matrix_
Eigen::Matrix3f intrinsics_matrix_
the intrinsics matrix.
Definition: organized_neighbor2.h:192
organized_neighbor2_impl.h
pcl::search::OrganizedNeighbor2::clipRange
void clipRange(int &begin, int &end, int min, int max) const
Definition: organized_neighbor2.h:175
pcl::search::OrganizedNeighbor2::Entry::Entry
Entry()
Definition: organized_neighbor2.h:142
pcl::search::OrganizedNeighbor2::PointCloudPtr
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: organized_neighbor2.h:64
pcl::search::OrganizedNeighbor2::Entry::operator<
bool operator<(const Entry &other) const
Definition: organized_neighbor2.h:146
pcl::search::OrganizedNeighbor2::IndicesConstPtr
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: organized_neighbor2.h:67
pcl::search::OrganizedNeighbor2::Entry::Entry
Entry(int idx, float dist)
Definition: organized_neighbor2.h:141
pcl::search::OrganizedNeighbor2::getProjectedRadiusSearchBox
void getProjectedRadiusSearchBox(const PointT &point, float squared_radius, unsigned &minX, unsigned &minY, unsigned &maxX, unsigned &maxY) const
Obtain a search box in 2D from a sphere with a radius in 3D.
Definition: organized_neighbor2_impl.h:242
pcl::search::OrganizedNeighbor2::testPoint
bool testPoint(const PointT &query, unsigned k, std::priority_queue< Entry > &queue, unsigned index) const
test if point given by index is among the k NN in results to the query point.
Definition: organized_neighbor2.h:156
pcl::search::OrganizedNeighbor2::setIntrinsicsMatrix
void setIntrinsicsMatrix(const Eigen::Matrix3d &intrinsics_matrix)
Definition: organized_neighbor2.h:134
pcl::search::OrganizedNeighbor2::mask_
std::vector< unsigned char > mask_
mask, indicating whether the point was in the indices list or not.
Definition: organized_neighbor2.h:197
pcl::search::OrganizedNeighbor2::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr())
Provide a pointer to the input data set, if user has focal length he must set it before calling this.
Definition: organized_neighbor2.h:90
pcl::search::OrganizedNeighbor2::PointCloudConstPtr
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: organized_neighbor2.h:66
pcl::search::OrganizedNeighbor2::OrganizedNeighbor2
OrganizedNeighbor2(bool sorted_results=false)
Constructor.
Definition: organized_neighbor2.h:80
pcl::search::OrganizedNeighbor2::nearestKSearch
int nearestKSearch(const PointT &p_q, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const
Search for the k-nearest neighbors for a given query point.
Definition: organized_neighbor2_impl.h:113
pcl::search::OrganizedNeighbor2::Entry
Definition: organized_neighbor2.h:140
pcl::search::OrganizedNeighbor2::radiusSearch
int radiusSearch(const PointT &p_q, double radius, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const
Search for all neighbors of query point that are within a given radius.
Definition: organized_neighbor2_impl.h:55
pcl::search::OrganizedNeighbor2::Ptr
boost::shared_ptr< pcl::search::OrganizedNeighbor2< PointT > > Ptr
Definition: organized_neighbor2.h:69
pcl::search::OrganizedNeighbor2
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds.
Definition: organized_neighbor2.h:60
pcl::search::OrganizedNeighbor2::Entry::distance
float distance
Definition: organized_neighbor2.h:144
pcl::search::OrganizedNeighbor2::projectPoint
Eigen::Vector2f projectPoint(const Eigen::Vector3f &p) const
projects a point into the image
Definition: organized_neighbor2_impl.h:287