Point Cloud Library (PCL)  1.9.1-dev
organized.h
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 #pragma once
41 
42 #include <pcl/point_cloud.h>
43 #include <pcl/point_types.h>
44 #include <pcl/search/search.h>
45 #include <pcl/common/eigen.h>
46 
47 #include <algorithm>
48 #include <queue>
49 #include <vector>
50 #include <pcl/common/projection_matrix.h>
51 
52 namespace pcl
53 {
54  namespace search
55  {
56  /** \brief OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds.
57  * \author Radu B. Rusu, Julius Kammerl, Suat Gedikli, Koen Buys
58  * \ingroup search
59  */
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 
79  /** \brief Constructor
80  * \param[in] sorted_results whether the results should be return sorted in ascending order on the distances or not.
81  * This applies only for radius search, since knn always returns sorted resutls
82  * \param[in] eps the threshold for the mean-squared-error of the estimation of the projection matrix.
83  * if the MSE is above this value, the point cloud is considered as not from a projective device,
84  * thus organized neighbor search can not be applied on that cloud.
85  * \param[in] pyramid_level the level of the down sampled point cloud to be used for projection matrix estimation
86  */
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 
98  /** \brief Empty deconstructor. */
100 
101  /** \brief Test whether this search-object is valid (input is organized AND from projective device)
102  * User should use this method after setting the input cloud, since setInput just prints an error
103  * if input is not organized or a projection matrix could not be determined.
104  * \return true if the input data is organized and from a projective device, false otherwise
105  */
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_ / std::sqrt (KR_KRT_.coeff (8))) >= (min_f * min_f));
115  }
116 
117  /** \brief Compute the camera matrix
118  * \param[out] camera_matrix the resultant computed camera matrix
119  */
120  void
121  computeCameraMatrix (Eigen::Matrix3f& camera_matrix) const;
122 
123  /** \brief Provide a pointer to the input data set, if user has focal length he must set it before calling this
124  * \param[in] cloud the const boost shared pointer to a PointCloud message
125  * \param[in] indices the const boost shared pointer to PointIndices
126  */
127  void
128  setInputCloud (const PointCloudConstPtr& cloud, const IndicesConstPtr &indices = IndicesConstPtr ()) override
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 
148  /** \brief Search for all neighbors of query point that are within a given radius.
149  * \param[in] p_q the given query point
150  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
151  * \param[out] k_indices the resultant indices of the neighboring points
152  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
153  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
154  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
155  * returned.
156  * \return number of neighbors found in radius
157  */
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 override;
164 
165  /** \brief estimated the projection matrix from the input cloud. */
166  void
168 
169  /** \brief Search for the k-nearest neighbors for a given query point.
170  * \note limiting the maximum search radius (with setMaxDistance) can lead to a significant improvement in search speed
171  * \param[in] p_q the given query point (\ref setInputCloud must be given a-priori!)
172  * \param[in] k the number of neighbors to search for (used only if horizontal and vertical window not given already!)
173  * \param[out] k_indices the resultant point indices (must be resized to \a k beforehand!)
174  * \param[out] k_sqr_distances \note this function does not return distances
175  * \return number of neighbors found
176  * @todo still need to implements this functionality
177  */
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 override;
183 
184  /** \brief projects a point into the image
185  * \param[in] p point in 3D World Coordinate Frame to be projected onto the image plane
186  * \param[out] q the 2D projected point in pixel coordinates (u,v)
187  * @return true if projection is valid, false otherwise
188  */
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 
207  /** \brief test if point given by index is among the k NN in results to the query point.
208  * \param[in] query query point
209  * \param[in] k number of maximum nn interested in
210  * \param[in,out] queue priority queue with k NN
211  * \param[in] index index on point to be tested
212  * \return whether the top element changed or not.
213  */
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] && std::isfinite (point.x))
219  {
220  //float squared_distance = (point.getVector3fMap () - query.getVector3fMap ()).squaredNorm ();
221  float dist_x = point.x - query.x;
222  float dist_y = point.y - query.y;
223  float dist_z = point.z - query.z;
224  float squared_distance = dist_x * dist_x + dist_y * dist_y + dist_z * dist_z;
225  if (queue.size () < k)
226  {
227  queue.push (Entry (index, squared_distance));
228  return queue.size () == k;
229  }
230  else if (queue.top ().distance > squared_distance)
231  {
232  queue.pop ();
233  queue.push (Entry (index, squared_distance));
234  return true; // top element has changed!
235  }
236  }
237  return false;
238  }
239 
240  inline void
241  clipRange (int& begin, int &end, int min, int max) const
242  {
243  begin = std::max (std::min (begin, max), min);
244  end = std::min (std::max (end, min), max);
245  }
246 
247  /** \brief Obtain a search box in 2D from a sphere with a radius in 3D
248  * \param[in] point the query point (sphere center)
249  * \param[in] squared_radius the squared sphere radius
250  * \param[out] minX the min X box coordinate
251  * \param[out] minY the min Y box coordinate
252  * \param[out] maxX the max X box coordinate
253  * \param[out] maxY the max Y box coordinate
254  */
255  void
256  getProjectedRadiusSearchBox (const PointT& point, float squared_radius, unsigned& minX, unsigned& minY,
257  unsigned& maxX, unsigned& maxY) const;
258 
259 
260  /** \brief the projection matrix. Either set by user or calculated by the first / each input cloud */
261  Eigen::Matrix<float, 3, 4, Eigen::RowMajor> projection_matrix_;
262 
263  /** \brief inveser of the left 3x3 projection matrix which is K * R (with K being the camera matrix and R the rotation matrix)*/
264  Eigen::Matrix<float, 3, 3, Eigen::RowMajor> KR_;
265 
266  /** \brief inveser of the left 3x3 projection matrix which is K * R (with K being the camera matrix and R the rotation matrix)*/
267  Eigen::Matrix<float, 3, 3, Eigen::RowMajor> KR_KRT_;
268 
269  /** \brief epsilon value for the MSE of the projection matrix estimation*/
270  const float eps_;
271 
272  /** \brief using only a subsample of points to calculate the projection matrix. pyramid_level_ = use down sampled cloud given by pyramid_level_*/
273  const unsigned pyramid_level_;
274 
275  /** \brief mask, indicating whether the point was in the indices list or not.*/
276  std::vector<unsigned char> mask_;
277  public:
278  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
279  };
280  }
281 }
282 
283 #ifdef PCL_NO_PRECOMPILE
284 #include <pcl/search/impl/organized.hpp>
285 #endif
~OrganizedNeighbor()
Empty deconstructor.
Definition: organized.h:99
bool projectPoint(const PointT &p, pcl::PointXY &q) const
projects a point into the image
Definition: organized.hpp:382
Eigen::Matrix< float, 3, 3, Eigen::RowMajor > KR_KRT_
inveser of the left 3x3 projection matrix which is K * R (with K being the camera matrix and R the ro...
Definition: organized.h:267
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
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.h:215
PointCloudConstPtr input_
Definition: search.h:402
void estimateProjectionMatrix()
estimated the projection matrix from the input cloud.
Definition: organized.hpp:337
Eigen::Matrix< float, 3, 3, Eigen::RowMajor > KR_
inveser of the left 3x3 projection matrix which is K * R (with K being the camera matrix and R the ro...
Definition: organized.h:264
Definition: bfgs.h:9
boost::shared_ptr< pcl::search::OrganizedNeighbor< PointT > > Ptr
Definition: organized.h:72
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: organized.h:67
A 2D point structure representing Euclidean xy coordinates.
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: organized.h:69
bool isValid() const
Test whether this search-object is valid (input is organized AND from projective device) User should ...
Definition: organized.h:107
IndicesConstPtr indices_
Definition: search.h:403
const unsigned pyramid_level_
using only a subsample of points to calculate the projection matrix.
Definition: organized.h:273
std::vector< unsigned char > mask_
mask, indicating whether the point was in the indices list or not.
Definition: organized.h:276
Entry()
Definition: organized.h:196
void computeCameraMatrix(Eigen::Matrix3f &camera_matrix) const
Compute the camera matrix.
Definition: organized.hpp:330
boost::shared_ptr< const pcl::search::OrganizedNeighbor< PointT > > ConstPtr
Definition: organized.h:73
OrganizedNeighbor(bool sorted_results=false, float eps=1e-4f, unsigned pyramid_level=5)
Constructor.
Definition: organized.h:87
Eigen::Matrix< float, 3, 4, Eigen::RowMajor > projection_matrix_
the projection matrix.
Definition: organized.h:261
const float eps_
epsilon value for the MSE of the projection matrix estimation
Definition: organized.h:270
void clipRange(int &begin, int &end, int min, int max) const
Definition: organized.h:241
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input data set, if user has focal length he must set it before calling this...
Definition: organized.h:128
unsigned index
Definition: organized.h:197
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.hpp:273
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:61
bool operator<(const Entry &other) const
Definition: organized.h:201
A point structure representing Euclidean xyz coordinates, and the RGB color.
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: organized.h:70
float distance
Definition: organized.h:198
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 override
Search for all neighbors of query point that are within a given radius.
Definition: organized.hpp:50
Entry(int idx, float dist)
Definition: organized.h:195
int nearestKSearch(const PointT &p_q, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const override
Search for the k-nearest neighbors for a given query point.
Definition: organized.hpp:117
Definition: organized.h:193
pcl::PointCloud< PointT > PointCloud
Definition: organized.h:66
Generic search class.
Definition: search.h:73