Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
organized.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 Willow Garage, Inc. 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: organized.h 5622 2012-04-25 14:17:31Z nerei $
37  *
38  */
39 
40 #ifndef PCL_SEARCH_ORGANIZED_NEIGHBOR_SEARCH_H_
41 #define PCL_SEARCH_ORGANIZED_NEIGHBOR_SEARCH_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 namespace pcl
53 {
54  namespace search
55  {
60  template<typename PointT>
61  class OrganizedNeighbor : public pcl::search::Search<PointT>
62  {
63 
64  public:
65  // public typedefs
67  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
68 
69  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
70  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
71 
72  typedef boost::shared_ptr<pcl::search::OrganizedNeighbor<PointT> > Ptr;
73  typedef boost::shared_ptr<const pcl::search::OrganizedNeighbor<PointT> > ConstPtr;
74 
78 
87  OrganizedNeighbor (bool sorted_results = false, float eps = 1e-4f, unsigned pyramid_level = 5)
88  : Search<PointT> ("OrganizedNeighbor", sorted_results)
89  , projection_matrix_ (Eigen::Matrix<float, 3, 4, Eigen::RowMajor>::Zero ())
90  , KR_ (Eigen::Matrix<float, 3, 3, Eigen::RowMajor>::Zero ())
91  , KR_KRT_ (Eigen::Matrix<float, 3, 3, Eigen::RowMajor>::Zero ())
92  , eps_ (eps)
93  , pyramid_level_ (pyramid_level)
94  , mask_ ()
95  {
96  }
97 
99  virtual ~OrganizedNeighbor () {}
100 
106  bool
107  isValid () const
108  {
109  // determinant (KR) = determinant (K) * determinant (R) = determinant (K) = f_x * f_y.
110  // If we expect at max an opening angle of 170degree in x-direction -> f_x = 2.0 * width / tan (85 degree);
111  // 2 * tan (85 degree) ~ 22.86
112  float min_f = 0.043744332f * static_cast<float>(input_->width);
113  //std::cout << "isValid: " << determinant3x3Matrix<Eigen::Matrix3f> (KR_ / sqrt (KR_KRT_.coeff (8))) << " >= " << (min_f * min_f) << std::endl;
114  return (determinant3x3Matrix<Eigen::Matrix3f> (KR_ / sqrt (KR_KRT_.coeff (8))) >= (min_f * min_f));
115  }
116 
120  void
121  computeCameraMatrix (Eigen::Matrix3f& camera_matrix) const;
122 
127  virtual void
129  {
130  input_ = cloud;
131 
132  mask_.resize (input_->size ());
133  input_ = cloud;
134  indices_ = indices;
135 
136  if (indices_.get () != NULL && indices_->size () != 0)
137  {
138  mask_.assign (input_->size (), 0);
139  for (std::vector<int>::const_iterator iIt = indices_->begin (); iIt != indices_->end (); ++iIt)
140  mask_[*iIt] = 1;
141  }
142  else
143  mask_.assign (input_->size (), 1);
144 
146  }
147 
158  int
159  radiusSearch (const PointT &p_q,
160  double radius,
161  std::vector<int> &k_indices,
162  std::vector<float> &k_sqr_distances,
163  unsigned int max_nn = 0) const;
164 
166  void
168 
178  int
179  nearestKSearch (const PointT &p_q,
180  int k,
181  std::vector<int> &k_indices,
182  std::vector<float> &k_sqr_distances) const;
183 
189  bool projectPoint (const PointT& p, pcl::PointXY& q) const;
190 
191  protected:
192 
193  struct Entry
194  {
195  Entry (int idx, float dist) : index (idx), distance (dist) {}
196  Entry () : index (0), distance (0) {}
197  unsigned index;
198  float distance;
199 
200  inline bool
201  operator < (const Entry& other) const
202  {
203  return (distance < other.distance);
204  }
205  };
206 
214  inline bool
215  testPoint (const PointT& query, unsigned k, std::priority_queue<Entry>& queue, unsigned index) const
216  {
217  const PointT& point = input_->points [index];
218  if (mask_ [index] && pcl_isfinite (point.x))
219  {
220  float squared_distance = (point.getVector3fMap () - query.getVector3fMap ()).squaredNorm ();
221  if (queue.size () < k)
222  queue.push (Entry (index, squared_distance));
223  else if (queue.top ().distance > squared_distance)
224  {
225  queue.pop ();
226  queue.push (Entry (index, squared_distance));
227  return true; // top element has changed!
228  }
229  }
230  return false;
231  }
232 
233  inline void
234  clipRange (int& begin, int &end, int min, int max) const
235  {
236  begin = std::max (std::min (begin, max), min);
237  end = std::min (std::max (end, min), max);
238  }
239 
248  void
249  getProjectedRadiusSearchBox (const PointT& point, float squared_radius, unsigned& minX, unsigned& minY,
250  unsigned& maxX, unsigned& maxY) const;
251 
252 
254  template <typename MatrixType> void
255  makeSymmetric (MatrixType& matrix, bool use_upper_triangular = true) const;
256 
258  Eigen::Matrix<float, 3, 4, Eigen::RowMajor> projection_matrix_;
259 
261  Eigen::Matrix<float, 3, 3, Eigen::RowMajor> KR_;
262 
264  Eigen::Matrix<float, 3, 3, Eigen::RowMajor> KR_KRT_;
265 
267  const float eps_;
268 
270  const unsigned pyramid_level_;
271 
273  std::vector<unsigned char> mask_;
274  public:
275  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
276  };
277  }
278 }
279 
280 #endif
281