OTB  10.0.0
Orfeo Toolbox
otbEuclideanDistanceMetricWithMissingValuePow2.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 #ifndef otbEuclideanDistanceMetricWithMissingValuePow2_hxx
24 #define otbEuclideanDistanceMetricWithMissingValuePow2_hxx
25 
27 #include "itkMeasurementVectorTraits.h"
28 #include "vcl_legacy_aliases.h"
29 
30 namespace otb
31 {
32 
33 namespace Statistics
34 {
35 
36 template <class TVector>
37 inline double EuclideanDistanceMetricWithMissingValuePow2<TVector>::Evaluate(const TVector& x1, const TVector& x2) const
38 {
39  if (itk::NumericTraits<TVector>::GetLength(x1) != itk::NumericTraits<TVector>::GetLength(x2))
40  {
41  itkExceptionMacro(<< "Vector lengths must be equal.");
42  }
43 
44  double temp, distance = itk::NumericTraits<double>::Zero;
45 
46  for (unsigned int i = 0; i < x1.Size(); ++i)
47  {
48  if (!IsMissingValue(x1[i]) && !IsMissingValue(x2[i]))
49  {
50  temp = x1[i] - x2[i];
51  distance += temp * temp;
52  }
53  }
54 
55  return distance;
56 }
57 
58 template <class TVector>
60 {
61  MeasurementVectorSizeType measurementVectorSize = this->GetMeasurementVectorSize();
62  if (measurementVectorSize == 0)
63  {
64  itkExceptionMacro(<< "Please set the MeasurementVectorSize first");
65  }
66  itk::Statistics::MeasurementVectorTraits::Assert(this->GetOrigin(), measurementVectorSize,
67  "EuclideanDistanceMetric::Evaluate Origin and input vector have different lengths");
68 
69  double temp, distance = itk::NumericTraits<double>::Zero;
70 
71  for (unsigned int i = 0; i < measurementVectorSize; ++i)
72  {
73  if (!IsMissingValue(this->GetOrigin()[i]) && !IsMissingValue(x[i]))
74  {
75  temp = this->GetOrigin()[i] - x[i];
76  distance += temp * temp;
77  }
78  }
79 
80  return distance;
81 }
82 
83 template <class TVector>
85 {
86  // FIXME throw NaN exception ??
87  if (IsMissingValue(a) || IsMissingValue(b))
88  return 0.0;
89 
90  double temp = a - b;
91  return temp * temp;
92 }
93 
94 template <class TVector>
95 /*static */
97 {
98  return static_cast<bool>(vnl_math_isnan(static_cast<double>(v)));
99 }
100 
101 template <class TVector>
102 /* static */
104 {
105  v = std::numeric_limits<ValueType>::signaling_NaN();
106 }
107 
108 } // end namespace statistics
109 
110 } // end namespace otb
111 
112 #endif
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.