OTB  9.0.0
Orfeo Toolbox
otbRadiometricIndex.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 otbRadiometricIndex_h
22 #define otbRadiometricIndex_h
23 
24 #include "itkVariableLengthVector.h"
25 #include "otbBandName.h"
26 #include <array>
27 #include <set>
28 #include <string>
29 #include <map>
30 #include <stdexcept>
31 
32 using namespace otb::BandName;
33 
34 namespace otb
35 {
36 namespace Functor
37 {
56 template <typename TInput, typename TOutput>
58 {
59 public:
61  using InputType = TInput;
62  using PixelType = itk::VariableLengthVector<InputType>;
63  using OutputType = TOutput;
65 
68 
70  static constexpr size_t NumberOfBands = static_cast<size_t>(BandNameType::MAX);
71 
72  static constexpr double Epsilon = 0.0000001;
73 
74  // Necessary to be used as an abstract base class
75  virtual ~RadiometricIndex() = default;
76 
81  RadiometricIndex(const std::set<BandNameType>& requiredBands) : m_RequiredBands(), m_BandIndices()
82  {
83  if (requiredBands.find(BandNameType::MAX) != requiredBands.end())
84  {
85  throw std::runtime_error("TBandNameEnum::MAX can not be used as a required band");
86  }
87 
88  // Fill the required bands array
89  m_RequiredBands.fill(false);
90  m_BandIndices.fill(0);
91 
92  for (auto b : requiredBands)
93  {
94  m_RequiredBands[static_cast<size_t>(b)] = true;
95  }
96  }
97 
102  std::set<BandNameType> GetRequiredBands() const
103  {
104  std::set<BandNameType> resp;
105  for (size_t i = 0; i < NumberOfBands; ++i)
106  {
107  if (m_RequiredBands[i])
108  {
109  resp.insert(static_cast<BandNameType>(i));
110  }
111  }
113 
114  return resp;
115  }
116 
122  void SetBandIndex(BandNameType band, size_t index)
123  {
124  if (band == BandNameType::MAX)
125  {
126  throw std::runtime_error("Can not set index for CommandBandName::MAX");
127  }
128  m_BandIndices[static_cast<size_t>(band)] = index;
129  }
131 
137  void SetBandsIndices(const std::map<BandNameType, size_t>& indicesMap)
138  {
139  for (auto it : indicesMap)
140  {
141  SetBandIndex(it.first, it.second);
142  }
143  }
145 
151  size_t GetBandIndex(BandNameType band) const
152  {
153  if (band == BandNameType::MAX)
154  {
155  throw std::runtime_error("Can not get index for CommandBandName::MAX");
156  }
157  return m_BandIndices[static_cast<size_t>(band)];
158  }
160 
167  virtual TOutput operator()(const itk::VariableLengthVector<TInput>& input) const = 0;
168 
169 protected:
181  size_t UncheckedBandIndex(BandNameType band) const
182  {
183  assert(band != BandNameType::MAX && "Can not retrieve index for band CommandBandName::MAX");
184  return m_BandIndices[static_cast<size_t>(band)];
185  }
187 
204  double Value(BandNameType band, const itk::VariableLengthVector<TInput>& input) const
205  {
206  assert(m_RequiredBands[static_cast<size_t>(band)] && "Retrieving value for a band that is not in the required bands list");
207  return static_cast<double>(input[UncheckedBandIndex(band) - 1]);
208  }
210 
211 private:
212  // Explicitly disable default constructor
213  RadiometricIndex() = delete;
214 
216  using RequiredBandsContainer = std::array<bool, NumberOfBands>;
218 
220  using BandIndicesContainer = std::array<size_t, NumberOfBands>;
222 };
223 
224 } // namespace Functor
225 } // End namespace otb
226 
227 #endif
otb::Functor::RadiometricIndex::GetRequiredBands
std::set< BandNameType > GetRequiredBands() const
Definition: otbRadiometricIndex.h:102
otb::Functor::RadiometricIndex::Value
double Value(BandNameType band, const itk::VariableLengthVector< TInput > &input) const
Definition: otbRadiometricIndex.h:204
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbBandName.h
otb::Functor::RadiometricIndex::m_BandIndices
BandIndicesContainer m_BandIndices
An array storing the required status for each band.
Definition: otbRadiometricIndex.h:221
otb::BandName::CommonBandNames
CommonBandNames
Definition: otbBandName.h:33
otb::Functor::RadiometricIndex< typename TReduceSpectralResponse::ValuePrecisionType, typename TReduceSpectralResponse::ValuePrecisionType >::PixelType
itk::VariableLengthVector< InputType > PixelType
Definition: otbRadiometricIndex.h:62
otb::CellFusionMode::MAX
@ MAX
Definition: otbMulti3DMapToDEMFilter.h:42
otb::Functor::RadiometricIndex::m_RequiredBands
RequiredBandsContainer m_RequiredBands
An array storing the required status for each band.
Definition: otbRadiometricIndex.h:217
otb::Functor::RadiometricIndex< typename TReduceSpectralResponse::ValuePrecisionType, typename TReduceSpectralResponse::ValuePrecisionType >::OutputType
typename TReduceSpectralResponse::ValuePrecisionType OutputType
Definition: otbRadiometricIndex.h:63
otb::Functor::RadiometricIndex::RadiometricIndex
RadiometricIndex(const std::set< BandNameType > &requiredBands)
Definition: otbRadiometricIndex.h:81
otb::Functor::RadiometricIndex< typename TReduceSpectralResponse::ValuePrecisionType, typename TReduceSpectralResponse::ValuePrecisionType >::InputType
typename TReduceSpectralResponse::ValuePrecisionType InputType
Types for input/output.
Definition: otbRadiometricIndex.h:61
otb::Functor::RadiometricIndex::GetBandIndex
vcl_size_t GetBandIndex(BandNameType band) const
Definition: otbRadiometricIndex.h:151
otb::Functor::RadiometricIndex
Base class for all radiometric indices.
Definition: otbRadiometricIndex.h:57
otb::Functor::RadiometricIndex::UncheckedBandIndex
vcl_size_t UncheckedBandIndex(BandNameType band) const
Definition: otbRadiometricIndex.h:181
otb::Functor::RadiometricIndex< typename TReduceSpectralResponse::ValuePrecisionType, typename TReduceSpectralResponse::ValuePrecisionType >::BandIndicesContainer
std::array< vcl_size_t, NumberOfBands > BandIndicesContainer
An array storing the indice for each band.
Definition: otbRadiometricIndex.h:220
otb::Functor::RadiometricIndex::SetBandsIndices
void SetBandsIndices(const std::map< BandNameType, vcl_size_t > &indicesMap)
Definition: otbRadiometricIndex.h:137
otb::Functor::RadiometricIndex::SetBandIndex
void SetBandIndex(BandNameType band, vcl_size_t index)
Definition: otbRadiometricIndex.h:122
otb::BandName
Definition: otbBandName.h:26
otb::Functor::RadiometricIndex< typename TReduceSpectralResponse::ValuePrecisionType, typename TReduceSpectralResponse::ValuePrecisionType >::RequiredBandsContainer
std::array< bool, NumberOfBands > RequiredBandsContainer
An array storing the required status for each band.
Definition: otbRadiometricIndex.h:216