Point Cloud Library (PCL)  1.11.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 
42 #ifndef PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
43 #define PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
44 
45 
46 namespace pcl
47 {
48 
49 namespace registration
50 {
51 
52 template <typename PointSource, typename PointTarget, typename Scalar> bool
54 {
55  // Set the target_cloud_updated_ variable to true, so that the kd-tree is not built - it is not needed for this class
56  target_cloud_updated_ = false;
58  return (false);
59 
60  /// Check if the target cloud is organized
61  if (!target_->isOrganized ())
62  {
63  PCL_WARN ("[pcl::registration::%s::initCompute] Target cloud is not organized.\n", getClassName ().c_str ());
64  return (false);
65  }
66 
67  /// Put the projection matrix together
68  projection_matrix_ (0, 0) = fx_;
69  projection_matrix_ (1, 1) = fy_;
70  projection_matrix_ (0, 2) = cx_;
71  projection_matrix_ (1, 2) = cy_;
72 
73  return (true);
74 }
75 
76 
77 template <typename PointSource, typename PointTarget, typename Scalar> void
79  pcl::Correspondences &correspondences,
80  double max_distance)
81 {
82  if (!initCompute ())
83  return;
84 
85  correspondences.resize (indices_->size ());
86  std::size_t c_index = 0;
87 
88  for (std::vector<int>::const_iterator src_it = indices_->begin (); src_it != indices_->end (); ++src_it)
89  {
90  if (isFinite ((*input_)[*src_it]))
91  {
92  Eigen::Vector4f p_src (src_to_tgt_transformation_ * (*input_)[*src_it].getVector4fMap ());
93  Eigen::Vector3f p_src3 (p_src[0], p_src[1], p_src[2]);
94  Eigen::Vector3f uv (projection_matrix_ * p_src3);
95 
96  /// Check if the point was behind the camera
97  if (uv[2] <= 0)
98  continue;
99 
100  int u = static_cast<int> (uv[0] / uv[2]);
101  int v = static_cast<int> (uv[1] / uv[2]);
102 
103  if (u >= 0 && u < static_cast<int> (target_->width) &&
104  v >= 0 && v < static_cast<int> (target_->height))
105  {
106  const PointTarget &pt_tgt = target_->at (u, v);
107  if (!isFinite (pt_tgt))
108  continue;
109  /// Check if the depth difference is larger than the threshold
110  if (std::abs (uv[2] - pt_tgt.z) > depth_threshold_)
111  continue;
112 
113  double dist = (p_src3 - pt_tgt.getVector3fMap ()).norm ();
114  if (dist < max_distance)
115  correspondences[c_index++] = pcl::Correspondence (*src_it, v * target_->width + u, static_cast<float> (dist));
116  }
117  }
118  }
119 
120  correspondences.resize (c_index);
121 }
122 
123 
124 template <typename PointSource, typename PointTarget, typename Scalar> void
126  pcl::Correspondences &correspondences,
127  double max_distance)
128 {
129  // Call the normal determineCorrespondences (...), as doing it both ways will not improve the results
130  determineCorrespondences (correspondences, max_distance);
131 }
132 
133 } // namespace registration
134 } // namespace pcl
135 
136 #endif // PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_ORGANIZED_PROJECTION_IMPL_HPP_
137 
Abstract CorrespondenceEstimationBase class.
void determineCorrespondences(Correspondences &correspondences, double max_distance)
Computes the correspondences, applying a maximum Euclidean distance threshold.
void determineReciprocalCorrespondences(Correspondences &correspondences, double max_distance)
Computes the correspondences, applying a maximum Euclidean distance threshold.
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Correspondence represents a match between two entities (e.g., points, descriptors,...