OTB  9.0.0
Orfeo Toolbox
otbMaskMuParserFunctor.hxx
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 otbMaskMuParserFunctor_hxx
22 #define otbMaskMuParserFunctor_hxx
23 
24 #include "otbMaskMuParserFunctor.h"
25 
26 namespace otb
27 {
28 namespace Functor
29 {
30 // constructor
31 template <class TInputPixel>
32 MaskMuParserFunctor<TInputPixel>::MaskMuParserFunctor() : m_Parser(ParserType::New()), m_NbOfBands(0), m_ParserResult(0), m_Intensity(0), m_SpectralAngle(0)
33 {
34 }
35 
36 // Destructor
37 template <class TInputPixel>
39 {
40 }
41 
42 template <class TInputPixel>
44 {
45  double value;
46 
47  if (p.Size() != m_NbOfBands)
48  {
49  this->SetNumberOfBands(p.GetSize());
50  }
51 
52  // we fill the buffer
53  for (unsigned int i = 0; i < m_NbOfBands; ++i)
54  {
55  m_AImage[i] = static_cast<double>(p[i]);
56  }
57 
58  // user defined variables
59  m_Intensity = 0.0;
60  for (unsigned int i = 0; i < m_NbOfBands; ++i)
61  {
62  m_Intensity += p[i];
63  }
64  m_Intensity = m_Intensity / (static_cast<double>(m_NbOfBands));
65 
66  m_SpectralAngle = m_SpectralAngleFunctor(p, m_SpectralAngleReferencePixel);
67 
68  value = m_Parser->Eval();
69 
70  return static_cast<bool>(value);
71 }
72 
73 template <class TInputPixel>
74 const std::map<std::string, Parser::ValueType*>& MaskMuParserFunctor<TInputPixel>::GetVar() const
75 {
76  return this->m_Parser->GetVar();
77 }
78 
79 template <class TInputPixel>
81 {
82  return this->m_Parser->GetFunList();
83 }
84 
85 
86 template <class TInputPixel>
87 void MaskMuParserFunctor<TInputPixel>::SetExpression(const std::string& expression)
88 {
89  m_Expression = expression;
90  m_Parser->SetExpr(m_Expression);
91 }
92 
94 template <class TInputPixel>
96 {
97  return m_Expression;
98 }
99 
100 template <class TInputPixel>
102 {
103  m_NbOfBands = NbOfBands;
104  std::ostringstream varName;
105 
106  m_AImage.resize(NbOfBands, 0.0);
107 
108  for (unsigned int i = 0; i < NbOfBands; ++i)
109  {
110  varName << "b" << i + 1;
111  m_Parser->DefineVar(varName.str(), &(m_AImage[i]));
112  varName.str("");
113  }
114 
115  // customized data
116  m_Parser->DefineVar("intensity", &m_Intensity);
117  m_Parser->DefineVar("spectralAngle", &m_SpectralAngle);
118 }
119 
121 template <class TInputPixel>
123 {
124  m_SpectralAngleReferencePixel = refPixel;
125 }
126 
128 template <class TInputPixel>
130 {
131  return m_Parser->CheckExpr();
132 }
133 
134 } // end namespace Functor
135 } // end namespace otb
136 
137 #endif
otb::Functor::MaskMuParserFunctor::SetExpression
void SetExpression(const std::string &expression)
Definition: otbMaskMuParserFunctor.hxx:87
otb::Functor::MaskMuParserFunctor::MaskMuParserFunctor
MaskMuParserFunctor()
Definition: otbMaskMuParserFunctor.hxx:32
otb::Functor::MaskMuParserFunctor::CheckExpression
bool CheckExpression()
Definition: otbMaskMuParserFunctor.hxx:129
otb::Functor::MaskMuParserFunctor::GetFunList
Parser::FunctionMapType GetFunList() const
Definition: otbMaskMuParserFunctor.hxx:80
otb::Parser::FunctionMapType
std::map< std::string, int > FunctionMapType
Definition: otbParser.h:64
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::MaskMuParserFunctor::GetVar
const std::map< std::string, Parser::ValueType * > & GetVar() const
Definition: otbMaskMuParserFunctor.hxx:74
otbMaskMuParserFunctor.h
otb::Functor::MaskMuParserFunctor::SetNumberOfBands
void SetNumberOfBands(unsigned int NbOfBands)
Definition: otbMaskMuParserFunctor.hxx:101
otb::Functor::MaskMuParserFunctor::PixelType
TInputPixel PixelType
Definition: otbMaskMuParserFunctor.h:73
otb::Functor::MaskMuParserFunctor::~MaskMuParserFunctor
~MaskMuParserFunctor() override
Definition: otbMaskMuParserFunctor.hxx:38
otb::Functor::MaskMuParserFunctor::operator()
bool operator()(const PixelType &p)
Definition: otbMaskMuParserFunctor.hxx:43
otb::Parser
Definition of the standard floating point parser. Standard implementation of the mathematical express...
Definition: otbParser.h:44
otb::Functor::MaskMuParserFunctor::SetSpectralAngleReferencePixel
void SetSpectralAngleReferencePixel(const PixelType &refPixel)
Definition: otbMaskMuParserFunctor.hxx:122
otb::Functor::MaskMuParserFunctor::GetExpression
std::string GetExpression() const
Definition: otbMaskMuParserFunctor.hxx:95