OTB  9.0.0
Orfeo Toolbox
otbSpectralAngleFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbSpectralAngleFunctor_h
22 #define otbSpectralAngleFunctor_h
23 
24 #include "otbMath.h"
25 #include <algorithm>
26 #include <vector>
27 #include <numeric>
28 
29 namespace otb
30 {
31 namespace Functor
32 {
33 
34 namespace SpectralAngleDetails
35 {
36 
41 template <class TInput, class TReference, class TOutput>
42 TOutput ComputeSpectralAngle(TInput const & input, typename TInput ::ValueType const & inputNorm,
43  TReference const & reference, typename TReference::ValueType refNorm)
44 {
45  auto minSize = std::min(input.Size(), reference.Size());
46  double scalarProduct = std::inner_product(&input[0], &input[minSize], &reference[0],0. );
47  auto normProd = inputNorm * refNorm;
48  if ((normProd < 1.e-12) || (scalarProduct / normProd > 1))
49  {
50  return static_cast<TOutput>(0.0);
51  }
52  else
53  {
54  return static_cast<TOutput>(std::acos(scalarProduct / normProd));
55  }
56 }
58 
59 } // end namespace SpectralAngleDetails
60 
66 template <class TInput, class TOutputValue>
68 {
69 public:
71  {
72  m_ReferencePixel.SetSize(4);
73  m_ReferencePixel.Fill(1);
74  }
75 
76  ~SpectralAngleFunctor() = default;
77 
78  // Binary operator
79  inline TOutputValue operator()(TInput const & inPix) const
80  {
81  return SpectralAngleDetails::ComputeSpectralAngle<TInput, TInput, TOutputValue>(inPix, inPix.GetNorm(), m_ReferencePixel, m_RefNorm);
82  }
83 
84  void SetReferencePixel(TInput const & ref)
85  {
86  m_ReferencePixel = ref;
87  m_RefNorm = ref.GetNorm();
88  }
89 
90  TInput GetReferencePixel() const
91  {
92  return m_ReferencePixel;
93  }
94 
95 private :
97  double m_RefNorm;
98 };
99 
105 template <class TInput, class TReference, class TOutput>
107 {
108 public:
109  SpectralAngleMapperFunctor() = default;
110  virtual ~SpectralAngleMapperFunctor() = default;
111 
112  // Binary operator
113  inline TOutput operator()(const TInput& inPix) const
114  {
115  TOutput res(m_ReferencePixels.size());
116 
117  auto inputNorm = inPix.GetNorm();
118 
119  for (unsigned int i = 0; i< m_ReferencePixels.size(); i++)
120  {
121  res[i] = SpectralAngleDetails::ComputeSpectralAngle<TInput, TInput, typename TOutput::ValueType>
122  (inPix, inputNorm, m_ReferencePixels[i], m_ReferenceNorm[i]);
123  }
124 
125  return res;
126  }
127 
128  size_t OutputSize(...) const
129  {
130  return m_ReferencePixels.size();
131  }
132 
133  void SetReferencePixels(std::vector<TReference> ref)
134  {
135  m_ReferencePixels = std::move(ref);
136  m_ReferenceNorm.clear();
137  // Precompute the norm of reference pixels
138  for (auto const & pixel : m_ReferencePixels)
139  {
140  m_ReferenceNorm.push_back(pixel.GetNorm());
141  }
142  }
143 
144  std::vector<TReference> const & GetReferencePixels() const
145  {
146  return m_ReferencePixels;
147  }
148 
149 private:
150  std::vector<TReference> m_ReferencePixels;
151  std::vector<double> m_ReferenceNorm;
152 };
153 
154 } // end namespace functor
155 } // end namespace otb
156 
157 #endif
otb::Functor::SpectralAngleMapperFunctor::GetReferencePixels
const std::vector< TReference > & GetReferencePixels() const
Definition: otbSpectralAngleFunctor.h:144
otb::Functor::SpectralAngleFunctor::~SpectralAngleFunctor
~SpectralAngleFunctor()=default
otb::Functor::SpectralAngleMapperFunctor::m_ReferenceNorm
std::vector< double > m_ReferenceNorm
Definition: otbSpectralAngleFunctor.h:151
otb::Functor::SpectralAngleFunctor
This functor computes the spectral angle according to a reference pixel.
Definition: otbSpectralAngleFunctor.h:67
otb::Functor::SpectralAngleMapperFunctor::SetReferencePixels
void SetReferencePixels(std::vector< TReference > ref)
Definition: otbSpectralAngleFunctor.h:133
otb::Functor::SpectralAngleMapperFunctor::~SpectralAngleMapperFunctor
virtual ~SpectralAngleMapperFunctor()=default
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::SpectralAngleFunctor::m_RefNorm
double m_RefNorm
Definition: otbSpectralAngleFunctor.h:97
otb::Functor::SpectralAngleFunctor::SetReferencePixel
void SetReferencePixel(TInput const &ref)
Definition: otbSpectralAngleFunctor.h:84
otb::Functor::SpectralAngleFunctor::m_ReferencePixel
TInput m_ReferencePixel
Definition: otbSpectralAngleFunctor.h:96
otb::Functor::SpectralAngleMapperFunctor
This functor computes the spectral angle according to a vector of reference pixel.
Definition: otbSpectralAngleFunctor.h:106
otb::Functor::SpectralAngleMapperFunctor::m_ReferencePixels
std::vector< TReference > m_ReferencePixels
Definition: otbSpectralAngleFunctor.h:150
otb::Functor::SpectralAngleMapperFunctor::OutputSize
vcl_size_t OutputSize(...) const
Definition: otbSpectralAngleFunctor.h:128
otb::Functor::SpectralAngleMapperFunctor::operator()
TOutput operator()(const TInput &inPix) const
Definition: otbSpectralAngleFunctor.h:113
otb::Functor::SpectralAngleFunctor::SpectralAngleFunctor
SpectralAngleFunctor()
Definition: otbSpectralAngleFunctor.h:70
otb::Functor::SpectralAngleFunctor::operator()
TOutputValue operator()(TInput const &inPix) const
Definition: otbSpectralAngleFunctor.h:79
otb::Functor::SpectralAngleDetails::ComputeSpectralAngle
TOutput ComputeSpectralAngle(TInput const &input, typename TInput ::ValueType const &inputNorm, TReference const &reference, typename TReference::ValueType refNorm)
Definition: otbSpectralAngleFunctor.h:42
otb::Functor::SpectralAngleFunctor::GetReferencePixel
TInput GetReferencePixel() const
Definition: otbSpectralAngleFunctor.h:90
otb::Functor::SpectralAngleMapperFunctor::SpectralAngleMapperFunctor
SpectralAngleMapperFunctor()=default