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