Point Cloud Library (PCL)  1.11.1
transformation_validation_euclidean.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  * 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 #pragma once
42 
43 #include <pcl/memory.h>
44 #include <pcl/pcl_macros.h>
45 #include <pcl/point_representation.h>
46 #include <pcl/search/kdtree.h>
47 #include <pcl/kdtree/kdtree.h>
48 #include <pcl/registration/transformation_validation.h>
49 
50 namespace pcl
51 {
52  namespace registration
53  {
54  /** \brief TransformationValidationEuclidean computes an L2SQR norm between a source and target
55  * dataset.
56  *
57  * To prevent points with bad correspondences to contribute to the overall score, the class also
58  * accepts a maximum_range parameter given via \ref setMaxRange that is used as a cutoff value for
59  * nearest neighbor distance comparisons.
60  *
61  * The output score is normalized with respect to the number of valid correspondences found.
62  *
63  * Usage example:
64  * \code
65  * pcl::TransformationValidationEuclidean<pcl::PointXYZ, pcl::PointXYZ> tve;
66  * tve.setMaxRange (0.01); // 1cm
67  * double score = tve.validateTransformation (source, target, transformation);
68  * \endcode
69  *
70  * \note The class is templated on the source and target point types as well as on the output scalar of the transformation matrix (i.e., float or double). Default: float.
71  * \author Radu B. Rusu
72  * \ingroup registration
73  */
74  template <typename PointSource, typename PointTarget, typename Scalar = float>
76  {
77  public:
79 
80  using Ptr = shared_ptr<TransformationValidation<PointSource, PointTarget, Scalar> >;
81  using ConstPtr = shared_ptr<const TransformationValidation<PointSource, PointTarget, Scalar> >;
82 
84  using KdTreePtr = typename KdTree::Ptr;
85 
87 
90 
91  /** \brief Constructor.
92  * Sets the \a max_range parameter to double::max, \a threshold_ to NaN
93  * and initializes the internal search \a tree to a FLANN kd-tree.
94  */
96  max_range_ (std::numeric_limits<double>::max ()),
97  threshold_ (std::numeric_limits<double>::quiet_NaN ()),
98  tree_ (new pcl::search::KdTree<PointTarget>),
99  force_no_recompute_ (false)
100  {
101  }
102 
104 
105  /** \brief Set the maximum allowable distance between a point and its correspondence in the
106  * target in order for a correspondence to be considered \a valid. Default: double::max.
107  * \param[in] max_range the new maximum allowable distance
108  */
109  inline void
110  setMaxRange (double max_range)
111  {
112  max_range_ = max_range;
113  }
114 
115  /** \brief Get the maximum allowable distance between a point and its
116  * correspondence, as set by the user.
117  */
118  inline double
120  {
121  return (max_range_);
122  }
123 
124 
125  /** \brief Provide a pointer to the search object used to find correspondences in
126  * the target cloud.
127  * \param[in] tree a pointer to the spatial search object.
128  * \param[in] force_no_recompute If set to true, this tree will NEVER be
129  * recomputed, regardless of calls to setInputTarget. Only use if you are
130  * confident that the tree will be set correctly.
131  */
132  inline void
134  bool force_no_recompute = false)
135  {
136  tree_ = tree;
137  if (force_no_recompute)
138  {
139  force_no_recompute_ = true;
140  }
141  }
142 
143  /** \brief Set a threshold for which a specific transformation is considered valid.
144  *
145  * \note Since we're using MSE (Mean Squared Error) as a metric, the threshold
146  * represents the mean Euclidean distance threshold over all nearest neighbors
147  * up to max_range.
148  *
149  * \param[in] threshold the threshold for which a transformation is vali
150  */
151  inline void
152  setThreshold (double threshold)
153  {
154  threshold_ = threshold;
155  }
156 
157  /** \brief Get the threshold for which a specific transformation is valid. */
158  inline double
160  {
161  return (threshold_);
162  }
163 
164  /** \brief Validate the given transformation with respect to the input cloud data, and return a score.
165  *
166  * \param[in] cloud_src the source point cloud dataset
167  * \param[in] cloud_tgt the target point cloud dataset
168  * \param[out] transformation_matrix the resultant transformation matrix
169  *
170  * \return the score or confidence measure for the given
171  * transformation_matrix with respect to the input data
172  */
173  double
175  const PointCloudSourceConstPtr &cloud_src,
176  const PointCloudTargetConstPtr &cloud_tgt,
177  const Matrix4 &transformation_matrix) const;
178 
179  /** \brief Comparator function for deciding which score is better after running the
180  * validation on multiple transforms.
181  *
182  * \param[in] score1 the first value
183  * \param[in] score2 the second value
184  *
185  * \return true if score1 is better than score2
186  */
187  virtual bool
188  operator() (const double &score1, const double &score2) const
189  {
190  return (score1 < score2);
191  }
192 
193  /** \brief Check if the score is valid for a specific transformation.
194  *
195  * \param[in] cloud_src the source point cloud dataset
196  * \param[in] cloud_tgt the target point cloud dataset
197  * \param[out] transformation_matrix the transformation matrix
198  *
199  * \return true if the transformation is valid, false otherwise.
200  */
201  virtual bool
203  const PointCloudSourceConstPtr &cloud_src,
204  const PointCloudTargetConstPtr &cloud_tgt,
205  const Matrix4 &transformation_matrix) const
206  {
207  if (std::isnan (threshold_))
208  {
209  PCL_ERROR ("[pcl::TransformationValidationEuclidean::isValid] Threshold not set! Please use setThreshold () before continuing.");
210  return (false);
211  }
212 
213  return (validateTransformation (cloud_src, cloud_tgt, transformation_matrix) < threshold_);
214  }
215 
216  protected:
217  /** \brief The maximum allowable distance between a point and its correspondence in the target
218  * in order for a correspondence to be considered \a valid. Default: double::max.
219  */
220  double max_range_;
221 
222  /** \brief The threshold for which a specific transformation is valid.
223  * Set to NaN by default, as we must require the user to set it.
224  */
225  double threshold_;
226 
227  /** \brief A pointer to the spatial search object. */
229 
230  /** \brief A flag which, if set, means the tree operating on the target cloud
231  * will never be recomputed*/
233 
234 
235  /** \brief Internal point representation uses only 3D coordinates for L2 */
237  {
240  public:
241  using Ptr = shared_ptr<MyPointRepresentation>;
242  using ConstPtr = shared_ptr<const MyPointRepresentation>;
243 
245  {
246  nr_dimensions_ = 3;
247  trivial_ = true;
248  }
249 
250  /** \brief Empty destructor */
252 
253  virtual void
254  copyToFloatArray (const PointTarget &p, float * out) const
255  {
256  out[0] = p.x;
257  out[1] = p.y;
258  out[2] = p.z;
259  }
260  };
261 
262  public:
264  };
265  }
266 }
267 
268 #include <pcl/registration/impl/transformation_validation_euclidean.hpp>
shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:75
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
Defines functions, macros and traits for allocating and using memory.
KdTreePtr tree_
A pointer to the spatial search object.
bool force_no_recompute_
A flag which, if set, means the tree operating on the target cloud will never be recomputed.
typename PointRepresentation< PointT >::ConstPtr PointRepresentationConstPtr
Definition: kdtree.h:80
double max_range_
The maximum allowable distance between a point and its correspondence in the target in order for a co...
double validateTransformation(const PointCloudSourceConstPtr &cloud_src, const PointCloudTargetConstPtr &cloud_tgt, const Matrix4 &transformation_matrix) const
Validate the given transformation with respect to the input cloud data, and return a score...
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
typename KdTree::PointRepresentationConstPtr PointRepresentationConstPtr
double getMaxRange()
Get the maximum allowable distance between a point and its correspondence, as set by the user...
void setMaxRange(double max_range)
Set the maximum allowable distance between a point and its correspondence in the target in order for ...
void setThreshold(double threshold)
Set a threshold for which a specific transformation is considered valid.
PointRepresentation provides a set of methods for converting a point structs/object into an n-dimensi...
typename TransformationValidation< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
void setSearchMethodTarget(const KdTreePtr &tree, bool force_no_recompute=false)
Provide a pointer to the search object used to find correspondences in the target cloud...
int nr_dimensions_
The number of dimensions in this point&#39;s vector (i.e.
typename TransformationValidation< PointSource, PointTarget >::PointCloudSourceConstPtr PointCloudSourceConstPtr
shared_ptr< const TransformationValidation< PointSource, PointTarget, Scalar > > ConstPtr
typename TransformationValidation< PointSource, PointTarget >::PointCloudTargetConstPtr PointCloudTargetConstPtr
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
double getThreshold()
Get the threshold for which a specific transformation is valid.
TransformationValidationEuclidean computes an L2SQR norm between a source and target dataset...
double threshold_
The threshold for which a specific transformation is valid.
bool trivial_
Indicates whether this point representation is trivial.
shared_ptr< TransformationValidation< PointSource, PointTarget, Scalar > > Ptr
virtual void copyToFloatArray(const PointTarget &p, float *out) const
Copy point data from input point to a float array.
virtual bool isValid(const PointCloudSourceConstPtr &cloud_src, const PointCloudTargetConstPtr &cloud_tgt, const Matrix4 &transformation_matrix) const
Check if the score is valid for a specific transformation.
Defines all the PCL and non-PCL macros used.
virtual bool operator()(const double &score1, const double &score2) const
Comparator function for deciding which score is better after running the validation on multiple trans...