Point Cloud Library (PCL)  1.7.1
correspondence_estimation_organized_projection.hpp
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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
42 #define PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
43 
44 ///////////////////////////////////////////////////////////////////////////////////////////
45 template <typename PointSource, typename PointTarget, typename Scalar> bool
47 {
48  // Set the target_cloud_updated_ variable to true, so that the kd-tree is not built - it is not needed for this class
49  target_cloud_updated_ = false;
51  return (false);
52 
53  /// Check if the target cloud is organized
54  if (!target_->isOrganized ())
55  {
56  PCL_WARN ("[pcl::registration::%s::initCompute] Target cloud is not organized.\n", getClassName ().c_str ());
57  return (false);
58  }
59 
60  /// Put the projection matrix together
61  projection_matrix_ (0, 0) = fx_;
62  projection_matrix_ (1, 1) = fy_;
63  projection_matrix_ (0, 2) = cx_;
64  projection_matrix_ (1, 2) = cy_;
65 
66  return (true);
67 }
68 
69 ///////////////////////////////////////////////////////////////////////////////////////////
70 template <typename PointSource, typename PointTarget, typename Scalar> void
72  pcl::Correspondences &correspondences,
73  double max_distance)
74 {
75  if (!initCompute ())
76  return;
77 
78  correspondences.resize (indices_->size ());
79  size_t c_index = 0;
80 
81  for (std::vector<int>::const_iterator src_it = indices_->begin (); src_it != indices_->end (); ++src_it)
82  {
83  if (isFinite (input_->points[*src_it]))
84  {
85  Eigen::Vector4f p_src (src_to_tgt_transformation_ * input_->points[*src_it].getVector4fMap ());
86  Eigen::Vector3f p_src3 (p_src[0], p_src[1], p_src[2]);
87  Eigen::Vector3f uv (projection_matrix_ * p_src3);
88 
89  /// Check if the point was behind the camera
90  if (uv[2] <= 0)
91  continue;
92 
93  int u = static_cast<int> (uv[0] / uv[2]);
94  int v = static_cast<int> (uv[1] / uv[2]);
95 
96  if (u >= 0 && u < static_cast<int> (target_->width) &&
97  v >= 0 && v < static_cast<int> (target_->height))
98  {
99  const PointTarget &pt_tgt = target_->at (u, v);
100  if (!isFinite (pt_tgt))
101  continue;
102  /// Check if the depth difference is larger than the threshold
103  if (fabs (uv[2] - pt_tgt.z) > depth_threshold_)
104  continue;
105 
106  double dist = (p_src3 - pt_tgt.getVector3fMap ()).norm ();
107  if (dist < max_distance)
108  correspondences[c_index++] = pcl::Correspondence (*src_it, v * target_->width + u, static_cast<float> (dist));
109  }
110  }
111  }
112 
113  correspondences.resize (c_index);
114 }
115 
116 ///////////////////////////////////////////////////////////////////////////////////////////
117 template <typename PointSource, typename PointTarget, typename Scalar> void
119  pcl::Correspondences &correspondences,
120  double max_distance)
121 {
122  // Call the normal determineCorrespondences (...), as doing it both ways will not improve the results
123  determineCorrespondences (correspondences, max_distance);
124 }
125 
126 #endif // PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
127