OTB  9.0.0
Orfeo Toolbox
otbOBIAMuParserFunctor.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 otbOBIAMuParserFunctor_h
24 #define otbOBIAMuParserFunctor_h
25 
26 #include "otbParser.h"
27 #include "otbMacro.h"
28 #include <string>
29 
30 
31 namespace otb
32 {
56 namespace Functor
57 {
58 
59 template <class TLabelObject>
60 // class ITK_EXPORT OBIAMuParserFunctor : public itk::LightObject
61 class ITK_EXPORT OBIAMuParserFunctor
62 {
63 public:
64  typedef Parser ParserType;
66 
67  std::string GetNameOfClass()
68  {
69  return "OBIAMuParserFunctor";
70  }
71 
72  inline bool operator()(const TLabelObject& a)
73  {
74 
75  double value;
76 
77  if (a.GetNumberOfAttributes() != m_AAttributes.size())
78  {
79  this->SetAttributes(a);
80  }
81 
82  for (unsigned int i = 0; i < m_AAttributes.size(); ++i)
83  {
84 
85  std::string name = (m_AttributesName[i]);
86  m_AAttributes[i] = a.GetAttribute(name.c_str());
87  }
88 
89  try
90  {
91  value = m_Parser->Eval();
92  }
93  catch (itk::ExceptionObject& err)
94  {
95  itkExceptionMacro(<< err);
96  }
97  return static_cast<bool>(value);
98  }
99 
100  void SetExpression(const std::string expression)
101  {
102  m_Expression = expression;
103  m_Parser->SetExpr(m_Expression);
104  }
105 
107  std::string GetExpression() const
108  {
109  return m_Expression;
110  }
111 
112  void ParseAttributeName(std::string& attributeName)
113  {
114 
115  for (unsigned int i = 0; i < attributeName.size(); ++i)
116  {
117  if (attributeName[i] == ':')
118  {
119  attributeName.erase(i, 1);
120  attributeName[i] = '_';
121  }
122  }
123  // TODO JGU
124  // replace "Band" by "b" for homogeneity with other functors
125  }
126 
127  void SetAttributes(const TLabelObject& a)
128  {
129 
130  unsigned int nbOfAttributes = a.GetNumberOfAttributes();
131 
132  m_AAttributes.resize(nbOfAttributes, 0.0);
133  m_AttributesName.resize(nbOfAttributes, "");
134  m_AttributesName = a.GetAvailableAttributes();
135  for (unsigned int i = 0; i < nbOfAttributes; ++i)
136  {
137  std::string attributeName = m_AttributesName.at(i);
138  ParseAttributeName(attributeName); // eliminate '::' from string name
139  m_Parser->DefineVar(attributeName, &(m_AAttributes[i]));
140  }
141  }
142 
143  void SetAttributes(std::vector<std::string> shapeAttributes, std::vector<std::string> statAttributes, unsigned int nbOfBands)
144  {
145  int index = 0;
146  unsigned int nbOfAttributes = shapeAttributes.size() + statAttributes.size() * nbOfBands;
147 
148  m_AAttributes.resize(nbOfAttributes, 0.0);
149  m_AttributesName.resize(nbOfAttributes, "");
150  std::ostringstream varName;
151  for (unsigned int i = 0; i < shapeAttributes.size(); ++i)
152  {
153 
154  varName << "SHAPE::" << shapeAttributes.at(i);
155  m_AttributesName.at(index) = varName.str();
156  varName.str("");
157  varName << "SHAPE_" << shapeAttributes.at(i);
158 
159  m_Parser->DefineVar(varName.str(), &(m_AAttributes[index]));
160  varName.str("");
161  index++;
162  }
163  for (unsigned int i = 0; i < statAttributes.size(); ++i)
164  {
165  for (unsigned int bandIndex = 1; bandIndex <= nbOfBands; bandIndex++)
166  {
167  varName << "STATS::Band" << bandIndex << "::" << statAttributes.at(i);
168  m_AttributesName.at(index) = varName.str();
169  varName.str("");
170  varName << "STATS_Band" << bandIndex << "_" << statAttributes.at(i);
171  m_Parser->DefineVar(varName.str(), &(m_AAttributes[index]));
172  varName.str("");
173  index++;
174  }
175  }
176  }
177 
180  {
181  return m_Parser->CheckExpr();
182  }
183 
184  const std::map<std::string, Parser::ValueType*>& GetVar() const
185  {
186  return this->m_Parser->GetVar();
187  }
188 
190  {
191  return this->m_Parser->GetFunList();
192  }
193 
195  {
196  m_Parser = ParserType::New();
197  m_AAttributes.resize(0);
198  };
199 
201 
202 protected:
203 private:
204  OBIAMuParserFunctor(const Self&) = delete;
205  void operator=(const Self&) = delete;
206 
207  std::string m_Expression;
208  ParserType::Pointer m_Parser;
209  std::vector<double> m_AAttributes;
210  std::vector<std::string> m_AttributesName;
212 };
213 } // end of Functor namespace
214 
215 
216 } // end namespace otb
217 
218 
219 #endif
otb::Functor::OBIAMuParserFunctor::~OBIAMuParserFunctor
~OBIAMuParserFunctor()
Definition: otbOBIAMuParserFunctor.h:200
otb::Functor::OBIAMuParserFunctor::SetAttributes
void SetAttributes(const TLabelObject &a)
Definition: otbOBIAMuParserFunctor.h:127
otb::Functor::OBIAMuParserFunctor::m_Parser
ParserType::Pointer m_Parser
Definition: otbOBIAMuParserFunctor.h:208
otb::Functor::OBIAMuParserFunctor::SetExpression
void SetExpression(const std::string expression)
Definition: otbOBIAMuParserFunctor.h:100
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::OBIAMuParserFunctor::ParserType
Parser ParserType
Definition: otbOBIAMuParserFunctor.h:64
otbMacro.h
otb::Functor::OBIAMuParserFunctor::m_AttributesName
std::vector< std::string > m_AttributesName
Definition: otbOBIAMuParserFunctor.h:210
otb::Functor::OBIAMuParserFunctor::m_AAttributes
std::vector< double > m_AAttributes
Definition: otbOBIAMuParserFunctor.h:209
otb::Functor::OBIAMuParserFunctor::m_Expression
std::string m_Expression
Definition: otbOBIAMuParserFunctor.h:207
otb::Functor::OBIAMuParserFunctor::Self
OBIAMuParserFunctor Self
Definition: otbOBIAMuParserFunctor.h:65
otb::Functor::OBIAMuParserFunctor::GetVar
const std::map< std::string, Parser::ValueType * > & GetVar() const
Definition: otbOBIAMuParserFunctor.h:184
otb::Functor::OBIAMuParserFunctor::operator()
bool operator()(const TLabelObject &a)
Definition: otbOBIAMuParserFunctor.h:72
otb::Functor::OBIAMuParserFunctor::OBIAMuParserFunctor
OBIAMuParserFunctor()
Definition: otbOBIAMuParserFunctor.h:194
otb::Functor::OBIAMuParserFunctor
Definition: otbOBIAMuParserFunctor.h:61
otbParser.h
otb::Functor::OBIAMuParserFunctor::GetNameOfClass
std::string GetNameOfClass()
Definition: otbOBIAMuParserFunctor.h:67
otb::Functor::OBIAMuParserFunctor::GetFunList
Parser::FunctionMapType GetFunList() const
Definition: otbOBIAMuParserFunctor.h:189
otb::Functor::OBIAMuParserFunctor::GetExpression
std::string GetExpression() const
Definition: otbOBIAMuParserFunctor.h:107
otb::Parser
Definition of the standard floating point parser. Standard implementation of the mathematical express...
Definition: otbParser.h:44
otb::Functor::OBIAMuParserFunctor::m_ParserResult
double m_ParserResult
Definition: otbOBIAMuParserFunctor.h:211
otb::Functor::OBIAMuParserFunctor::ParseAttributeName
void ParseAttributeName(std::string &attributeName)
Definition: otbOBIAMuParserFunctor.h:112
otb::Functor::OBIAMuParserFunctor::CheckExpression
bool CheckExpression()
Definition: otbOBIAMuParserFunctor.h:179
otb::Functor::OBIAMuParserFunctor::SetAttributes
void SetAttributes(std::vector< std::string > shapeAttributes, std::vector< std::string > statAttributes, unsigned int nbOfBands)
Definition: otbOBIAMuParserFunctor.h:143