OTB  6.7.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-2019 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 
101  void SetExpression(const std::string expression)
102  {
103  m_Expression = expression;
104  m_Parser->SetExpr(m_Expression);
105  }
106 
108  std::string GetExpression() const
109  {
110  return m_Expression;
111  }
112 
113  void ParseAttributeName(std::string &attributeName)
114  {
115 
116  for (unsigned int i = 0; i < attributeName.size(); ++i)
117  {
118  if (attributeName[i] == ':')
119  {
120  attributeName.erase(i, 1);
121  attributeName[i] = '_';
122  }
123  }
124  // TODO JGU
125  // replace "Band" by "b" for homogeneity with other functors
126 
127  }
128 
129  void SetAttributes(const TLabelObject &a)
130  {
131 
132  unsigned int nbOfAttributes = a.GetNumberOfAttributes();
133 
134  m_AAttributes.resize(nbOfAttributes, 0.0);
135  m_AttributesName.resize(nbOfAttributes, "");
136  m_AttributesName = a.GetAvailableAttributes();
137  for (unsigned int i = 0; i < nbOfAttributes; ++i)
138  {
139  std::string attributeName = m_AttributesName.at(i);
140  ParseAttributeName(attributeName); //eliminate '::' from string name
141  m_Parser->DefineVar(attributeName, &(m_AAttributes[i]));
142  }
143 
144  }
145 
146  void SetAttributes(std::vector<std::string> shapeAttributes, std::vector<std::string> statAttributes,
147  unsigned int nbOfBands)
148  {
149  int index = 0;
150  unsigned int nbOfAttributes = shapeAttributes.size() + statAttributes.size() * nbOfBands;
151 
152  m_AAttributes.resize(nbOfAttributes, 0.0);
153  m_AttributesName.resize(nbOfAttributes, "");
154  std::ostringstream varName;
155  for (unsigned int i = 0; i < shapeAttributes.size(); ++i)
156  {
157 
158  varName << "SHAPE::" << shapeAttributes.at(i);
159  m_AttributesName.at(index) = varName.str();
160  varName.str("");
161  varName << "SHAPE_" << shapeAttributes.at(i);
162 
163  m_Parser->DefineVar(varName.str(), &(m_AAttributes[index]));
164  varName.str("");
165  index++;
166  }
167  for (unsigned int i = 0; i < statAttributes.size(); ++i)
168  {
169  for (unsigned int bandIndex = 1; bandIndex <= nbOfBands; bandIndex++)
170  {
171  varName << "STATS::Band" << bandIndex << "::" << statAttributes.at(i);
172  m_AttributesName.at(index) = varName.str();
173  varName.str("");
174  varName << "STATS_Band" << bandIndex << "_" << statAttributes.at(i);
175  m_Parser->DefineVar(varName.str(), &(m_AAttributes[index]));
176  varName.str("");
177  index++;
178  }
179  }
180  }
181 
184  {
185  return m_Parser->CheckExpr();
186  }
187 
188  const std::map<std::string, Parser::ValueType*>& GetVar() const
189  {
190  return this->m_Parser->GetVar();
191  }
192 
194  {
195  return this->m_Parser->GetFunList();
196  }
197 
199  {
200  m_Parser = ParserType::New();
201  m_AAttributes.resize(0);
202  }
203 ;
204 
206  {
207  }
208 ;
209 
210 protected:
211 
212 private:
213 
214  OBIAMuParserFunctor(const Self &) = delete;
215  void operator =(const Self &) = delete;
216 
217  std::string m_Expression;
219  std::vector<double> m_AAttributes;
220  std::vector<std::string> m_AttributesName;
222 
223 };
224 } // end of Functor namespace
225 
226 
227 }//end namespace otb
228 
229 
230 #endif
void ParseAttributeName(std::string &attributeName)
Definition of the standard floating point parser. Standard implementation of the mathematical express...
Definition: otbParser.h:44
void SetAttributes(const TLabelObject &a)
const std::map< std::string, Parser::ValueType * > & GetVar() const
bool operator()(const TLabelObject &a)
void SetExpression(const std::string expression)
std::map< std::string, int > FunctionMapType
Definition: otbParser.h:64
std::vector< std::string > m_AttributesName
void SetAttributes(std::vector< std::string > shapeAttributes, std::vector< std::string > statAttributes, unsigned int nbOfBands)
Parser::FunctionMapType GetFunList() const