OTB  9.0.0
Orfeo Toolbox
otbConnectedComponentMuParserFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
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 otbConnectedComponentMuParserFunctor_h
24 #define otbConnectedComponentMuParserFunctor_h
25 
26 #include "otbParser.h"
27 #include "otbMacro.h"
29 
30 #include <vnl/algo/vnl_lsqr.h>
31 #include <vnl/vnl_sparse_matrix_linear_system.h>
32 #include <vnl/vnl_least_squares_function.h>
33 #include <string>
34 
35 
36 namespace otb
37 {
70 namespace Functor
71 {
72 
73 template <class TInput>
75 {
76 
77 public:
78  typedef Parser ParserType;
80 
81  std::string GetNameOfClass()
82  {
83  return "ConnectedComponentMuParserFunctor";
84  }
85 
86  inline bool operator()(const TInput& p1, const TInput& p2)
87  {
88 
89  double value;
90 
91  if (p1.GetSize() != m_NbOfBands)
92  {
93  this->SetNumberOfBands(p1.GetSize());
94  }
95 
96  // we fill the buffer
97  for (unsigned int i = 0; i < m_NbOfBands; ++i)
98  {
99  m_AImageP1[i] = static_cast<double>(p1[i]);
100  m_AImageP2[i] = static_cast<double>(p2[i]);
101  }
102 
103  m_Distance = 0.0;
104  m_IntensityP1 = 0.0;
105  m_IntensityP2 = 0.0;
106 
107  for (unsigned int i = 0; i < m_NbOfBands; ++i)
108  {
109  m_Distance += (p1[i] - p2[i]) * (p1[i] - p2[i]);
110  m_IntensityP1 += p1[i];
111  m_IntensityP2 += p2[i];
112  }
113 
114  m_IntensityP1 = m_IntensityP1 / (static_cast<double>(m_NbOfBands));
115  m_IntensityP2 = m_IntensityP2 / (static_cast<double>(m_NbOfBands));
116 
117  m_Distance = std::sqrt(m_Distance);
118 
120  m_SpectralAngle = spectralAngleFunctor(p1, p2);
121 
122  value = m_Parser->Eval();
123 
124  return static_cast<bool>(value);
125  }
126 
127  void SetExpression(const std::string &expression)
128  {
129  m_Expression = expression;
130  m_Parser->SetExpr(m_Expression);
131  }
132 
134  std::string GetExpression() const
135  {
136  return m_Expression;
137  }
138 
141  {
142  return m_Parser->CheckExpr();
143  }
144 
145  void SetNumberOfBands(unsigned int NbOfBands)
146  {
147 
148  m_NbOfBands = NbOfBands;
149  std::ostringstream varName;
150 
151  m_AImageP1.resize(NbOfBands, 0.0);
152  m_AImageP2.resize(NbOfBands, 0.0);
153 
154  for (unsigned int i = 0; i < NbOfBands; ++i)
155  {
156  varName << "p1b" << i + 1;
157  m_Parser->DefineVar(varName.str(), &(m_AImageP1[i]));
158  varName.str("");
159  varName << "p2b" << i + 1;
160  m_Parser->DefineVar(varName.str(), &(m_AImageP2[i]));
161  varName.str("");
162  }
163  // customized data
164  // m_NbVar++;
165  // this->SetDataSize(m_NbVar);
166  m_Parser->DefineVar("distance", &m_Distance);
167  m_Parser->DefineVar("spectralAngle", &m_SpectralAngle);
168  m_Parser->DefineVar("intensity_p1", &m_IntensityP1);
169  m_Parser->DefineVar("intensity_p2", &m_IntensityP2);
170  // this->SetVarName(m_NbVar-1,"spectralDistance");
171  }
172 
173  const std::map<std::string, Parser::ValueType*>& GetVar() const
174  {
175  return this->m_Parser->GetVar();
176  }
177 
179  {
180  return this->m_Parser->GetFunList();
181  }
182 
184  : m_Parser(ParserType::New()), m_NbOfBands(0)
185  {}
186 
188  {
189  }
190 
191 private:
192  ConnectedComponentMuParserFunctor(const Self&) = delete;
193  void operator=(const Self&) = delete;
194 
195  std::string m_Expression;
197  std::vector<double> m_AImageP1;
198  std::vector<double> m_AImageP2;
199  double m_Distance;
203  std::vector<std::string> m_VarName;
204  unsigned int m_NbOfBands;
206 };
207 } // end of Functor namespace
208 
209 
210 } // end namespace otb
211 
212 
213 #endif
otb::Functor::ConnectedComponentMuParserFunctor::m_Expression
std::string m_Expression
Definition: otbConnectedComponentMuParserFunctor.h:195
otb::Functor::ConnectedComponentMuParserFunctor::GetNameOfClass
std::string GetNameOfClass()
Definition: otbConnectedComponentMuParserFunctor.h:81
otb::Functor::ConnectedComponentMuParserFunctor::m_NbOfBands
unsigned int m_NbOfBands
Definition: otbConnectedComponentMuParserFunctor.h:204
otb::Functor::BinarySpectralAngleFunctor
Definition: otbBinarySpectralAngleFunctor.h:44
otb::Functor::ConnectedComponentMuParserFunctor::GetFunList
Parser::FunctionMapType GetFunList() const
Definition: otbConnectedComponentMuParserFunctor.h:178
otb::Functor::ConnectedComponentMuParserFunctor::ParserType
Parser ParserType
Definition: otbConnectedComponentMuParserFunctor.h:78
otb::Functor::ConnectedComponentMuParserFunctor::m_ParserResult
double m_ParserResult
Definition: otbConnectedComponentMuParserFunctor.h:205
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::ConnectedComponentMuParserFunctor::Self
ConnectedComponentMuParserFunctor Self
Definition: otbConnectedComponentMuParserFunctor.h:79
otb::Functor::ConnectedComponentMuParserFunctor::CheckExpression
bool CheckExpression()
Definition: otbConnectedComponentMuParserFunctor.h:140
otbMacro.h
otb::Functor::ConnectedComponentMuParserFunctor::m_VarName
std::vector< std::string > m_VarName
Definition: otbConnectedComponentMuParserFunctor.h:203
otb::Functor::ConnectedComponentMuParserFunctor::GetExpression
std::string GetExpression() const
Definition: otbConnectedComponentMuParserFunctor.h:134
otb::Functor::ConnectedComponentMuParserFunctor::SetExpression
void SetExpression(const std::string &expression)
Definition: otbConnectedComponentMuParserFunctor.h:127
otb::Functor::ConnectedComponentMuParserFunctor::m_AImageP2
std::vector< double > m_AImageP2
Definition: otbConnectedComponentMuParserFunctor.h:198
otb::Functor::ConnectedComponentMuParserFunctor::GetVar
const std::map< std::string, Parser::ValueType * > & GetVar() const
Definition: otbConnectedComponentMuParserFunctor.h:173
otb::Functor::ConnectedComponentMuParserFunctor
Definition: otbConnectedComponentMuParserFunctor.h:74
otbBinarySpectralAngleFunctor.h
otb::Functor::ConnectedComponentMuParserFunctor::m_Parser
ParserType::Pointer m_Parser
Definition: otbConnectedComponentMuParserFunctor.h:196
otb::Functor::ConnectedComponentMuParserFunctor::SetNumberOfBands
void SetNumberOfBands(unsigned int NbOfBands)
Definition: otbConnectedComponentMuParserFunctor.h:145
otb::Functor::ConnectedComponentMuParserFunctor::m_IntensityP1
double m_IntensityP1
Definition: otbConnectedComponentMuParserFunctor.h:200
otb::Functor::ConnectedComponentMuParserFunctor::m_SpectralAngle
double m_SpectralAngle
Definition: otbConnectedComponentMuParserFunctor.h:202
otb::Functor::ConnectedComponentMuParserFunctor::m_Distance
double m_Distance
Definition: otbConnectedComponentMuParserFunctor.h:199
otb::Functor::ConnectedComponentMuParserFunctor::operator()
bool operator()(const TInput &p1, const TInput &p2)
Definition: otbConnectedComponentMuParserFunctor.h:86
otbParser.h
otb::Functor::ConnectedComponentMuParserFunctor::m_AImageP1
std::vector< double > m_AImageP1
Definition: otbConnectedComponentMuParserFunctor.h:197
otb::Parser
Definition of the standard floating point parser. Standard implementation of the mathematical express...
Definition: otbParser.h:44
otb::Functor::ConnectedComponentMuParserFunctor::~ConnectedComponentMuParserFunctor
~ConnectedComponentMuParserFunctor()
Definition: otbConnectedComponentMuParserFunctor.h:187
otb::Functor::ConnectedComponentMuParserFunctor::m_IntensityP2
double m_IntensityP2
Definition: otbConnectedComponentMuParserFunctor.h:201
otb::Parser::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbParser.h:50
otb::Functor::ConnectedComponentMuParserFunctor::ConnectedComponentMuParserFunctor
ConnectedComponentMuParserFunctor()
Definition: otbConnectedComponentMuParserFunctor.h:183