Point Cloud Library (PCL)  1.7.1
distances.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 #ifndef PCL_REGISTRATION_DISTANCES_H
41 #define PCL_REGISTRATION_DISTANCES_H
42 
43 #include <pcl/registration/eigen.h>
44 #include <vector>
45 
46 namespace pcl
47 {
48  namespace distances
49  {
50 
51  /** \brief Compute the median value from a set of doubles
52  * \param[in] fvec the set of doubles
53  * \param[in] m the number of doubles in the set
54  */
55  inline double
56  computeMedian (double *fvec, int m)
57  {
58  // Copy the values to vectors for faster sorting
59  std::vector<double> data (m);
60  memcpy (&data[0], fvec, sizeof (double) * m);
61 
62  std::nth_element(data.begin(), data.begin() + (data.size () >> 1), data.end());
63  return (data[data.size () >> 1]);
64  }
65 
66  /** \brief Use a Huber kernel to estimate the distance between two vectors
67  * \param[in] p_src the first eigen vector
68  * \param[in] p_tgt the second eigen vector
69  * \param[in] sigma the sigma value
70  */
71  inline double
72  huber (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt, double sigma)
73  {
74  Eigen::Array4f diff = (p_tgt.array () - p_src.array ()).abs ();
75  double norm = 0.0;
76  for (int i = 0; i < 3; ++i)
77  {
78  if (diff[i] < sigma)
79  norm += diff[i] * diff[i];
80  else
81  norm += 2.0 * sigma * diff[i] - sigma * sigma;
82  }
83  return (norm);
84  }
85 
86  /** \brief Use a Huber kernel to estimate the distance between two vectors
87  * \param[in] diff the norm difference between two vectors
88  * \param[in] sigma the sigma value
89  */
90  inline double
91  huber (double diff, double sigma)
92  {
93  double norm = 0.0;
94  if (diff < sigma)
95  norm += diff * diff;
96  else
97  norm += 2.0 * sigma * diff - sigma * sigma;
98  return (norm);
99  }
100 
101  /** \brief Use a Gedikli kernel to estimate the distance between two vectors
102  * (for more information, see
103  * \param[in] val the norm difference between two vectors
104  * \param[in] clipping the clipping value
105  * \param[in] slope the slope. Default: 4
106  */
107  inline double
108  gedikli (double val, double clipping, double slope = 4)
109  {
110  return (1.0 / (1.0 + pow (fabs(val) / clipping, slope)));
111  }
112 
113  /** \brief Compute the Manhattan distance between two eigen vectors.
114  * \param[in] p_src the first eigen vector
115  * \param[in] p_tgt the second eigen vector
116  */
117  inline double
118  l1 (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt)
119  {
120  return ((p_src.array () - p_tgt.array ()).abs ().sum ());
121  }
122 
123  /** \brief Compute the Euclidean distance between two eigen vectors.
124  * \param[in] p_src the first eigen vector
125  * \param[in] p_tgt the second eigen vector
126  */
127  inline double
128  l2 (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt)
129  {
130  return ((p_src - p_tgt).norm ());
131  }
132 
133  /** \brief Compute the squared Euclidean distance between two eigen vectors.
134  * \param[in] p_src the first eigen vector
135  * \param[in] p_tgt the second eigen vector
136  */
137  inline double
138  l2Sqr (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt)
139  {
140  return ((p_src - p_tgt).squaredNorm ());
141  }
142  }
143 }
144 
145 #endif