Point Cloud Library (PCL)  1.9.1-dev
norms.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, 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  */
38 
39 #ifndef PCL_COMMON_NORMS_IMPL_HPP_
40 #define PCL_COMMON_NORMS_IMPL_HPP_
41 
42 #include <pcl/pcl_macros.h>
43 #include <pcl/console/print.h>
44 
45 namespace pcl
46 {
47 
48 template <typename FloatVectorT> inline float
49 selectNorm (FloatVectorT a, FloatVectorT b, int dim, NormType norm_type)
50 {
51  // {L1, L2_SQR, L2, LINF, JM, B, SUBLINEAR, CS, DIV, PF, K, KL, HIK};
52  switch (norm_type)
53  {
54  case (L1):
55  return L1_Norm (a, b, dim);
56  case (L2_SQR):
57  return L2_Norm_SQR (a, b, dim);
58  case (L2):
59  return L2_Norm (a, b, dim);
60  case (LINF):
61  return Linf_Norm (a, b, dim);
62  case (JM):
63  return JM_Norm (a, b, dim);
64  case (B):
65  return B_Norm (a, b, dim);
66  case (SUBLINEAR):
67  return Sublinear_Norm (a, b, dim);
68  case (CS):
69  return CS_Norm (a, b, dim);
70  case (DIV):
71  return Div_Norm (a, b, dim);
72  case (KL):
73  return KL_Norm (a, b, dim);
74  case (HIK):
75  return HIK_Norm (a, b, dim);
76 
77  case (PF):
78  case (K):
79  default:
80  PCL_ERROR ("[pcl::selectNorm] For PF and K norms you have to explicitly call the method, as they need additional parameters\n");
81  return -1;
82  }
83 }
84 
85 
86 template <typename FloatVectorT> inline float
87 L1_Norm (FloatVectorT a, FloatVectorT b, int dim)
88 {
89  float norm = 0.0f;
90  for (int i = 0; i < dim; ++i)
91  norm += std::abs(a[i] - b[i]);
92  return norm;
93 }
94 
95 
96 template <typename FloatVectorT> inline float
97 L2_Norm_SQR (FloatVectorT a, FloatVectorT b, int dim)
98 {
99  float norm = 0.0;
100  for (int i = 0; i < dim; ++i)
101  {
102  float diff = a[i] - b[i];
103  norm += diff*diff;
104  }
105  return norm;
106 }
107 
108 
109 template <typename FloatVectorT> inline float
110 L2_Norm (FloatVectorT a, FloatVectorT b, int dim)
111 {
112  return std::sqrt (L2_Norm_SQR(a, b, dim));
113 }
114 
115 
116 template <typename FloatVectorT> inline float
117 Linf_Norm (FloatVectorT a, FloatVectorT b, int dim)
118 {
119  float norm = 0.0;
120  for (int i = 0; i < dim; ++i)
121  norm = (std::max)(std::abs(a[i] - b[i]), norm);
122  return norm;
123 }
124 
125 
126 template <typename FloatVectorT> inline float
127 JM_Norm (FloatVectorT a, FloatVectorT b, int dim)
128 {
129  float norm = 0.0;
130 
131  for (int i = 0; i < dim; ++i)
132  norm += (std::sqrt (a[i]) - std::sqrt (b[i])) * (std::sqrt (a[i]) - std::sqrt (b[i]));
133 
134  return std::sqrt (norm);
135 }
136 
137 
138 template <typename FloatVectorT> inline float
139 B_Norm (FloatVectorT a, FloatVectorT b, int dim)
140 {
141  float norm = 0.0, result;
142 
143  for (int i = 0; i < dim; ++i)
144  norm += std::sqrt (a[i] * b[i]);
145 
146  if (norm > 0)
147  result = -std::log (norm);
148  else
149  result = 0;
150 
151  return result;
152 }
153 
154 
155 template <typename FloatVectorT> inline float
156 Sublinear_Norm (FloatVectorT a, FloatVectorT b, int dim)
157 {
158  float norm = 0.0;
159 
160  for (int i = 0; i < dim; ++i)
161  norm += std::sqrt (std::abs (a[i] - b[i]));
162 
163  return norm;
164 }
165 
166 
167 template <typename FloatVectorT> inline float
168 CS_Norm (FloatVectorT a, FloatVectorT b, int dim)
169 {
170  float norm = 0.0;
171 
172  for (int i = 0; i < dim; ++i)
173  if ((a[i] + b[i]) != 0)
174  norm += (a[i] - b[i]) * (a[i] - b[i]) / (a[i] + b[i]);
175  else
176  norm += 0;
177  return norm;
178 }
179 
180 
181 template <typename FloatVectorT> inline float
182 Div_Norm (FloatVectorT a, FloatVectorT b, int dim)
183 {
184  float norm = 0.0;
185 
186  for (int i = 0; i < dim; ++i)
187  if ((a[i] / b[i]) > 0)
188  norm += (a[i] - b[i]) * std::log (a[i] / b[i]);
189  else
190  norm += 0;
191  return norm;
192 }
193 
194 
195 template <typename FloatVectorT> inline float
196 PF_Norm (FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
197 {
198  float norm = 0.0;
199 
200  for (int i = 0; i < dim; ++i)
201  norm += (P1 * a[i] - P2 * b[i]) * (P1 * a[i] - P2 * b[i]);
202  return std::sqrt (norm);
203 }
204 
205 
206 template <typename FloatVectorT> inline float
207 K_Norm (FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
208 {
209  float norm = 0.0;
210 
211  for (int i = 0; i < dim; ++i)
212  norm += std::abs (P1 * a[i] - P2 * b[i]);
213  return norm;
214 }
215 
216 
217 template <typename FloatVectorT> inline float
218 KL_Norm (FloatVectorT a, FloatVectorT b, int dim)
219 {
220  float norm = 0.0;
221 
222  for (int i = 0; i < dim; ++i)
223  if ( (b[i] != 0) && ((a[i] / b[i]) > 0) )
224  norm += a[i] * std::log (a[i] / b[i]);
225  else
226  norm += 0;
227  return norm;
228 }
229 
230 
231 template <typename FloatVectorT> inline float
232 HIK_Norm(FloatVectorT a, FloatVectorT b, int dim)
233 {
234  float norm = 0.0f;
235  for (int i = 0; i < dim; ++i)
236  norm += (std::min)(a[i], b[i]);
237  return norm;
238 }
239 
240 }
241 #endif
242 
Definition: norms.h:54
float Sublinear_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the sublinear norm of the vector between two points.
Definition: norms.hpp:156
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
NormType
Enum that defines all the types of norms available.
Definition: norms.h:54
float L2_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the L2 norm of the vector between two points.
Definition: norms.hpp:110
float CS_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the CS norm of the vector between two points.
Definition: norms.hpp:168
float L2_Norm_SQR(FloatVectorT a, FloatVectorT b, int dim)
Compute the squared L2 norm of the vector between two points.
Definition: norms.hpp:97
Definition: norms.h:54
Definition: norms.h:54
Definition: norms.h:54
Definition: norms.h:54
float Div_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the div norm of the vector between two points.
Definition: norms.hpp:182
float KL_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the KL between two discrete probability density functions.
Definition: norms.hpp:218
Definition: norms.h:54
float JM_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the JM norm of the vector between two points.
Definition: norms.hpp:127
Definition: norms.h:54
float Linf_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the L-infinity norm of the vector between two points.
Definition: norms.hpp:117
float HIK_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the HIK norm of the vector between two points.
Definition: norms.hpp:232
Definition: norms.h:54
float selectNorm(FloatVectorT a, FloatVectorT b, int dim, NormType norm_type)
Method that calculates any norm type available, based on the norm_type variable.
Definition: norms.hpp:49
float K_Norm(FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
Compute the K norm of the vector between two points.
Definition: norms.hpp:207
Definition: norms.h:54
float L1_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the L1 norm of the vector between two points.
Definition: norms.hpp:87
float PF_Norm(FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
Compute the PF norm of the vector between two points.
Definition: norms.hpp:196
Definition: norms.h:54
float B_Norm(FloatVectorT a, FloatVectorT b, int dim)
Compute the B norm of the vector between two points.
Definition: norms.hpp:139