Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
norms.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR a PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <pcl/pcl_macros.h>
37 
38 namespace pcl
39 {
40 
41 template <typename FloatVectorT> inline float
42 selectNorm (FloatVectorT a, FloatVectorT b, int dim, NormType norm_type)
43 {
44  // {L1, L2_SQR, L2, LINF, JM, B, SUBLINEAR, CS, DIV, PF, K, KL, HIK};
45  switch (norm_type)
46  {
47  case (L1):
48  return L1_Norm (a, b, dim);
49  case (L2_SQR):
50  return L2_Norm_SQR (a, b, dim);
51  case (L2):
52  return L2_Norm (a, b, dim);
53  case (LINF):
54  return Linf_Norm (a, b, dim);
55  case (JM):
56  return JM_Norm (a, b, dim);
57  case (B):
58  return B_Norm (a, b, dim);
59  case (SUBLINEAR):
60  return Sublinear_Norm (a, b, dim);
61  case (CS):
62  return CS_Norm (a, b, dim);
63  case (DIV):
64  return Div_Norm (a, b, dim);
65  case (KL):
66  return KL_Norm (a, b, dim);
67  case (HIK):
68  return HIK_Norm (a, b, dim);
69 
70  case (PF):
71  case (K):
72  default:
73  PCL_ERROR ("[pcl::selectNorm] For PF and K norms you have to explicitly call the method, as they need additional parameters\n");
74  return -1;
75  }
76 }
77 
78 
79 template <typename FloatVectorT> inline float
80 L1_Norm (FloatVectorT a, FloatVectorT b, int dim)
81 {
82  float norm = 0.0f;
83  for (int i = 0; i < dim; ++i)
84  norm += fabsf(a[i] - b[i]);
85  return norm;
86 }
87 
88 
89 template <typename FloatVectorT> inline float
90 L2_Norm_SQR (FloatVectorT a, FloatVectorT b, int dim)
91 {
92  float norm = 0.0;
93  for (int i = 0; i < dim; ++i)
94  {
95  float diff = a[i] - b[i];
96  norm += diff*diff;
97  }
98  return norm;
99 }
100 
101 
102 template <typename FloatVectorT> inline float
103 L2_Norm (FloatVectorT a, FloatVectorT b, int dim)
104 {
105  return sqrtf(L2_Norm_SQR(a, b, dim));
106 }
107 
108 
109 template <typename FloatVectorT> inline float
110 Linf_Norm (FloatVectorT a, FloatVectorT b, int dim)
111 {
112  float norm = 0.0;
113  for (int i = 0; i < dim; ++i)
114  norm = (std::max)(fabsf(a[i] - b[i]), norm);
115  return norm;
116 }
117 
118 
119 template <typename FloatVectorT> inline float
120 JM_Norm (FloatVectorT a, FloatVectorT b, int dim)
121 {
122  float norm = 0.0;
123 
124  for (int i = 0; i < dim; ++i)
125  norm += (sqrtf (a[i]) - sqrtf (b[i])) * (sqrtf (a[i]) - sqrtf (b[i]));
126 
127  return sqrtf (norm);
128 }
129 
130 
131 template <typename FloatVectorT> inline float
132 B_Norm (FloatVectorT a, FloatVectorT b, int dim)
133 {
134  float norm = 0.0, result;
135 
136  for (int i = 0; i < dim; ++i)
137  norm += sqrtf (a[i] * b[i]);
138 
139  if (norm > 0)
140  result = -logf (norm);
141  else
142  result = 0;
143 
144  return result;
145 }
146 
147 
148 template <typename FloatVectorT> inline float
149 Sublinear_Norm (FloatVectorT a, FloatVectorT b, int dim)
150 {
151  float norm = 0.0;
152 
153  for (int i = 0; i < dim; ++i)
154  norm += sqrtf (fabsf (a[i] - b[i]));
155 
156  return norm;
157 }
158 
159 
160 template <typename FloatVectorT> inline float
161 CS_Norm (FloatVectorT a, FloatVectorT b, int dim)
162 {
163  float norm = 0.0;
164 
165  for (int i = 0; i < dim; ++i)
166  if ((a[i] + b[i]) != 0)
167  norm += (a[i] - b[i]) * (a[i] - b[i]) / (a[i] + b[i]);
168  else
169  norm += 0;
170  return norm;
171 }
172 
173 
174 template <typename FloatVectorT> inline float
175 Div_Norm (FloatVectorT a, FloatVectorT b, int dim)
176 {
177  float norm = 0.0;
178 
179  for (int i = 0; i < dim; ++i)
180  if ((a[i] / b[i]) > 0)
181  norm += (a[i] - b[i]) * logf (a[i] / b[i]);
182  else
183  norm += 0;
184  return norm;
185 }
186 
187 
188 template <typename FloatVectorT> inline float
189 PF_Norm (FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
190 {
191  float norm = 0.0;
192 
193  for (int i = 0; i < dim; ++i)
194  norm += (P1 * a[i] - P2 * b[i]) * (P1 * a[i] - P2 * b[i]);
195  return sqrtf (norm);
196 }
197 
198 
199 template <typename FloatVectorT> inline float
200 K_Norm (FloatVectorT a, FloatVectorT b, int dim, float P1, float P2)
201 {
202  float norm = 0.0;
203 
204  for (int i = 0; i < dim; ++i)
205  norm += fabsf (P1 * a[i] - P2 * b[i]);
206  return norm;
207 }
208 
209 
210 template <typename FloatVectorT> inline float
211 KL_Norm (FloatVectorT a, FloatVectorT b, int dim)
212 {
213  float norm = 0.0;
214 
215  for (int i = 0; i < dim; ++i)
216  if ( (b[i] != 0) && ((a[i] / b[i]) > 0) )
217  norm += a[i] * logf (a[i] / b[i]);
218  else
219  norm += 0;
220  return norm;
221 }
222 
223 
224 template <typename FloatVectorT> inline float
225 HIK_Norm(FloatVectorT a, FloatVectorT b, int dim)
226 {
227  float norm = 0.0f;
228  for (int i = 0; i < dim; ++i)
229  norm += (std::min)(a[i], b[i]);
230  return norm;
231 }
232 
233 }