OTB  9.0.0
Orfeo Toolbox
otbFlexibleDistanceWithMissingValue.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 otbFlexibleDistanceWithMissingValue_hxx
24 #define otbFlexibleDistanceWithMissingValue_hxx
25 
27 #include "itkNumericTraits.h"
28 
29 namespace otb
30 {
31 
32 namespace Statistics
33 {
34 
35 template <class TVector>
36 inline double FlexibleDistanceWithMissingValue<TVector>::Evaluate(const TVector& x1, const TVector& x2) const
37 {
38  if (IsEuclidean())
39  return Superclass::Evaluate(x1, x2);
40 
41  if (itk::NumericTraits<TVector>::GetLength(x1) != itk::NumericTraits<TVector>::GetLength(x2))
42  {
43  itkExceptionMacro(<< "Vector lengths must be equal.");
44  }
45 
46  double temp, distance = itk::NumericTraits<double>::Zero;
47 
48  for (unsigned int i = 0; i < x1.Size(); ++i)
49  {
50  if (!this->IsMissingValue(x1[i]) && !this->IsMissingValue(x2[i]))
51  {
52  temp = std::pow(std::abs(std::pow(x1[i], this->Alpha) - std::pow(x2[i], this->Alpha)), this->Beta);
53  distance += temp;
54  }
55  }
56 
57  return distance;
58 }
59 
60 template <class TVector>
61 inline double FlexibleDistanceWithMissingValue<TVector>::Evaluate(const TVector& x) const
62 {
63  if (IsEuclidean())
64  return Superclass::Evaluate(x);
65 
66  MeasurementVectorSizeType measurementVectorSize = this->GetMeasurementVectorSize();
67 
68  if (measurementVectorSize == 0)
69  {
70  itkExceptionMacro(<< "Please set the MeasurementVectorSize first");
71  }
72 
73  itk::Statistics::MeasurementVectorTraits::Assert(this->GetOrigin(), measurementVectorSize,
74  "EuclideanDistanceMetric::Evaluate Origin and input vector have different lengths");
75 
76  double temp, distance = itk::NumericTraits<double>::Zero;
77 
78  for (unsigned int i = 0; i < measurementVectorSize; ++i)
79  {
80  if (!this->IsMissingValue(this->GetOrigin()[i]) && !this->IsMissingValue(x[i]))
81  {
82  temp = std::pow(std::abs(std::pow(this->GetOrigin()[i], this->Alpha) - std::pow(x[i], this->Alpha)), this->Beta);
83  distance += temp;
84  }
85  }
86 
87  return distance;
88 }
89 
90 template <class TVector>
92 {
93  if (IsEuclidean())
94  return Superclass::Evaluate(a, b);
95 
96  // FIXME throw NaN exception instead of returning 0. ??
97  if (this->IsMissingValue(a) || this->IsMissingValue(b))
98  return 0.0;
99 
100  double temp = std::pow(std::abs(std::pow(a, this->Alpha) - std::pow(b, this->Alpha)), this->Beta);
101  return temp;
102 }
103 
104 template <class TVector>
106 {
107  Alpha = a;
108  Beta = b;
109 }
110 
111 template <class TVector>
113 {
114  if ((Alpha == 1.0) && (Beta == 2.0))
115  {
116  return true;
117  }
118  else
119  {
120  return false;
121  }
122 }
123 
124 } // end namespace statistics
125 } // end namespace otb
126 
127 #endif
otb::Statistics::FlexibleDistanceWithMissingValue::SetAlphaBeta
static void SetAlphaBeta(double a, double b)
Definition: otbFlexibleDistanceWithMissingValue.hxx:105
otb::Statistics::EuclideanDistanceMetricWithMissingValuePow2::MeasurementVectorSizeType
Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: otbEuclideanDistanceMetricWithMissingValuePow2.h:61
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Statistics::FlexibleDistanceWithMissingValue::Evaluate
double Evaluate(const TVector &x) const override
Definition: otbFlexibleDistanceWithMissingValue.hxx:61
otbFlexibleDistanceWithMissingValue.h
otb::Statistics::FlexibleDistanceWithMissingValue::IsEuclidean
static bool IsEuclidean()
Definition: otbFlexibleDistanceWithMissingValue.hxx:112
otb::Statistics::EuclideanDistanceMetricWithMissingValuePow2::ValueType
TVector::ValueType ValueType
Definition: otbEuclideanDistanceMetricWithMissingValuePow2.h:67